function PageCacheTest::testQueryParameterFormatRequests

Same name in other branches
  1. 9 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testQueryParameterFormatRequests()
  2. 9 core/modules/hal/tests/src/Functional/page_cache/PageCacheTest.php \Drupal\Tests\hal\Functional\page_cache\PageCacheTest::testQueryParameterFormatRequests()
  3. 8.9.x core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testQueryParameterFormatRequests()
  4. 11.x core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testQueryParameterFormatRequests()

Tests support for different cache items with different request formats.

The request formats are specified via a query parameter.

File

core/modules/page_cache/tests/src/Functional/PageCacheTest.php, line 123

Class

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

Namespace

Drupal\Tests\page_cache\Functional

Code

public function testQueryParameterFormatRequests() : void {
    $this->enablePageCaching();
    $accept_header_cache_url = Url::fromRoute('system_test.page_cache_accept_header');
    $accept_header_cache_url_with_json = Url::fromRoute('system_test.page_cache_accept_header', [
        '_format' => 'json',
    ]);
    $accept_header_cache_url_with_ajax = Url::fromRoute('system_test.page_cache_accept_header', [
        '_format' => 'json',
    ], [
        'query' => [
            '_wrapper_format' => 'drupal_ajax',
        ],
    ]);
    $this->drupalGet($accept_header_cache_url);
    // Verify that HTML page was not yet cached.
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->drupalGet($accept_header_cache_url);
    // Verify that HTML page was cached.
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    // Verify that the correct HTML response was returned.
    $this->assertSession()
        ->responseContains('<p>oh hai this is html.</p>');
    $this->drupalGet($accept_header_cache_url_with_json);
    // Verify that JSON response was not yet cached.
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->drupalGet($accept_header_cache_url_with_json);
    // Verify that JSON response was cached.
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    // Verify that the correct JSON response was returned.
    $this->assertSession()
        ->responseContains('{"content":"oh hai this is json"}');
    $this->drupalGet($accept_header_cache_url_with_ajax);
    // Verify that AJAX response was not yet cached.
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->drupalGet($accept_header_cache_url_with_ajax);
    // Verify that AJAX response was cached.
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    // Verify that the correct AJAX response was returned.
    $this->assertSession()
        ->responseContains('{"content":"oh hai this is ajax"}');
}

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