Skip to content

Commit

Permalink
fix: backend article preview shop cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominikrt committed Aug 28, 2024
1 parent f5256ae commit 7534ab0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 5 additions & 2 deletions engine/Shopware/Plugins/Default/Core/Router/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -53,14 +54,15 @@ 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/')
) {
return;
}

$shop = $this->getShopByRequest($request);
$shop = $this->getShopByRequest($request, $response);

if (!$shop->getHost()) {
$shop->setHost($request->getHttpHost());
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions tests/Functional/Plugins/Core/Router/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}

0 comments on commit 7534ab0

Please sign in to comment.