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