core/lib/Drupal/Core/Path/PathValidator.php | 6 ++---- core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php | 10 ++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/lib/Drupal/Core/Path/PathValidator.php b/core/lib/Drupal/Core/Path/PathValidator.php index 96de50a..efce473 100644 --- a/core/lib/Drupal/Core/Path/PathValidator.php +++ b/core/lib/Drupal/Core/Path/PathValidator.php @@ -153,7 +153,7 @@ protected function getPathAttributes($path, Request $request, $access_check) { $router = $this->accessAwareRouter; } - $existing_request_context = $router->getContext(); + $initial_request_context = $router->getContext() ? $router->getContext() : new RequestContext(); $path = $this->pathProcessor->processInbound('/' . $path, $request); try { @@ -175,9 +175,7 @@ protected function getPathAttributes($path, Request $request, $access_check) { $result = FALSE; } - if ($existing_request_context) { - $router->setContext($existing_request_context); - } + $router->setContext($initial_request_context); return $result; } diff --git a/core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php b/core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php index a7e6122..b4be4ae 100644 --- a/core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php +++ b/core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php @@ -2,6 +2,7 @@ namespace Drupal\KernelTests\Core\Path; +use Drupal\Core\Routing\RequestContext; use Drupal\Core\Url; use Drupal\entity_test\Entity\EntityTest; use Drupal\KernelTests\KernelTestBase; @@ -44,8 +45,17 @@ public function testGetUrlIfValidWithoutAccessCheck() { 'PATCH', 'DELETE', NULL, // Used in CLI context. + FALSE, // If no request was even pushed onto the request stack, and hence ]; foreach ($methods as $method) { + if ($method === FALSE) { + $request_stack = $this->container->get('request_stack'); + while ($request_stack->getCurrentRequest()) { + $request_stack->pop(); + } + $this->container->set('router.request_context', new RequestContext()); + } + $requestContext->setMethod($method); /** @var \Drupal\Core\Url $url */ $url = $pathValidator->getUrlIfValidWithoutAccessCheck($entity->toUrl()->toString());