You are here

public function RouterTest::testFinishResponseSubscriber in Drupal 9

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

Code

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,
  ]);
  sort($expected_cache_contexts);

  // Confirm that the router can get to a controller.
  $this
    ->drupalGet('router_test/test1');
  $this
    ->assertSession()
    ->pageTextContains('test1');
  $session = $this
    ->getSession();

  // Check expected headers from FinishResponseSubscriber.
  $this
    ->assertSession()
    ->responseHeaderEquals('X-UA-Compatible', 'IE=edge');
  $this
    ->assertSession()
    ->responseHeaderEquals('Content-language', 'en');
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Content-Type-Options', 'nosniff');
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Frame-Options', 'SAMEORIGIN');
  $this
    ->assertSession()
    ->responseHeaderDoesNotExist('Vary');
  $this
    ->drupalGet('router_test/test2');
  $this
    ->assertSession()
    ->pageTextContains('test2');

  // Check expected headers from FinishResponseSubscriber.
  $headers = $session
    ->getResponseHeaders();
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', $expected_cache_contexts));
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache-Tags', 'config:user.role.anonymous http_response rendered');
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache-Max-Age', '-1 (Permanent)');

  // Confirm that the page wrapping is being added, so we're not getting a
  // raw body returned.
  $this
    ->assertSession()
    ->responseContains('</html>');

  // 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');

  // 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');
  $expected_cache_contexts = Cache::mergeContexts($renderer_required_cache_contexts, [
    'url',
  ]);
  sort($expected_cache_contexts);
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', $expected_cache_contexts));
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache-Tags', 'config:user.role.anonymous foo http_response rendered');
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache-Max-Age', '60');

  // 2. controller result: render array, per-role cacheable route access.
  $this
    ->drupalGet('router_test/test19');
  $expected_cache_contexts = Cache::mergeContexts($renderer_required_cache_contexts, [
    'url',
    'user.roles',
  ]);
  sort($expected_cache_contexts);
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', $expected_cache_contexts));
  $this
    ->assertSession()
    ->responseHeaderEquals('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']));
  $this
    ->assertFalse(isset($headers['X-Drupal-Cache-Max-Age']));

  // 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']));
  $this
    ->assertFalse(isset($headers['X-Drupal-Cache-Max-Age']));

  // 5. controller result: CacheableResponse object, globally cacheable route access.
  $this
    ->drupalGet('router_test/test21');
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache-Contexts', '');
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response');

  // 6. controller result: CacheableResponse object, per-role cacheable route access.
  $this
    ->drupalGet('router_test/test22');
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache-Contexts', 'user.roles');
  $this
    ->assertSession()
    ->responseHeaderEquals('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
    ->assertTrue(isset($headers['X-Drupal-Cache-Max-Age']));
  $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']));
  $this
    ->assertFalse(isset($headers['X-Drupal-Cache-Max-Age']));
}