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 cache is rendered.
$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());