function PageCacheTest::testPageCacheHeaders

Same name in other branches
  1. 11.x core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCacheHeaders()

Tests page cache headers.

1 call to PageCacheTest::testPageCacheHeaders()
PageCacheTest::testPageCache in core/modules/page_cache/tests/src/Functional/PageCacheTest.php
Tests page caching.

File

core/modules/page_cache/tests/src/Functional/PageCacheTest.php, line 248

Class

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

Namespace

Drupal\Tests\page_cache\Functional

Code

protected function testPageCacheHeaders() : void {
    $this->enablePageCaching();
    // Fill the cache.
    $this->drupalGet('system-test/set-header', [
        'query' => [
            'name' => 'Foo',
            'value' => 'bar',
        ],
    ]);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertSession()
        ->responseHeaderContains('Vary', 'cookie');
    // Symfony's Response logic determines a specific order for the subvalues
    // of the Cache-Control header, even if they are explicitly passed in to
    // the response header bag in a different order.
    $this->assertCacheMaxAge(300);
    $this->assertSession()
        ->responseHeaderEquals('Expires', 'Sun, 19 Nov 1978 05:00:00 GMT');
    $this->assertSession()
        ->responseHeaderEquals('Foo', 'bar');
    // Check cache.
    $this->drupalGet('system-test/set-header', [
        'query' => [
            'name' => 'Foo',
            'value' => 'bar',
        ],
    ]);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    $this->assertSession()
        ->responseHeaderContains('Vary', 'cookie');
    $this->assertCacheMaxAge(300);
    $this->assertSession()
        ->responseHeaderEquals('Expires', 'Sun, 19 Nov 1978 05:00:00 GMT');
    $this->assertSession()
        ->responseHeaderEquals('Foo', 'bar');
    // Check replacing default headers.
    $this->drupalGet('system-test/set-header', [
        'query' => [
            'name' => 'Expires',
            'value' => 'Fri, 19 Nov 2008 05:00:00 GMT',
        ],
    ]);
    $this->assertSession()
        ->responseHeaderEquals('Expires', 'Fri, 19 Nov 2008 05:00:00 GMT');
    $this->drupalGet('system-test/set-header', [
        'query' => [
            'name' => 'Vary',
            'value' => 'User-Agent',
        ],
    ]);
    $this->assertSession()
        ->responseHeaderContains('Vary', 'user-agent');
    // Check that authenticated users bypass the cache.
    $user = $this->drupalCreateUser();
    $this->drupalLogin($user);
    $this->drupalGet('system-test/set-header', [
        'query' => [
            'name' => 'Foo',
            'value' => 'bar',
        ],
    ]);
    $this->assertSession()
        ->responseHeaderDoesNotExist('X-Drupal-Cache');
    $this->assertSession()
        ->responseHeaderNotContains('Vary', 'cookie');
    $this->assertSession()
        ->responseHeaderEquals('Cache-Control', 'must-revalidate, no-cache, private');
    $this->assertSession()
        ->responseHeaderEquals('Expires', 'Sun, 19 Nov 1978 05:00:00 GMT');
    $this->assertSession()
        ->responseHeaderEquals('Foo', 'bar');
    // Until bubbling of max-age up to the response is supported, verify that
    // a custom #cache max-age set on an element does not affect page max-age.
    $this->drupalLogout();
    $this->drupalGet('system-test/cache_max_age_page');
    $this->assertCacheMaxAge(300);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.