function EntityReferenceFormatterTest::testLabelFormatter
Same name in other branches
- 9 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testLabelFormatter()
- 8.9.x core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testLabelFormatter()
- 10 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testLabelFormatter()
Tests the label formatter.
File
-
core/
modules/ field/ tests/ src/ Kernel/ EntityReference/ EntityReferenceFormatterTest.php, line 347
Class
- EntityReferenceFormatterTest
- Tests the formatters functionality.
Namespace
Drupal\Tests\field\Kernel\EntityReferenceCode
public function testLabelFormatter() : void {
$this->installEntitySchema('entity_test_label');
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container
->get('renderer');
$formatter = 'entity_reference_label';
// We need to create an anonymous user for access checks in the formatter.
$this->createUser(values: [
'uid' => 0,
'status' => 0,
'name' => '',
]);
// The 'link' settings is TRUE by default.
$build = $this->buildRenderArray([
$this->referencedEntity,
$this->unsavedReferencedEntity,
], $formatter);
$expected_field_cacheability = [
'contexts' => [],
'tags' => [],
'max-age' => Cache::PERMANENT,
];
$this->assertEquals($expected_field_cacheability, $build['#cache'], 'The field render array contains the entity access cacheability metadata');
$expected_item_1 = [
'#type' => 'link',
'#title' => $this->referencedEntity
->label(),
'#url' => $this->referencedEntity
->toUrl(),
'#options' => $this->referencedEntity
->toUrl()
->getOptions(),
'#cache' => [
'contexts' => [
'user.permissions',
],
'tags' => $this->referencedEntity
->getCacheTags(),
],
];
$this->assertEquals($renderer->renderRoot($expected_item_1), $renderer->renderRoot($build[0]), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
$this->assertEquals(CacheableMetadata::createFromRenderArray($expected_item_1), CacheableMetadata::createFromRenderArray($build[0]));
// The second referenced entity is "autocreated", therefore not saved and
// lacking any URL info.
$expected_item_2 = [
'#plain_text' => $this->unsavedReferencedEntity
->label(),
'#entity' => $this->unsavedReferencedEntity,
'#cache' => [
'contexts' => [
'user.permissions',
],
'tags' => $this->unsavedReferencedEntity
->getCacheTags(),
'max-age' => Cache::PERMANENT,
],
];
$this->assertEquals($expected_item_2, $build[1], sprintf('The render array returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));
// Test with the 'link' setting set to FALSE.
$build = $this->buildRenderArray([
$this->referencedEntity,
$this->unsavedReferencedEntity,
], $formatter, [
'link' => FALSE,
]);
$this->assertEquals($this->referencedEntity
->label(), $build[0]['#plain_text'], sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
$this->assertEquals($this->unsavedReferencedEntity
->label(), $build[1]['#plain_text'], sprintf('The markup returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));
// Test an entity type that doesn't have any link templates.
$field_storage_config = FieldStorageConfig::loadByName($this->entityType, $this->fieldName);
$field_storage_config->setSetting('target_type', 'entity_test_label');
$field_storage_config->save();
$referenced_entity_with_no_link_template = EntityTestLabel::create([
'name' => $this->randomMachineName(),
]);
$referenced_entity_with_no_link_template->save();
$build = $this->buildRenderArray([
$referenced_entity_with_no_link_template,
], $formatter, [
'link' => TRUE,
]);
$this->assertEquals($referenced_entity_with_no_link_template->label(), $build[0]['#plain_text'], sprintf('The markup returned by the %s formatter is correct for an entity type with no valid link template.', $formatter));
// Test link visibility if the URL is not accessible.
$entity_with_user = EntityTest::create([
'name' => $this->randomMachineName(),
'user_id' => $this->createUser(),
]);
$entity_with_user->save();
$build = $entity_with_user->get('user_id')
->view([
'type' => $formatter,
'settings' => [
'link' => TRUE,
],
]);
$this->assertEquals($build[0]['#plain_text'], $entity_with_user->get('user_id')->entity
->label(), 'For inaccessible links, the label should be displayed in plain text.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.