3 namespace Drupal\Tests\comment\Kernel;
5 use Drupal\comment\Entity\CommentType;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Entity\Entity\EntityViewDisplay;
8 use Drupal\Core\Entity\Entity\EntityViewMode;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\field\Entity\FieldStorageConfig;
11 use Drupal\KernelTests\KernelTestBase;
14 * Tests integration of comment with other components.
18 class CommentIntegrationTest extends KernelTestBase {
23 public static $modules = ['comment', 'field', 'entity_test', 'user', 'system', 'dblog'];
28 protected function setUp() {
30 $this->installEntitySchema('entity_test');
31 $this->installEntitySchema('user');
32 $this->installEntitySchema('comment');
33 $this->installSchema('dblog', ['watchdog']);
35 // Create a new 'comment' comment-type.
38 'label' => $this->randomString(),
43 * Tests view mode setting integration.
45 * @see comment_entity_view_display_presave()
46 * @see CommentDefaultFormatter::calculateDependencies()
48 public function testViewMode() {
49 $mode = mb_strtolower($this->randomMachineName());
50 // Create a new comment view mode and a view display entity.
51 EntityViewMode::create([
52 'id' => "comment.$mode",
53 'targetEntityType' => 'comment',
54 'settings' => ['comment_type' => 'comment'],
56 EntityViewDisplay::create([
57 'targetEntityType' => 'comment',
58 'bundle' => 'comment',
60 ])->setStatus(TRUE)->save();
62 // Create a comment field attached to a host 'entity_test' entity.
63 FieldStorageConfig::create([
64 'entity_type' => 'entity_test',
66 'field_name' => $field_name = mb_strtolower($this->randomMachineName()),
68 'comment_type' => 'comment',
72 'entity_type' => 'entity_test',
73 'bundle' => 'entity_test',
74 'field_name' => $field_name,
78 'type' => 'comment_default',
79 'settings' => ['view_mode' => $mode, 'pager_id' => 0],
81 // Create a new 'entity_test' view display on host entity that uses the
82 // custom comment display in field formatter to show the field.
83 EntityViewDisplay::create([
84 'targetEntityType' => 'entity_test',
85 'bundle' => 'entity_test',
87 ])->setComponent($field_name, $component)->setStatus(TRUE)->save();
89 $host_display_id = 'entity_test.entity_test.default';
90 $comment_display_id = "comment.comment.$mode";
92 // Disable the "comment.comment.$mode" display.
93 EntityViewDisplay::load($comment_display_id)->setStatus(FALSE)->save();
95 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $host_display */
96 $host_display = EntityViewDisplay::load($host_display_id);
98 // Check that the field formatter has been disabled on host view display.
99 $this->assertNull($host_display->getComponent($field_name));
100 $this->assertTrue($host_display->get('hidden')[$field_name]);
102 // Check that the proper warning has been logged.
104 '@id' => $host_display_id,
105 '@name' => $field_name,
106 '@display' => EntityViewMode::load("comment.$mode")->label(),
109 $logged = (bool) Database::getConnection()->select('watchdog')
110 ->fields('watchdog', ['wid'])
111 ->condition('type', 'system')
112 ->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.")
113 ->condition('variables', serialize($arguments))
116 $this->assertTrue($logged);
118 // Re-enable the comment view display.
119 EntityViewDisplay::load($comment_display_id)->setStatus(TRUE)->save();
120 // Re-enable the comment field formatter on host entity view display.
121 EntityViewDisplay::load($host_display_id)->setComponent($field_name, $component)->save();
123 // Delete the "comment.$mode" view mode.
124 EntityViewMode::load("comment.$mode")->delete();
126 // Check that the comment view display entity has been deleted too.
127 $this->assertNull(EntityViewDisplay::load($comment_display_id));
129 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
130 $host_display = EntityViewDisplay::load($host_display_id);
132 // Check that the field formatter has been disabled on host view display.
133 $this->assertNull($host_display->getComponent($field_name));
134 $this->assertTrue($host_display->get('hidden')[$field_name]);