3 namespace Drupal\Tests\comment\Kernel;
5 use Drupal\comment\Entity\CommentType;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Core\Database\Database;
8 use Drupal\Core\Entity\Entity\EntityViewDisplay;
9 use Drupal\Core\Entity\Entity\EntityViewMode;
10 use Drupal\field\Entity\FieldConfig;
11 use Drupal\field\Entity\FieldStorageConfig;
12 use Drupal\KernelTests\KernelTestBase;
15 * Tests integration of comment with other components.
19 class CommentIntegrationTest extends KernelTestBase {
24 public static $modules = ['comment', 'field', 'entity_test', 'user', 'system', 'dblog'];
29 protected function setUp() {
31 $this->installEntitySchema('entity_test');
32 $this->installEntitySchema('user');
33 $this->installEntitySchema('comment');
34 $this->installSchema('dblog', ['watchdog']);
36 // Create a new 'comment' comment-type.
39 'label' => $this->randomString(),
44 * Tests view mode setting integration.
46 * @see comment_entity_view_display_presave()
47 * @see CommentDefaultFormatter::calculateDependencies()
49 public function testViewMode() {
50 $mode = Unicode::strtolower($this->randomMachineName());
51 // Create a new comment view mode and a view display entity.
52 EntityViewMode::create([
53 'id' => "comment.$mode",
54 'targetEntityType' => 'comment',
55 'settings' => ['comment_type' => 'comment'],
57 EntityViewDisplay::create([
58 'targetEntityType' => 'comment',
59 'bundle' => 'comment',
61 ])->setStatus(TRUE)->save();
63 // Create a comment field attached to a host 'entity_test' entity.
64 FieldStorageConfig::create([
65 'entity_type' => 'entity_test',
67 'field_name' => $field_name = Unicode::strtolower($this->randomMachineName()),
69 'comment_type' => 'comment',
73 'entity_type' => 'entity_test',
74 'bundle' => 'entity_test',
75 'field_name' => $field_name,
79 'type' => 'comment_default',
80 'settings' => ['view_mode' => $mode, 'pager_id' => 0],
82 // Create a new 'entity_test' view display on host entity that uses the
83 // custom comment display in field formatter to show the field.
84 EntityViewDisplay::create([
85 'targetEntityType' => 'entity_test',
86 'bundle' => 'entity_test',
88 ])->setComponent($field_name, $component)->setStatus(TRUE)->save();
90 $host_display_id = 'entity_test.entity_test.default';
91 $comment_display_id = "comment.comment.$mode";
93 // Disable the "comment.comment.$mode" display.
94 EntityViewDisplay::load($comment_display_id)->setStatus(FALSE)->save();
96 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $host_display */
97 $host_display = EntityViewDisplay::load($host_display_id);
99 // Check that the field formatter has been disabled on host view display.
100 $this->assertNull($host_display->getComponent($field_name));
101 $this->assertTrue($host_display->get('hidden')[$field_name]);
103 // Check that the proper warning has been logged.
105 '@id' => $host_display_id,
106 '@name' => $field_name,
107 '@display' => EntityViewMode::load("comment.$mode")->label(),
110 $logged = (bool) Database::getConnection()->select('watchdog')
111 ->fields('watchdog', ['wid'])
112 ->condition('type', 'system')
113 ->condition('message', "View display '@id': Comment field formatter '@name' was disabled because it is using the comment view display '@display' (@mode) that was just disabled.")
114 ->condition('variables', serialize($arguments))
117 $this->assertTrue($logged);
119 // Re-enable the comment view display.
120 EntityViewDisplay::load($comment_display_id)->setStatus(TRUE)->save();
121 // Re-enable the comment field formatter on host entity view display.
122 EntityViewDisplay::load($host_display_id)->setComponent($field_name, $component)->save();
124 // Delete the "comment.$mode" view mode.
125 EntityViewMode::load("comment.$mode")->delete();
127 // Check that the comment view display entity has been deleted too.
128 $this->assertNull(EntityViewDisplay::load($comment_display_id));
130 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
131 $host_display = EntityViewDisplay::load($host_display_id);
133 // Check that the field formatter has been disabled on host view display.
134 $this->assertNull($host_display->getComponent($field_name));
135 $this->assertTrue($host_display->get('hidden')[$field_name]);