You are here

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

Tests cacheability of a CacheableResponse.

Tests the difference between having a controller return a plain Symfony Response object versus returning a Response object that implements the CacheableResponseInterface.

File

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

public function testCacheableResponseResponses() {
  $config = $this
    ->config('system.performance');
  $config
    ->set('cache.page.max_age', 300);
  $config
    ->save();

  // GET a URL, which would be marked as a cache miss if it were cacheable.
  $this
    ->drupalGet('/system-test/respond-reponse');
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), 'Drupal page cache header not found.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'must-revalidate, no-cache, post-check=0, pre-check=0, private', 'Cache-Control header was sent');

  // GET it again, verify it's still not cached.
  $this
    ->drupalGet('/system-test/respond-reponse');
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), 'Drupal page cache header not found.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'must-revalidate, no-cache, post-check=0, pre-check=0, private', 'Cache-Control header was sent');

  // GET a URL, which would be marked as a cache miss if it were cacheable.
  $this
    ->drupalGet('/system-test/respond-public-response');
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), 'Drupal page cache header not found.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'max-age=60, public', 'Cache-Control header was sent');

  // GET it again, verify it's still not cached.
  $this
    ->drupalGet('/system-test/respond-public-response');
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), 'Drupal page cache header not found.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'max-age=60, public', 'Cache-Control header was sent');

  // GET a URL, which should be marked as a cache miss.
  $this
    ->drupalGet('/system-test/respond-cacheable-reponse');
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'max-age=300, public', 'Cache-Control header was sent.');

  // GET it again, it should now be a cache hit.
  $this
    ->drupalGet('/system-test/respond-cacheable-reponse');
  $this
    ->assertEqual($this
    ->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  $this
    ->assertEqual($this
    ->drupalGetHeader('Cache-Control'), 'max-age=300, public', 'Cache-Control header was sent.');

  // Uninstall page cache. This should flush all caches so the next call to a
  // previously cached page should be a miss now.
  $this->container
    ->get('module_installer')
    ->uninstall([
    'page_cache',
  ]);

  // GET a URL that was cached by Page Cache before, it should not be now.
  $this
    ->drupalGet('/respond-cacheable-reponse');
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), 'Drupal page cache header not found.');
}