function PageCacheTest::testPageCache

Tests cache headers.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php, line 88
Definition of Drupal\system\Tests\Bootstrap\PageCacheTest.

Class

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

Namespace

Drupal\system\Tests\Bootstrap

Code

function testPageCache() {
  $config = config('system.performance');
  $config
    ->set('cache.page.enabled', 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($this
    ->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary header was sent.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'public, max-age=0', '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($this
    ->drupalGetHeader('Vary'), 'Cookie,Accept-Encoding', 'Vary: Cookie header was sent.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'public, max-age=0', '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($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($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.');
}