You are here

public function EntityViewBuilderTest::testEntityViewBuilderCache in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityViewBuilderTest.php \Drupal\system\Tests\Entity\EntityViewBuilderTest::testEntityViewBuilderCache()

Tests entity render cache handling.

File

core/modules/system/src/Tests/Entity/EntityViewBuilderTest.php, line 41
Contains \Drupal\system\Tests\Entity\EntityViewBuilderTest.

Class

EntityViewBuilderTest
Tests the entity view builder.

Namespace

Drupal\system\Tests\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.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.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
    ->assertTrue($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
    ->assertTrue($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);
}