You are here

public function EntityReferenceFormatterTest::testEntityFormatterRecursiveRendering in Drupal 9

Same name and namespace in other branches
  1. 8 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 236

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->name->value);
  $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->name->value);
  $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->name->value);
  $this
    ->assertEquals($expected_occurrences, $actual_occurrences);
  $actual_occurrences = substr_count($output, $referencing_entity_2->name->value);
  $this
    ->assertEquals($expected_occurrences, $actual_occurrences);
}