function EntityReferenceFormatterTest::testEntityFormatterIterativeRendering

Tests that iterative rendering is allowed by recursive render protection.

File

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

Class

EntityReferenceFormatterTest
Tests the formatters functionality.

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testEntityFormatterIterativeRendering() : 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();
  // Set count to higher than the previous render protection limit of 20.
  $count = 21;
  // Render the same entity multiple times to check that iterative rendering
  // is allowed as long as the entity is not being recursively rendered.
  $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()));
}

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