function EntityReferenceFormatterTest::testEntityReferenceRecursionProtectionWithRepeatedReferencingEntity

Tests multiple renderings of an entity that references another.

File

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

Class

EntityReferenceFormatterTest
Tests the formatters functionality.

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testEntityReferenceRecursionProtectionWithRepeatedReferencingEntity() : void {
  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $view_builder = $this->entityTypeManager
    ->getViewBuilder($this->entityType);
  $this->createNonCacheableViewMode();
  $storage = \Drupal::entityTypeManager()->getStorage($this->entityType);
  $entity = $storage->create([
    'name' => $this->randomMachineName(),
  ]);
  $entity->save();
  $referencing_entity = $storage->create([
    'name' => $this->randomMachineName(),
    $this->fieldName => $entity->id(),
  ]);
  $referencing_entity->save();
  // Large-scale repetition within a single render root is not recursion.
  $count = 30;
  $build = $view_builder->viewMultiple(array_fill(0, $count, $referencing_entity), $this->nonCacheableViewMode);
  $output = (string) $renderer->renderRoot($build);
  // The title of entity_test entities is printed twice by default, so we have
  // to multiply our count by 2.
  $this->assertSame($count * 2, substr_count($output, $entity->label()));
  // Large-scale repetition across render roots is not recursion.
  for ($i = 0; $i < $count; $i++) {
    $build = $view_builder->view($referencing_entity, $this->nonCacheableViewMode);
    $output = (string) $renderer->renderRoot($build);
    // The title of entity_test entities is printed twice by default.
    $this->assertSame(2, substr_count($output, $entity->label()));
  }
}

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