function EntityReferenceFormatterTest::testEntityFormatterRecursiveRendering

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testEntityFormatterRecursiveRendering()
  2. 10 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testEntityFormatterRecursiveRendering()
  3. 11.x core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testEntityFormatterRecursiveRendering()

Tests the recursive rendering protection of the entity formatter.

File

core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php, line 238

Class

EntityReferenceFormatterTest
Tests the formatters functionality.

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testEntityFormatterRecursiveRendering() {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    $formatter = 'entity_reference_entity_view';
    $view_builder = $this->entityTypeManager
        ->getViewBuilder($this->entityType);
    // Set the default view mode to use the 'entity_reference_entity_view'
    // formatter.
    \Drupal::service('entity_display.repository')->getViewDisplay($this->entityType, $this->bundle)
        ->setComponent($this->fieldName, [
        'type' => $formatter,
    ])
        ->save();
    $storage = \Drupal::entityTypeManager()->getStorage($this->entityType);
    $referencing_entity_1 = $storage->create([
        'name' => $this->randomMachineName(),
    ]);
    $referencing_entity_1->save();
    // Create a self-reference.
    $referencing_entity_1->{$this->fieldName}->entity = $referencing_entity_1;
    $referencing_entity_1->save();
    // Check that the recursive rendering stops after it reaches the specified
    // limit.
    $build = $view_builder->view($referencing_entity_1, 'default');
    $output = $renderer->renderRoot($build);
    // The title of entity_test entities is printed twice by default, so we have
    // to multiply the formatter's recursive rendering protection limit by 2.
    // Additionally, we have to take into account 2 additional occurrences of
    // the entity title because we're rendering the full entity, not just the
    // reference field.
    $expected_occurrences = EntityReferenceEntityFormatter::RECURSIVE_RENDER_LIMIT * 2 + 2;
    $actual_occurrences = substr_count($output, $referencing_entity_1->label());
    $this->assertEquals($expected_occurrences, $actual_occurrences);
    // Repeat the process with another entity in order to check that the
    // 'recursive_render_id' counter is generated properly.
    $referencing_entity_2 = $storage->create([
        'name' => $this->randomMachineName(),
    ]);
    $referencing_entity_2->save();
    $referencing_entity_2->{$this->fieldName}->entity = $referencing_entity_2;
    $referencing_entity_2->save();
    $build = $view_builder->view($referencing_entity_2, 'default');
    $output = $renderer->renderRoot($build);
    $actual_occurrences = substr_count($output, $referencing_entity_2->label());
    $this->assertEquals($expected_occurrences, $actual_occurrences);
    // Now render both entities at the same time and check again.
    $build = $view_builder->viewMultiple([
        $referencing_entity_1,
        $referencing_entity_2,
    ], 'default');
    $output = $renderer->renderRoot($build);
    $actual_occurrences = substr_count($output, $referencing_entity_1->label());
    $this->assertEquals($expected_occurrences, $actual_occurrences);
    $actual_occurrences = substr_count($output, $referencing_entity_2->label());
    $this->assertEquals($expected_occurrences, $actual_occurrences);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.