diff options
author | Lee Rowlands <lee.rowlands@previousnext.com.au> | 2023-12-28 08:03:14 +1000 |
---|---|---|
committer | Lee Rowlands <lee.rowlands@previousnext.com.au> | 2023-12-28 08:03:14 +1000 |
commit | fbee2baaab8fb8f8be29462e4798b51e1fe758d4 (patch) | |
tree | 976ab6bc1996f933d735884dfc83298074745fb4 /core/tests | |
parent | c980ece64ec31e8ae96a3c73d450472dff69b531 (diff) | |
download | drupal-fbee2baaab8fb8f8be29462e4798b51e1fe758d4.tar.gz drupal-fbee2baaab8fb8f8be29462e4798b51e1fe758d4.zip |
Issue #2484991 by znerol, voleger, andypost, dww, anmolgoyal74, markdorison, ankithashetty, larowlan, daffie, alexpott: Add the session to the request in KernelTestBase, BrowserTestBase, and drush
Diffstat (limited to 'core/tests')
20 files changed, 160 insertions, 3 deletions
diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php index e83b425dc42c..f0035f4b1f4e 100644 --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -11,6 +11,7 @@ use Drupal\Tests\StreamCapturer; use Drupal\Tests\Traits\Core\CronRunTrait; use Drupal\user\Entity\Role; use PHPUnit\Framework\ExpectationFailedException; +use Symfony\Component\HttpFoundation\Request; /** * Tests BrowserTestBase functionality. @@ -522,6 +523,30 @@ class BrowserTestBaseTest extends BrowserTestBase { } /** + * Tests that a usable session is on the request in test-runner. + */ + public function testSessionOnRequest(): void { + /** @var \Symfony\Component\HttpFoundation\Session\Session $session */ + $session = $this->container->get('request_stack')->getSession(); + + $session->set('some-val', 'do-not-cleanup'); + $this->assertEquals('do-not-cleanup', $session->get('some-val')); + + $session->set('some-other-val', 'do-cleanup'); + $this->assertEquals('do-cleanup', $session->remove('some-other-val')); + } + + /** + * Tests deprecation of modified request stack lacking a session. + * + * @group legacy + */ + public function testDeprecatedSessionMissing(): void { + $this->expectDeprecation('Pushing requests without a session onto the request_stack is deprecated in drupal:10.3.0 and an error will be thrown from drupal:11.0.0. See https://www.drupal.org/node/3337193'); + $this->container->get('request_stack')->push(Request::create('/')); + } + + /** * Tests that deprecation headers do not get duplicated. * * @group legacy diff --git a/core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php index 7cd1ae5682cd..52c226c93ea3 100644 --- a/core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php @@ -15,6 +15,8 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; /** * Base class for testing the interactive installer. @@ -118,6 +120,7 @@ abstract class InstallerTestBase extends BrowserTestBase { // server information so that XDebug works. // @see install_begin_request() $request = Request::create($GLOBALS['base_url'] . '/core/install.php', 'GET', [], $_COOKIE, [], $_SERVER); + $request->setSession(new Session(new MockArraySessionStorage())); $this->container = new ContainerBuilder(); $request_stack = new RequestStack(); $request_stack->push($request); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php index 42ff9e293aa0..f941802c1434 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php @@ -13,6 +13,8 @@ use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; // cspell:ignore merhaba siema xsiemax @@ -464,6 +466,7 @@ class EntityQueryTest extends EntityKernelTestBase { $request->query->replace([ 'page' => '0,2', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $this->queryResults = $this->storage ->getQuery() @@ -500,6 +503,7 @@ class EntityQueryTest extends EntityKernelTestBase { 'sort' => 'asc', 'order' => 'Type', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $header = [ diff --git a/core/tests/Drupal/KernelTests/Core/File/FileUrlGeneratorTest.php b/core/tests/Drupal/KernelTests/Core/File/FileUrlGeneratorTest.php index a4a004c825f7..5359ee3fc56a 100644 --- a/core/tests/Drupal/KernelTests/Core/File/FileUrlGeneratorTest.php +++ b/core/tests/Drupal/KernelTests/Core/File/FileUrlGeneratorTest.php @@ -4,6 +4,8 @@ namespace Drupal\KernelTests\Core\File; use Drupal\Core\File\Exception\InvalidStreamWrapperException; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; /** * @coversDefaultClass \Drupal\Core\File\FileUrlGenerator @@ -154,6 +156,7 @@ class FileUrlGeneratorTest extends FileTestBase { // Create a mock Request for transformRelative(). $request = Request::create($GLOBALS['base_url']); + $request->setSession(new Session(new MockArraySessionStorage())); $this->container->get('request_stack')->push($request); \Drupal::setContainer($this->container); @@ -183,6 +186,7 @@ class FileUrlGeneratorTest extends FileTestBase { // Create a mock Request for transformRelative(). $request = Request::create($GLOBALS['base_url']); + $request->setSession(new Session(new MockArraySessionStorage())); $this->container->get('request_stack')->push($request); \Drupal::setContainer($this->container); @@ -200,6 +204,7 @@ class FileUrlGeneratorTest extends FileTestBase { // Create a mock Request for transformRelative(). $request = Request::create($GLOBALS['base_url']); + $request->setSession(new Session(new MockArraySessionStorage())); $this->container->get('request_stack')->push($request); \Drupal::setContainer($this->container); diff --git a/core/tests/Drupal/KernelTests/Core/File/UrlTransformRelativeTest.php b/core/tests/Drupal/KernelTests/Core/File/UrlTransformRelativeTest.php index 4a4d7e5a42b3..c24bd75e3f54 100644 --- a/core/tests/Drupal/KernelTests/Core/File/UrlTransformRelativeTest.php +++ b/core/tests/Drupal/KernelTests/Core/File/UrlTransformRelativeTest.php @@ -4,6 +4,8 @@ namespace Drupal\KernelTests\Core\File; use Drupal\KernelTests\KernelTestBase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; /** * Tests URL transform to relative. @@ -35,6 +37,7 @@ class UrlTransformRelativeTest extends KernelTestBase { $_SERVER['HTTPS'] = $https; $request = Request::createFromGlobals(); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::requestStack()->push($request); $this->assertSame($expected, \Drupal::service('file_url_generator')->transformRelative($url, $root_relative)); diff --git a/core/tests/Drupal/KernelTests/Core/Form/ExternalFormUrlTest.php b/core/tests/Drupal/KernelTests/Core/Form/ExternalFormUrlTest.php index b2486202890b..caa39c1cb722 100644 --- a/core/tests/Drupal/KernelTests/Core/Form/ExternalFormUrlTest.php +++ b/core/tests/Drupal/KernelTests/Core/Form/ExternalFormUrlTest.php @@ -7,6 +7,8 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\KernelTests\KernelTestBase; use Drupal\user\Entity\User; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; /** * Ensures that form actions can't be tricked into sending to external URLs. @@ -76,6 +78,7 @@ class ExternalFormUrlTest extends KernelTestBase implements FormInterface { $request_stack->pop(); $request_stack->pop(); $request = Request::create($original_request->getSchemeAndHttpHost() . '//example.org'); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $form = \Drupal::formBuilder()->getForm($this); @@ -91,6 +94,7 @@ class ExternalFormUrlTest extends KernelTestBase implements FormInterface { $request_stack = \Drupal::service('request_stack'); $original_request = $request_stack->pop(); $request = Request::create($original_request->getSchemeAndHttpHost() . '/example.org'); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $form = \Drupal::formBuilder()->getForm($this); diff --git a/core/tests/Drupal/KernelTests/Core/Form/FormActionXssTest.php b/core/tests/Drupal/KernelTests/Core/Form/FormActionXssTest.php index fc67b354e943..2cf6e10b9091 100644 --- a/core/tests/Drupal/KernelTests/Core/Form/FormActionXssTest.php +++ b/core/tests/Drupal/KernelTests/Core/Form/FormActionXssTest.php @@ -7,6 +7,8 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\KernelTests\KernelTestBase; use Drupal\user\Entity\User; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; // cspell:ignore attribute\'close @@ -77,6 +79,7 @@ class FormActionXssTest extends KernelTestBase implements FormInterface { $request_stack->pop(); $request_stack->pop(); $request = Request::create($original_request->getSchemeAndHttpHost() . '/test/"injected=\'attribute\'close="'); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $form = \Drupal::formBuilder()->getForm($this); diff --git a/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php b/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php index 3624b76e35e7..11faa4aeb4ac 100644 --- a/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Pager/PagerManagerTest.php @@ -4,6 +4,8 @@ namespace Drupal\KernelTests\Core\Pager; use Drupal\KernelTests\KernelTestBase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; /** * @group Pager @@ -22,6 +24,7 @@ class PagerManagerTest extends KernelTestBase { 'other' => 'arbitrary', ]; $request = Request::create('http://example.com', 'GET', $test_parameters); + $request->setSession(new Session(new MockArraySessionStorage())); /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */ $request_stack = $this->container->get('request_stack'); @@ -43,6 +46,7 @@ class PagerManagerTest extends KernelTestBase { */ public function testFindPage() { $request = Request::create('http://example.com', 'GET', ['page' => '0,10']); + $request->setSession(new Session(new MockArraySessionStorage())); /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */ $request_stack = $this->container->get('request_stack'); diff --git a/core/tests/Drupal/KernelTests/Core/Pager/RequestPagerTest.php b/core/tests/Drupal/KernelTests/Core/Pager/RequestPagerTest.php index b21f75bc6f0c..598f68dbafef 100644 --- a/core/tests/Drupal/KernelTests/Core/Pager/RequestPagerTest.php +++ b/core/tests/Drupal/KernelTests/Core/Pager/RequestPagerTest.php @@ -4,6 +4,8 @@ namespace Drupal\KernelTests\Core\Pager; use Drupal\KernelTests\KernelTestBase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; /** * @group Pager @@ -17,6 +19,7 @@ class RequestPagerTest extends KernelTestBase { */ public function testFindPage() { $request = Request::create('http://example.com', 'GET', ['page' => '0,10']); + $request->setSession(new Session(new MockArraySessionStorage())); /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */ $request_stack = $this->container->get('request_stack'); @@ -35,6 +38,7 @@ class RequestPagerTest extends KernelTestBase { 'other' => 'arbitrary', ]; $request = Request::create('http://example.com', 'GET', array_merge(['page' => '0,10'], $test_parameters)); + $request->setSession(new Session(new MockArraySessionStorage())); /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */ $request_stack = $this->container->get('request_stack'); diff --git a/core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php b/core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php index 34a7bdf0006b..588fecae8c5b 100644 --- a/core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php +++ b/core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php @@ -55,10 +55,12 @@ class PathValidatorTest extends KernelTestBase { FALSE, ]; foreach ($methods as $method) { + /** @var \Symfony\Component\HttpFoundation\Request|null $request */ + $request = NULL; if ($method === FALSE) { $request_stack = $this->container->get('request_stack'); while ($request_stack->getCurrentRequest()) { - $request_stack->pop(); + $request = $request_stack->pop(); } $this->container->set('router.request_context', new RequestContext()); } @@ -69,6 +71,12 @@ class PathValidatorTest extends KernelTestBase { $this->assertEquals($method, $requestContext->getMethod()); $this->assertInstanceOf(Url::class, $url); $this->assertSame(['entity_test' => $entity->id()], $url->getRouteParameters()); + + if ($method === FALSE) { + // Restore main request. + $request_stack = $this->container->get('request_stack'); + $request_stack->push($request); + } } } diff --git a/core/tests/Drupal/KernelTests/Core/Plugin/Condition/RequestPathTest.php b/core/tests/Drupal/KernelTests/Core/Plugin/Condition/RequestPathTest.php index 5eb8c624ceb1..8c8bc7942528 100644 --- a/core/tests/Drupal/KernelTests/Core/Plugin/Condition/RequestPathTest.php +++ b/core/tests/Drupal/KernelTests/Core/Plugin/Condition/RequestPathTest.php @@ -7,6 +7,8 @@ use Drupal\KernelTests\KernelTestBase; use Drupal\system\Tests\Routing\MockAliasManager; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; /** * Tests system.module's request path condition. @@ -83,6 +85,7 @@ class RequestPathTest extends KernelTestBase { $pages = "/my/pass/page\r\n/my/pass/page2\r\n/foo"; $request = Request::create('/my/pass/page2'); + $request->setSession(new Session(new MockArraySessionStorage())); $this->requestStack->push($request); /** @var \Drupal\system\Plugin\Condition\RequestPath $condition */ diff --git a/core/tests/Drupal/KernelTests/Core/Plugin/Condition/ResponseStatusTest.php b/core/tests/Drupal/KernelTests/Core/Plugin/Condition/ResponseStatusTest.php index 9f5690a6bf3a..33c407508e75 100644 --- a/core/tests/Drupal/KernelTests/Core/Plugin/Condition/ResponseStatusTest.php +++ b/core/tests/Drupal/KernelTests/Core/Plugin/Condition/ResponseStatusTest.php @@ -9,6 +9,8 @@ use Drupal\KernelTests\KernelTestBase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpKernel\Exception\HttpException; /** @@ -61,6 +63,7 @@ class ResponseStatusTest extends KernelTestBase { $request = new Request(); $request->attributes->set('exception', new HttpException($response_code)); } + $request->setSession(new Session(new MockArraySessionStorage())); $this->requestStack->push($request); /** @var \Drupal\system\Plugin\Condition\ResponseStatus $condition */ diff --git a/core/tests/Drupal/KernelTests/Core/Render/Element/TableSortExtenderTest.php b/core/tests/Drupal/KernelTests/Core/Render/Element/TableSortExtenderTest.php index b605b2111d45..c0bd45713a69 100644 --- a/core/tests/Drupal/KernelTests/Core/Render/Element/TableSortExtenderTest.php +++ b/core/tests/Drupal/KernelTests/Core/Render/Element/TableSortExtenderTest.php @@ -5,6 +5,8 @@ namespace Drupal\KernelTests\Core\Render\Element; use Drupal\Core\Utility\TableSort; use Drupal\KernelTests\KernelTestBase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; /** * Tests table sorting. @@ -31,6 +33,7 @@ class TableSortExtenderTest extends KernelTestBase { ]; $request = Request::createFromGlobals(); $request->query->replace([]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $ts = TableSort::getContextFromRequest($headers, $request); $this->assertEquals($expected_ts, $ts, 'Simple table headers sorted correctly.'); @@ -43,6 +46,7 @@ class TableSortExtenderTest extends KernelTestBase { // headers are overridable. 'order' => 'bar', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $ts = TableSort::getContextFromRequest($headers, $request); $this->assertEquals($expected_ts, $ts, 'Simple table headers plus non-overriding $_GET parameters sorted correctly.'); @@ -56,6 +60,7 @@ class TableSortExtenderTest extends KernelTestBase { // it in the links that it creates. 'alpha' => 'beta', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $expected_ts['sort'] = 'desc'; $expected_ts['query'] = ['alpha' => 'beta']; @@ -83,6 +88,7 @@ class TableSortExtenderTest extends KernelTestBase { $request->query->replace([ 'order' => '2', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $ts = TableSort::getContextFromRequest($headers, $request); $expected_ts = [ @@ -101,6 +107,7 @@ class TableSortExtenderTest extends KernelTestBase { // exist. 'order' => 'bar', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $ts = TableSort::getContextFromRequest($headers, $request); $expected_ts = [ @@ -121,6 +128,7 @@ class TableSortExtenderTest extends KernelTestBase { // it in the links that it creates. 'alpha' => 'beta', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $expected_ts = [ 'name' => '1', @@ -165,6 +173,7 @@ class TableSortExtenderTest extends KernelTestBase { $request->query->replace([ 'order' => '1', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $ts = TableSort::getContextFromRequest($headers, $request); $expected_ts = [ @@ -181,6 +190,7 @@ class TableSortExtenderTest extends KernelTestBase { $request->query->replace([ 'order' => '2', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $ts = TableSort::getContextFromRequest($headers, $request); $expected_ts = [ @@ -197,6 +207,7 @@ class TableSortExtenderTest extends KernelTestBase { $request->query->replace([ 'order' => '3', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $ts = TableSort::getContextFromRequest($headers, $request); $expected_ts = [ @@ -213,6 +224,7 @@ class TableSortExtenderTest extends KernelTestBase { $request->query->replace([ 'order' => '4', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $ts = TableSort::getContextFromRequest($headers, $request); $expected_ts = [ @@ -229,6 +241,7 @@ class TableSortExtenderTest extends KernelTestBase { $request->query->replace([ 'order' => '5', ]); + $request->setSession(new Session(new MockArraySessionStorage())); \Drupal::getContainer()->get('request_stack')->push($request); $ts = TableSort::getContextFromRequest($headers, $request); $expected_ts = [ diff --git a/core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteNoneTest.php b/core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteNoneTest.php index 21e62d9680af..7e35066eebab 100644 --- a/core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteNoneTest.php +++ b/core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteNoneTest.php @@ -8,6 +8,8 @@ use Drupal\Core\Render\BubbleableMetadata; use Drupal\KernelTests\KernelTestBase; use Drupal\Core\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\Routing\Route; /** @@ -59,6 +61,7 @@ class RouteNoneTest extends KernelTestBase { $request = Request::create('/subdir', 'GET', [], [], [], $server); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<front>'); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/')); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $request_context->fromRequest($request); @@ -76,6 +79,7 @@ class RouteNoneTest extends KernelTestBase { $request = Request::create('/subdir/node/add', 'GET', [], [], [], $server); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'node.add'); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add')); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $request_context->fromRequest($request); @@ -93,6 +97,7 @@ class RouteNoneTest extends KernelTestBase { $request = Request::create('/', 'GET', [], [], [], $server); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<front>'); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/')); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $request_context->fromRequest($request); @@ -110,6 +115,7 @@ class RouteNoneTest extends KernelTestBase { $request = Request::create('/node/add', 'GET', [], [], [], $server); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'node.add'); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add')); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $request_context->fromRequest($request); diff --git a/core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteProcessorCurrentIntegrationTest.php b/core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteProcessorCurrentIntegrationTest.php index 1a99b00171f0..1c585d8955da 100644 --- a/core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteProcessorCurrentIntegrationTest.php +++ b/core/tests/Drupal/KernelTests/Core/RouteProcessor/RouteProcessorCurrentIntegrationTest.php @@ -8,6 +8,8 @@ use Drupal\Core\Render\BubbleableMetadata; use Drupal\KernelTests\KernelTestBase; use Drupal\Core\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\Routing\Route; /** @@ -60,6 +62,7 @@ class RouteProcessorCurrentIntegrationTest extends KernelTestBase { $request = Request::create('/subdir/', 'GET', [], [], [], $server); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<front>'); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/')); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $request_context->fromRequest($request); @@ -75,6 +78,7 @@ class RouteProcessorCurrentIntegrationTest extends KernelTestBase { $request = Request::create('/subdir/node/add', 'GET', [], [], [], $server); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'node.add'); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add')); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $request_context->fromRequest($request); @@ -90,6 +94,7 @@ class RouteProcessorCurrentIntegrationTest extends KernelTestBase { $request = Request::create('/', 'GET', [], [], [], $server); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<front>'); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/')); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $request_context->fromRequest($request); @@ -105,6 +110,7 @@ class RouteProcessorCurrentIntegrationTest extends KernelTestBase { $request = Request::create('/node/add', 'GET', [], [], [], $server); $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'node.add'); $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add')); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $request_context->fromRequest($request); @@ -119,6 +125,7 @@ class RouteProcessorCurrentIntegrationTest extends KernelTestBase { 'SERVER_NAME' => 'http://www.example.com', ]; $request = Request::create('/invalid-path', 'GET', [], [], [], $server); + $request->setSession(new Session(new MockArraySessionStorage())); $request_stack->push($request); $request_context->fromRequest($request); diff --git a/core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php b/core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php index 77e37755f3c6..b9544e824127 100644 --- a/core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php +++ b/core/tests/Drupal/KernelTests/Core/StringTranslation/TranslationStringTest.php @@ -58,8 +58,10 @@ class TranslationStringTest extends KernelTestBase { // Reboot the container so that different services are injected and the new // settings are picked. $kernel = $this->container->get('kernel'); - $kernel->shutdown(); - $kernel->boot(); + // @todo This used to call shutdown() and boot(). rebuildContainer() is + // needed until we stop pushing the request twice and only popping it once. + // @see https://www.drupal.org/i/2613044 + $kernel->rebuildContainer(); $settings = Settings::getAll(); $settings['locale_custom_strings_de'] = ['' => ['Example @number' => 'Example @number translated']]; // Recreate the settings static. diff --git a/core/tests/Drupal/KernelTests/Core/Theme/ImageTest.php b/core/tests/Drupal/KernelTests/Core/Theme/ImageTest.php index 509b6dea77f1..c5b6336e5e85 100644 --- a/core/tests/Drupal/KernelTests/Core/Theme/ImageTest.php +++ b/core/tests/Drupal/KernelTests/Core/Theme/ImageTest.php @@ -4,6 +4,8 @@ namespace Drupal\KernelTests\Core\Theme; use Drupal\KernelTests\KernelTestBase; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; /** * Tests built-in image theme functions. @@ -43,6 +45,7 @@ class ImageTest extends KernelTestBase { // the Request containing the correct hostname. KernelTestBase doesn't set // it, so push another request onto the stack to ensure it's correct. $request = Request::create('/', 'GET', [], [], [], $_SERVER); + $request->setSession(new Session(new MockArraySessionStorage())); $this->container = \Drupal::service('kernel')->getContainer(); $this->container->get('request_stack')->push($request); diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index 46b4d8801899..0a07f19c0643 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -37,6 +37,7 @@ use Drupal\Core\Routing\RouteObjectInterface; use Symfony\Component\Routing\Route; use Symfony\Component\VarDumper\VarDumper; use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; +use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException; /** * Base class for functional integration tests. @@ -667,6 +668,19 @@ abstract class KernelTestBase extends TestCase implements ServiceProviderInterfa * {@inheritdoc} */ protected function tearDown(): void { + if ($this->container) { + // Clean up mock session started in DrupalKernel::preHandle(). + try { + /** @var \Symfony\Component\HttpFoundation\Session\Session $session */ + $session = $this->container->get('request_stack')->getSession(); + $session->clear(); + $session->save(); + } + catch (SessionNotFoundException) { + @trigger_error('Pushing requests without a session onto the request_stack is deprecated in drupal:10.3.0 and an error will be thrown from drupal:11.0.0. See https://www.drupal.org/node/3337193', E_USER_DEPRECATED); + } + } + // Destroy the testing kernel. if (isset($this->kernel)) { $this->kernel->shutdown(); diff --git a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php index 689cf8b953e0..be83c0a2672f 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php +++ b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php @@ -11,6 +11,7 @@ use Drupal\user\Entity\Role; use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\visitor\vfsStreamStructureVisitor; use PHPUnit\Framework\SkippedTestError; +use Symfony\Component\HttpFoundation\Request; /** * @coversDefaultClass \Drupal\KernelTests\KernelTestBase @@ -236,6 +237,34 @@ class KernelTestBaseTest extends KernelTestBase { } /** + * Tests that a usable session is on the request. + * + * @covers ::bootKernel + */ + public function testSessionOnRequest(): void { + /** @var \Symfony\Component\HttpFoundation\Session\Session $session */ + $session = $this->container->get('request_stack')->getSession(); + + $session->set('some-val', 'do-not-cleanup'); + $this->assertEquals('do-not-cleanup', $session->get('some-val')); + + $session->set('some-other-val', 'do-cleanup'); + $this->assertEquals('do-cleanup', $session->remove('some-other-val')); + } + + /** + * Tests deprecation of modified request stack lacking a session. + * + * @covers ::tearDown + * + * @group legacy + */ + public function testDeprecatedSessionMissing(): void { + $this->expectDeprecation('Pushing requests without a session onto the request_stack is deprecated in drupal:10.3.0 and an error will be thrown from drupal:11.0.0. See https://www.drupal.org/node/3337193'); + $this->container->get('request_stack')->push(Request::create('/')); + } + + /** * Tests the assumption that local time is in 'Australia/Sydney'. */ public function testLocalTimeZone() { diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 38b41f4163d1..082292646eef 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -24,6 +24,7 @@ use Drupal\TestTools\TestVarDumper; use GuzzleHttp\Cookie\CookieJar; use PHPUnit\Framework\TestCase; use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; +use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException; use Symfony\Component\VarDumper\VarDumper; /** @@ -446,6 +447,19 @@ abstract class BrowserTestBase extends TestCase { protected function tearDown(): void { parent::tearDown(); + if ($this->container) { + // Cleanup mock session started in DrupalKernel::preHandle(). + try { + /** @var \Symfony\Component\HttpFoundation\Session\Session $session */ + $session = $this->container->get('request_stack')->getSession(); + $session->clear(); + $session->save(); + } + catch (SessionNotFoundException) { + @trigger_error('Pushing requests without a session onto the request_stack is deprecated in drupal:10.3.0 and an error will be thrown from drupal:11.0.0. See https://www.drupal.org/node/3337193', E_USER_DEPRECATED); + } + } + // Destroy the testing kernel. if (isset($this->kernel)) { $this->cleanupEnvironment(); |