function EntityReferenceFormatterTest::testEntityReferenceRecursiveProtectionWithManyRenderedEntities

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

Renders the same entity referenced from different places.

File

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

Class

EntityReferenceFormatterTest
Tests the formatters functionality.

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testEntityReferenceRecursiveProtectionWithManyRenderedEntities() : void {
    $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 = $this->entityTypeManager
        ->getStorage($this->entityType);
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $referenced_entity */
    $referenced_entity = $storage->create([
        'name' => $this->randomMachineName(),
    ]);
    $range = range(0, 30);
    $referencing_entities = array_map(function () use ($storage, $referenced_entity) {
        $referencing_entity = $storage->create([
            'name' => $this->randomMachineName(),
            $this->fieldName => $referenced_entity,
        ]);
        $referencing_entity->save();
        return $referencing_entity;
    }, $range);
    $build = $view_builder->viewMultiple($referencing_entities, 'default');
    $output = $this->render($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 = 30 * 2 + 2;
    $actual_occurrences = substr_count($output, $referenced_entity->get('name')->value);
    $this->assertEquals($expected_occurrences, $actual_occurrences);
}

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