function EntityViewBuilderTest::testEntityViewBuilderCache

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

Tests entity render cache handling.

File

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

Class

EntityViewBuilderTest
Tests the entity view builder.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityViewBuilderCache() {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    $cache_contexts_manager = \Drupal::service("cache_contexts_manager");
    $cache = \Drupal::cache();
    // Force a request via GET so we can get drupal_render() cache working.
    $request = \Drupal::request();
    $request_method = $request->server
        ->get('REQUEST_METHOD');
    $request->setMethod('GET');
    $entity_test = $this->createTestEntity('entity_test');
    // Test that new entities (before they are saved for the first time) do not
    // generate a cache entry.
    $build = $this->container
        ->get('entity_type.manager')
        ->getViewBuilder('entity_test')
        ->view($entity_test, 'full');
    $this->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == [
        'tags',
        'contexts',
        'max-age',
    ], 'The render array element of new (unsaved) entities is not cached, but does have cache tags set.');
    // Get a fully built entity view render array.
    $entity_test->save();
    $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';
    // Test that a cache entry is created.
    $renderer->renderRoot($build);
    $this->assertNotEmpty($this->container
        ->get('cache.' . $bin)
        ->get($cid), 'The entity render element has been cached.');
    // Re-save the entity and check that the cache entry has been deleted.
    $cache->set('kittens', 'Kitten data', Cache::PERMANENT, $build['#cache']['tags']);
    $entity_test->save();
    $this->assertFalse($this->container
        ->get('cache.' . $bin)
        ->get($cid), 'The entity render cache has been cleared when the entity was saved.');
    $this->assertFalse($cache->get('kittens'), 'The entity saving has invalidated cache tags.');
    // Rebuild the render array (creating a new cache entry in the process) and
    // delete the entity to check the cache entry is deleted.
    unset($build['#printed']);
    $renderer->renderRoot($build);
    $this->assertNotEmpty($this->container
        ->get('cache.' . $bin)
        ->get($cid), 'The entity render element has been cached.');
    $entity_test->delete();
    $this->assertFalse($this->container
        ->get('cache.' . $bin)
        ->get($cid), 'The entity render cache 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.