annotate core/modules/block/tests/src/Functional/BlockCacheTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\block\Functional;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\Cache;
Chris@0 6 use Drupal\Tests\BrowserTestBase;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests block caching.
Chris@0 10 *
Chris@0 11 * @group block
Chris@0 12 */
Chris@0 13 class BlockCacheTest extends BrowserTestBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Modules to install.
Chris@0 17 *
Chris@0 18 * @var array
Chris@0 19 */
Chris@0 20 public static $modules = ['block', 'block_test', 'test_page_test'];
Chris@0 21
Chris@0 22 /**
Chris@0 23 * A user with permission to create and edit books and to administer blocks.
Chris@0 24 *
Chris@0 25 * @var object
Chris@0 26 */
Chris@0 27 protected $adminUser;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * An authenticated user to test block caching.
Chris@0 31 *
Chris@0 32 * @var object
Chris@0 33 */
Chris@0 34 protected $normalUser;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * Another authenticated user to test block caching.
Chris@0 38 *
Chris@0 39 * @var object
Chris@0 40 */
Chris@0 41 protected $normalUserAlt;
Chris@0 42
Chris@0 43 /**
Chris@0 44 * The block used by this test.
Chris@0 45 *
Chris@0 46 * @var \Drupal\block\BlockInterface
Chris@0 47 */
Chris@0 48 protected $block;
Chris@0 49
Chris@0 50 protected function setUp() {
Chris@0 51 parent::setUp();
Chris@0 52
Chris@0 53 // Create an admin user, log in and enable test blocks.
Chris@0 54 $this->adminUser = $this->drupalCreateUser(['administer blocks', 'access administration pages']);
Chris@0 55 $this->drupalLogin($this->adminUser);
Chris@0 56
Chris@0 57 // Create additional users to test caching modes.
Chris@0 58 $this->normalUser = $this->drupalCreateUser();
Chris@0 59 $this->normalUserAlt = $this->drupalCreateUser();
Chris@0 60 // Sync the roles, since drupalCreateUser() creates separate roles for
Chris@0 61 // the same permission sets.
Chris@0 62 $this->normalUserAlt->roles = $this->normalUser->getRoles();
Chris@0 63 $this->normalUserAlt->save();
Chris@0 64
Chris@0 65 // Enable our test block.
Chris@0 66 $this->block = $this->drupalPlaceBlock('test_cache');
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Test "user.roles" cache context.
Chris@0 71 */
Chris@0 72 public function testCachePerRole() {
Chris@0 73 \Drupal::state()->set('block_test.cache_contexts', ['user.roles']);
Chris@0 74
Chris@0 75 // Enable our test block. Set some content for it to display.
Chris@0 76 $current_content = $this->randomMachineName();
Chris@0 77 \Drupal::state()->set('block_test.content', $current_content);
Chris@0 78 $this->drupalLogin($this->normalUser);
Chris@0 79 $this->drupalGet('');
Chris@0 80 $this->assertText($current_content, 'Block content displays.');
Chris@0 81
Chris@0 82 // Change the content, but the cached copy should still be served.
Chris@0 83 $old_content = $current_content;
Chris@0 84 $current_content = $this->randomMachineName();
Chris@0 85 \Drupal::state()->set('block_test.content', $current_content);
Chris@0 86 $this->drupalGet('');
Chris@0 87 $this->assertText($old_content, 'Block is served from the cache.');
Chris@0 88
Chris@0 89 // Clear the cache and verify that the stale data is no longer there.
Chris@0 90 Cache::invalidateTags(['block_view']);
Chris@0 91 $this->drupalGet('');
Chris@0 92 $this->assertNoText($old_content, 'Block cache clear removes stale cache data.');
Chris@0 93 $this->assertText($current_content, 'Fresh block content is displayed after clearing the cache.');
Chris@0 94
Chris@0 95 // Test whether the cached data is served for the correct users.
Chris@0 96 $old_content = $current_content;
Chris@0 97 $current_content = $this->randomMachineName();
Chris@0 98 \Drupal::state()->set('block_test.content', $current_content);
Chris@0 99 $this->drupalLogout();
Chris@0 100 $this->drupalGet('');
Chris@0 101 $this->assertNoText($old_content, 'Anonymous user does not see content cached per-role for normal user.');
Chris@0 102
Chris@0 103 $this->drupalLogin($this->normalUserAlt);
Chris@0 104 $this->drupalGet('');
Chris@0 105 $this->assertText($old_content, 'User with the same roles sees per-role cached content.');
Chris@0 106
Chris@0 107 $this->drupalLogin($this->adminUser);
Chris@0 108 $this->drupalGet('');
Chris@0 109 $this->assertNoText($old_content, 'Admin user does not see content cached per-role for normal user.');
Chris@0 110
Chris@0 111 $this->drupalLogin($this->normalUser);
Chris@0 112 $this->drupalGet('');
Chris@0 113 $this->assertText($old_content, 'Block is served from the per-role cache.');
Chris@0 114 }
Chris@0 115
Chris@0 116 /**
Chris@0 117 * Test a cacheable block without any additional cache context.
Chris@0 118 */
Chris@0 119 public function testCachePermissions() {
Chris@0 120 // user.permissions is a required context, so a user with different
Chris@0 121 // permissions will see a different version of the block.
Chris@0 122 \Drupal::state()->set('block_test.cache_contexts', []);
Chris@0 123
Chris@0 124 $current_content = $this->randomMachineName();
Chris@0 125 \Drupal::state()->set('block_test.content', $current_content);
Chris@0 126
Chris@0 127 $this->drupalGet('');
Chris@0 128 $this->assertText($current_content, 'Block content displays.');
Chris@0 129
Chris@0 130 $old_content = $current_content;
Chris@0 131 $current_content = $this->randomMachineName();
Chris@0 132 \Drupal::state()->set('block_test.content', $current_content);
Chris@0 133
Chris@0 134 $this->drupalGet('user');
Chris@0 135 $this->assertText($old_content, 'Block content served from cache.');
Chris@0 136
Chris@0 137 $this->drupalLogout();
Chris@0 138 $this->drupalGet('user');
Chris@0 139 $this->assertText($current_content, 'Block content not served from cache.');
Chris@0 140 }
Chris@0 141
Chris@0 142 /**
Chris@0 143 * Test non-cacheable block.
Chris@0 144 */
Chris@0 145 public function testNoCache() {
Chris@0 146 \Drupal::state()->set('block_test.cache_max_age', 0);
Chris@0 147
Chris@0 148 $current_content = $this->randomMachineName();
Chris@0 149 \Drupal::state()->set('block_test.content', $current_content);
Chris@0 150
Chris@0 151 // If max_age = 0 has no effect, the next request would be cached.
Chris@0 152 $this->drupalGet('');
Chris@0 153 $this->assertText($current_content, 'Block content displays.');
Chris@0 154
Chris@0 155 // A cached copy should not be served.
Chris@0 156 $current_content = $this->randomMachineName();
Chris@0 157 \Drupal::state()->set('block_test.content', $current_content);
Chris@0 158 $this->drupalGet('');
Chris@0 159 $this->assertText($current_content, 'Maximum age of zero prevents blocks from being cached.');
Chris@0 160 }
Chris@0 161
Chris@0 162 /**
Chris@0 163 * Test "user" cache context.
Chris@0 164 */
Chris@0 165 public function testCachePerUser() {
Chris@0 166 \Drupal::state()->set('block_test.cache_contexts', ['user']);
Chris@0 167
Chris@0 168 $current_content = $this->randomMachineName();
Chris@0 169 \Drupal::state()->set('block_test.content', $current_content);
Chris@0 170 $this->drupalLogin($this->normalUser);
Chris@0 171
Chris@0 172 $this->drupalGet('');
Chris@0 173 $this->assertText($current_content, 'Block content displays.');
Chris@0 174
Chris@0 175 $old_content = $current_content;
Chris@0 176 $current_content = $this->randomMachineName();
Chris@0 177 \Drupal::state()->set('block_test.content', $current_content);
Chris@0 178
Chris@0 179 $this->drupalGet('');
Chris@0 180 $this->assertText($old_content, 'Block is served from per-user cache.');
Chris@0 181
Chris@0 182 $this->drupalLogin($this->normalUserAlt);
Chris@0 183 $this->drupalGet('');
Chris@0 184 $this->assertText($current_content, 'Per-user block cache is not served for other users.');
Chris@0 185
Chris@0 186 $this->drupalLogin($this->normalUser);
Chris@0 187 $this->drupalGet('');
Chris@0 188 $this->assertText($old_content, 'Per-user block cache is persistent.');
Chris@0 189 }
Chris@0 190
Chris@0 191 /**
Chris@0 192 * Test "url" cache context.
Chris@0 193 */
Chris@0 194 public function testCachePerPage() {
Chris@0 195 \Drupal::state()->set('block_test.cache_contexts', ['url']);
Chris@0 196
Chris@0 197 $current_content = $this->randomMachineName();
Chris@0 198 \Drupal::state()->set('block_test.content', $current_content);
Chris@0 199
Chris@0 200 $this->drupalGet('test-page');
Chris@0 201 $this->assertText($current_content, 'Block content displays on the test page.');
Chris@0 202
Chris@0 203 $old_content = $current_content;
Chris@0 204 $current_content = $this->randomMachineName();
Chris@0 205 \Drupal::state()->set('block_test.content', $current_content);
Chris@0 206
Chris@0 207 $this->drupalGet('user');
Chris@0 208 $this->assertResponse(200);
Chris@0 209 $this->assertNoText($old_content, 'Block content cached for the test page does not show up for the user page.');
Chris@0 210 $this->drupalGet('test-page');
Chris@0 211 $this->assertResponse(200);
Chris@0 212 $this->assertText($old_content, 'Block content cached for the test page.');
Chris@0 213 }
Chris@0 214
Chris@0 215 }