You are here

public function EntityViewBuilderTest::testEntityViewBuilderCacheWithReferences in Zircon Profile 8.0

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

Tests entity render cache with references.

File

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

Class

EntityViewBuilderTest
Tests the entity view builder.

Namespace

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