function HandlerFieldUserNameTest::testUserName

Same name in other branches
  1. 9 core/modules/user/tests/src/Functional/Views/HandlerFieldUserNameTest.php \Drupal\Tests\user\Functional\Views\HandlerFieldUserNameTest::testUserName()
  2. 8.9.x core/modules/user/tests/src/Functional/Views/HandlerFieldUserNameTest.php \Drupal\Tests\user\Functional\Views\HandlerFieldUserNameTest::testUserName()
  3. 11.x core/modules/user/tests/src/Functional/Views/HandlerFieldUserNameTest.php \Drupal\Tests\user\Functional\Views\HandlerFieldUserNameTest::testUserName()

File

core/modules/user/tests/src/Functional/Views/HandlerFieldUserNameTest.php, line 30

Class

HandlerFieldUserNameTest
Tests the handler of the user: name field.

Namespace

Drupal\Tests\user\Functional\Views

Code

public function testUserName() : void {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $new_user = $this->drupalCreateUser([
        'access user profiles',
    ]);
    $this->drupalLogin($new_user);
    // Set defaults.
    $view = Views::getView('test_views_handler_field_user_name');
    $view->initHandlers();
    $view->field['name']->options['link_to_user'] = TRUE;
    $view->field['name']->options['type'] = 'user_name';
    $view->field['name']
        ->init($view, $view->getDisplay('default'));
    $view->field['name']->options['id'] = 'name';
    $this->executeView($view);
    $anon_name = $this->config('user.settings')
        ->get('anonymous');
    $view->result[0]->_entity
        ->setUsername('');
    $view->result[0]->_entity->uid->value = 0;
    $render = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
        return $view->field['name']
            ->advancedRender($view->result[0]);
    });
    $this->assertStringContainsString($anon_name, $render, 'For user 0 it should use the default anonymous name by default.');
    $render = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($view, $new_user) {
        return $view->field['name']
            ->advancedRender($view->result[$new_user->id()]);
    });
    $this->assertStringContainsString($new_user->getDisplayName(), $render, 'If link to user is checked the username should be part of the output.');
    $this->assertStringContainsString('user/' . $new_user->id(), $render, 'If link to user is checked the link to the user should appear as well.');
    $view->field['name']->options['link_to_user'] = FALSE;
    $view->field['name']->options['type'] = 'string';
    $render = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($view, $new_user) {
        return $view->field['name']
            ->advancedRender($view->result[$new_user->id()]);
    });
    $this->assertEquals($new_user->getDisplayName(), $render, 'If the user is not linked the username should be printed out for a normal user.');
}

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