You are here

public function RouterTest::testFinishResponseSubscriber in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Routing/RouterTest.php \Drupal\system\Tests\Routing\RouterTest::testFinishResponseSubscriber()

Confirms that our FinishResponseSubscriber logic works properly.

File

core/modules/system/src/Tests/Routing/RouterTest.php, line 34
Contains \Drupal\system\Tests\Routing\RouterTest.

Class

RouterTest
Functional class for the full integrated routing system.

Namespace

Drupal\system\Tests\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,
  ]);

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

  // Check expected headers from FinishResponseSubscriber.
  $headers = $this
    ->drupalGetHeaders();
  $this
    ->assertEqual($headers['x-ua-compatible'], 'IE=edge');
  $this
    ->assertEqual($headers['content-language'], 'en');
  $this
    ->assertEqual($headers['x-content-type-options'], 'nosniff');
  $this
    ->assertEqual($headers['x-frame-options'], 'SAMEORIGIN');
  $this
    ->drupalGet('router_test/test2');
  $this
    ->assertRaw('test2', 'The correct string was returned because the route was successful.');

  // Check expected headers from FinishResponseSubscriber.
  $headers = $this
    ->drupalGetHeaders();
  $this
    ->assertEqual($headers['x-drupal-cache-contexts'], implode(' ', $expected_cache_contexts));
  $this
    ->assertEqual($headers['x-drupal-cache-tags'], 'config:user.role.anonymous 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
    ->assertNoPattern('#</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 = $this
    ->drupalGetHeaders();
  $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 rendered');

  // 2. controller result: render array, per-role cacheable route access.
  $this
    ->drupalGet('router_test/test19');
  $headers = $this
    ->drupalGetHeaders();
  $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 rendered');

  // 3. controller result: Response object, globally cacheable route access.
  $this
    ->drupalGet('router_test/test1');
  $headers = $this
    ->drupalGetHeaders();
  $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 = $this
    ->drupalGetHeaders();
  $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 = $this
    ->drupalGetHeaders();
  $this
    ->assertEqual($headers['x-drupal-cache-contexts'], '');
  $this
    ->assertEqual($headers['x-drupal-cache-tags'], '');

  // 6. controller result: CacheableResponse object, per-role cacheable route access.
  $this
    ->drupalGet('router_test/test22');
  $headers = $this
    ->drupalGetHeaders();
  $this
    ->assertEqual($headers['x-drupal-cache-contexts'], 'user.roles');
  $this
    ->assertEqual($headers['x-drupal-cache-tags'], '');

  // 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 = $this
    ->drupalGetHeaders();
  $this
    ->assertTrue(isset($headers['x-drupal-cache-contexts']));
  $this
    ->assertTrue(isset($headers['x-drupal-cache-tags']));
  $this
    ->setHttpResponseDebugCacheabilityHeaders(FALSE);
  $this
    ->drupalGet('router_test/test18');
  $headers = $this
    ->drupalGetHeaders();
  $this
    ->assertFalse(isset($headers['x-drupal-cache-contexts']));
  $this
    ->assertFalse(isset($headers['x-drupal-cache-tags']));
}