48861f999b5d4b86680424c0e3daae3940b469c2
[yaffs-website] / web / core / modules / page_cache / tests / src / Functional / PageCacheTagsIntegrationTest.php
1 <?php
2
3 namespace Drupal\Tests\page_cache\Functional;
4
5 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\node\NodeInterface;
8 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
9 use Drupal\Tests\BrowserTestBase;
10
11 /**
12  * Enables the page cache and tests its cache tags in various scenarios.
13  *
14  * @group Cache
15  * @see \Drupal\Tests\page_cache\Functional\PageCacheTest
16  * @see \Drupal\node\Tests\NodePageCacheTest
17  * @see \Drupal\menu_ui\Tests\MenuTest::testMenuBlockPageCacheTags()
18  */
19 class PageCacheTagsIntegrationTest extends BrowserTestBase {
20
21   use AssertPageCacheContextsAndTagsTrait;
22
23   protected $profile = 'standard';
24
25   protected $dumpHeaders = TRUE;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
33     $this->enablePageCaching();
34   }
35
36   /**
37    * Test that cache tags are properly bubbled up to the page level.
38    */
39   public function testPageCacheTags() {
40     // Create two nodes.
41     $author_1 = $this->drupalCreateUser();
42     $node_1 = $this->drupalCreateNode([
43       'uid' => $author_1->id(),
44       'title' => 'Node 1',
45       'body' => [
46         0 => ['value' => 'Body 1', 'format' => 'basic_html'],
47       ],
48       'promote' => NodeInterface::PROMOTED,
49     ]);
50     $author_2 = $this->drupalCreateUser();
51     $node_2 = $this->drupalCreateNode([
52       'uid' => $author_2->id(),
53       'title' => 'Node 2',
54       'body' => [
55         0 => ['value' => 'Body 2', 'format' => 'full_html'],
56       ],
57       'promote' => NodeInterface::PROMOTED,
58     ]);
59
60     // Place a block, but only make it visible on full node page 2.
61     $block = $this->drupalPlaceBlock('views_block:comments_recent-block_1', [
62       'visibility' => [
63         'request_path' => [
64           'pages' => '/node/' . $node_2->id(),
65         ],
66       ],
67     ]);
68
69     $cache_contexts = [
70       'languages:' . LanguageInterface::TYPE_INTERFACE,
71       'route',
72       'theme',
73       'timezone',
74       'user',
75       // The placed block is only visible on certain URLs through a visibility
76       // condition.
77       'url.path',
78       'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
79       // These two cache contexts are added by BigPipe.
80       'cookies:big_pipe_nojs',
81       'session.exists',
82     ];
83
84     // Full node page 1.
85     $this->assertPageCacheContextsAndTags($node_1->urlInfo(), $cache_contexts, [
86       'http_response',
87       'rendered',
88       'block_view',
89       'config:block_list',
90       'config:block.block.bartik_branding',
91       'config:block.block.bartik_breadcrumbs',
92       'config:block.block.bartik_content',
93       'config:block.block.bartik_tools',
94       'config:block.block.bartik_footer',
95       'config:block.block.bartik_help',
96       'config:block.block.bartik_search',
97       'config:block.block.' . $block->id(),
98       'config:block.block.bartik_powered',
99       'config:block.block.bartik_main_menu',
100       'config:block.block.bartik_account_menu',
101       'config:block.block.bartik_messages',
102       'config:block.block.bartik_local_actions',
103       'config:block.block.bartik_local_tasks',
104       'config:block.block.bartik_page_title',
105       'node_view',
106       'node:' . $node_1->id(),
107       'user:0',
108       'user:' . $author_1->id(),
109       'config:filter.format.basic_html',
110       'config:color.theme.bartik',
111       'config:search.settings',
112       'config:system.menu.account',
113       'config:system.menu.tools',
114       'config:system.menu.footer',
115       'config:system.menu.main',
116       'config:system.site',
117       // FinishResponseSubscriber adds this cache tag to responses that have the
118       // 'user.permissions' cache context for anonymous users.
119       'config:user.role.anonymous',
120     ]);
121
122     // Render the view block adds the languages cache context.
123     $cache_contexts[] = 'languages:' . LanguageInterface::TYPE_CONTENT;
124
125     // Full node page 2.
126     $this->assertPageCacheContextsAndTags($node_2->urlInfo(), $cache_contexts, [
127       'http_response',
128       'rendered',
129       'block_view',
130       'config:block_list',
131       'config:block.block.bartik_branding',
132       'config:block.block.bartik_breadcrumbs',
133       'config:block.block.bartik_content',
134       'config:block.block.bartik_tools',
135       'config:block.block.bartik_help',
136       'config:block.block.bartik_search',
137       'config:block.block.' . $block->id(),
138       'config:block.block.bartik_footer',
139       'config:block.block.bartik_powered',
140       'config:block.block.bartik_main_menu',
141       'config:block.block.bartik_account_menu',
142       'config:block.block.bartik_messages',
143       'config:block.block.bartik_local_actions',
144       'config:block.block.bartik_local_tasks',
145       'config:block.block.bartik_page_title',
146       'node_view',
147       'node:' . $node_2->id(),
148       'user:' . $author_2->id(),
149       'config:color.theme.bartik',
150       'config:filter.format.full_html',
151       'config:search.settings',
152       'config:system.menu.account',
153       'config:system.menu.tools',
154       'config:system.menu.footer',
155       'config:system.menu.main',
156       'config:system.site',
157       'comment_list',
158       'node_list',
159       'config:views.view.comments_recent',
160       // FinishResponseSubscriber adds this cache tag to responses that have the
161       // 'user.permissions' cache context for anonymous users.
162       'config:user.role.anonymous',
163       'user:0',
164     ]);
165   }
166
167 }