public function PageCacheTest::testPageCache in Drupal 9
Same name and namespace in other branches
- 8 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCache()
- 10 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testPageCache()
Tests cache headers.
File
- core/
modules/ page_cache/ tests/ src/ Functional/ PageCacheTest.php, line 258
Class
- PageCacheTest
- Enables the page cache and tests it with various HTTP requests.
Namespace
Drupal\Tests\page_cache\FunctionalCode
public function testPageCache() {
$config = $this
->config('system.performance');
$config
->set('cache.page.max_age', 300);
$config
->save();
// 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
->assertSession()
->responseHeaderEquals('Cache-Control', 'max-age=300, public');
$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
->assertSession()
->responseHeaderEquals('Cache-Control', 'max-age=300, public');
$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_maxage_page');
$this
->assertSession()
->responseHeaderEquals('Cache-Control', 'max-age=300, public');
}