You are here

public function PageCacheTest::testPageCacheAnonymousRolePermissions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/page_cache/src/Tests/PageCacheTest.php \Drupal\page_cache\Tests\PageCacheTest::testPageCacheAnonymousRolePermissions()

Tests the automatic presence of the anonymous role's cache tag.

The 'user.permissions' cache context ensures that if the permissions for a role are modified, users are not served stale render cache content. But, when entire responses are cached in reverse proxies, the value for the cache context is never calculated, causing the stale response to not be invalidated. Therefore, when varying by permissions and the current user is the anonymous user, the cache tag for the 'anonymous' role must be added.

This test verifies that, and it verifies that it does not happen for other roles.

File

core/modules/page_cache/src/Tests/PageCacheTest.php, line 290
Contains \Drupal\page_cache\Tests\PageCacheTest.

Class

PageCacheTest
Enables the page cache and tests it with various HTTP requests.

Namespace

Drupal\page_cache\Tests

Code

public function testPageCacheAnonymousRolePermissions() {
  $config = $this
    ->config('system.performance');
  $config
    ->set('cache.page.max_age', 300);
  $config
    ->save();
  $content_url = Url::fromRoute('system_test.permission_dependent_content');
  $route_access_url = Url::fromRoute('system_test.permission_dependent_route_access');

  // 1. anonymous user, without permission.
  $this
    ->drupalGet($content_url);
  $this
    ->assertText('Permission to pet llamas: no!');
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertCacheTag('config:user.role.anonymous');
  $this
    ->drupalGet($route_access_url);
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertCacheTag('config:user.role.anonymous');

  // 2. anonymous user, with permission.
  user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
    'pet llamas',
  ]);
  $this
    ->drupalGet($content_url);
  $this
    ->assertText('Permission to pet llamas: yes!');
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertCacheTag('config:user.role.anonymous');
  $this
    ->drupalGet($route_access_url);
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertCacheTag('config:user.role.anonymous');

  // 3. authenticated user, without permission.
  $auth_user = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($auth_user);
  $this
    ->drupalGet($content_url);
  $this
    ->assertText('Permission to pet llamas: no!');
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertNoCacheTag('config:user.role.authenticated');
  $this
    ->drupalGet($route_access_url);
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertNoCacheTag('config:user.role.authenticated');

  // 4. authenticated user, with permission.
  user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
    'pet llamas',
  ]);
  $this
    ->drupalGet($content_url);
  $this
    ->assertText('Permission to pet llamas: yes!');
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertNoCacheTag('config:user.role.authenticated');
  $this
    ->drupalGet($route_access_url);
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertNoCacheTag('config:user.role.authenticated');
}