function PageCacheTest::testPageCompression

Tests page compression.

The test should pass even if zlib.output_compression is enabled in php.ini, .htaccess or similar, or if compression is done outside PHP, e.g. by the mod_deflate Apache module.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php, line 134
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 testPageCompression() {
  $config = config('system.performance');
  $config
    ->set('cache.page.enabled', 1);
  $config
    ->save();

  // Fill the cache and verify that output is compressed.
  $this
    ->drupalGet('', array(), array(
    'Accept-Encoding: gzip,deflate',
  ));
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
  $this
    ->drupalSetContent(gzinflate(substr($this
    ->drupalGetContent(), 10, -8)));
  $this
    ->assertRaw('</html>', 'Page was gzip compressed.');

  // Verify that cached output is compressed.
  $this
    ->drupalGet('', array(), array(
    'Accept-Encoding: gzip,deflate',
  ));
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Content-Encoding'), 'gzip', 'A Content-Encoding header was sent.');
  $this
    ->drupalSetContent(gzinflate(substr($this
    ->drupalGetContent(), 10, -8)));
  $this
    ->assertRaw('</html>', 'Page was gzip compressed.');

  // Verify that a client without compression support gets an uncompressed page.
  $this
    ->drupalGet('');
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  $this
    ->assertFalse($this
    ->drupalGetHeader('Content-Encoding'), 'A Content-Encoding header was not sent.');
  $this
    ->assertTitle(t('Test page | @site-name', array(
    '@site-name' => config('system.site')
      ->get('name'),
  )), 'Site title matches.');
  $this
    ->assertRaw('</html>', 'Page was not compressed.');
}