From 29798768797ba14100b48068e3b071cde839077c Mon Sep 17 00:00:00 2001 From: Pieter Hoste Date: Thu, 25 Feb 2021 12:31:12 +0100 Subject: [PATCH 01/13] Restore UNIQUE_CHECKS mysql variable to its original value when done with changing the database structure. This prevents potentially inserting duplicated values while manipulating data in the setup:upgrade command. --- .../Declaration/Schema/OperationsExecutor.php | 14 ++++++++------ .../Declaration/Schema/OperationsExecutorTest.php | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/Setup/Declaration/Schema/OperationsExecutor.php b/lib/internal/Magento/Framework/Setup/Declaration/Schema/OperationsExecutor.php index ed1baff1d3323..3ed7c9d69fc41 100644 --- a/lib/internal/Magento/Framework/Setup/Declaration/Schema/OperationsExecutor.php +++ b/lib/internal/Magento/Framework/Setup/Declaration/Schema/OperationsExecutor.php @@ -132,10 +132,10 @@ public function getDestructiveOperations() private function startSetupForAllConnections() { foreach ($this->sharding->getResources() as $resource) { - $this->resourceConnection->getConnection($resource) - ->startSetup(); - $this->resourceConnection->getConnection($resource) - ->query('SET UNIQUE_CHECKS=0'); + $connection = $this->resourceConnection->getConnection($resource); + + $connection->startSetup(); + $connection->query('SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0'); } } @@ -148,8 +148,10 @@ private function startSetupForAllConnections() private function endSetupForAllConnections() { foreach ($this->sharding->getResources() as $resource) { - $this->resourceConnection->getConnection($resource) - ->endSetup(); + $connection = $this->resourceConnection->getConnection($resource); + + $connection->query('SET UNIQUE_CHECKS=IF(@OLD_UNIQUE_CHECKS=0, 0, 1)'); + $connection->endSetup(); } } diff --git a/lib/internal/Magento/Framework/Setup/Test/Unit/Declaration/Schema/OperationsExecutorTest.php b/lib/internal/Magento/Framework/Setup/Test/Unit/Declaration/Schema/OperationsExecutorTest.php index 1090f2e04e190..ced07a1fc7650 100644 --- a/lib/internal/Magento/Framework/Setup/Test/Unit/Declaration/Schema/OperationsExecutorTest.php +++ b/lib/internal/Magento/Framework/Setup/Test/Unit/Declaration/Schema/OperationsExecutorTest.php @@ -160,7 +160,7 @@ public function testExecute() $connectionMock = $this->getMockBuilder(Mysql::class) ->disableOriginalConstructor() ->getMock(); - $this->resourceConnectionMock->expects(self::exactly(3)) + $this->resourceConnectionMock->expects(self::exactly(2)) ->method('getConnection') ->with('default') ->willReturn($connectionMock); From f5fe281479e49b942bb9beffae70f4228d691bb4 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Mon, 5 Jul 2021 14:15:30 -0500 Subject: [PATCH 02/13] MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists --- .../Model/Cart/AddProductsToCart.php | 20 +++- .../Model/Cart/AddSimpleProductToCart.php | 110 +++++++++++++++++- 2 files changed, 125 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php index 0360d9ccf5476..ab9a5a7041d3d 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php @@ -8,9 +8,9 @@ namespace Magento\QuoteGraphQl\Model\Cart; use Magento\Framework\GraphQl\Exception\GraphQlInputException; -use Magento\Framework\Message\MessageInterface; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Model\Quote; +use Magento\Framework\App\CacheInterface; /** * Adding products to cart using GraphQL @@ -27,16 +27,24 @@ class AddProductsToCart */ private $addProductToCart; + /** + * @var CacheInterface + */ + private $cache; + /** * @param CartRepositoryInterface $cartRepository * @param AddSimpleProductToCart $addProductToCart + * @param CacheInterface $cache */ public function __construct( CartRepositoryInterface $cartRepository, - AddSimpleProductToCart $addProductToCart + AddSimpleProductToCart $addProductToCart, + CacheInterface $cache ) { $this->cartRepository = $cartRepository; $this->addProductToCart = $addProductToCart; + $this->cache = $cache; } /** @@ -50,10 +58,16 @@ public function __construct( */ public function execute(Quote $cart, array $cartItems): void { + $ck = 'cart_processing_mutex_' . $cart->getId(); + while ($this->cache->load($ck) === '1') { + // wait till other process working with the same cart complete + usleep(rand (300, 600)); + } + $this->cache->save('1', $ck, [], 1); foreach ($cartItems as $cartItemData) { $this->addProductToCart->execute($cart, $cartItemData); } - $this->cartRepository->save($cart); + $this->cache->remove($ck); } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index f2dd6389d2c4a..32840b47ba197 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -9,11 +9,21 @@ use Exception; use Magento\Catalog\Api\ProductRepositoryInterface; +use Magento\Catalog\Model\Product; +use Magento\Framework\DataObject; +use Magento\Quote\Model\Quote\Item; +use Magento\Catalog\Model\Product\Type\AbstractType; +use Magento\Framework\Model\Context; +use Magento\Framework\Event\ManagerInterface; +use Magento\Framework\Phrase; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Quote\Model\Quote; +use Magento\Quote\Model\Quote\Item\Processor; use Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestBuilder; +use Magento\Framework\App\CacheInterface; /** * Add simple product to cart mutation @@ -30,16 +40,33 @@ class AddSimpleProductToCart */ private $buyRequestBuilder; + /** + * @var Processor + */ + private $itemProcessor; + + /** + * @var ManagerInterface + */ + private $eventManager; + /** * @param ProductRepositoryInterface $productRepository * @param BuyRequestBuilder $buyRequestBuilder + * @param Processor $itemProcessor + * @param Context $context */ public function __construct( ProductRepositoryInterface $productRepository, - BuyRequestBuilder $buyRequestBuilder + BuyRequestBuilder $buyRequestBuilder, + Processor $itemProcessor, + Context $context, + CacheInterface $cache ) { $this->productRepository = $productRepository; $this->buyRequestBuilder = $buyRequestBuilder; + $this->itemProcessor = $itemProcessor; + $this->eventManager = $context->getEventDispatcher(); } /** @@ -62,7 +89,11 @@ public function execute(Quote $cart, array $cartItemData): void } try { - $result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData)); + $result = $this->addProductToCartWithConcurrency( + $cart, + $product, + $this->buyRequestBuilder->build($cartItemData) + ); } catch (Exception $e) { throw new GraphQlInputException( __( @@ -100,4 +131,79 @@ private function extractSku(array $cartItemData): string } return (string)$cartItemData['data']['sku']; } + + /** + * @param Quote $cart + * @param Product $product + * @param DataObject $request + * @return Item + * @throws LocalizedException + */ + private function addProductToCartWithConcurrency(Quote $cart, Product $product, DataObject $request) : Item + { + if (!$product->isSalable()) { + throw new LocalizedException( + __('Product that you are trying to add is not available.') + ); + } + $cartCandidates = $product->getTypeInstance()->prepareForCartAdvanced( + $request, + $product, + AbstractType::PROCESS_MODE_FULL + ); + if (is_string($cartCandidates) || $cartCandidates instanceof Phrase) { + throw new LocalizedException((string)$cartCandidates); + } + if (!is_array($cartCandidates)) { + $cartCandidates = [$cartCandidates]; + } + $parentItem = null; + $errors = []; + $items = []; + foreach ($cartCandidates as $candidate) { + $stickWithinParent = $candidate->getParentProductId() ? $parentItem : null; + $candidate->setStickWithinParent($stickWithinParent); + $item = null; + $itemsCollection = $cart->getItemsCollection(false); + + foreach ($itemsCollection as $item) { + if (!$item->isDeleted() && $item->representProduct($product)) { + break; + } + } + + if (!$item) { + $item = $this->itemProcessor->init($candidate, $request); + $item->setQuote($cart); + $item->setOptions($candidate->getCustomOptions()); + $item->setProduct($candidate); + $cart->addItem($item); + } + $items[] = $item; + + if (!$parentItem) { + $parentItem = $item; + } + if ($parentItem && $candidate->getParentProductId() && !$item->getParentItem()) { + $item->setParentItem($parentItem); + } + + $this->itemProcessor->prepare($item, $request, $candidate); + + if ($item->getHasError()) { + $cart->deleteItem($item); + foreach ($item->getMessage(false) as $message) { + if (!in_array($message, $errors)) { + $errors[] = $message; + } + } + break; + } + } + if (!empty($errors)) { + throw new LocalizedException(__(implode("\n", $errors))); + } + $this->eventManager->dispatch('sales_quote_product_add_after', ['items' => $items]); + return $parentItem; + } } From 73c3f2bfd96fdd3f009e02502ed315885b0bf62a Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 6 Jul 2021 09:35:31 -0500 Subject: [PATCH 03/13] MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists --- .../QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index 32840b47ba197..1cd6798c7d83e 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -199,6 +199,16 @@ private function addProductToCartWithConcurrency(Quote $cart, Product $product, } break; } + + $itemsToUpdate = []; + foreach ($cart->getItems() as $itemToUpdate) { + if ($itemToUpdate->getItemId() === $item->getItemId()) { + $itemsToUpdate[] = $item; + } else { + $itemsToUpdate[] = $itemToUpdate; + } + } + $cart->setItems($itemsToUpdate); } if (!empty($errors)) { throw new LocalizedException(__(implode("\n", $errors))); From 918060db21874017c3a19e416a6ebc10c35a090a Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 6 Jul 2021 11:41:07 -0500 Subject: [PATCH 04/13] MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists --- .../Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index 1cd6798c7d83e..e919ef3a312b7 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -89,11 +89,14 @@ public function execute(Quote $cart, array $cartItemData): void } try { + $result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData)); + /* $result = $this->addProductToCartWithConcurrency( $cart, $product, $this->buyRequestBuilder->build($cartItemData) ); + */ } catch (Exception $e) { throw new GraphQlInputException( __( From 19cc4b9370d32f0efdaebe642cf624827de7a081 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 6 Jul 2021 13:07:36 -0500 Subject: [PATCH 05/13] MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists --- .../QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index e919ef3a312b7..2a4aa75dd0c7a 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -89,6 +89,12 @@ public function execute(Quote $cart, array $cartItemData): void } try { + $items = []; + $collection = $cart->getItemsCollection(false); + foreach ($collection as $item) { + $items[] = $item; + } + $cart->setItems($items); $result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData)); /* $result = $this->addProductToCartWithConcurrency( From 34c9d4f070960cb818613172045a9f8af226ce3a Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 6 Jul 2021 17:10:10 -0500 Subject: [PATCH 06/13] MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists --- .../Model/Cart/AddProductsToCart.php | 36 +++++++++++++------ .../Model/Cart/AddSimpleProductToCart.php | 6 ---- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php index ab9a5a7041d3d..b01c39e805de1 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php @@ -10,7 +10,7 @@ use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Model\Quote; -use Magento\Framework\App\CacheInterface; +use Magento\Framework\Lock\LockManagerInterface; /** * Adding products to cart using GraphQL @@ -28,23 +28,23 @@ class AddProductsToCart private $addProductToCart; /** - * @var CacheInterface + * @var LockManagerInterface */ - private $cache; + private $lockManager; /** * @param CartRepositoryInterface $cartRepository * @param AddSimpleProductToCart $addProductToCart - * @param CacheInterface $cache + * @param LockManagerInterface $lockManager */ public function __construct( CartRepositoryInterface $cartRepository, AddSimpleProductToCart $addProductToCart, - CacheInterface $cache + LockManagerInterface $lockManager ) { $this->cartRepository = $cartRepository; $this->addProductToCart = $addProductToCart; - $this->cache = $cache; + $this->lockManager = $lockManager; } /** @@ -58,16 +58,32 @@ public function __construct( */ public function execute(Quote $cart, array $cartItems): void { - $ck = 'cart_processing_mutex_' . $cart->getId(); - while ($this->cache->load($ck) === '1') { + $lockName = 'cart_processing_lock_' . $cart->getId(); + while ($this->lockManager->isLocked($lockName)) { // wait till other process working with the same cart complete usleep(rand (300, 600)); } - $this->cache->save('1', $ck, [], 1); + $this->lockManager->lock($lockName); + $this->refreshCartCache($cart); foreach ($cartItems as $cartItemData) { $this->addProductToCart->execute($cart, $cartItemData); } $this->cartRepository->save($cart); - $this->cache->remove($ck); + $this->lockManager->unlock($lockName); + } + + /** + * Refresh cart collection cache + * + * @param Quote $cart + */ + private function refreshCartCache(Quote $cart) : void + { + $items = []; + $collection = $cart->getItemsCollection(false); + foreach ($collection as $item) { + $items[] = $item; + } + $cart->setItems($items); } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index 2a4aa75dd0c7a..e919ef3a312b7 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -89,12 +89,6 @@ public function execute(Quote $cart, array $cartItemData): void } try { - $items = []; - $collection = $cart->getItemsCollection(false); - foreach ($collection as $item) { - $items[] = $item; - } - $cart->setItems($items); $result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData)); /* $result = $this->addProductToCartWithConcurrency( From ec98d13975fb373b0153dc122697cddf1c451380 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 6 Jul 2021 21:42:22 -0500 Subject: [PATCH 07/13] MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists --- .../Model/Cart/AddProductsToCart.php | 10 +- .../Model/Cart/AddSimpleProductToCart.php | 98 ------------------- 2 files changed, 7 insertions(+), 101 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php index b01c39e805de1..2b9a9121824a2 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php @@ -59,12 +59,16 @@ public function __construct( public function execute(Quote $cart, array $cartItems): void { $lockName = 'cart_processing_lock_' . $cart->getId(); + $needToRefreshCache = false; while ($this->lockManager->isLocked($lockName)) { // wait till other process working with the same cart complete - usleep(rand (300, 600)); + usleep(rand(100, 600)); + $needToRefreshCache = true; + } + $this->lockManager->lock($lockName, 1); + if ($needToRefreshCache) { + $this->refreshCartCache($cart); } - $this->lockManager->lock($lockName); - $this->refreshCartCache($cart); foreach ($cartItems as $cartItemData) { $this->addProductToCart->execute($cart, $cartItemData); } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index e919ef3a312b7..b981be6e2d37e 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -9,14 +9,8 @@ use Exception; use Magento\Catalog\Api\ProductRepositoryInterface; -use Magento\Catalog\Model\Product; -use Magento\Framework\DataObject; -use Magento\Quote\Model\Quote\Item; -use Magento\Catalog\Model\Product\Type\AbstractType; use Magento\Framework\Model\Context; use Magento\Framework\Event\ManagerInterface; -use Magento\Framework\Phrase; -use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; @@ -90,13 +84,6 @@ public function execute(Quote $cart, array $cartItemData): void try { $result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData)); - /* - $result = $this->addProductToCartWithConcurrency( - $cart, - $product, - $this->buyRequestBuilder->build($cartItemData) - ); - */ } catch (Exception $e) { throw new GraphQlInputException( __( @@ -134,89 +121,4 @@ private function extractSku(array $cartItemData): string } return (string)$cartItemData['data']['sku']; } - - /** - * @param Quote $cart - * @param Product $product - * @param DataObject $request - * @return Item - * @throws LocalizedException - */ - private function addProductToCartWithConcurrency(Quote $cart, Product $product, DataObject $request) : Item - { - if (!$product->isSalable()) { - throw new LocalizedException( - __('Product that you are trying to add is not available.') - ); - } - $cartCandidates = $product->getTypeInstance()->prepareForCartAdvanced( - $request, - $product, - AbstractType::PROCESS_MODE_FULL - ); - if (is_string($cartCandidates) || $cartCandidates instanceof Phrase) { - throw new LocalizedException((string)$cartCandidates); - } - if (!is_array($cartCandidates)) { - $cartCandidates = [$cartCandidates]; - } - $parentItem = null; - $errors = []; - $items = []; - foreach ($cartCandidates as $candidate) { - $stickWithinParent = $candidate->getParentProductId() ? $parentItem : null; - $candidate->setStickWithinParent($stickWithinParent); - $item = null; - $itemsCollection = $cart->getItemsCollection(false); - - foreach ($itemsCollection as $item) { - if (!$item->isDeleted() && $item->representProduct($product)) { - break; - } - } - - if (!$item) { - $item = $this->itemProcessor->init($candidate, $request); - $item->setQuote($cart); - $item->setOptions($candidate->getCustomOptions()); - $item->setProduct($candidate); - $cart->addItem($item); - } - $items[] = $item; - - if (!$parentItem) { - $parentItem = $item; - } - if ($parentItem && $candidate->getParentProductId() && !$item->getParentItem()) { - $item->setParentItem($parentItem); - } - - $this->itemProcessor->prepare($item, $request, $candidate); - - if ($item->getHasError()) { - $cart->deleteItem($item); - foreach ($item->getMessage(false) as $message) { - if (!in_array($message, $errors)) { - $errors[] = $message; - } - } - break; - } - - $itemsToUpdate = []; - foreach ($cart->getItems() as $itemToUpdate) { - if ($itemToUpdate->getItemId() === $item->getItemId()) { - $itemsToUpdate[] = $item; - } else { - $itemsToUpdate[] = $itemToUpdate; - } - } - $cart->setItems($itemsToUpdate); - } - if (!empty($errors)) { - throw new LocalizedException(__(implode("\n", $errors))); - } - $this->eventManager->dispatch('sales_quote_product_add_after', ['items' => $items]); - return $parentItem; - } } From adb7ff01277d61ed458123fa1b9d03f723b6c3f7 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 6 Jul 2021 21:46:18 -0500 Subject: [PATCH 08/13] MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists --- .../Model/Cart/AddSimpleProductToCart.php | 23 +------------------ 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php index b981be6e2d37e..f2dd6389d2c4a 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php @@ -9,15 +9,11 @@ use Exception; use Magento\Catalog\Api\ProductRepositoryInterface; -use Magento\Framework\Model\Context; -use Magento\Framework\Event\ManagerInterface; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Quote\Model\Quote; -use Magento\Quote\Model\Quote\Item\Processor; use Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestBuilder; -use Magento\Framework\App\CacheInterface; /** * Add simple product to cart mutation @@ -34,33 +30,16 @@ class AddSimpleProductToCart */ private $buyRequestBuilder; - /** - * @var Processor - */ - private $itemProcessor; - - /** - * @var ManagerInterface - */ - private $eventManager; - /** * @param ProductRepositoryInterface $productRepository * @param BuyRequestBuilder $buyRequestBuilder - * @param Processor $itemProcessor - * @param Context $context */ public function __construct( ProductRepositoryInterface $productRepository, - BuyRequestBuilder $buyRequestBuilder, - Processor $itemProcessor, - Context $context, - CacheInterface $cache + BuyRequestBuilder $buyRequestBuilder ) { $this->productRepository = $productRepository; $this->buyRequestBuilder = $buyRequestBuilder; - $this->itemProcessor = $itemProcessor; - $this->eventManager = $context->getEventDispatcher(); } /** From 6ab215e7e24b4498f683db7fedcd769f6bee7354 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Mon, 12 Jul 2021 15:13:01 -0500 Subject: [PATCH 09/13] MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists --- .../Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php index 2b9a9121824a2..23ae5adff03e2 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php @@ -58,6 +58,7 @@ public function __construct( */ public function execute(Quote $cart, array $cartItems): void { + /* $lockName = 'cart_processing_lock_' . $cart->getId(); $needToRefreshCache = false; while ($this->lockManager->isLocked($lockName)) { @@ -69,11 +70,12 @@ public function execute(Quote $cart, array $cartItems): void if ($needToRefreshCache) { $this->refreshCartCache($cart); } + */ foreach ($cartItems as $cartItemData) { $this->addProductToCart->execute($cart, $cartItemData); } $this->cartRepository->save($cart); - $this->lockManager->unlock($lockName); + //$this->lockManager->unlock($lockName); } /** From 2c550fb74426ab542f76fb31ea73a35bc9ce3ab6 Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Mon, 12 Jul 2021 15:32:58 -0500 Subject: [PATCH 10/13] MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists --- .../Resolver/AddSimpleProductsToCart.php | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php index 2135f3798d190..0adfbcc1d18fa 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/AddSimpleProductsToCart.php @@ -13,6 +13,7 @@ use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\QuoteGraphQl\Model\Cart\AddProductsToCart; use Magento\QuoteGraphQl\Model\Cart\GetCartForUser; +use Magento\Framework\Lock\LockManagerInterface; /** * Add simple products to cart GraphQl resolver @@ -30,16 +31,24 @@ class AddSimpleProductsToCart implements ResolverInterface */ private $addProductsToCart; + /** + * @var LockManagerInterface + */ + private $lockManager; + /** * @param GetCartForUser $getCartForUser * @param AddProductsToCart $addProductsToCart + * @param LockManagerInterface $lockManager */ public function __construct( GetCartForUser $getCartForUser, - AddProductsToCart $addProductsToCart + AddProductsToCart $addProductsToCart, + LockManagerInterface $lockManager ) { $this->getCartForUser = $getCartForUser; $this->addProductsToCart = $addProductsToCart; + $this->lockManager = $lockManager; } /** @@ -58,12 +67,20 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value throw new GraphQlInputException(__('Required parameter "cart_items" is missing')); } $cartItems = $args['input']['cart_items']; - $storeId = (int)$context->getExtensionAttributes()->getStore()->getId(); + + $lockName = 'cart_processing_lock_' . $maskedCartId; + while ($this->lockManager->isLocked($lockName)) { + // wait till other process working with the same cart complete + usleep(rand(100, 600)); + } + $this->lockManager->lock($lockName, 1); + $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId); $this->addProductsToCart->execute($cart, $cartItems); - $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId); + + $this->lockManager->unlock($lockName); return [ 'cart' => [ 'model' => $cart, From f83c2063a68e04fdd737fc104b635eaf57c04ebe Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 13 Jul 2021 09:47:35 -0500 Subject: [PATCH 11/13] MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists --- .../Model/Cart/AddProductsToCart.php | 25 +------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php index 23ae5adff03e2..7c9c5e37451c9 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php @@ -10,7 +10,6 @@ use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Quote\Api\CartRepositoryInterface; use Magento\Quote\Model\Quote; -use Magento\Framework\Lock\LockManagerInterface; /** * Adding products to cart using GraphQL @@ -27,24 +26,16 @@ class AddProductsToCart */ private $addProductToCart; - /** - * @var LockManagerInterface - */ - private $lockManager; - /** * @param CartRepositoryInterface $cartRepository * @param AddSimpleProductToCart $addProductToCart - * @param LockManagerInterface $lockManager */ public function __construct( CartRepositoryInterface $cartRepository, - AddSimpleProductToCart $addProductToCart, - LockManagerInterface $lockManager + AddSimpleProductToCart $addProductToCart ) { $this->cartRepository = $cartRepository; $this->addProductToCart = $addProductToCart; - $this->lockManager = $lockManager; } /** @@ -58,24 +49,10 @@ public function __construct( */ public function execute(Quote $cart, array $cartItems): void { - /* - $lockName = 'cart_processing_lock_' . $cart->getId(); - $needToRefreshCache = false; - while ($this->lockManager->isLocked($lockName)) { - // wait till other process working with the same cart complete - usleep(rand(100, 600)); - $needToRefreshCache = true; - } - $this->lockManager->lock($lockName, 1); - if ($needToRefreshCache) { - $this->refreshCartCache($cart); - } - */ foreach ($cartItems as $cartItemData) { $this->addProductToCart->execute($cart, $cartItemData); } $this->cartRepository->save($cart); - //$this->lockManager->unlock($lockName); } /** From 3cfaecd6b2eb4f4f87f7fd44784a8098f1ba81ac Mon Sep 17 00:00:00 2001 From: Oleksandr Iegorov Date: Tue, 13 Jul 2021 09:48:13 -0500 Subject: [PATCH 12/13] MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists --- .../QuoteGraphQl/Model/Cart/AddProductsToCart.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php index 7c9c5e37451c9..86cbb839d34fb 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php @@ -54,19 +54,4 @@ public function execute(Quote $cart, array $cartItems): void } $this->cartRepository->save($cart); } - - /** - * Refresh cart collection cache - * - * @param Quote $cart - */ - private function refreshCartCache(Quote $cart) : void - { - $items = []; - $collection = $cart->getItemsCollection(false); - foreach ($collection as $item) { - $items[] = $item; - } - $cart->setItems($items); - } } From d420337043c8202c720ee62bd02c9d1aca1ffd17 Mon Sep 17 00:00:00 2001 From: Alex Kolesnyk Date: Thu, 5 Aug 2021 14:04:05 -0500 Subject: [PATCH 13/13] MQE-2834: Chrome crashes in MFTF tests on 2.4-develop CE with PB --- .../Test/Mftf/Test/AwsS3StorefrontPrintOrderGuestTest.xml | 3 +++ .../CheckCheckoutSuccessPageAsRegisterCustomerTest.xml | 3 +++ .../Sales/Test/Mftf/Test/StorefrontPrintOrderGuestTest.xml | 3 +++ .../Test/Mftf/Test/StorefrontRedirectToOrderHistoryTest.xml | 3 +++ 4 files changed, 12 insertions(+) diff --git a/app/code/Magento/AwsS3/Test/Mftf/Test/AwsS3StorefrontPrintOrderGuestTest.xml b/app/code/Magento/AwsS3/Test/Mftf/Test/AwsS3StorefrontPrintOrderGuestTest.xml index 0ee7185fc6c71..00c62eb314ec8 100644 --- a/app/code/Magento/AwsS3/Test/Mftf/Test/AwsS3StorefrontPrintOrderGuestTest.xml +++ b/app/code/Magento/AwsS3/Test/Mftf/Test/AwsS3StorefrontPrintOrderGuestTest.xml @@ -15,6 +15,9 @@ + + + diff --git a/app/code/Magento/Checkout/Test/Mftf/Test/CheckCheckoutSuccessPageTest/CheckCheckoutSuccessPageAsRegisterCustomerTest.xml b/app/code/Magento/Checkout/Test/Mftf/Test/CheckCheckoutSuccessPageTest/CheckCheckoutSuccessPageAsRegisterCustomerTest.xml index ff0365a2f686c..f7b0fe144327a 100644 --- a/app/code/Magento/Checkout/Test/Mftf/Test/CheckCheckoutSuccessPageTest/CheckCheckoutSuccessPageAsRegisterCustomerTest.xml +++ b/app/code/Magento/Checkout/Test/Mftf/Test/CheckCheckoutSuccessPageTest/CheckCheckoutSuccessPageAsRegisterCustomerTest.xml @@ -15,6 +15,9 @@ + + + diff --git a/app/code/Magento/Sales/Test/Mftf/Test/StorefrontPrintOrderGuestTest.xml b/app/code/Magento/Sales/Test/Mftf/Test/StorefrontPrintOrderGuestTest.xml index 074a976de9425..f09e63b41f1fc 100644 --- a/app/code/Magento/Sales/Test/Mftf/Test/StorefrontPrintOrderGuestTest.xml +++ b/app/code/Magento/Sales/Test/Mftf/Test/StorefrontPrintOrderGuestTest.xml @@ -16,6 +16,9 @@ + + + diff --git a/app/code/Magento/Sales/Test/Mftf/Test/StorefrontRedirectToOrderHistoryTest.xml b/app/code/Magento/Sales/Test/Mftf/Test/StorefrontRedirectToOrderHistoryTest.xml index ccc675f20de01..79395a0e7bb39 100644 --- a/app/code/Magento/Sales/Test/Mftf/Test/StorefrontRedirectToOrderHistoryTest.xml +++ b/app/code/Magento/Sales/Test/Mftf/Test/StorefrontRedirectToOrderHistoryTest.xml @@ -16,6 +16,9 @@ + + +