You are here

public function CommentUserNameTest::testUsername in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/comment/src/Tests/Views/CommentUserNameTest.php \Drupal\comment\Tests\Views\CommentUserNameTest::testUsername()

Test the username formatter.

File

core/modules/comment/src/Tests/Views/CommentUserNameTest.php, line 103
Contains \Drupal\comment\Tests\Views\CommentUserNameTest.

Class

CommentUserNameTest
Tests comment user name field

Namespace

Drupal\comment\Tests\Views

Code

public function testUsername() {
  $view_id = $this
    ->randomMachineName();
  $view = View::create([
    'id' => $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
    ->verbose($this
    ->getRawContent());
  $this
    ->assertLink('My comment title');
  $this
    ->assertLink('Anonymous comment title');
  $this
    ->assertLink($this->adminUser
    ->label());
  $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
    ->verbose($this
    ->getRawContent());
  $this
    ->assertLink('My comment title');
  $this
    ->assertLink('Anonymous comment title');
}