View source
<?php
namespace Drupal\Tests\comment\Kernel\Views;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\views\Views;
class CommentAdminViewTest extends ViewsKernelTestBase {
protected $comments = [];
protected $adminUser;
protected static $modules = [
'user',
'comment',
'entity_test',
'language',
'locale',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->installEntitySchema('user');
$this
->installEntitySchema('comment');
$this
->installEntitySchema('entity_test');
$this
->installConfig([
'user',
]);
User::create([
'uid' => 1,
'name' => 'user1',
])
->save();
ConfigurableLanguage::createFromLangcode('ur')
->save();
$this->container
->get('kernel')
->rebuildContainer();
$storage = \Drupal::entityTypeManager()
->getStorage('user');
$storage
->create([
'uid' => 0,
'name' => '',
'status' => 0,
])
->save();
$admin_role = Role::create([
'id' => 'admin',
'permissions' => [
'administer comments',
'skip comment approval',
],
]);
$admin_role
->save();
$this->adminUser = User::create([
'name' => $this
->randomMachineName(),
'roles' => [
$admin_role
->id(),
],
]);
$this->adminUser
->save();
CommentType::create([
'id' => 'comment',
'label' => 'Default comments',
'target_entity_type_id' => 'entity_test',
'description' => 'Default comment field',
])
->save();
$entity = EntityTest::create();
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$comment = Comment::create([
'subject' => 'My comment title',
'uid' => $this->adminUser
->id(),
'entity_type' => 'entity_test',
'field_name' => 'comment',
'comment_type' => 'comment',
'status' => 1,
'entity_id' => $entity
->id(),
]);
$comment
->save();
$this->comments[] = $comment;
$comment_anonymous = Comment::create([
'subject' => 'Anonymous comment title',
'uid' => 0,
'name' => 'barry',
'mail' => 'test@example.com',
'homepage' => 'https://example.com',
'entity_type' => 'entity_test',
'field_name' => 'comment',
'comment_type' => 'comment',
'created' => 123456,
'status' => 1,
'entity_id' => $entity
->id(),
]);
$comment_anonymous
->save();
$this->comments[] = $comment_anonymous;
}
public function testFilters() {
$this
->doTestFilters('page_published');
foreach ($this->comments as $comment) {
$comment
->setUnpublished();
$comment
->save();
}
$this
->doTestFilters('page_unapproved');
}
protected function doTestFilters($display_id) {
$comment = $this->comments[0];
$comment_anonymous = $this->comments[1];
$account_switcher = \Drupal::service('account_switcher');
$renderer = \Drupal::service('renderer');
$account_switcher
->switchTo($this->adminUser);
$executable = Views::getView('comment');
$build = $executable
->preview($display_id);
$this
->setRawContent($renderer
->renderRoot($build));
$this
->assertField('subject');
$this
->assertField('author_name');
$this
->assertField('langcode');
$elements = $this
->cssSelect('input[type="checkbox"]');
$this
->assertCount(2, $elements, 'There are two comments on the page.');
$this
->assertText($comment
->label());
$this
->assertText($comment_anonymous
->label());
$executable
->destroy();
$executable
->setExposedInput([
'subject' => 'Anonymous',
]);
$build = $executable
->preview($display_id);
$this
->setRawContent($renderer
->renderRoot($build));
$elements = $this
->cssSelect('input[type="checkbox"]');
$this
->assertCount(1, $elements, 'Only anonymous comment is visible.');
$this
->assertNoText($comment
->label());
$this
->assertText($comment_anonymous
->label());
$executable
->destroy();
$executable
->setExposedInput([
'subject' => 'My comment',
]);
$build = $executable
->preview($display_id);
$this
->setRawContent($renderer
->renderRoot($build));
$elements = $this
->cssSelect('input[type="checkbox"]');
$this
->assertCount(1, $elements, 'Only admin comment is visible.');
$this
->assertText($comment
->label());
$this
->assertNoText($comment_anonymous
->label());
$executable
->destroy();
$executable
->setExposedInput([
'author_name' => 'barry',
]);
$build = $executable
->preview($display_id);
$this
->setRawContent($renderer
->renderRoot($build));
$elements = $this
->cssSelect('input[type="checkbox"]');
$this
->assertCount(1, $elements, 'Only anonymous comment is visible.');
$this
->assertNoText($comment
->label());
$this
->assertText($comment_anonymous
->label());
$executable
->destroy();
$executable
->setExposedInput([
'author_name' => $this->adminUser
->label(),
]);
$build = $executable
->preview($display_id);
$this
->setRawContent($renderer
->renderRoot($build));
$elements = $this
->cssSelect('input[type="checkbox"]');
$this
->assertCount(1, $elements, 'Only admin comment is visible.');
$this
->assertText($comment
->label());
$this
->assertNoText($comment_anonymous
->label());
$executable
->destroy();
$executable
->setExposedInput([
'langcode' => '***LANGUAGE_site_default***',
]);
$build = $executable
->preview($display_id);
$this
->setRawContent($renderer
->renderRoot($build));
$elements = $this
->cssSelect('input[type="checkbox"]');
$this
->assertCount(2, $elements, 'Both comments are visible.');
$this
->assertText($comment
->label());
$this
->assertText($comment_anonymous
->label());
$executable
->destroy();
if (!$comment
->hasTranslation('ur')) {
$comment_translation = $comment
->addTranslation('ur', [
'subject' => 'ur title',
]);
$comment_translation
->save();
}
else {
$comment_translation = $comment
->getTranslation('ur');
$comment_translation
->setUnpublished();
$comment_translation
->save();
}
if (!$comment_anonymous
->hasTranslation('ur')) {
$comment_anonymous_translation = $comment_anonymous
->addTranslation('ur', [
'subject' => 'ur Anonymous title',
]);
$comment_anonymous_translation
->save();
}
else {
$comment_anonymous_translation = $comment_anonymous
->getTranslation('ur');
$comment_anonymous_translation
->setUnpublished();
$comment_anonymous_translation
->save();
}
$executable
->setExposedInput([
'langcode' => 'ur',
]);
$build = $executable
->preview($display_id);
$this
->setRawContent($renderer
->renderRoot($build));
$elements = $this
->cssSelect('input[type="checkbox"]');
$this
->assertCount(2, $elements, 'Both comments are visible.');
$this
->assertNoText($comment
->label());
$this
->assertNoText($comment_anonymous
->label());
$this
->assertText($comment_translation
->label());
$this
->assertText($comment_anonymous_translation
->label());
$executable
->destroy();
}
}