function EntityViewBuilderTest::testEntityViewBuilderCacheWithReferences

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

Tests entity render cache with references.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php, line 97

Class

EntityViewBuilderTest
Tests the entity view builder.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityViewBuilderCacheWithReferences() {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    $cache_contexts_manager = \Drupal::service("cache_contexts_manager");
    // Force a request via GET so we can get
    // \Drupal::service('renderer')->render() cache working.
    $request = \Drupal::request();
    $request_method = $request->server
        ->get('REQUEST_METHOD');
    $request->setMethod('GET');
    // Create an entity reference field and an entity that will be referenced.
    $this->createEntityReferenceField('entity_test', 'entity_test', 'reference_field', 'Reference', 'entity_test');
    \Drupal::service('entity_display.repository')->getViewDisplay('entity_test', 'entity_test', 'full')
        ->setComponent('reference_field', [
        'type' => 'entity_reference_entity_view',
        'settings' => [
            'link' => FALSE,
        ],
    ])
        ->save();
    $entity_test_reference = $this->createTestEntity('entity_test');
    $entity_test_reference->save();
    // Get a fully built entity view render array for the referenced entity.
    $build = $this->container
        ->get('entity_type.manager')
        ->getViewBuilder('entity_test')
        ->view($entity_test_reference, 'full');
    $cid_parts = array_merge($build['#cache']['keys'], $cache_contexts_manager->convertTokensToKeys([
        'languages:' . LanguageInterface::TYPE_INTERFACE,
        'theme',
        'user.permissions',
    ])
        ->getKeys());
    $cid_reference = implode(':', $cid_parts);
    $bin_reference = $build['#cache']['bin'];
    // Mock the build array to not require the theme registry.
    unset($build['#theme']);
    $build['#markup'] = 'entity_render_test';
    $renderer->renderRoot($build);
    // Test that a cache entry was created for the referenced entity.
    $this->assertNotEmpty($this->container
        ->get('cache.' . $bin_reference)
        ->get($cid_reference), 'The entity render element for the referenced entity has been cached.');
    // Create another entity that references the first one.
    $entity_test = $this->createTestEntity('entity_test');
    $entity_test->reference_field->entity = $entity_test_reference;
    $entity_test->save();
    // Get a fully built entity view render array.
    $build = $this->container
        ->get('entity_type.manager')
        ->getViewBuilder('entity_test')
        ->view($entity_test, 'full');
    $cid_parts = array_merge($build['#cache']['keys'], $cache_contexts_manager->convertTokensToKeys([
        'languages:' . LanguageInterface::TYPE_INTERFACE,
        'theme',
        'user.permissions',
    ])
        ->getKeys());
    $cid = implode(':', $cid_parts);
    $bin = $build['#cache']['bin'];
    // Mock the build array to not require the theme registry.
    unset($build['#theme']);
    $build['#markup'] = 'entity_render_test';
    $renderer->renderRoot($build);
    // Test that a cache entry is created.
    $this->assertNotEmpty($this->container
        ->get('cache.' . $bin)
        ->get($cid), 'The entity render element has been cached.');
    // Save the entity and verify that both cache entries have been deleted.
    $entity_test_reference->save();
    $this->assertFalse($this->container
        ->get('cache.' . $bin)
        ->get($cid), 'The entity render cache has been cleared when the entity was deleted.');
    $this->assertFalse($this->container
        ->get('cache.' . $bin_reference)
        ->get($cid_reference), 'The entity render cache for the referenced entity has been cleared when the entity was deleted.');
    // Restore the previous request method.
    $request->setMethod($request_method);
}

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