Skip to content

Commit

Permalink
[frontend-api] addProductToListMutation: ensure new product list is c…
Browse files Browse the repository at this point in the history
…reated with non-conflicting uuid (#3126)
  • Loading branch information
vitek-rostislav committed Apr 24, 2024
2 parents 784888e + 8693bde commit 1f4caf9
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions UPGRADE-14.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,11 @@ Follow the instructions in relevant sections, e.g. `shopsys/coding-standards` or
- see #project-base-diff to update your project

- take promo code into account in priceByTransportQuery and priceByPaymentQuery ([#3118](https://github.com/shopsys/shopsys/pull/3118))

- see #project-base-diff to update your project

- addProductToListMutation: ensure new product list is created with non-conflicting uuid ([#3126](https://github.com/shopsys/shopsys/pull/3126))
- add new tests to `ProductListLoggedCustomerTest` and `ProductListNotLoggedCustomerTest` classes
- see #project-base-diff to update your project

### Storefront
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public function findAnonymousProductList(
return $this->productListRepository->findAnonymousProductList($uuid, $productListType);
}

/**
* @param string $uuid
* @return bool
*/
public function existsProductListWithUuid(string $uuid): bool
{
return $this->productListRepository->existsProductListWithUuid($uuid);
}

/**
* @param \Shopsys\FrameworkBundle\Model\Product\List\ProductList $productList
* @param \Shopsys\FrameworkBundle\Model\Product\Product $product
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,13 @@ protected function getRepository(): EntityRepository
{
return $this->entityManager->getRepository(ProductList::class);
}

/**
* @param string $uuid
* @return bool
*/
public function existsProductListWithUuid(string $uuid): bool
{
return $this->getRepository()->count(['uuid' => $uuid]) > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shopsys\FrontendApiBundle\Model\Mutation\ProductList;

use Overblog\GraphQLBundle\Definition\Argument;
use Ramsey\Uuid\Uuid;
use Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser;
use Shopsys\FrameworkBundle\Model\Product\Exception\ProductNotFoundException;
use Shopsys\FrameworkBundle\Model\Product\List\Exception\ProductAlreadyInListException;
Expand Down Expand Up @@ -60,6 +61,10 @@ public function addProductToListMutation(Argument $argument): ProductList
}

if ($productList === null) {
if ($productListUuid !== null && $this->productListFacade->existsProductListWithUuid($productListUuid)) {
$productListUuid = Uuid::uuid4()->toString();
}

$productListData = $this->productListDataFactory->create($productListType, $customerUser, $productListUuid);
$productList = $this->productListFacade->create($productListData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,28 @@ public function testAddProductCreatesNewListWhenNewUuidIsProvided(string $produc
$this->assertSame([$productToAddId], array_column($data['products'], 'id'));
}

/**
* @dataProvider \Tests\FrontendApiBundle\Functional\Product\ProductList\ProductListTypesDataProvider::getProductListTypes
* @param string $productListType
*/
public function testAddProductCreatesNewListWithNewUuidWhenUuidOfAnonymousListIsProvided(
string $productListType,
): void {
$anonymousProductListUuid = $this->getAnonymousProductListUuid($productListType);
$productToAddId = 69;
$productToAdd = $this->getReference(ProductDataFixture::PRODUCT_PREFIX . $productToAddId);
$response = $this->getResponseContentForGql(__DIR__ . '/graphql/AddProductToListMutation.graphql', [
'productListUuid' => $anonymousProductListUuid,
'productUuid' => $productToAdd->getUuid(),
'type' => $productListType,
]);
$data = $this->getResponseDataForGraphQlType($response, 'AddProductToList');

$this->assertNotSame($anonymousProductListUuid, $data['uuid']);
$this->assertSame($productListType, $data['type']);
$this->assertSame([$productToAddId], array_column($data['products'], 'id'));
}

/**
* @dataProvider productListDataProvider
* @param string $productListType
Expand Down Expand Up @@ -242,4 +264,17 @@ public function productListDataProvider(): Iterator
'expectedProductIds' => [1],
];
}

/**
* @param string $productListType
* @return string
*/
private function getAnonymousProductListUuid(string $productListType): string
{
return match ($productListType) {
ProductListTypeEnum::COMPARISON => ProductListDataFixture::PRODUCT_LIST_COMPARISON_NOT_LOGGED_CUSTOMER_UUID,
ProductListTypeEnum::WISHLIST => ProductListDataFixture::PRODUCT_LIST_WISHLIST_NOT_LOGGED_CUSTOMER_UUID,
default => throw new UnknownProductListTypeException($productListType),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,28 @@ public function testAddProductCreatesNewListWhenNewUuidIsProvided(string $produc
$this->assertSame([$productToAddId], array_column($data['products'], 'id'));
}

/**
* @dataProvider \Tests\FrontendApiBundle\Functional\Product\ProductList\ProductListTypesDataProvider::getProductListTypes
* @param string $productListType
*/
public function testAddProductCreatesNewListWithNewUuidWhenUuidOfCustomerUserListIsProvided(
string $productListType,
): void {
$customerUserProductListUuid = $this->getCustomerUserProductListUuid($productListType);
$productToAddId = 69;
$productToAdd = $this->getReference(ProductDataFixture::PRODUCT_PREFIX . $productToAddId);
$response = $this->getResponseContentForGql(__DIR__ . '/graphql/AddProductToListMutation.graphql', [
'productListUuid' => $customerUserProductListUuid,
'productUuid' => $productToAdd->getUuid(),
'type' => $productListType,
]);
$data = $this->getResponseDataForGraphQlType($response, 'AddProductToList');

$this->assertNotSame($customerUserProductListUuid, $data['uuid']);
$this->assertSame($productListType, $data['type']);
$this->assertSame([$productToAddId], array_column($data['products'], 'id'));
}

/**
* @dataProvider \Tests\FrontendApiBundle\Functional\Product\ProductList\ProductListTypesDataProvider::getProductListTypes
* @param string $productListType
Expand Down Expand Up @@ -471,4 +493,17 @@ private function assertOriginalAnonymousListsDoNotExist(): void
$this->assertTrue($originalAnonymousWishlist === null, 'Original anonymous wishlist should not exist anymore');
$this->assertTrue($originalAnonymousComparison === null, 'Original anonymous comparison should not exist anymore');
}

/**
* @param string $productListType
* @return string
*/
private function getCustomerUserProductListUuid(string $productListType): string
{
return match ($productListType) {
ProductListTypeEnum::COMPARISON => ProductListDataFixture::PRODUCT_LIST_COMPARISON_LOGGED_CUSTOMER_UUID,
ProductListTypeEnum::WISHLIST => ProductListDataFixture::PRODUCT_LIST_WISHLIST_LOGGED_CUSTOMER_UUID,
default => throw new UnknownProductListTypeException($productListType),
};
}
}

0 comments on commit 1f4caf9

Please sign in to comment.