Skip to content

Commit

Permalink
Add shop association endpoint, and improve multishop tests to check u…
Browse files Browse the repository at this point in the history
…pdates with different shop parameters including list of shop IDs
  • Loading branch information
jolelievre committed Dec 30, 2024
1 parent 284354c commit 121c9d6
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ApiPlatform/Resources/Product/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class Product
#[LocalizedValue]
public array $descriptions;

#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer']])]
public array $shopIds;

public const QUERY_MAPPING = [
'[_context][shopConstraint]' => '[shopConstraint]',
'[_context][langId]' => '[displayLanguageId]',
Expand Down
64 changes: 64 additions & 0 deletions src/ApiPlatform/Resources/Product/ProductShops.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Product;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Product\Exception\ProductNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Product\Query\GetProductForEditing;
use PrestaShop\PrestaShop\Core\Domain\Product\Shop\Command\SetProductShopsCommand;
use PrestaShop\PrestaShop\Core\Domain\Shop\Exception\ShopAssociationNotFound;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSPartialUpdate;
use Symfony\Component\HttpFoundation\Response;

#[ApiResource(
operations: [
new CQRSPartialUpdate(
uriTemplate: '/product/{productId}/shops',
CQRSCommand: SetProductShopsCommand::class,
CQRSQuery: GetProductForEditing::class,
scopes: [
'product_write',
],
CQRSQueryMapping: Product::QUERY_MAPPING,
CQRSCommandMapping: [
'[associatedShopIds]' => '[shopIds]',
],
),
],
exceptionToStatus: [
ProductNotFoundException::class => Response::HTTP_NOT_FOUND,
ShopAssociationNotFound::class => Response::HTTP_NOT_FOUND,
],
)]
class ProductShops extends Product
{
public int $sourceShopId;

#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer']])]
public array $associatedShopIds;
}
146 changes: 146 additions & 0 deletions tests/Integration/ApiPlatform/ProductMultiShopEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public static function setUpBeforeClass(): void
'fr-FR' => '',
],
'active' => false,
'shopIds' => [
self::DEFAULT_SHOP_ID,
],
];

$featureFlagManager = self::getContainer()->get('PrestaShop\PrestaShop\Core\FeatureFlag\FeatureFlagManager');
Expand Down Expand Up @@ -204,6 +207,149 @@ public function testGetProductForSecondShopIsFailing(int $productId): int
return $productId;
}

/**
* @depends testGetProductForSecondShopIsFailing
*
* @param int $productId
*
* @return int
*/
public function testAssociateProductToShops(int $productId): int
{
$allShopIds = [
self::DEFAULT_SHOP_ID,
self::$secondShopId,
self::$thirdShopId,
self::$fourthShopId,
];
$bearerToken = $this->getBearerToken(['product_write']);
$response = static::createClient()->request('PATCH', '/product/' . $productId . '/shops', [
'auth_bearer' => $bearerToken,
'extra' => [
'parameters' => [
'shopId' => self::DEFAULT_SHOP_ID,
],
],
'json' => [
'sourceShopId' => self::DEFAULT_SHOP_ID,
'associatedShopIds' => $allShopIds,
],
]);
$updatedProduct = json_decode($response->getContent(), true);
$this->assertEquals($productId, $updatedProduct['productId']);
$this->assertEquals($allShopIds, $updatedProduct['shopIds']);

return $productId;
}

/**
* @depends testAssociateProductToShops
*
* @param int $productId
*
* @return int
*/
public function testUpdateProductForShops(int $productId): int
{
$bearerToken = $this->getBearerToken(['product_write']);
// Modify name for all shops
static::createClient()->request('PATCH', '/product/' . $productId, [
'auth_bearer' => $bearerToken,
'extra' => [
'parameters' => [
'allShops' => true,
],
],
'json' => [
'names' => [
'en-US' => 'global product name',
],
],
]);
self::assertResponseStatusCodeSame(200);

// Check that all shops have been modified
foreach ([self::DEFAULT_SHOP_ID, self::$secondShopId, self::$thirdShopId, self::$fourthShopId] as $shopId) {
$product = $this->getProduct($productId, $shopId);
$this->assertEquals('global product name', $product['names']['en-US']);
}

// Modify names for second group shop
static::createClient()->request('PATCH', '/product/' . $productId, [
'auth_bearer' => $bearerToken,
'extra' => [
'parameters' => [
'shopGroupId' => self::$secondShopGroupId,
],
],
'json' => [
'names' => [
'en-US' => 'second group product name',
],
],
]);
self::assertResponseStatusCodeSame(200);

// Modify names for first shop
static::createClient()->request('PATCH', '/product/' . $productId, [
'auth_bearer' => $bearerToken,
'extra' => [
'parameters' => [
'shopId' => self::DEFAULT_SHOP_ID,
],
],
'json' => [
'names' => [
'en-US' => 'first shop product name',
],
],
]);
self::assertResponseStatusCodeSame(200);

// Modify names for shop2 and shop4
static::createClient()->request('PATCH', '/product/' . $productId, [
'auth_bearer' => $bearerToken,
'extra' => [
'parameters' => [
'shopIds' => [self::$secondShopId, self::$fourthShopId],
],
],
'json' => [
'names' => [
'en-US' => 'even shops product name',
],
],
]);
self::assertResponseStatusCodeSame(200);

// Now check each shop modified content
$product = $this->getProduct($productId, self::DEFAULT_SHOP_ID);
$this->assertEquals('first shop product name', $product['names']['en-US']);
$product = $this->getProduct($productId, self::$secondShopId);
$this->assertEquals('even shops product name', $product['names']['en-US']);
$product = $this->getProduct($productId, self::$thirdShopId);
$this->assertEquals('second group product name', $product['names']['en-US']);
$product = $this->getProduct($productId, self::$fourthShopId);
$this->assertEquals('even shops product name', $product['names']['en-US']);

return $productId;
}

protected function getProduct(int $productId, int $shopId): array
{
$bearerToken = $this->getBearerToken(['product_read']);
$response = static::createClient()->request('GET', '/product/' . $productId, [
'auth_bearer' => $bearerToken,
'extra' => [
'parameters' => [
'shopId' => $shopId,
],
],
]);

return json_decode($response->getContent(), true);
}

protected function assertProductData(int $productId, array $expectedData, ResponseInterface $response): void
{
// Merge expected data with default one, this way no need to always specify all the fields
Expand Down

0 comments on commit 121c9d6

Please sign in to comment.