You are here

public function PageCacheTest::testQueryParameterFormatRequests in Drupal 9

Same name and namespace in other branches
  1. 8 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()

Tests support for different cache items with different request formats specified via a query parameter.

File

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

Class

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

Namespace

Drupal\Tests\page_cache\Functional

Code

public function testQueryParameterFormatRequests() {
  $config = $this
    ->config('system.performance');
  $config
    ->set('cache.page.max_age', 300);
  $config
    ->save();
  $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',
  ]);
  $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"}');

  // Enable REST support for nodes and hal+json.
  \Drupal::service('module_installer')
    ->install([
    'node',
    'rest',
    'hal',
    '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, we expect that the first request is a cache miss
  // and it serves HTML.
  $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 HAL request, followed by an
  // ordinary HTML one.
  \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');
}