public function RouterTest::testFinishResponseSubscriber in Drupal 8
Same name and namespace in other branches
- 9 core/modules/system/tests/src/Functional/Routing/RouterTest.php \Drupal\Tests\system\Functional\Routing\RouterTest::testFinishResponseSubscriber()
Confirms that our FinishResponseSubscriber logic works properly.
File
- core/
modules/ system/ tests/ src/ Functional/ Routing/ RouterTest.php, line 34
Class
- RouterTest
- Functional class for the full integrated routing system.
Namespace
Drupal\Tests\system\Functional\RoutingCode
public function testFinishResponseSubscriber() {
$renderer_required_cache_contexts = [
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
];
$expected_cache_contexts = Cache::mergeContexts($renderer_required_cache_contexts, [
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
]);
// Confirm that the router can get to a controller.
$this
->drupalGet('router_test/test1');
$this
->assertRaw('test1', 'The correct string was returned because the route was successful.');
$session = $this
->getSession();
// Check expected headers from FinishResponseSubscriber.
$headers = $session
->getResponseHeaders();
$this
->assertEquals($headers['X-UA-Compatible'], [
'IE=edge',
]);
$this
->assertEquals($headers['Content-language'], [
'en',
]);
$this
->assertEquals($headers['X-Content-Type-Options'], [
'nosniff',
]);
$this
->assertEquals($headers['X-Frame-Options'], [
'SAMEORIGIN',
]);
$this
->assertNull($this
->drupalGetHeader('Vary'), 'Vary header is not set.');
$this
->drupalGet('router_test/test2');
$this
->assertRaw('test2', 'The correct string was returned because the route was successful.');
// Check expected headers from FinishResponseSubscriber.
$headers = $session
->getResponseHeaders();
$this
->assertEqual($headers['X-Drupal-Cache-Contexts'], [
implode(' ', $expected_cache_contexts),
]);
$this
->assertEqual($headers['X-Drupal-Cache-Tags'], [
'config:user.role.anonymous http_response rendered',
]);
// Confirm that the page wrapping is being added, so we're not getting a
// raw body returned.
$this
->assertRaw('</html>', 'Page markup was found.');
// In some instances, the subrequest handling may get confused and render
// a page inception style. This test verifies that is not happening.
$this
->assertSession()
->responseNotMatches('#</body>.*</body>#s', 'There was no double-page effect from a misrendered subrequest.');
// Confirm that route-level access check's cacheability is applied to the
// X-Drupal-Cache-Contexts and X-Drupal-Cache-Tags headers.
// 1. controller result: render array, globally cacheable route access.
$this
->drupalGet('router_test/test18');
$headers = $session
->getResponseHeaders();
$this
->assertEqual($headers['X-Drupal-Cache-Contexts'], [
implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, [
'url',
])),
]);
$this
->assertEqual($headers['X-Drupal-Cache-Tags'], [
'config:user.role.anonymous foo http_response rendered',
]);
// 2. controller result: render array, per-role cacheable route access.
$this
->drupalGet('router_test/test19');
$headers = $session
->getResponseHeaders();
$this
->assertEqual($headers['X-Drupal-Cache-Contexts'], [
implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, [
'url',
'user.roles',
])),
]);
$this
->assertEqual($headers['X-Drupal-Cache-Tags'], [
'config:user.role.anonymous foo http_response rendered',
]);
// 3. controller result: Response object, globally cacheable route access.
$this
->drupalGet('router_test/test1');
$headers = $session
->getResponseHeaders();
$this
->assertFalse(isset($headers['X-Drupal-Cache-Contexts']));
$this
->assertFalse(isset($headers['X-Drupal-Cache-Tags']));
// 4. controller result: Response object, per-role cacheable route access.
$this
->drupalGet('router_test/test20');
$headers = $session
->getResponseHeaders();
$this
->assertFalse(isset($headers['X-Drupal-Cache-Contexts']));
$this
->assertFalse(isset($headers['X-Drupal-Cache-Tags']));
// 5. controller result: CacheableResponse object, globally cacheable route access.
$this
->drupalGet('router_test/test21');
$headers = $session
->getResponseHeaders();
$this
->assertEqual($headers['X-Drupal-Cache-Contexts'], [
'',
]);
$this
->assertEqual($headers['X-Drupal-Cache-Tags'], [
'http_response',
]);
// 6. controller result: CacheableResponse object, per-role cacheable route access.
$this
->drupalGet('router_test/test22');
$headers = $session
->getResponseHeaders();
$this
->assertEqual($headers['X-Drupal-Cache-Contexts'], [
'user.roles',
]);
$this
->assertEqual($headers['X-Drupal-Cache-Tags'], [
'http_response',
]);
// Finally, verify that the X-Drupal-Cache-Contexts and X-Drupal-Cache-Tags
// headers are not sent when their container parameter is set to FALSE.
$this
->drupalGet('router_test/test18');
$headers = $session
->getResponseHeaders();
$this
->assertTrue(isset($headers['X-Drupal-Cache-Contexts']));
$this
->assertTrue(isset($headers['X-Drupal-Cache-Tags']));
$this
->setContainerParameter('http.response.debug_cacheability_headers', FALSE);
$this
->rebuildContainer();
$this
->resetAll();
$this
->drupalGet('router_test/test18');
$headers = $session
->getResponseHeaders();
$this
->assertFalse(isset($headers['X-Drupal-Cache-Contexts']));
$this
->assertFalse(isset($headers['X-Drupal-Cache-Tags']));
}