Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / CacheWebTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
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;
9
10 /**
11  * Tests pluggable caching for views via a web test.
12  *
13  * @group views
14  * @see views_plugin_cache
15  */
16 class 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 function setUp($import_test_views = TRUE) {
38     parent::setUp($import_test_views);
39
40     $this->enableViewsTestModule();
41   }
42
43   /**
44    * Tests the output caching on an actual page.
45    */
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', [
51       'type' => 'time',
52       'options' => [
53         'results_lifespan' => '3600',
54         'output_lifespan' => '3600',
55       ],
56     ]);
57     $view->save();
58     $this->container->get('router.builder')->rebuildIfNeeded();
59
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));
65
66     $this->drupalGet('test-display');
67     $this->assertResponse(200);
68     $this->assertTrue($render_cache->get($cache_element));
69     $cache_tags = [
70       'config:user.role.anonymous',
71       'config:views.view.test_display',
72       'node_list',
73       'rendered',
74     ];
75     $this->assertCacheTags($cache_tags);
76
77     $this->drupalGet('test-display');
78     $this->assertResponse(200);
79     $this->assertTrue($render_cache->get($cache_element));
80     $this->assertCacheTags($cache_tags);
81   }
82
83   /**
84    * Tests that a display without caching still contains the cache metadata.
85    */
86   public function testDisplayWithoutCacheStillBubblesMetadata() {
87     $view = Views::getView('test_display');
88
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.');
92   }
93
94 }