You are here

function PageCacheTest::testPageCache 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::testPageCache()

Tests cache headers.

File

core/modules/page_cache/src/Tests/PageCacheTest.php, line 229
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

function testPageCache() {
  $config = $this
    ->config('system.performance');
  $config
    ->set('cache.page.max_age', 300);
  $config
    ->set('response.gzip', 1);
  $config
    ->save();

  // Fill the cache.
  $this
    ->drupalGet('system-test/set-header', array(
    'query' => array(
      'name' => 'Foo',
      'value' => 'bar',
    ),
  ));
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
  $this
    ->assertEqual(strtolower($this
    ->drupalGetHeader('Vary')), 'cookie,accept-encoding', 'Vary header was sent.');

  // 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
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'max-age=300, public', 'Cache-Control header was sent.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');

  // Check cache.
  $this
    ->drupalGet('system-test/set-header', array(
    'query' => array(
      'name' => 'Foo',
      'value' => 'bar',
    ),
  ));
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  $this
    ->assertEqual(strtolower($this
    ->drupalGetHeader('Vary')), 'cookie,accept-encoding', 'Vary: Cookie header was sent.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'max-age=300, public', 'Cache-Control header was sent.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');

  // Check replacing default headers.
  $this
    ->drupalGet('system-test/set-header', array(
    'query' => array(
      'name' => 'Expires',
      'value' => 'Fri, 19 Nov 2008 05:00:00 GMT',
    ),
  ));
  $this
    ->assertEqual($this
    ->drupalGetHeader('Expires'), 'Fri, 19 Nov 2008 05:00:00 GMT', 'Default header was replaced.');
  $this
    ->drupalGet('system-test/set-header', array(
    'query' => array(
      'name' => 'Vary',
      'value' => 'User-Agent',
    ),
  ));
  $this
    ->assertEqual(strtolower($this
    ->drupalGetHeader('Vary')), 'user-agent,accept-encoding', 'Default header was replaced.');

  // Check that authenticated users bypass the cache.
  $user = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('system-test/set-header', array(
    'query' => array(
      'name' => 'Foo',
      'value' => 'bar',
    ),
  ));
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.');
  $this
    ->assertTrue(strpos(strtolower($this
    ->drupalGetHeader('Vary')), 'cookie') === FALSE, 'Vary: Cookie header was not sent.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'must-revalidate, no-cache, post-check=0, pre-check=0, private', 'Cache-Control header was sent.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');

  // 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
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'max-age=300, public');
}