Skip to content

Commit 7534ab0

Browse files
committed
fix: backend article preview shop cookie
1 parent f5256ae commit 7534ab0

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

engine/Shopware/Plugins/Default/Core/Router/Bootstrap.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Shopware\Models\Shop\Shop;
3030
use Shopware\Models\Shop\Template;
3131
use Symfony\Component\HttpFoundation\Cookie;
32+
use Symfony\Component\HttpFoundation\Response;
3233

3334
class Shopware_Plugins_Core_Router_Bootstrap extends Shopware_Components_Plugin_Bootstrap
3435
{
@@ -53,14 +54,15 @@ public function install()
5354
public function onRouteStartup(Enlight_Controller_EventArgs $args)
5455
{
5556
$request = $args->getRequest();
57+
$response = $args->getResponse();
5658

5759
if (str_starts_with($request->getPathInfo(), '/backend')
5860
|| str_starts_with($request->getPathInfo(), '/api/')
5961
) {
6062
return;
6163
}
6264

63-
$shop = $this->getShopByRequest($request);
65+
$shop = $this->getShopByRequest($request, $response);
6466

6567
if (!$shop->getHost()) {
6668
$shop->setHost($request->getHttpHost());
@@ -306,7 +308,7 @@ protected function upgradeShop($request, $response)
306308
*
307309
* @return Shop
308310
*/
309-
protected function getShopByRequest(Request $request)
311+
protected function getShopByRequest(Request $request, Response $response)
310312
{
311313
$repository = $this->get(ModelManager::class)->getRepository(Shop::class);
312314

@@ -317,6 +319,7 @@ protected function getShopByRequest(Request $request)
317319

318320
if ($shop === null && $request->getCookie('shop') !== null) {
319321
$shop = $repository->getActiveById($request->getCookie('shop'));
322+
$response->headers->clearCookie('shop');
320323
}
321324

322325
if ($shop && $request->getCookie('shop') !== null && $request->getPost('__shop') === null) {

tests/Functional/Plugins/Core/Router/BootstrapTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Enlight_Controller_Response_ResponseTestCase;
3131
use PHPUnit\Framework\TestCase;
3232
use Shopware\Tests\Functional\Traits\ContainerTrait;
33+
use Symfony\Component\HttpFoundation\Cookie;
3334
use Symfony\Component\HttpFoundation\Response;
3435

3536
class BootstrapTest extends TestCase
@@ -52,4 +53,32 @@ public function testOnRouteShutdown(): void
5253
static::assertSame(Response::HTTP_MOVED_PERMANENTLY, $response->getStatusCode());
5354
static::assertSame('shopware.php/www.test.de', $response->getHeader('location'));
5455
}
56+
57+
public function testOnRouteStartupClearShopCookie(): void
58+
{
59+
$pluginBootstrap = $this->getContainer()->get('plugins')->Core()->Router();
60+
61+
$request = new Enlight_Controller_Request_RequestTestCase();
62+
$request->setRequestUri('shopware.php/www.test.de');
63+
$request->setCookie('shop', 2);
64+
$response = new Enlight_Controller_Response_ResponseTestCase();
65+
$args = new Enlight_Controller_EventArgs([
66+
'request' => $request,
67+
'response' => $response,
68+
]);
69+
$pluginBootstrap->onRouteStartup($args);
70+
71+
static::assertSame(Response::HTTP_OK, $response->getStatusCode());
72+
73+
// Option A
74+
static::assertIsString($response->headers->get('Set-Cookie'));
75+
static::assertStringContainsString('shop=deleted', $response->headers->get('Set-Cookie'));
76+
77+
// Option B
78+
$cookie = $response->headers->getCookies();
79+
static::assertCount(1, $cookie);
80+
static::assertInstanceOf(Cookie::class, $cookie[0]);
81+
static::assertSame('shop', $cookie[0]->getName());
82+
static::assertTrue($cookie[0]->isCleared());
83+
}
5584
}

0 commit comments

Comments
 (0)