1<?php 2 3namespace Drupal\Tests\views\Functional\Plugin; 4 5use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; 6use Drupal\Tests\views\Functional\ViewTestBase; 7use Drupal\views\Plugin\views\display\DisplayPluginBase; 8use Drupal\views\Views; 9 10/** 11 * Tests pluggable caching for views via a web test. 12 * 13 * @group views 14 * @see views_plugin_cache 15 */ 16class CacheWebTest extends ViewTestBase { 17 18 use AssertPageCacheContextsAndTagsTrait; 19 20 /** 21 * Views used by this test. 22 * 23 * @var array 24 */ 25 public static $testViews = ['test_display']; 26 27 /** 28 * Modules to enable. 29 * 30 * @var array 31 */ 32 public static $modules = ['taxonomy']; 33 34 /** 35 * {@inheritdoc} 36 */ 37 protected $defaultTheme = 'stark'; 38 39 /** 40 * {@inheritdoc} 41 */ 42 protected function setUp($import_test_views = TRUE) { 43 parent::setUp($import_test_views); 44 45 $this->enableViewsTestModule(); 46 } 47 48 /** 49 * Tests the output caching on an actual page. 50 */ 51 public function testCacheOutputOnPage() { 52 $view = Views::getView('test_display'); 53 $view->storage->setStatus(TRUE); 54 $view->setDisplay('page_1'); 55 $view->display_handler->overrideOption('cache', [ 56 'type' => 'time', 57 'options' => [ 58 'results_lifespan' => '3600', 59 'output_lifespan' => '3600', 60 ], 61 ]); 62 $view->save(); 63 $this->container->get('router.builder')->rebuildIfNeeded(); 64 65 /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */ 66 $render_cache = \Drupal::service('render_cache'); 67 $cache_element = DisplayPluginBase::buildBasicRenderable('test_display', 'page_1'); 68 $cache_element['#cache'] += ['contexts' => $this->container->getParameter('renderer.config')['required_cache_contexts']]; 69 $this->assertFalse($render_cache->get($cache_element)); 70 71 $this->drupalGet('test-display'); 72 $this->assertSession()->statusCodeEquals(200); 73 $this->assertNotEmpty($render_cache->get($cache_element)); 74 $cache_tags = [ 75 'config:user.role.anonymous', 76 'config:views.view.test_display', 77 'node_list', 78 'rendered', 79 ]; 80 $this->assertCacheTags($cache_tags); 81 82 $this->drupalGet('test-display'); 83 $this->assertSession()->statusCodeEquals(200); 84 $this->assertNotEmpty($render_cache->get($cache_element)); 85 $this->assertCacheTags($cache_tags); 86 } 87 88 /** 89 * Tests that a display without caching still contains the cache metadata. 90 */ 91 public function testDisplayWithoutCacheStillBubblesMetadata() { 92 $view = Views::getView('test_display'); 93 94 $uncached_block = $view->buildRenderable('block_1', [], FALSE); 95 $cached_block = $view->buildRenderable('block_1', [], TRUE); 96 $this->assertEqual($uncached_block['#cache']['contexts'], $cached_block['#cache']['contexts'], 'Cache contexts are the same when you render the view cached and uncached.'); 97 } 98 99} 100