function PageCacheTest::testCacheableResponseResponses

Same name in other branches
  1. 9 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testCacheableResponseResponses()
  2. 8.9.x core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testCacheableResponseResponses()
  3. 11.x core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\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/tests/src/Functional/PageCacheTest.php, line 495

Class

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

Namespace

Drupal\Tests\page_cache\Functional

Code

public function testCacheableResponseResponses() : void {
    $this->enablePageCaching();
    // GET a URL, which would be marked as a cache miss if it were cacheable.
    $this->drupalGet('/system-test/respond-response');
    $this->assertSession()
        ->responseHeaderDoesNotExist('X-Drupal-Cache');
    $this->assertSession()
        ->responseHeaderEquals('Cache-Control', 'must-revalidate, no-cache, private');
    // GET it again, verify it's still not cached.
    $this->drupalGet('/system-test/respond-response');
    $this->assertSession()
        ->responseHeaderDoesNotExist('X-Drupal-Cache');
    $this->assertSession()
        ->responseHeaderEquals('Cache-Control', 'must-revalidate, no-cache, private');
    // GET a URL, which would be marked as a cache miss if it were cacheable.
    $this->drupalGet('/system-test/respond-public-response');
    $this->assertSession()
        ->responseHeaderDoesNotExist('X-Drupal-Cache');
    $this->assertSession()
        ->responseHeaderEquals('Cache-Control', 'max-age=60, public');
    // GET it again, verify it's still not cached.
    $this->drupalGet('/system-test/respond-public-response');
    $this->assertSession()
        ->responseHeaderDoesNotExist('X-Drupal-Cache');
    $this->assertSession()
        ->responseHeaderEquals('Cache-Control', 'max-age=60, public');
    // GET a URL, which should be marked as a cache miss.
    $this->drupalGet('/system-test/respond-cacheable-response');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertCacheMaxAge(300);
    // GET it again, it should now be a cache hit.
    $this->drupalGet('/system-test/respond-cacheable-response');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    $this->assertCacheMaxAge(300);
    // 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-response');
    $this->assertSession()
        ->responseHeaderDoesNotExist('X-Drupal-Cache');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.