function CacheTagTest::testTagCaching
Same name in other branches
- 9 core/modules/views/tests/src/Functional/Plugin/CacheTagTest.php \Drupal\Tests\views\Functional\Plugin\CacheTagTest::testTagCaching()
- 10 core/modules/views/tests/src/Functional/Plugin/CacheTagTest.php \Drupal\Tests\views\Functional\Plugin\CacheTagTest::testTagCaching()
- 11.x core/modules/views/tests/src/Functional/Plugin/CacheTagTest.php \Drupal\Tests\views\Functional\Plugin\CacheTagTest::testTagCaching()
Tests the tag cache plugin.
File
-
core/
modules/ views/ tests/ src/ Functional/ Plugin/ CacheTagTest.php, line 122
Class
- CacheTagTest
- Tests tag cache plugin.
Namespace
Drupal\Tests\views\Functional\PluginCode
public function testTagCaching() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$view = Views::getView('test_tag_cache');
$build = $view->buildRenderable();
$renderer->renderPlain($build);
// Saving the view should invalidate the tags.
$cache_plugin = $view->display_handler
->getPlugin('cache');
$this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.');
$this->assertNotEmpty($this->getRenderCache($view), 'Output cache found.');
$view->storage
->save();
$this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after the view is saved.');
$this->assertFalse($this->getRenderCache($view), 'Output cache empty after the view is saved.');
$view->destroy();
$build = $view->buildRenderable();
$renderer->renderPlain($build);
// Test invalidating the nodes in this view invalidates the cache.
$cache_plugin = $view->display_handler
->getPlugin('cache');
$this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.');
$this->assertNotEmpty($this->getRenderCache($view), 'Output cache found.');
$this->nodeViewBuilder
->resetCache($this->pages);
$this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after resetCache is called with pages.');
$this->assertFalse($this->getRenderCache($view), 'Output cache empty after resetCache is called with pages.');
$view->destroy();
$build = $view->buildRenderable();
$renderer->renderPlain($build);
// Test saving a node in this view invalidates the cache.
$cache_plugin = $view->display_handler
->getPlugin('cache');
$this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.');
$this->assertNotEmpty($this->getRenderCache($view), 'Output cache found.');
$node = reset($this->pages);
$node->save();
$this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after a page node is saved.');
$this->assertFalse($this->getRenderCache($view), 'Output cache empty after a page node is saved.');
$view->destroy();
$build = $view->buildRenderable();
$renderer->renderPlain($build);
// Test saving a node not in this view invalidates the cache too.
$cache_plugin = $view->display_handler
->getPlugin('cache');
$this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.');
$this->assertNotEmpty($this->getRenderCache($view), 'Output cache found.');
$this->article
->save();
$this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after an article node is saved.');
$this->assertFalse($this->getRenderCache($view), 'Output cache empty after an article node is saved.');
$view->destroy();
$build = $view->buildRenderable();
$renderer->renderPlain($build);
// Test that invalidating a tag for a user, does not invalidate the cache,
// as the user entity type will not be contained in the views cache tags.
$cache_plugin = $view->display_handler
->getPlugin('cache');
$this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.');
$this->assertNotEmpty($this->getRenderCache($view), 'Output cache found.');
$this->userViewBuilder
->resetCache([
$this->user,
]);
$cache_plugin = $view->display_handler
->getPlugin('cache');
$this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found after a user is invalidated.');
$this->assertNotEmpty($this->getRenderCache($view), 'Output cache found after a user is invalidated.');
$view->destroy();
// Invalidate the views cache tags in order to invalidate the render
// caching.
\Drupal::service('cache_tags.invalidator')->invalidateTags($view->storage
->getCacheTagsToInvalidate());
$build = $view->buildRenderable();
$renderer->renderPlain($build);
// Test the cacheFlush method invalidates the cache.
$cache_plugin = $view->display_handler
->getPlugin('cache');
$this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.');
$this->assertNotEmpty($this->getRenderCache($view), 'Output cache found.');
$cache_plugin->cacheFlush();
$cache_plugin = $view->display_handler
->getPlugin('cache');
$this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after the cacheFlush() method is called.');
$this->assertFalse($this->getRenderCache($view), 'Output cache empty after the cacheFlush() method is called.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.