86e89499057ad28dd01697f1296cabbf4c119e79
[yaffs-website] / web / core / modules / comment / tests / src / Kernel / Views / CommentAdminViewTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Kernel\Views;
4
5 use Drupal\comment\Entity\Comment;
6 use Drupal\comment\Entity\CommentType;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\language\Entity\ConfigurableLanguage;
9 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
10 use Drupal\user\Entity\Role;
11 use Drupal\user\Entity\User;
12 use Drupal\views\Views;
13
14 /**
15  * Tests comment admin view filters.
16  *
17  * @group comment
18  */
19 class CommentAdminViewTest extends ViewsKernelTestBase {
20
21   /**
22    * Comments.
23    *
24    * @var \Drupal\comment\Entity\Comment[]
25    */
26   protected $comments = [];
27
28   /**
29    * Admin user.
30    *
31    * @var \Drupal\user\UserInterface
32    */
33   protected $adminUser;
34   /**
35    * {@inheritdoc}
36    */
37   public static $modules = [
38     'user',
39     'comment',
40     'entity_test',
41     'language',
42     'locale',
43   ];
44
45   /**
46    * {@inheritdoc}
47    */
48   protected function setUp($import_test_views = TRUE) {
49     parent::setUp($import_test_views);
50
51     $this->installEntitySchema('user');
52     $this->installEntitySchema('comment');
53     $this->installEntitySchema('entity_test');
54     // Create the anonymous role.
55     $this->installConfig(['user']);
56
57     // Enable another language.
58     ConfigurableLanguage::createFromLangcode('ur')->save();
59     // Rebuild the container to update the default language container variable.
60     $this->container->get('kernel')->rebuildContainer();
61
62     // Create an anonymous user.
63     $storage = \Drupal::entityTypeManager()->getStorage('user');
64     // Insert a row for the anonymous user.
65     $storage
66       ->create([
67         'uid' => 0,
68         'name' => '',
69         'status' => 0,
70       ])
71       ->save();
72     // Created admin role.
73     $admin_role = Role::create([
74       'id' => 'admin',
75       'permissions' => ['administer comments'],
76     ]);
77     $admin_role->save();
78     // Create the admin user.
79     $this->adminUser = User::create([
80       'name' => $this->randomMachineName(),
81       'roles' => [$admin_role->id()],
82     ]);
83     $this->adminUser->save();
84     // Create a comment type.
85     CommentType::create([
86       'id' => 'comment',
87       'label' => 'Default comments',
88       'target_entity_type_id' => 'entity_test',
89       'description' => 'Default comment field',
90     ])->save();
91     // Create a commented entity.
92     $entity = EntityTest::create();
93     $entity->name->value = $this->randomMachineName();
94     $entity->save();
95
96     // Create some comments.
97     $comment = Comment::create([
98       'subject' => 'My comment title',
99       'uid' => $this->adminUser->id(),
100       'entity_type' => 'entity_test',
101       'comment_type' => 'comment',
102       'status' => 1,
103       'entity_id' => $entity->id(),
104     ]);
105     $comment->save();
106
107     $this->comments[] = $comment;
108
109     $comment_anonymous = Comment::create([
110       'subject' => 'Anonymous comment title',
111       'uid' => 0,
112       'name' => 'barry',
113       'mail' => 'test@example.com',
114       'homepage' => 'https://example.com',
115       'entity_type' => 'entity_test',
116       'comment_type' => 'comment',
117       'created' => 123456,
118       'status' => 1,
119       'entity_id' => $entity->id(),
120     ]);
121     $comment_anonymous->save();
122     $this->comments[] = $comment_anonymous;
123   }
124
125   /**
126    * Tests comment admin view filters.
127    */
128   public function testFilters() {
129     $this->doTestFilters('page_published');
130     // Unpublish the comments to test the Unapproved comments tab.
131     foreach ($this->comments as $comment) {
132       $comment->setUnpublished();
133       $comment->save();
134     }
135     $this->doTestFilters('page_unapproved');
136   }
137
138   /**
139    * Tests comment admin view display.
140    *
141    * @param string $display_id
142    *   The display ID.
143    */
144   protected function doTestFilters($display_id) {
145     $comment = $this->comments[0];
146     $comment_anonymous = $this->comments[1];
147     /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
148     $account_switcher = \Drupal::service('account_switcher');
149
150     /* @var \Drupal\Core\Render\RendererInterface $renderer */
151     $renderer = \Drupal::service('renderer');
152
153     $account_switcher->switchTo($this->adminUser);
154     $executable = Views::getView('comment');
155     $build = $executable->preview($display_id);
156     $this->setRawContent($renderer->renderRoot($build));
157     $this->verbose($this->getRawContent());
158
159     // Assert the exposed filters on the admin page.
160     $this->assertField('subject');
161     $this->assertField('author_name');
162     $this->assertField('langcode');
163
164     $elements = $this->cssSelect('input[type="checkbox"]');
165     $this->assertEquals(2, count($elements), 'There are two comments on the page.');
166     $this->assertText($comment->label());
167     $this->assertText($comment_anonymous->label());
168     $executable->destroy();
169
170     // Test the Subject filter.
171     $executable->setExposedInput(['subject' => 'Anonymous']);
172     $build = $executable->preview($display_id);
173     $this->setRawContent($renderer->renderRoot($build));
174     $this->verbose($this->getRawContent());
175
176     $elements = $this->cssSelect('input[type="checkbox"]');
177     $this->assertEquals(1, count($elements), 'Only anonymous comment is visible.');
178     $this->assertNoText($comment->label());
179     $this->assertText($comment_anonymous->label());
180     $executable->destroy();
181
182     $executable->setExposedInput(['subject' => 'My comment']);
183     $build = $executable->preview($display_id);
184     $this->setRawContent($renderer->renderRoot($build));
185     $this->verbose($this->getRawContent());
186
187     $elements = $this->cssSelect('input[type="checkbox"]');
188     $this->assertEquals(1, count($elements), 'Only admin comment is visible.');
189     $this->assertText($comment->label());
190     $this->assertNoText($comment_anonymous->label());
191     $executable->destroy();
192
193     // Test the combine filter using author name.
194     $executable->setExposedInput(['author_name' => 'barry']);
195     $build = $executable->preview($display_id);
196     $this->setRawContent($renderer->renderRoot($build));
197     $this->verbose($this->getRawContent());
198
199     $elements = $this->cssSelect('input[type="checkbox"]');
200     $this->assertEquals(1, count($elements), 'Only anonymous comment is visible.');
201     $this->assertNoText($comment->label());
202     $this->assertText($comment_anonymous->label());
203     $executable->destroy();
204
205     // Test the combine filter using username.
206     $executable->setExposedInput(['author_name' => $this->adminUser->label()]);
207     $build = $executable->preview($display_id);
208     $this->setRawContent($renderer->renderRoot($build));
209     $this->verbose($this->getRawContent());
210
211     $elements = $this->cssSelect('input[type="checkbox"]');
212     $this->assertEquals(1, count($elements), 'Only admin comment is visible.');
213     $this->assertText($comment->label());
214     $this->assertNoText($comment_anonymous->label());
215     $executable->destroy();
216
217     // Test the language filter.
218     $executable->setExposedInput(['langcode' => '***LANGUAGE_site_default***']);
219     $build = $executable->preview($display_id);
220     $this->setRawContent($renderer->renderRoot($build));
221     $this->verbose($this->getRawContent());
222
223     $elements = $this->cssSelect('input[type="checkbox"]');
224     $this->assertEquals(2, count($elements), 'Both comments are visible.');
225     $this->assertText($comment->label());
226     $this->assertText($comment_anonymous->label());
227     $executable->destroy();
228
229     // Tests comment translation filter.
230     if (!$comment->hasTranslation('ur')) {
231       // If we don't have the translation then create one.
232       $comment_translation = $comment->addTranslation('ur', ['subject' => 'ur title']);
233       $comment_translation->save();
234     }
235     else {
236       // If we have the translation then unpublish it.
237       $comment_translation = $comment->getTranslation('ur');
238       $comment_translation->setUnpublished();
239       $comment_translation->save();
240     }
241     if (!$comment_anonymous->hasTranslation('ur')) {
242       // If we don't have the translation then create one.
243       $comment_anonymous_translation = $comment_anonymous->addTranslation('ur', ['subject' => 'ur Anonymous title']);
244       $comment_anonymous_translation->save();
245     }
246     else {
247       // If we have the translation then unpublish it.
248       $comment_anonymous_translation = $comment_anonymous->getTranslation('ur');
249       $comment_anonymous_translation->setUnpublished();
250       $comment_anonymous_translation->save();
251     }
252
253     $executable->setExposedInput(['langcode' => 'ur']);
254     $build = $executable->preview($display_id);
255     $this->setRawContent($renderer->renderRoot($build));
256     $this->verbose($this->getRawContent());
257
258     $elements = $this->cssSelect('input[type="checkbox"]');
259     $this->assertEquals(2, count($elements), 'Both comments are visible.');
260     $this->assertNoText($comment->label());
261     $this->assertNoText($comment_anonymous->label());
262     $this->assertText($comment_translation->label());
263     $this->assertText($comment_anonymous_translation->label());
264     $executable->destroy();
265   }
266
267 }