You are here

public function UserNameFormatterTest::testFormatter in Drupal 8

Same name and namespace in other branches
  1. 9 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
    ->assertEqual('username', $result[0]['#theme']);
  $this
    ->assertEqual(spl_object_hash($user), spl_object_hash($result[0]['#account']));
  $result = $user->{$this->fieldName}
    ->view([
    'type' => 'user_name',
    'settings' => [
      'link_to_entity' => FALSE,
    ],
  ]);
  $this
    ->assertEqual($user
    ->getDisplayName(), $result[0]['#markup']);
  $user = User::getAnonymousUser();
  $result = $user->{$this->fieldName}
    ->view([
    'type' => 'user_name',
  ]);
  $this
    ->assertEqual('username', $result[0]['#theme']);
  $this
    ->assertEqual(spl_object_hash($user), spl_object_hash($result[0]['#account']));
  $result = $user->{$this->fieldName}
    ->view([
    'type' => 'user_name',
    'settings' => [
      'link_to_entity' => FALSE,
    ],
  ]);
  $this
    ->assertEqual($user
    ->getDisplayName(), $result[0]['#markup']);
  $this
    ->assertEqual($this
    ->config('user.settings')
    ->get('anonymous'), $result[0]['#markup']);
}