function PageCacheTest::testQueryParameterFormatRequests

Same name in this branch
  1. 9 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testQueryParameterFormatRequests()
Same name in other branches
  1. 8.9.x core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testQueryParameterFormatRequests()
  2. 10 core/modules/page_cache/tests/src/Functional/PageCacheTest.php \Drupal\Tests\page_cache\Functional\PageCacheTest::testQueryParameterFormatRequests()
  3. 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.

Request formats are specified via a query parameter.

File

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

Class

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

Namespace

Drupal\Tests\hal\Functional\page_cache

Code

public function testQueryParameterFormatRequests() {
    $config = $this->config('system.performance');
    $config->set('cache.page.max_age', 300);
    $config->save();
    // Enable REST support for nodes and hal+json.
    \Drupal::service('module_installer')->install([
        'node',
        'hal',
        'rest',
        'basic_auth',
    ]);
    $this->drupalCreateContentType([
        'type' => 'article',
    ]);
    $node = $this->drupalCreateNode([
        'type' => 'article',
    ]);
    $node_uri = $node->toUrl();
    $node_url_with_hal_json_format = $node->toUrl('canonical')
        ->setRouteParameter('_format', 'hal_json');
    $this->drupalGet($node_uri);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertSession()
        ->responseHeaderEquals('Content-Type', 'text/html; charset=UTF-8');
    $this->drupalGet($node_uri);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    $this->assertSession()
        ->responseHeaderEquals('Content-Type', 'text/html; charset=UTF-8');
    // Now request a HAL page twice, we expect that the first request is a cache
    // miss and both requests serve 'application/hal+json'.
    $this->drupalGet($node_url_with_hal_json_format);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertSession()
        ->responseHeaderEquals('Content-Type', 'application/hal+json');
    $this->drupalGet($node_url_with_hal_json_format);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    $this->assertSession()
        ->responseHeaderEquals('Content-Type', 'application/hal+json');
    // Clear the page cache. After that request a double HAL request, followed
    // by two ordinary HTML ones.
    \Drupal::cache('page')->deleteAll();
    $this->drupalGet($node_url_with_hal_json_format);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertSession()
        ->responseHeaderEquals('Content-Type', 'application/hal+json');
    $this->drupalGet($node_url_with_hal_json_format);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    $this->assertSession()
        ->responseHeaderEquals('Content-Type', 'application/hal+json');
    $this->drupalGet($node_uri);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this->assertSession()
        ->responseHeaderEquals('Content-Type', 'text/html; charset=UTF-8');
    $this->drupalGet($node_uri);
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
    $this->assertSession()
        ->responseHeaderEquals('Content-Type', 'text/html; charset=UTF-8');
}

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