1 bootstrap.test BootstrapPageCacheTestCase::testPageCompression()

Test 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

core/modules/simpletest/tests/bootstrap.test, line 361

Class

BootstrapPageCacheTestCase

Code

function testPageCompression() {
  config_set('system.core', 'cache', 1);
  config_set('system.core', 'page_cache_background_fetch', 0);
  config_set('system.core', 'page_cache_maximum_age', 300);
  $test_path = 'system-test/hello-world';

  // Fill the cache and verify that output is compressed.
  $this->backdropGet($test_path, array(), array('Accept-Encoding: gzip,deflate'));
  $this->assertEqual($this->backdropGetHeader('X-Backdrop-Cache'), 'MISS', 'Page was not cached.');
  $this->backdropSetContent(gzinflate(substr($this->backdropGetContent(), 10, -8)));
  $this->assertRaw('</html>', 'Page was gzip compressed.');

  // Verify that cached output is compressed.
  sleep(5); // Delay to ensure caches are set.
  $this->backdropGet($test_path, array(), array('Accept-Encoding: gzip,deflate'));
  $this->assertEqual($this->backdropGetHeader('X-Backdrop-Cache'), 'HIT', 'Page was cached.');
  $this->assertEqual($this->backdropGetHeader('Content-Encoding'), 'gzip', 'A Content-Encoding header was sent.');
  $this->backdropSetContent(gzinflate(substr($this->backdropGetContent(), 10, -8)));
  $this->assertRaw('</html>', 'Page was gzip compressed.');

  // Verify that a client without compression support gets an uncompressed page.
  $this->backdropGet($test_path);
  $this->assertEqual($this->backdropGetHeader('X-Backdrop-Cache'), 'HIT', 'Page was cached.');
  $this->assertFalse($this->backdropGetHeader('Content-Encoding'), 'A Content-Encoding header was not sent.');
  $this->assertTitle(t('Hello world! | @site-name', array('@site-name' => config_get_translated('system.core', 'site_name'))), 'Site title matches.');
  $this->assertRaw('</html>', 'Page was not compressed.');

  // Disable compression mode.
  config_set('system.core', 'page_compression', FALSE);

  // Verify if cached page is still available for a client with compression support.
  $this->backdropGet($test_path, array(), array('Accept-Encoding: gzip,deflate'));
  $this->backdropSetContent(gzinflate(substr($this->backdropGetContent(), 10, -8)));
  $this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support enabled).');

  // Verify if cached page is still available for a client without compression support.
  $this->backdropGet($test_path);
  $this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support disabled).');
}