You are here

public function UserNameFormatterTest::testFormatter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Kernel/Field/UserNameFormatterTest.php \Drupal\Tests\user\Kernel\Field\UserNameFormatterTest::testFormatter()
  2. 10 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 74

Class

UserNameFormatterTest
Tests the user_name formatter.

Namespace

Drupal\Tests\user\Kernel\Field

Code

public function testFormatter() {
  $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']);
}