Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / comment / tests / src / Kernel / CommentIntegrationTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Kernel;
4
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;
13
14 /**
15  * Tests integration of comment with other components.
16  *
17  * @group comment
18  */
19 class CommentIntegrationTest extends KernelTestBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   public static $modules = ['comment', 'field', 'entity_test', 'user', 'system', 'dblog'];
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::setUp();
31     $this->installEntitySchema('entity_test');
32     $this->installEntitySchema('user');
33     $this->installEntitySchema('comment');
34     $this->installSchema('dblog', ['watchdog']);
35
36     // Create a new 'comment' comment-type.
37     CommentType::create([
38       'id' => 'comment',
39       'label' => $this->randomString(),
40     ])->save();
41   }
42
43   /**
44    * Tests view mode setting integration.
45    *
46    * @see comment_entity_view_display_presave()
47    * @see CommentDefaultFormatter::calculateDependencies()
48    */
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'],
56     ])->save();
57     EntityViewDisplay::create([
58       'targetEntityType' => 'comment',
59       'bundle' => 'comment',
60       'mode' => $mode,
61     ])->setStatus(TRUE)->save();
62
63     // Create a comment field attached to a host 'entity_test' entity.
64     FieldStorageConfig::create([
65       'entity_type' => 'entity_test',
66       'type' => 'comment',
67       'field_name' => $field_name = Unicode::strtolower($this->randomMachineName()),
68       'settings' => [
69         'comment_type' => 'comment',
70       ],
71     ])->save();
72     FieldConfig::create([
73       'entity_type' => 'entity_test',
74       'bundle' => 'entity_test',
75       'field_name' => $field_name,
76     ])->save();
77
78     $component = [
79       'type' => 'comment_default',
80       'settings' => ['view_mode' => $mode, 'pager_id' => 0],
81     ];
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',
87       'mode' => 'default',
88     ])->setComponent($field_name, $component)->setStatus(TRUE)->save();
89
90     $host_display_id = 'entity_test.entity_test.default';
91     $comment_display_id = "comment.comment.$mode";
92
93     // Disable the "comment.comment.$mode" display.
94     EntityViewDisplay::load($comment_display_id)->setStatus(FALSE)->save();
95
96     /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $host_display */
97     $host_display = EntityViewDisplay::load($host_display_id);
98
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]);
102
103     // Check that the proper warning has been logged.
104     $arguments = [
105       '@id' => $host_display_id,
106       '@name' => $field_name,
107       '@display' => EntityViewMode::load("comment.$mode")->label(),
108       '@mode' => $mode,
109     ];
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))
115       ->execute()
116       ->fetchField();
117     $this->assertTrue($logged);
118
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();
123
124     // Delete the "comment.$mode" view mode.
125     EntityViewMode::load("comment.$mode")->delete();
126
127     // Check that the comment view display entity has been deleted too.
128     $this->assertNull(EntityViewDisplay::load($comment_display_id));
129
130     /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
131     $host_display = EntityViewDisplay::load($host_display_id);
132
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]);
136   }
137
138 }