3 namespace Drupal\Tests\block\Functional;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Tests\BrowserTestBase;
13 class BlockCacheTest extends BrowserTestBase {
20 public static $modules = ['block', 'block_test', 'test_page_test'];
23 * A user with permission to create and edit books and to administer blocks.
30 * An authenticated user to test block caching.
34 protected $normalUser;
37 * Another authenticated user to test block caching.
41 protected $normalUserAlt;
44 * The block used by this test.
46 * @var \Drupal\block\BlockInterface
50 protected function setUp() {
53 // Create an admin user, log in and enable test blocks.
54 $this->adminUser = $this->drupalCreateUser(['administer blocks', 'access administration pages']);
55 $this->drupalLogin($this->adminUser);
57 // Create additional users to test caching modes.
58 $this->normalUser = $this->drupalCreateUser();
59 $this->normalUserAlt = $this->drupalCreateUser();
60 // Sync the roles, since drupalCreateUser() creates separate roles for
61 // the same permission sets.
62 $this->normalUserAlt->roles = $this->normalUser->getRoles();
63 $this->normalUserAlt->save();
65 // Enable our test block.
66 $this->block = $this->drupalPlaceBlock('test_cache');
70 * Test "user.roles" cache context.
72 public function testCachePerRole() {
73 \Drupal::state()->set('block_test.cache_contexts', ['user.roles']);
75 // Enable our test block. Set some content for it to display.
76 $current_content = $this->randomMachineName();
77 \Drupal::state()->set('block_test.content', $current_content);
78 $this->drupalLogin($this->normalUser);
80 $this->assertText($current_content, 'Block content displays.');
82 // Change the content, but the cached copy should still be served.
83 $old_content = $current_content;
84 $current_content = $this->randomMachineName();
85 \Drupal::state()->set('block_test.content', $current_content);
87 $this->assertText($old_content, 'Block is served from the cache.');
89 // Clear the cache and verify that the stale data is no longer there.
90 Cache::invalidateTags(['block_view']);
92 $this->assertNoText($old_content, 'Block cache clear removes stale cache data.');
93 $this->assertText($current_content, 'Fresh block content is displayed after clearing the cache.');
95 // Test whether the cached data is served for the correct users.
96 $old_content = $current_content;
97 $current_content = $this->randomMachineName();
98 \Drupal::state()->set('block_test.content', $current_content);
99 $this->drupalLogout();
100 $this->drupalGet('');
101 $this->assertNoText($old_content, 'Anonymous user does not see content cached per-role for normal user.');
103 $this->drupalLogin($this->normalUserAlt);
104 $this->drupalGet('');
105 $this->assertText($old_content, 'User with the same roles sees per-role cached content.');
107 $this->drupalLogin($this->adminUser);
108 $this->drupalGet('');
109 $this->assertNoText($old_content, 'Admin user does not see content cached per-role for normal user.');
111 $this->drupalLogin($this->normalUser);
112 $this->drupalGet('');
113 $this->assertText($old_content, 'Block is served from the per-role cache.');
117 * Test a cacheable block without any additional cache context.
119 public function testCachePermissions() {
120 // user.permissions is a required context, so a user with different
121 // permissions will see a different version of the block.
122 \Drupal::state()->set('block_test.cache_contexts', []);
124 $current_content = $this->randomMachineName();
125 \Drupal::state()->set('block_test.content', $current_content);
127 $this->drupalGet('');
128 $this->assertText($current_content, 'Block content displays.');
130 $old_content = $current_content;
131 $current_content = $this->randomMachineName();
132 \Drupal::state()->set('block_test.content', $current_content);
134 $this->drupalGet('user');
135 $this->assertText($old_content, 'Block content served from cache.');
137 $this->drupalLogout();
138 $this->drupalGet('user');
139 $this->assertText($current_content, 'Block content not served from cache.');
143 * Test non-cacheable block.
145 public function testNoCache() {
146 \Drupal::state()->set('block_test.cache_max_age', 0);
148 $current_content = $this->randomMachineName();
149 \Drupal::state()->set('block_test.content', $current_content);
151 // If max_age = 0 has no effect, the next request would be cached.
152 $this->drupalGet('');
153 $this->assertText($current_content, 'Block content displays.');
155 // A cached copy should not be served.
156 $current_content = $this->randomMachineName();
157 \Drupal::state()->set('block_test.content', $current_content);
158 $this->drupalGet('');
159 $this->assertText($current_content, 'Maximum age of zero prevents blocks from being cached.');
163 * Test "user" cache context.
165 public function testCachePerUser() {
166 \Drupal::state()->set('block_test.cache_contexts', ['user']);
168 $current_content = $this->randomMachineName();
169 \Drupal::state()->set('block_test.content', $current_content);
170 $this->drupalLogin($this->normalUser);
172 $this->drupalGet('');
173 $this->assertText($current_content, 'Block content displays.');
175 $old_content = $current_content;
176 $current_content = $this->randomMachineName();
177 \Drupal::state()->set('block_test.content', $current_content);
179 $this->drupalGet('');
180 $this->assertText($old_content, 'Block is served from per-user cache.');
182 $this->drupalLogin($this->normalUserAlt);
183 $this->drupalGet('');
184 $this->assertText($current_content, 'Per-user block cache is not served for other users.');
186 $this->drupalLogin($this->normalUser);
187 $this->drupalGet('');
188 $this->assertText($old_content, 'Per-user block cache is persistent.');
192 * Test "url" cache context.
194 public function testCachePerPage() {
195 \Drupal::state()->set('block_test.cache_contexts', ['url']);
197 $current_content = $this->randomMachineName();
198 \Drupal::state()->set('block_test.content', $current_content);
200 $this->drupalGet('test-page');
201 $this->assertText($current_content, 'Block content displays on the test page.');
203 $old_content = $current_content;
204 $current_content = $this->randomMachineName();
205 \Drupal::state()->set('block_test.content', $current_content);
207 $this->drupalGet('user');
208 $this->assertResponse(200);
209 $this->assertNoText($old_content, 'Block content cached for the test page does not show up for the user page.');
210 $this->drupalGet('test-page');
211 $this->assertResponse(200);
212 $this->assertText($old_content, 'Block content cached for the test page.');