Chris@0: adminUser = $this->drupalCreateUser(['administer blocks', 'access administration pages']); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Create additional users to test caching modes. Chris@0: $this->normalUser = $this->drupalCreateUser(); Chris@0: $this->normalUserAlt = $this->drupalCreateUser(); Chris@0: // Sync the roles, since drupalCreateUser() creates separate roles for Chris@0: // the same permission sets. Chris@0: $this->normalUserAlt->roles = $this->normalUser->getRoles(); Chris@0: $this->normalUserAlt->save(); Chris@0: Chris@0: // Enable our test block. Chris@0: $this->block = $this->drupalPlaceBlock('test_cache'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test "user.roles" cache context. Chris@0: */ Chris@0: public function testCachePerRole() { Chris@0: \Drupal::state()->set('block_test.cache_contexts', ['user.roles']); Chris@0: Chris@0: // Enable our test block. Set some content for it to display. Chris@0: $current_content = $this->randomMachineName(); Chris@0: \Drupal::state()->set('block_test.content', $current_content); Chris@0: $this->drupalLogin($this->normalUser); Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($current_content, 'Block content displays.'); Chris@0: Chris@0: // Change the content, but the cached copy should still be served. Chris@0: $old_content = $current_content; Chris@0: $current_content = $this->randomMachineName(); Chris@0: \Drupal::state()->set('block_test.content', $current_content); Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($old_content, 'Block is served from the cache.'); Chris@0: Chris@0: // Clear the cache and verify that the stale data is no longer there. Chris@0: Cache::invalidateTags(['block_view']); Chris@0: $this->drupalGet(''); Chris@0: $this->assertNoText($old_content, 'Block cache clear removes stale cache data.'); Chris@0: $this->assertText($current_content, 'Fresh block content is displayed after clearing the cache.'); Chris@0: Chris@0: // Test whether the cached data is served for the correct users. Chris@0: $old_content = $current_content; Chris@0: $current_content = $this->randomMachineName(); Chris@0: \Drupal::state()->set('block_test.content', $current_content); Chris@0: $this->drupalLogout(); Chris@0: $this->drupalGet(''); Chris@0: $this->assertNoText($old_content, 'Anonymous user does not see content cached per-role for normal user.'); Chris@0: Chris@0: $this->drupalLogin($this->normalUserAlt); Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($old_content, 'User with the same roles sees per-role cached content.'); Chris@0: Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->drupalGet(''); Chris@0: $this->assertNoText($old_content, 'Admin user does not see content cached per-role for normal user.'); Chris@0: Chris@0: $this->drupalLogin($this->normalUser); Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($old_content, 'Block is served from the per-role cache.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test a cacheable block without any additional cache context. Chris@0: */ Chris@0: public function testCachePermissions() { Chris@0: // user.permissions is a required context, so a user with different Chris@0: // permissions will see a different version of the block. Chris@0: \Drupal::state()->set('block_test.cache_contexts', []); Chris@0: Chris@0: $current_content = $this->randomMachineName(); Chris@0: \Drupal::state()->set('block_test.content', $current_content); Chris@0: Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($current_content, 'Block content displays.'); Chris@0: Chris@0: $old_content = $current_content; Chris@0: $current_content = $this->randomMachineName(); Chris@0: \Drupal::state()->set('block_test.content', $current_content); Chris@0: Chris@0: $this->drupalGet('user'); Chris@0: $this->assertText($old_content, 'Block content served from cache.'); Chris@0: Chris@0: $this->drupalLogout(); Chris@0: $this->drupalGet('user'); Chris@0: $this->assertText($current_content, 'Block content not served from cache.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test non-cacheable block. Chris@0: */ Chris@0: public function testNoCache() { Chris@0: \Drupal::state()->set('block_test.cache_max_age', 0); Chris@0: Chris@0: $current_content = $this->randomMachineName(); Chris@0: \Drupal::state()->set('block_test.content', $current_content); Chris@0: Chris@0: // If max_age = 0 has no effect, the next request would be cached. Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($current_content, 'Block content displays.'); Chris@0: Chris@0: // A cached copy should not be served. Chris@0: $current_content = $this->randomMachineName(); Chris@0: \Drupal::state()->set('block_test.content', $current_content); Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($current_content, 'Maximum age of zero prevents blocks from being cached.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test "user" cache context. Chris@0: */ Chris@0: public function testCachePerUser() { Chris@0: \Drupal::state()->set('block_test.cache_contexts', ['user']); Chris@0: Chris@0: $current_content = $this->randomMachineName(); Chris@0: \Drupal::state()->set('block_test.content', $current_content); Chris@0: $this->drupalLogin($this->normalUser); Chris@0: Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($current_content, 'Block content displays.'); Chris@0: Chris@0: $old_content = $current_content; Chris@0: $current_content = $this->randomMachineName(); Chris@0: \Drupal::state()->set('block_test.content', $current_content); Chris@0: Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($old_content, 'Block is served from per-user cache.'); Chris@0: Chris@0: $this->drupalLogin($this->normalUserAlt); Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($current_content, 'Per-user block cache is not served for other users.'); Chris@0: Chris@0: $this->drupalLogin($this->normalUser); Chris@0: $this->drupalGet(''); Chris@0: $this->assertText($old_content, 'Per-user block cache is persistent.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test "url" cache context. Chris@0: */ Chris@0: public function testCachePerPage() { Chris@0: \Drupal::state()->set('block_test.cache_contexts', ['url']); Chris@0: Chris@0: $current_content = $this->randomMachineName(); Chris@0: \Drupal::state()->set('block_test.content', $current_content); Chris@0: Chris@0: $this->drupalGet('test-page'); Chris@0: $this->assertText($current_content, 'Block content displays on the test page.'); Chris@0: Chris@0: $old_content = $current_content; Chris@0: $current_content = $this->randomMachineName(); Chris@0: \Drupal::state()->set('block_test.content', $current_content); Chris@0: Chris@0: $this->drupalGet('user'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertNoText($old_content, 'Block content cached for the test page does not show up for the user page.'); Chris@0: $this->drupalGet('test-page'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText($old_content, 'Block content cached for the test page.'); Chris@0: } Chris@0: Chris@0: }