function ExceptionHandlingTest::testRouterContextRestoredAfterErrorPage

Same name and namespace in other branches
  1. main core/tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php \Drupal\KernelTests\Core\Routing\ExceptionHandlingTest::testRouterContextRestoredAfterErrorPage()

Tests the router context is restored after showing an HTML error page.

File

core/tests/Drupal/KernelTests/Core/Routing/ExceptionHandlingTest.php, line 130

Class

ExceptionHandlingTest
Tests the exception handling for various cases.

Namespace

Drupal\KernelTests\Core\Routing

Code

public function testRouterContextRestoredAfterErrorPage() : void {
  /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
  $kernel = \Drupal::getContainer()->get('http_kernel');
  $request = Request::create('/not-found', 'POST');
  $request->setFormat('html', [
    'text/html',
  ]);
  $this->assertSame(Response::HTTP_NOT_FOUND, $kernel->handle($request)
    ->getStatusCode());
  // The router must reuse the shared context, not the error page's clone.
  $this->assertSame($this->container
    ->get('router.request_context'), $this->container
    ->get('router.no_access_checks')
    ->getContext());
  // POST-only routes should not fall back to GET.
  $response = $kernel->handle(Request::create('/router_test/post-only', 'POST'));
  $this->assertSame(Response::HTTP_OK, $response->getStatusCode());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.