class UserNameFormatterTest
Same name and namespace in other branches
- 11.x core/modules/user/tests/src/Kernel/Field/UserNameFormatterTest.php \Drupal\Tests\user\Kernel\Field\UserNameFormatterTest
- 10 core/modules/user/tests/src/Kernel/Field/UserNameFormatterTest.php \Drupal\Tests\user\Kernel\Field\UserNameFormatterTest
- 8.9.x core/modules/user/tests/src/Kernel/Field/UserNameFormatterTest.php \Drupal\Tests\user\Kernel\Field\UserNameFormatterTest
Tests the user_name formatter.
@group field
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\user\Kernel\Field\UserNameFormatterTest extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of UserNameFormatterTest
File
-
core/
modules/ user/ tests/ src/ Kernel/ Field/ UserNameFormatterTest.php, line 15
Namespace
Drupal\Tests\user\Kernel\FieldView source
class UserNameFormatterTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'field',
'user',
'system',
];
/**
* @var string
*/
protected $entityType;
/**
* @var string
*/
protected $bundle;
/**
* @var string
*/
protected $fieldName;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->installConfig([
'field',
]);
$this->installEntitySchema('user');
$this->installSchema('system', [
'sequences',
]);
$this->entityType = 'user';
$this->bundle = $this->entityType;
$this->fieldName = 'name';
}
/**
* Renders fields of a given entity with a given display.
*
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* The entity object with attached fields to render.
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display to render the fields in.
*
* @return string
* The rendered entity fields.
*/
protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
$content = $display->build($entity);
$content = $this->render($content);
return $content;
}
/**
* Tests the formatter output.
*/
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']);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.