function AssertViewsCacheTagsTrait::assertViewsCacheTags
Same name in other branches
- 9 core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php \Drupal\views\Tests\AssertViewsCacheTagsTrait::assertViewsCacheTags()
- 10 core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php \Drupal\views\Tests\AssertViewsCacheTagsTrait::assertViewsCacheTags()
- 11.x core/modules/views/src/Tests/AssertViewsCacheTagsTrait.php \Drupal\views\Tests\AssertViewsCacheTagsTrait::assertViewsCacheTags()
Asserts a view's result & render cache items' cache tags.
This methods uses a full view object in order to render the view.
Parameters
\Drupal\views\ViewExecutable $view: The view to test, must have caching enabled.
null|string[] $expected_results_cache: NULL when expecting no results cache item, a set of cache tags expected to be set on the results cache item otherwise.
bool $views_caching_is_enabled: Whether to expect an output cache item. If TRUE, the cache tags must match those in $expected_render_array_cache_tags.
string[] $expected_render_array_cache_tags: A set of cache tags expected to be set on the built view's render array.
Return value
array The render array.
3 calls to AssertViewsCacheTagsTrait::assertViewsCacheTags()
- FrontPageTest::doTestFrontPageViewCacheTags in core/
modules/ node/ tests/ src/ Functional/ Views/ FrontPageTest.php - Tests the cache tags on the front page.
- RenderCacheIntegrationTest::assertCacheTagsForEntityBasedView in core/
modules/ views/ tests/ src/ Kernel/ RenderCacheIntegrationTest.php - Tests cache tags on output & result cache items for an entity-based view.
- RenderCacheIntegrationTest::assertCacheTagsForFieldBasedView in core/
modules/ views/ tests/ src/ Kernel/ RenderCacheIntegrationTest.php - Tests cache tags on output & result cache items for a field-based view.
File
-
core/
modules/ views/ src/ Tests/ AssertViewsCacheTagsTrait.php, line 34
Class
Namespace
Drupal\views\TestsCode
protected function assertViewsCacheTags(ViewExecutable $view, $expected_results_cache, $views_caching_is_enabled, array $expected_render_array_cache_tags) {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
/** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
$render_cache = \Drupal::service('render_cache');
$build = $view->buildRenderable();
$original = $build;
// Ensure the current request is a GET request so that render caching is
// active for direct rendering of views, just like for actual requests.
/** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
$request_stack = \Drupal::service('request_stack');
$request = new Request();
$request->server
->set('REQUEST_TIME', REQUEST_TIME);
$view->setRequest($request);
$request_stack->push($request);
$renderer->renderRoot($build);
// Render array cache tags.
$this->pass('Checking render array cache tags.');
sort($expected_render_array_cache_tags);
$this->assertEqual($build['#cache']['tags'], $expected_render_array_cache_tags);
if ($views_caching_is_enabled) {
$this->pass('Checking Views results cache item cache tags.');
/** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache_plugin */
$cache_plugin = $view->display_handler
->getPlugin('cache');
// Results cache.
// Ensure that the views query is built.
$view->build();
$results_cache_item = \Drupal::cache('data')->get($cache_plugin->generateResultsKey());
if (is_array($expected_results_cache)) {
$this->assertNotEmpty($results_cache_item, 'Results cache item found.');
if ($results_cache_item) {
sort($expected_results_cache);
$this->assertEqual($results_cache_item->tags, $expected_results_cache);
}
}
else {
$this->assertNull($results_cache_item, 'Results cache item not found.');
}
$this->pass('Checking Views render cache item cache tags.');
$original['#cache'] += [
'contexts' => [],
];
$original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container
->getParameter('renderer.config')['required_cache_contexts']);
$render_cache_item = $render_cache->get($original);
if ($views_caching_is_enabled === TRUE) {
$this->assertNotEmpty($render_cache_item, 'Render cache item found.');
if ($render_cache_item) {
$this->assertEqual($render_cache_item['#cache']['tags'], $expected_render_array_cache_tags);
}
}
else {
$this->assertNull($render_cache_item, 'Render cache item not found.');
}
}
$view->destroy();
$request_stack->pop();
return $build;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.