View source
<?php
namespace Drupal\Tests\views\Functional\Plugin;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
class CacheTagTest extends ViewTestBase {
public static $testViews = [
'test_tag_cache',
];
protected static $modules = [
'node',
];
protected $defaultTheme = 'stark';
protected $nodeStorage;
protected $nodeViewBuilder;
protected $userViewBuilder;
protected $pages;
protected $article;
protected $user;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this->nodeStorage = $this->container
->get('entity_type.manager')
->getStorage('node');
$this->nodeViewBuilder = $this->container
->get('entity_type.manager')
->getViewBuilder('node');
$this->userViewBuilder = $this->container
->get('entity_type.manager')
->getViewBuilder('user');
for ($i = 1; $i <= 5; $i++) {
$this->pages[] = $this
->drupalCreateNode([
'title' => "Test {$i}",
'type' => 'page',
]);
}
$this->article = $this
->drupalCreateNode([
'title' => "Test article",
'type' => 'article',
]);
$this->user = $this
->drupalCreateUser();
\Drupal::request()
->setMethod('GET');
}
protected function getRenderCache(ViewExecutable $view) {
$render_cache = \Drupal::service('render_cache');
$view->element = [
'#cache' => [],
];
$build = $view
->buildRenderable();
$build['#cache']['contexts'] = Cache::mergeContexts($build['#cache']['contexts'], $this->container
->getParameter('renderer.config')['required_cache_contexts']);
return $render_cache
->get($build);
}
public function testTagCaching() {
$renderer = \Drupal::service('renderer');
$view = Views::getView('test_tag_cache');
$build = $view
->buildRenderable();
$renderer
->renderPlain($build);
$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);
$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);
$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);
$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);
$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();
\Drupal::service('cache_tags.invalidator')
->invalidateTags($view->storage
->getCacheTagsToInvalidate());
$build = $view
->buildRenderable();
$renderer
->renderPlain($build);
$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.');
}
}