3 namespace Drupal\Tests\views\Functional\Plugin;
5 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\Plugin\views\display\DisplayPluginBase;
8 use Drupal\views\Views;
11 * Tests pluggable caching for views via a web test.
14 * @see views_plugin_cache
16 class CacheWebTest extends ViewTestBase {
18 use AssertPageCacheContextsAndTagsTrait;
21 * Views used by this test.
25 public static $testViews = ['test_display'];
32 public static $modules = ['taxonomy'];
37 protected function setUp($import_test_views = TRUE) {
38 parent::setUp($import_test_views);
40 $this->enableViewsTestModule();
44 * Tests the output caching on an actual page.
46 public function testCacheOutputOnPage() {
47 $view = Views::getView('test_display');
48 $view->storage->setStatus(TRUE);
49 $view->setDisplay('page_1');
50 $view->display_handler->overrideOption('cache', [
53 'results_lifespan' => '3600',
54 'output_lifespan' => '3600',
58 $this->container->get('router.builder')->rebuildIfNeeded();
60 /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
61 $render_cache = \Drupal::service('render_cache');
62 $cache_element = DisplayPluginBase::buildBasicRenderable('test_display', 'page_1');
63 $cache_element['#cache'] += ['contexts' => $this->container->getParameter('renderer.config')['required_cache_contexts']];
64 $this->assertFalse($render_cache->get($cache_element));
66 $this->drupalGet('test-display');
67 $this->assertResponse(200);
68 $this->assertTrue($render_cache->get($cache_element));
70 'config:user.role.anonymous',
71 'config:views.view.test_display',
75 $this->assertCacheTags($cache_tags);
77 $this->drupalGet('test-display');
78 $this->assertResponse(200);
79 $this->assertTrue($render_cache->get($cache_element));
80 $this->assertCacheTags($cache_tags);
84 * Tests that a display without caching still contains the cache metadata.
86 public function testDisplayWithoutCacheStillBubblesMetadata() {
87 $view = Views::getView('test_display');
89 $uncached_block = $view->buildRenderable('block_1', [], FALSE);
90 $cached_block = $view->buildRenderable('block_1', [], TRUE);
91 $this->assertEqual($uncached_block['#cache']['contexts'], $cached_block['#cache']['contexts'], 'Cache contexts are the same when you render the view cached and uncached.');