You are here

public function EntityViewBuilderTest::testEntityViewBuilderCache in Drupal 9

Same name and namespace in other branches
  1. 8 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
    ->assertNotEmpty($build['#cache']);
  $this
    ->assertEquals([
    'tags',
    'contexts',
    'max-age',
  ], array_keys($build['#cache']), '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);
}