You are here

public function PageCacheTest::testPageCacheAnonymousRolePermissions in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCacheAnonymousRolePermissions()
  2. 10 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\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/tests/src/Functional/PageCacheTest.php, line 318

Class

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

Namespace

Drupal\Tests\page_cache\Functional

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
    ->assertSession()
    ->pageTextContains('Permission to pet llamas: no!');
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.role.anonymous');
  $this
    ->drupalGet($route_access_url);
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.role.anonymous');

  // 2. anonymous user, with permission.
  user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
    'pet llamas',
  ]);
  $this
    ->drupalGet($content_url);
  $this
    ->assertSession()
    ->pageTextContains('Permission to pet llamas: yes!');
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.role.anonymous');
  $this
    ->drupalGet($route_access_url);
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.role.anonymous');

  // 3. authenticated user, without permission.
  $auth_user = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($auth_user);
  $this
    ->drupalGet($content_url);
  $this
    ->assertSession()
    ->pageTextContains('Permission to pet llamas: no!');
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertSession()
    ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'config:user.role.authenticated');
  $this
    ->drupalGet($route_access_url);
  $this
    ->assertCacheContext('user.permissions');
  $this
    ->assertSession()
    ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'config:user.role.authenticated');

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