3 namespace Drupal\Tests\comment\Kernel\Views;
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;
15 * Tests comment admin view filters.
19 class CommentAdminViewTest extends ViewsKernelTestBase {
24 * @var \Drupal\comment\Entity\Comment[]
26 protected $comments = [];
31 * @var \Drupal\user\UserInterface
37 public static $modules = [
48 protected function setUp($import_test_views = TRUE) {
49 parent::setUp($import_test_views);
51 $this->installEntitySchema('user');
52 $this->installEntitySchema('comment');
53 $this->installEntitySchema('entity_test');
54 // Create the anonymous role.
55 $this->installConfig(['user']);
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();
62 // Create an anonymous user.
63 $storage = \Drupal::entityTypeManager()->getStorage('user');
64 // Insert a row for the anonymous user.
72 // Created admin role.
73 $admin_role = Role::create([
75 'permissions' => ['administer comments'],
78 // Create the admin user.
79 $this->adminUser = User::create([
80 'name' => $this->randomMachineName(),
81 'roles' => [$admin_role->id()],
83 $this->adminUser->save();
84 // Create a comment type.
87 'label' => 'Default comments',
88 'target_entity_type_id' => 'entity_test',
89 'description' => 'Default comment field',
91 // Create a commented entity.
92 $entity = EntityTest::create();
93 $entity->name->value = $this->randomMachineName();
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',
103 'entity_id' => $entity->id(),
107 $this->comments[] = $comment;
109 $comment_anonymous = Comment::create([
110 'subject' => 'Anonymous comment title',
113 'mail' => 'test@example.com',
114 'homepage' => 'https://example.com',
115 'entity_type' => 'entity_test',
116 'comment_type' => 'comment',
119 'entity_id' => $entity->id(),
121 $comment_anonymous->save();
122 $this->comments[] = $comment_anonymous;
126 * Tests comment admin view filters.
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();
135 $this->doTestFilters('page_unapproved');
139 * Tests comment admin view display.
141 * @param string $display_id
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');
150 /* @var \Drupal\Core\Render\RendererInterface $renderer */
151 $renderer = \Drupal::service('renderer');
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());
159 // Assert the exposed filters on the admin page.
160 $this->assertField('subject');
161 $this->assertField('author_name');
162 $this->assertField('langcode');
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();
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());
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();
182 $executable->setExposedInput(['subject' => 'My comment']);
183 $build = $executable->preview($display_id);
184 $this->setRawContent($renderer->renderRoot($build));
185 $this->verbose($this->getRawContent());
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();
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());
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();
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());
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();
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());
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();
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();
236 // If we have the translation then unpublish it.
237 $comment_translation = $comment->getTranslation('ur');
238 $comment_translation->setUnpublished();
239 $comment_translation->save();
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();
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();
253 $executable->setExposedInput(['langcode' => 'ur']);
254 $build = $executable->preview($display_id);
255 $this->setRawContent($renderer->renderRoot($build));
256 $this->verbose($this->getRawContent());
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();