function UserNameFormatterTest::testFormatter

Same name in other branches
  1. 9 core/modules/user/tests/src/Kernel/Field/UserNameFormatterTest.php \Drupal\Tests\user\Kernel\Field\UserNameFormatterTest::testFormatter()
  2. 8.9.x core/modules/user/tests/src/Kernel/Field/UserNameFormatterTest.php \Drupal\Tests\user\Kernel\Field\UserNameFormatterTest::testFormatter()
  3. 11.x core/modules/user/tests/src/Kernel/Field/UserNameFormatterTest.php \Drupal\Tests\user\Kernel\Field\UserNameFormatterTest::testFormatter()

Tests the formatter output.

File

core/modules/user/tests/src/Kernel/Field/UserNameFormatterTest.php, line 73

Class

UserNameFormatterTest
Tests the user_name formatter.

Namespace

Drupal\Tests\user\Kernel\Field

Code

public function testFormatter() : void {
    $user = User::create([
        'name' => 'test name',
    ]);
    $user->save();
    $result = $user->{$this->fieldName}
        ->view([
        'type' => 'user_name',
    ]);
    $this->assertEquals('username', $result[0]['#theme']);
    $this->assertEquals(spl_object_hash($user), spl_object_hash($result[0]['#account']));
    $result = $user->{$this->fieldName}
        ->view([
        'type' => 'user_name',
        'settings' => [
            'link_to_entity' => FALSE,
        ],
    ]);
    $this->assertEquals($user->getDisplayName(), $result[0]['#markup']);
    $user = User::getAnonymousUser();
    $result = $user->{$this->fieldName}
        ->view([
        'type' => 'user_name',
    ]);
    $this->assertEquals('username', $result[0]['#theme']);
    $this->assertEquals(spl_object_hash($user), spl_object_hash($result[0]['#account']));
    $result = $user->{$this->fieldName}
        ->view([
        'type' => 'user_name',
        'settings' => [
            'link_to_entity' => FALSE,
        ],
    ]);
    $this->assertEquals($user->getDisplayName(), $result[0]['#markup']);
    $this->assertEquals($this->config('user.settings')
        ->get('anonymous'), $result[0]['#markup']);
}

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