diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index 8172b48..ea086f4 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -368,8 +368,11 @@ protected function getKernelParameters() { protected function initializeContainer() { $this->containerNeedsDumping = FALSE; $persist = $this->getServicesToPersist(); - // The request service requires custom persisting logic, since it is also - // potentially scoped. + // The request and request_context services require custom persisting logic. + // This is because during WebTestBase::setUp the container is initialized + // multiple times and it is necessary to persist those two services. + // Additionally it is necessary to keep track of the request context and + // restore after it if exists here. $request_scope = FALSE; if (isset($this->container)) { if ($this->container->isScopeActive('request')) { @@ -378,6 +381,9 @@ protected function initializeContainer() { if ($this->container->initialized('request')) { $request = $this->container->get('request'); } + if ($this->container->initialized('router.request_context')) { + $request_context = $this->container->get('router.request_context'); + } } $this->container = NULL; $class = $this->getClassName(); @@ -456,6 +462,9 @@ protected function initializeContainer() { if (isset($request)) { $this->container->set('request', $request); } + if (isset($request_context)) { + $this->container->set('router.request_context', $request_context); + } \Drupal::setContainer($this->container); } diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index 9d876a4..0c8fddd 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -1147,8 +1147,17 @@ protected function rebuildContainer($environment = 'testing') { // DrupalKernel replaces the container in \Drupal::getContainer() with a // different object, so we need to replace the instance on this test class. $this->container = \Drupal::getContainer(); - // The current user is set in TestBase::prepareEnvironment(). + + // Restore the request. $this->container->set('request', $request); + + // The request context is normally set by the router_listener from within + // its KernelEvents::REQUEST listener. In the simpletest parent site this + // event is not fired, therefore it is necessary to updated the request + // context manually here. + $this->container->get('router.request_context')->fromRequest($request); + + // The current user is set in TestBase::prepareEnvironment(). $this->container->set('current_user', \Drupal::currentUser()); } diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index 8efa40a..3df8e34 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -3687,6 +3687,8 @@ protected function prepareRequestForGenerator($clean_urls = TRUE, $override_serv $request = Request::create($request_path, 'GET', array(), array(), array(), $server); $generator->setRequest($request); + $this->container->get('router.request_context')->fromRequest($request); return $request; } + }