function CommentAdminViewTest::doTestFilters

Same name in other branches
  1. 9 core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php \Drupal\Tests\comment\Kernel\Views\CommentAdminViewTest::doTestFilters()
  2. 8.9.x core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php \Drupal\Tests\comment\Kernel\Views\CommentAdminViewTest::doTestFilters()
  3. 11.x core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php \Drupal\Tests\comment\Kernel\Views\CommentAdminViewTest::doTestFilters()

Tests comment admin view display.

Parameters

string $display_id: The display ID.

1 call to CommentAdminViewTest::doTestFilters()
CommentAdminViewTest::testFilters in core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php
Tests comment admin view filters.

File

core/modules/comment/tests/src/Kernel/Views/CommentAdminViewTest.php, line 154

Class

CommentAdminViewTest
Tests comment admin view filters.

Namespace

Drupal\Tests\comment\Kernel\Views

Code

protected function doTestFilters($display_id) {
    $comment = $this->comments[0];
    $comment_anonymous = $this->comments[1];
    
    /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
    $account_switcher = \Drupal::service('account_switcher');
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $account_switcher->switchTo($this->adminUser);
    $executable = Views::getView('comment');
    $build = $executable->preview($display_id);
    $this->setRawContent($renderer->renderRoot($build));
    // Assert the exposed filters on the admin page.
    $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();
    // Test the Subject filter.
    $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();
    // Test the combine filter using author name.
    $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();
    // Test the combine filter using username.
    $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();
    // Test the language filter.
    $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();
    // Tests comment translation filter.
    if (!$comment->hasTranslation('ur')) {
        // If we don't have the translation then create one.
        $comment_translation = $comment->addTranslation('ur', [
            'subject' => 'ur title',
        ]);
        $comment_translation->save();
    }
    else {
        // If we have the translation then unpublish it.
        $comment_translation = $comment->getTranslation('ur');
        $comment_translation->setUnpublished();
        $comment_translation->save();
    }
    if (!$comment_anonymous->hasTranslation('ur')) {
        // If we don't have the translation then create one.
        $comment_anonymous_translation = $comment_anonymous->addTranslation('ur', [
            'subject' => 'ur Anonymous title',
        ]);
        $comment_anonymous_translation->save();
    }
    else {
        // If we have the translation then unpublish it.
        $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();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.