UuidFormatterTest.php in Drupal 9
File
core/modules/field/tests/src/Kernel/String/UuidFormatterTest.php
View source
<?php
namespace Drupal\Tests\field\Kernel\String;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
class UuidFormatterTest extends KernelTestBase {
protected static $modules = [
'field',
'entity_test',
'system',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'system',
'field',
]);
$this
->installEntitySchema('entity_test');
}
public function testUuidStringFormatter() {
$entity = EntityTest::create([]);
$entity
->save();
$uuid_field = $entity
->get('uuid');
$render_array = $uuid_field
->view([]);
$this
->assertSame($entity
->uuid(), $render_array[0]['#context']['value'], 'The rendered UUID matches the entity UUID.');
$this
->assertStringContainsString($entity
->uuid(), $this
->render($render_array), 'The rendered UUID found.');
$render_array = $uuid_field
->view([
'settings' => [
'link_to_entity' => TRUE,
],
]);
$this
->assertSame('link', $render_array[0]['#type']);
$this
->assertSame($entity
->uuid(), $render_array[0]['#title']['#context']['value']);
$this
->assertSame($entity
->toUrl()
->toString(), $render_array[0]['#url']
->toString());
$rendered = $this
->render($render_array);
$this
->assertStringContainsString($entity
->uuid(), $rendered, 'The rendered UUID found.');
$this
->assertStringContainsString($entity
->toUrl()
->toString(), $rendered, 'The rendered entity URL found.');
}
}