function EntityViewHookTest::testHookEntityDisplayBuildAlter

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityViewHookTest.php \Drupal\KernelTests\Core\Entity\EntityViewHookTest::testHookEntityDisplayBuildAlter()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityViewHookTest.php \Drupal\KernelTests\Core\Entity\EntityViewHookTest::testHookEntityDisplayBuildAlter()
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityViewHookTest.php \Drupal\KernelTests\Core\Entity\EntityViewHookTest::testHookEntityDisplayBuildAlter()

Tests hook_entity_display_build_alter().

File

core/tests/Drupal/KernelTests/Core/Entity/EntityViewHookTest.php, line 27

Class

EntityViewHookTest
Test view/render hooks for entities.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testHookEntityDisplayBuildAlter() : void {
    entity_test_create_bundle('display_build_alter_bundle');
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    $entity_ids = [];
    // Create some entities to test.
    for ($i = 0; $i < 5; $i++) {
        $entity = EntityTest::create([
            'name' => $this->randomMachineName(),
            'type' => 'display_build_alter_bundle',
        ]);
        $entity->save();
        $entity_ids[] = $entity->id();
    }
    
    /** @var \Drupal\entity_test\EntityTestViewBuilder $view_builder */
    $view_builder = $this->container
        ->get('entity_type.manager')
        ->getViewBuilder('entity_test');
    
    /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('entity_test');
    $storage->resetCache();
    $entities = $storage->loadMultiple($entity_ids);
    $build = $view_builder->viewMultiple($entities);
    $output = $renderer->renderRoot($build);
    $this->setRawContent($output->__toString());
    // Confirm that the content added in
    // entity_test_entity_display_build_alter() appears multiple times, not
    // just for the final entity.
    foreach ($entity_ids as $id) {
        $this->assertText('Content added in hook_entity_display_build_alter for entity id ' . $id);
    }
}

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