function CommentUserNameTest::testUsername

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php \Drupal\Tests\comment\Kernel\Views\CommentUserNameTest::testUsername()
  2. 8.9.x core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php \Drupal\Tests\comment\Kernel\Views\CommentUserNameTest::testUsername()
  3. 10 core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php \Drupal\Tests\comment\Kernel\Views\CommentUserNameTest::testUsername()

Tests the username formatter.

File

core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php, line 110

Class

CommentUserNameTest
Tests comment user name field.

Namespace

Drupal\Tests\comment\Kernel\Views

Code

public function testUsername() : void {
    $view_id = $this->randomMachineName();
    $view = View::create([
        'id' => $view_id,
        'label' => $view_id,
        'base_table' => 'comment_field_data',
        'display' => [
            'default' => [
                'display_plugin' => 'default',
                'id' => 'default',
                'display_options' => [
                    'fields' => [
                        'name' => [
                            'table' => 'comment_field_data',
                            'field' => 'name',
                            'id' => 'name',
                            'plugin_id' => 'field',
                            'type' => 'comment_username',
                        ],
                        'subject' => [
                            'table' => 'comment_field_data',
                            'field' => 'subject',
                            'id' => 'subject',
                            'plugin_id' => 'field',
                            'type' => 'string',
                            'settings' => [
                                'link_to_entity' => TRUE,
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ]);
    $view->save();
    
    /** @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($view_id);
    $build = $executable->preview();
    $this->setRawContent($renderer->renderRoot($build));
    $this->assertLink('My comment title');
    $this->assertLink('Anonymous comment title');
    // Display plugin of the view is showing the name field. When comment
    // belongs to an authenticated user the name field has no value.
    $comment_author = $this->xpath('//div[contains(@class, :class)]/span[normalize-space(text())=""]', [
        ':class' => 'views-field-subject',
    ]);
    $this->assertNotEmpty($comment_author);
    // When comment belongs to an anonymous user the name field has a value and
    // it is rendered correctly.
    $this->assertLink('barry (not verified)');
    $account_switcher->switchTo(new AnonymousUserSession());
    $executable = Views::getView($view_id);
    $executable->storage
        ->invalidateCaches();
    $build = $executable->preview();
    $this->setRawContent($renderer->renderRoot($build));
    // No access to user-profiles, so shouldn't be able to see links.
    $this->assertNoLink($this->adminUser
        ->label());
    // Note: External users aren't pointing to drupal user profiles.
    $this->assertLink('barry (not verified)');
    $this->assertLink('My comment title');
    $this->assertLink('Anonymous comment title');
}

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