public function EntityReferenceFormatterTest::testLabelFormatter in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php \Drupal\field\Tests\EntityReference\EntityReferenceFormatterTest::testLabelFormatter()
Tests the label formatter.
File
- core/
modules/ field/ src/ Tests/ EntityReference/ EntityReferenceFormatterTest.php, line 207 - Contains \Drupal\field\Tests\EntityReference\EntityReferenceFormatterTest.
Class
- EntityReferenceFormatterTest
- Tests the formatters functionality.
Namespace
Drupal\field\Tests\EntityReferenceCode
public function testLabelFormatter() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container
->get('renderer');
$formatter = 'entity_reference_label';
// 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
->assertEqual($build['#cache'], $expected_field_cacheability, 'The field render array contains the entity access cacheability metadata');
$expected_item_1 = array(
'#type' => 'link',
'#title' => $this->referencedEntity
->label(),
'#url' => $this->referencedEntity
->urlInfo(),
'#options' => $this->referencedEntity
->urlInfo()
->getOptions(),
'#cache' => array(
'contexts' => [
'user.permissions',
],
'tags' => $this->referencedEntity
->getCacheTags(),
),
);
$this
->assertEqual($renderer
->renderRoot($build[0]), $renderer
->renderRoot($expected_item_1), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
$this
->assertEqual(CacheableMetadata::createFromRenderArray($build[0]), CacheableMetadata::createFromRenderArray($expected_item_1));
// The second referenced entity is "autocreated", therefore not saved and
// lacking any URL info.
$expected_item_2 = array(
'#plain_text' => $this->unsavedReferencedEntity
->label(),
'#cache' => array(
'contexts' => [
'user.permissions',
],
'tags' => $this->unsavedReferencedEntity
->getCacheTags(),
'max-age' => Cache::PERMANENT,
),
);
$this
->assertEqual($build[1], $expected_item_2, 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, array(
'link' => FALSE,
));
$this
->assertEqual($build[0]['#plain_text'], $this->referencedEntity
->label(), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
$this
->assertEqual($build[1]['#plain_text'], $this->unsavedReferencedEntity
->label(), 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, which means
// \Drupal\Core\Entity\EntityInterface::urlInfo() will throw an exception
// and the label formatter will output only the label instead of a link.
$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 = entity_create('entity_test_label', array(
'name' => $this
->randomMachineName(),
));
$referenced_entity_with_no_link_template
->save();
$build = $this
->buildRenderArray([
$referenced_entity_with_no_link_template,
], $formatter, array(
'link' => TRUE,
));
$this
->assertEqual($build[0]['#plain_text'], $referenced_entity_with_no_link_template
->label(), sprintf('The markup returned by the %s formatter is correct for an entity type with no valid link template.', $formatter));
}