PageCacheTagsIntegrationTest.php 5.28 KB
Newer Older
1
2
<?php

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
13

/**
 * Enables the page cache and tests its cache tags in various scenarios.
 *
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
22
  use AssertPageCacheContextsAndTagsTrait;

23
24
25
26
  protected $profile = 'standard';

  protected $dumpHeaders = TRUE;

27
28
29
  /**
   * {@inheritdoc}
   */
30
  protected function setUp() {
31
32
    parent::setUp();

33
    $this->enablePageCaching();
34
35
36
37
38
  }

  /**
   * Test that cache tags are properly bubbled up to the page level.
   */
39
  public function testPageCacheTags() {
40
41
    // Create two nodes.
    $author_1 = $this->drupalCreateUser();
42
    $node_1 = $this->drupalCreateNode([
43
44
      'uid' => $author_1->id(),
      'title' => 'Node 1',
45
46
47
      'body' => [
        0 => ['value' => 'Body 1', 'format' => 'basic_html'],
      ],
48
      'promote' => NodeInterface::PROMOTED,
49
    ]);
50
    $author_2 = $this->drupalCreateUser();
51
    $node_2 = $this->drupalCreateNode([
52
53
      'uid' => $author_2->id(),
      'title' => 'Node 2',
54
55
56
      'body' => [
        0 => ['value' => 'Body 2', 'format' => 'full_html'],
      ],
57
      'promote' => NodeInterface::PROMOTED,
58
    ]);
59

60
    // Place a block, but only make it visible on full node page 2.
61
62
63
    $block = $this->drupalPlaceBlock('views_block:comments_recent-block_1', [
      'visibility' => [
        'request_path' => [
64
          'pages' => '/node/' . $node_2->id(),
65
66
67
        ],
      ],
    ]);
68

69
    $cache_contexts = [
70
      'languages:' . LanguageInterface::TYPE_INTERFACE,
71
      'route',
72
73
      'theme',
      'timezone',
74
      'user',
75
76
      // The placed block is only visible on certain URLs through a visibility
      // condition.
77
78
      'url.path',
      'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
79
80
81
      // These two cache contexts are added by BigPipe.
      'cookies:big_pipe_nojs',
      'session.exists',
82
83
    ];

84
    // Full node page 1.
85
    $this->assertPageCacheContextsAndTags($node_1->urlInfo(), $cache_contexts, [
86
      'http_response',
87
88
      'rendered',
      'block_view',
89
      'local_task',
90
      'config:block_list',
91
      'config:block.block.bartik_branding',
92
      'config:block.block.bartik_breadcrumbs',
93
94
95
      'config:block.block.bartik_content',
      'config:block.block.bartik_tools',
      'config:block.block.bartik_footer',
96
97
98
      'config:block.block.bartik_help',
      'config:block.block.bartik_search',
      'config:block.block.' . $block->id(),
99
100
101
      'config:block.block.bartik_powered',
      'config:block.block.bartik_main_menu',
      'config:block.block.bartik_account_menu',
102
      'config:block.block.bartik_messages',
103
104
      'config:block.block.bartik_local_actions',
      'config:block.block.bartik_local_tasks',
105
      'config:block.block.bartik_page_title',
106
      'node_view',
107
      'node:' . $node_1->id(),
108
      'user:0',
109
      'user:' . $author_1->id(),
110
      'config:filter.format.basic_html',
111
      'config:color.theme.bartik',
112
      'config:search.settings',
113
114
115
116
      'config:system.menu.account',
      'config:system.menu.tools',
      'config:system.menu.footer',
      'config:system.menu.main',
117
      'config:system.site',
118
119
120
      // FinishResponseSubscriber adds this cache tag to responses that have the
      // 'user.permissions' cache context for anonymous users.
      'config:user.role.anonymous',
121
    ]);
122

123
124
125
    // Render the view block adds the languages cache context.
    $cache_contexts[] = 'languages:' . LanguageInterface::TYPE_CONTENT;

126
    // Full node page 2.
127
    $this->assertPageCacheContextsAndTags($node_2->urlInfo(), $cache_contexts, [
128
      'http_response',
129
130
      'rendered',
      'block_view',
131
      'local_task',
132
      'config:block_list',
133
      'config:block.block.bartik_branding',
134
      'config:block.block.bartik_breadcrumbs',
135
136
      'config:block.block.bartik_content',
      'config:block.block.bartik_tools',
137
138
      'config:block.block.bartik_help',
      'config:block.block.bartik_search',
139
140
141
142
143
      'config:block.block.' . $block->id(),
      'config:block.block.bartik_footer',
      'config:block.block.bartik_powered',
      'config:block.block.bartik_main_menu',
      'config:block.block.bartik_account_menu',
144
      'config:block.block.bartik_messages',
145
146
      'config:block.block.bartik_local_actions',
      'config:block.block.bartik_local_tasks',
147
      'config:block.block.bartik_page_title',
148
      'node_view',
149
      'node:' . $node_2->id(),
150
      'user:' . $author_2->id(),
151
      'config:color.theme.bartik',
152
      'config:filter.format.full_html',
153
      'config:search.settings',
154
155
156
157
      'config:system.menu.account',
      'config:system.menu.tools',
      'config:system.menu.footer',
      'config:system.menu.main',
158
      'config:system.site',
159
160
161
      'comment_list',
      'node_list',
      'config:views.view.comments_recent',
162
163
164
      // FinishResponseSubscriber adds this cache tag to responses that have the
      // 'user.permissions' cache context for anonymous users.
      'config:user.role.anonymous',
165
      'user:0',
166
    ]);
167
168
169
  }

}