diff --git a/engine/Shopware/Plugins/Default/Core/Router/Bootstrap.php b/engine/Shopware/Plugins/Default/Core/Router/Bootstrap.php index 99f7d756d9a..e5a47414f44 100644 --- a/engine/Shopware/Plugins/Default/Core/Router/Bootstrap.php +++ b/engine/Shopware/Plugins/Default/Core/Router/Bootstrap.php @@ -29,6 +29,7 @@ use Shopware\Models\Shop\Shop; use Shopware\Models\Shop\Template; use Symfony\Component\HttpFoundation\Cookie; +use Symfony\Component\HttpFoundation\Response; class Shopware_Plugins_Core_Router_Bootstrap extends Shopware_Components_Plugin_Bootstrap { @@ -53,6 +54,7 @@ public function install() public function onRouteStartup(Enlight_Controller_EventArgs $args) { $request = $args->getRequest(); + $response = $args->getResponse(); if (str_starts_with($request->getPathInfo(), '/backend') || str_starts_with($request->getPathInfo(), '/api/') @@ -60,7 +62,7 @@ public function onRouteStartup(Enlight_Controller_EventArgs $args) return; } - $shop = $this->getShopByRequest($request); + $shop = $this->getShopByRequest($request, $response); if (!$shop->getHost()) { $shop->setHost($request->getHttpHost()); @@ -306,7 +308,7 @@ protected function upgradeShop($request, $response) * * @return Shop */ - protected function getShopByRequest(Request $request) + protected function getShopByRequest(Request $request, Response $response) { $repository = $this->get(ModelManager::class)->getRepository(Shop::class); @@ -317,6 +319,7 @@ protected function getShopByRequest(Request $request) if ($shop === null && $request->getCookie('shop') !== null) { $shop = $repository->getActiveById($request->getCookie('shop')); + $response->headers->clearCookie('shop'); } if ($shop && $request->getCookie('shop') !== null && $request->getPost('__shop') === null) { diff --git a/tests/Functional/Plugins/Core/Router/BootstrapTest.php b/tests/Functional/Plugins/Core/Router/BootstrapTest.php index 5ef806a48e6..7f0d3ca7a7d 100644 --- a/tests/Functional/Plugins/Core/Router/BootstrapTest.php +++ b/tests/Functional/Plugins/Core/Router/BootstrapTest.php @@ -30,6 +30,7 @@ use Enlight_Controller_Response_ResponseTestCase; use PHPUnit\Framework\TestCase; use Shopware\Tests\Functional\Traits\ContainerTrait; +use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\Response; class BootstrapTest extends TestCase @@ -52,4 +53,32 @@ public function testOnRouteShutdown(): void static::assertSame(Response::HTTP_MOVED_PERMANENTLY, $response->getStatusCode()); static::assertSame('shopware.php/www.test.de', $response->getHeader('location')); } + + public function testOnRouteStartupClearShopCookie(): void + { + $pluginBootstrap = $this->getContainer()->get('plugins')->Core()->Router(); + + $request = new Enlight_Controller_Request_RequestTestCase(); + $request->setRequestUri('shopware.php/www.test.de'); + $request->setCookie('shop', 2); + $response = new Enlight_Controller_Response_ResponseTestCase(); + $args = new Enlight_Controller_EventArgs([ + 'request' => $request, + 'response' => $response, + ]); + $pluginBootstrap->onRouteStartup($args); + + static::assertSame(Response::HTTP_OK, $response->getStatusCode()); + + // Option A + static::assertIsString($response->headers->get('Set-Cookie')); + static::assertStringContainsString('shop=deleted', $response->headers->get('Set-Cookie')); + + // Option B + $cookie = $response->headers->getCookies(); + static::assertCount(1, $cookie); + static::assertInstanceOf(Cookie::class, $cookie[0]); + static::assertSame('shop', $cookie[0]->getName()); + static::assertTrue($cookie[0]->isCleared()); + } }