function PageCacheTest::testQueryParameterFormatRequests in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/page_cache/src/Tests/PageCacheTest.php \Drupal\page_cache\Tests\PageCacheTest::testQueryParameterFormatRequests()
Tests support for different cache items with different request formats specified via a query parameter.
File
- core/
modules/ page_cache/ src/ Tests/ PageCacheTest.php, line 117 - Contains \Drupal\page_cache\Tests\PageCacheTest.
Class
- PageCacheTest
- Enables the page cache and tests it with various HTTP requests.
Namespace
Drupal\page_cache\TestsCode
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);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'HTML page was not yet cached.');
$this
->drupalGet($accept_header_cache_url);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'HTML page was cached.');
$this
->assertRaw('<p>oh hai this is html.</p>', 'The correct HTML response was returned.');
$this
->drupalGet($accept_header_cache_url_with_json);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Json response was not yet cached.');
$this
->drupalGet($accept_header_cache_url_with_json);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Json response was cached.');
$this
->assertRaw('{"content":"oh hai this is json"}', 'The correct Json response was returned.');
// Enable REST support for nodes and hal+json.
\Drupal::service('module_installer')
->install([
'node',
'rest',
'hal',
]);
$this
->drupalCreateContentType([
'type' => 'article',
]);
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$node_uri = $node
->urlInfo();
$node_url_with_hal_json_format = $node
->urlInfo('canonical')
->setRouteParameter('_format', 'hal_json');
/** @var \Drupal\user\RoleInterface $role */
$role = Role::load('anonymous');
$role
->grantPermission('restful get entity:node');
$role
->save();
$this
->drupalGet($node_uri);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this
->assertEqual($this
->drupalGetHeader('Content-Type'), 'text/html; charset=UTF-8');
$this
->drupalGet($node_uri);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT');
$this
->assertEqual($this
->drupalGetHeader('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
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this
->assertEqual($this
->drupalGetHeader('Content-Type'), 'application/hal+json');
$this
->drupalGet($node_url_with_hal_json_format);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT');
$this
->assertEqual($this
->drupalGetHeader('Content-Type'), 'application/hal+json');
// Clear the page cache. After that request a HAL request, followed by an
// ordinary HTML one.
\Drupal::cache('render')
->deleteAll();
$this
->drupalGet($node_url_with_hal_json_format);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this
->assertEqual($this
->drupalGetHeader('Content-Type'), 'application/hal+json');
$this
->drupalGet($node_url_with_hal_json_format);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT');
$this
->assertEqual($this
->drupalGetHeader('Content-Type'), 'application/hal+json');
$this
->drupalGet($node_uri);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this
->assertEqual($this
->drupalGetHeader('Content-Type'), 'text/html; charset=UTF-8');
$this
->drupalGet($node_uri);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), 'HIT');
$this
->assertEqual($this
->drupalGetHeader('Content-Type'), 'text/html; charset=UTF-8');
}