Skip to content

Commit

Permalink
Handle special parameter shopIds in API shop context listener to defi…
Browse files Browse the repository at this point in the history
…ne a ShopCollection as the context
  • Loading branch information
jolelievre committed Dec 19, 2024
1 parent f7916eb commit 87d3dec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use PrestaShop\PrestaShop\Adapter\Feature\MultistoreFeature;
use PrestaShop\PrestaShop\Core\Context\ShopContextBuilder;
use PrestaShop\PrestaShop\Core\Domain\Configuration\ShopConfigurationInterface;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopCollection;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint;
use PrestaShopBundle\Controller\Api\OAuth2\AccessTokenController;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -73,6 +74,8 @@ public function onKernelRequest(RequestEvent $event): void
$this->shopContextBuilder->setShopConstraint($shopConstraint);
if ($shopConstraint->getShopId()) {
$this->shopContextBuilder->setShopId($shopConstraint->getShopId()->getValue());
} elseif ($shopConstraint instanceof ShopCollection && $shopConstraint->hasShopIds()) {
$this->shopContextBuilder->setShopId($shopConstraint->getShopIds()[0]->getValue());
} else {
$this->shopContextBuilder->setShopId($this->getConfiguredDefaultShopId());
}
Expand All @@ -97,13 +100,22 @@ private function getShopConstraintFromRequest(Request $request): ?ShopConstraint
return ShopConstraint::shopGroup((int) $request->get('shopGroupId'));
}

if ($request->get('shopIds')) {
$shopIds = $request->get('shopIds');
if (is_string($shopIds)) {
$shopIds = explode(',', $shopIds);
}

return ShopCollection::shops(array_map(fn (string $shopId) => (int) $shopId, $shopIds));
}

// Parameter allShops indicate the all shops context regardless of its value, it can be empty it's enough
if ($request->query->has('allShops') || $request->request->has('allShops') || $request->attributes->has('allShops')) {
return ShopConstraint::allShops();
}

// Special use case when calling the access token controller, we don't want to block the endpoint even if no
// context parameters as specified, so we use the default shop as a fallback
// context parameters was specified, so we use the default shop as a fallback
if ($request->attributes->get('_controller') === AccessTokenController::class) {
return ShopConstraint::shop($this->getConfiguredDefaultShopId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use PrestaShop\PrestaShop\Adapter\Feature\MultistoreFeature;
use PrestaShop\PrestaShop\Core\Context\ShopContextBuilder;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopCollection;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint;
use PrestaShopBundle\Controller\Api\OAuth2\AccessTokenController;
use PrestaShopBundle\EventListener\API\Context\ShopContextListener;
Expand All @@ -42,6 +43,7 @@ class ShopContextListenerTest extends ContextEventListenerTestCase
{
private const DEFAULT_SHOP_ID = 42;
private const QUERY_SHOP_ID = 51;
private const QUERY_SECOND_SHOP_ID = 99;
private const QUERY_SHOP_GROUP_ID = 69;

public function testShopContextWhenMultishopDisabled(): void
Expand Down Expand Up @@ -168,6 +170,42 @@ public function getMultishopRequests(): iterable
ShopConstraint::allShops(),
self::DEFAULT_SHOP_ID,
];

yield 'shop collection query parameter string list' => [
new Request(['shopIds' => self::QUERY_SHOP_ID . ',' . self::QUERY_SECOND_SHOP_ID]),
ShopCollection::shops([self::QUERY_SHOP_ID, self::QUERY_SECOND_SHOP_ID]),
self::QUERY_SHOP_ID,
];

yield 'shop collection request parameter string list' => [
new Request([], ['shopIds' => self::QUERY_SECOND_SHOP_ID . ',' . self::QUERY_SHOP_ID]),
ShopCollection::shops([self::QUERY_SECOND_SHOP_ID, self::QUERY_SHOP_ID]),
self::QUERY_SECOND_SHOP_ID,
];

yield 'shop collection attribute parameter string list' => [
new Request([], [], ['shopIds' => self::QUERY_SECOND_SHOP_ID . ', ' . self::QUERY_SHOP_ID]),
ShopCollection::shops([self::QUERY_SECOND_SHOP_ID, self::QUERY_SHOP_ID]),
self::QUERY_SECOND_SHOP_ID,
];

yield 'shop collection query parameter array' => [
new Request(['shopIds' => [self::QUERY_SHOP_ID, self::QUERY_SECOND_SHOP_ID]]),
ShopCollection::shops([self::QUERY_SHOP_ID, self::QUERY_SECOND_SHOP_ID]),
self::QUERY_SHOP_ID,
];

yield 'shop collection request parameter array' => [
new Request([], ['shopIds' => [self::QUERY_SECOND_SHOP_ID, self::QUERY_SHOP_ID]]),
ShopCollection::shops([self::QUERY_SECOND_SHOP_ID, self::QUERY_SHOP_ID]),
self::QUERY_SECOND_SHOP_ID,
];

yield 'shop collection attribute parameter array' => [
new Request([], [], ['shopIds' => [self::QUERY_SECOND_SHOP_ID, self::QUERY_SHOP_ID]]),
ShopCollection::shops([self::QUERY_SECOND_SHOP_ID, self::QUERY_SHOP_ID]),
self::QUERY_SECOND_SHOP_ID,
];
}

public function testMissingRequestParametersWhenMultishopIsEnabled(): void
Expand Down

0 comments on commit 87d3dec

Please sign in to comment.