function EntityReferenceFormatterTest::testEntityFormatter
Same name in other branches
- 9 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testEntityFormatter()
- 8.9.x core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testEntityFormatter()
- 10 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testEntityFormatter()
Tests the entity formatter.
File
-
core/
modules/ field/ tests/ src/ Kernel/ EntityReference/ EntityReferenceFormatterTest.php, line 200
Class
- EntityReferenceFormatterTest
- Tests the formatters functionality.
Namespace
Drupal\Tests\field\Kernel\EntityReferenceCode
public function testEntityFormatter() : void {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container
->get('renderer');
$formatter = 'entity_reference_entity_view';
$build = $this->buildRenderArray([
$this->referencedEntity,
$this->unsavedReferencedEntity,
], $formatter);
// Test the first field item.
$expected_rendered_name_field_1 = '
<div>' . $this->referencedEntity
->label() . '</div>
';
$expected_rendered_body_field_1 = '
<div>
<div>Body</div>
<div><p>Hello, world!</p></div>
</div>
';
$renderer->renderRoot($build[0]);
$this->assertSame('default | ' . $this->referencedEntity
->label() . $expected_rendered_name_field_1 . $expected_rendered_body_field_1, (string) $build[0]['#markup'], 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($expected_cache_tags, $build[0]['#cache']['tags'], "The {$formatter} formatter has the expected cache tags.");
// Test the second field item.
$expected_rendered_name_field_2 = '
<div>' . $this->unsavedReferencedEntity
->label() . '</div>
';
$expected_rendered_body_field_2 = '
<div>
<div>Body</div>
<div><p>Hello, unsaved world!</p></div>
</div>
';
$renderer->renderRoot($build[1]);
$this->assertSame('default | ' . $this->unsavedReferencedEntity
->label() . $expected_rendered_name_field_2 . $expected_rendered_body_field_2, (string) $build[1]['#markup'], sprintf('The markup returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.