You are here

public function EntityViewBuilderTest::testEntityViewBuilderCacheToggling 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::testEntityViewBuilderCacheToggling()

Tests entity render cache toggling.

File

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

Class

EntityViewBuilderTest
Tests the entity view builder.

Namespace

Drupal\system\Tests\Entity

Code

public function testEntityViewBuilderCacheToggling() {
  $entity_test = $this
    ->createTestEntity('entity_test');
  $entity_test
    ->save();

  // Test a view mode in default conditions: render caching is enabled for
  // the entity type and the view mode.
  $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',
    'keys',
    'bin',
  ], 'A view mode with render cache enabled has the correct output (cache tags, keys, contexts, max-age and bin).');

  // Test that a view mode can opt out of render caching.
  $build = $this->container
    ->get('entity.manager')
    ->getViewBuilder('entity_test')
    ->view($entity_test, 'test');
  $this
    ->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == [
    'tags',
    'contexts',
    'max-age',
  ], 'A view mode with render cache disabled has the correct output (only cache tags, contexts and max-age).');

  // Test that an entity type can opt out of render caching completely.
  $entity_test_no_cache = $this
    ->createTestEntity('entity_test_label');
  $entity_test_no_cache
    ->save();
  $build = $this->container
    ->get('entity.manager')
    ->getViewBuilder('entity_test_label')
    ->view($entity_test_no_cache, 'full');
  $this
    ->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == [
    'tags',
    'contexts',
    'max-age',
  ], 'An entity type can opt out of render caching regardless of view mode configuration, but always has cache tags, contexts and max-age set.');
}