public function DynamicEntityReferenceFormatterTest::testEntityFormatter in Dynamic Entity Reference 8
Same name and namespace in other branches
- 8.2 tests/src/Kernel/DynamicEntityReferenceFormatterTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\DynamicEntityReferenceFormatterTest::testEntityFormatter()
Tests the entity formatter.
File
- tests/
src/ Kernel/ DynamicEntityReferenceFormatterTest.php, line 214
Class
- DynamicEntityReferenceFormatterTest
- Tests the formatters functionality.
Namespace
Drupal\Tests\dynamic_entity_reference\KernelCode
public function testEntityFormatter() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container
->get('renderer');
$formatter = 'dynamic_entity_reference_entity_view';
$build = $this
->buildRenderArray([
$this->referencedEntity,
$this->unsavedReferencedEntity,
], $formatter, [
$this->referencedEntity
->getEntityTypeId() => [
'view_mode' => 'default',
'link' => FALSE,
],
$this->unsavedReferencedEntity
->getEntityTypeId() => [
'view_mode' => 'default',
'link' => FALSE,
],
]);
// Test the first field item.
$expected_rendered_name_field_1 = '
<div class="field field--name-name field--type-string field--label-hidden field__item">' . $this->referencedEntity
->label() . '</div>
';
$expected_rendered_body_field_1 = '
<div class="clearfix text-formatted field field--name-body field--type-text field--label-above">
<div class="field__label">Body</div>
<div class="field__item"><p>Hello, world!</p></div>
</div>
';
$renderer
->renderRoot($build[0]);
$this
->assertEquals($build[0]['#markup'], 'default | ' . $this->referencedEntity
->label() . $expected_rendered_name_field_1 . $expected_rendered_body_field_1, sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
$expected_cache_tags = Cache::mergeTags(\Drupal::entityTypeManager()
->getViewBuilder($this->entityType)
->getCacheTags(), $this->referencedEntity
->getCacheTags());
$expected_cache_tags = Cache::mergeTags($expected_cache_tags, FilterFormat::load('full_html')
->getCacheTags());
$this
->assertEquals($build[0]['#cache']['tags'], $expected_cache_tags, (string) new FormattableMarkup('The @formatter formatter has the expected cache tags.', [
'@formatter' => $formatter,
]));
// Test the second field item.
$expected_rendered_name_field_2 = '
<div class="field field--name-name field--type-string field--label-hidden field__item">' . $this->unsavedReferencedEntity
->label() . '</div>
';
$expected_rendered_body_field_2 = '
<div class="clearfix text-formatted field field--name-body field--type-text field--label-above">
<div class="field__label">Body</div>
<div class="field__item"><p>Hello, unsaved world!</p></div>
</div>
';
$renderer
->renderRoot($build[1]);
$this
->assertEquals($build[1]['#markup'], 'default | ' . $this->unsavedReferencedEntity
->label() . $expected_rendered_name_field_2 . $expected_rendered_body_field_2, sprintf('The markup returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));
}