From 77fee571409509a8f4e6c825a03de4eaf3ce3388 Mon Sep 17 00:00:00 2001 From: AleksLi Date: Tue, 25 Feb 2020 22:12:29 +0100 Subject: [PATCH 1/8] MC-26683: Removed get errors of cart allowing to add product to cart --- .../QuoteGraphQl/Model/Cart/AddProductsToCart.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php index 91c77a1a3ecc5..94fc525209997 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php @@ -54,16 +54,6 @@ public function execute(Quote $cart, array $cartItems): void $this->addProductToCart->execute($cart, $cartItemData); } - if ($cart->getData('has_error')) { - $e = new GraphQlInputException(__('Shopping cart errors')); - $errors = $cart->getErrors(); - foreach ($errors as $error) { - /** @var MessageInterface $error */ - $e->addError(new GraphQlInputException(__($error->getText()))); - } - throw $e; - } - $this->cartRepository->save($cart); } } From 0a79989737fd30ac4f39e0a24d75b5328d635f2c Mon Sep 17 00:00:00 2001 From: AleksLi Date: Wed, 26 Feb 2020 21:37:41 +0100 Subject: [PATCH 2/8] MC-26683: Updated description of the class --- app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php index 94fc525209997..cfe78389fffe4 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php @@ -8,12 +8,11 @@ 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; /** - * Add products to cart + * Adding products to cart using GraphQL */ class AddProductsToCart { From af251eb2311a7d477f5c6516195b7032704c755a Mon Sep 17 00:00:00 2001 From: AleksLi Date: Fri, 6 Mar 2020 21:10:28 +0100 Subject: [PATCH 3/8] MC-26683: Some sort of example how I see the solution of this. --- .../Magento/GraphQl/Controller/GraphQl.php | 2 + .../Model/Cart/AddProductsToCart.php | 18 +++- .../Exception/GraphQlCartInputException.php | 97 +++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 lib/internal/Magento/Framework/GraphQl/Exception/GraphQlCartInputException.php diff --git a/app/code/Magento/GraphQl/Controller/GraphQl.php b/app/code/Magento/GraphQl/Controller/GraphQl.php index 2d72fde91b031..a5b8a14ae0793 100644 --- a/app/code/Magento/GraphQl/Controller/GraphQl.php +++ b/app/code/Magento/GraphQl/Controller/GraphQl.php @@ -160,6 +160,8 @@ public function dispatch(RequestInterface $request) : ResponseInterface } catch (\Exception $error) { $result['errors'] = isset($result) && isset($result['errors']) ? $result['errors'] : []; $result['errors'][] = $this->graphQlError->create($error); + // here we should have data from GraphQlCartInputException + $result['data'] = $error->getData(); $statusCode = ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS; } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php index cfe78389fffe4..b9722d276975a 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php @@ -8,11 +8,12 @@ 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; /** - * Adding products to cart using GraphQL + * Add products to cart */ class AddProductsToCart { @@ -43,16 +44,29 @@ public function __construct( * * @param Quote $cart * @param array $cartItems + * @return \Magento\Framework\GraphQl\Exception\GraphQlCartInputException * @throws GraphQlInputException * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException */ - public function execute(Quote $cart, array $cartItems): void + public function execute(Quote $cart, array $cartItems): \Magento\Framework\GraphQl\Exception\GraphQlCartInputException { foreach ($cartItems as $cartItemData) { $this->addProductToCart->execute($cart, $cartItemData); } + if ($cart->getData('has_error')) { + $e = new \Magento\Framework\GraphQl\Exception\GraphQlCartInputException(__('Shopping cart errors')); + $errors = $cart->getErrors(); + foreach ($errors as $error) { + /** @var MessageInterface $error */ + $e->addError(new GraphQlInputException(__($error->getText()))); + } + $e->addData($cartItems); + + throw $e; + } + $this->cartRepository->save($cart); } } diff --git a/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlCartInputException.php b/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlCartInputException.php new file mode 100644 index 0000000000000..2dbbc1c20476e --- /dev/null +++ b/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlCartInputException.php @@ -0,0 +1,97 @@ +isSafe = $isSafe; + parent::__construct($phrase, $cause, $code); + } + + /** + * @inheritdoc + */ + public function isClientSafe() : bool + { + return $this->isSafe; + } + + /** + * @inheritdoc + */ + public function getCategory() : string + { + return self::EXCEPTION_CATEGORY; + } + + /** + * Add child error if used as aggregate exception + * + * @param LocalizedException $exception + * @return $this + */ + public function addError(LocalizedException $exception): self + { + $this->errors[] = $exception; + return $this; + } + + /** + * Get child errors if used as aggregate exception + * + * @return LocalizedException[] + */ + public function getErrors(): array + { + return $this->errors; + } + + /** + * @param array $data + * @return GraphQlInputException + */ + public function addData(array $data): self + { + $this->data = $data; + return $this; + } +} From c47a6a26aae710f2fe64e92d25e03e456c90ae79 Mon Sep 17 00:00:00 2001 From: AleksLi Date: Mon, 9 Mar 2020 22:17:08 +0100 Subject: [PATCH 4/8] MC-26683: Added errors to the return model --- .../Magento/GraphQl/Controller/GraphQl.php | 2 - .../Model/Cart/AddProductsToCart.php | 17 +--- .../QuoteGraphQl/Model/Resolver/CartItems.php | 7 ++ .../Exception/GraphQlCartInputException.php | 97 ------------------- 4 files changed, 9 insertions(+), 114 deletions(-) delete mode 100644 lib/internal/Magento/Framework/GraphQl/Exception/GraphQlCartInputException.php diff --git a/app/code/Magento/GraphQl/Controller/GraphQl.php b/app/code/Magento/GraphQl/Controller/GraphQl.php index a5b8a14ae0793..2d72fde91b031 100644 --- a/app/code/Magento/GraphQl/Controller/GraphQl.php +++ b/app/code/Magento/GraphQl/Controller/GraphQl.php @@ -160,8 +160,6 @@ public function dispatch(RequestInterface $request) : ResponseInterface } catch (\Exception $error) { $result['errors'] = isset($result) && isset($result['errors']) ? $result['errors'] : []; $result['errors'][] = $this->graphQlError->create($error); - // here we should have data from GraphQlCartInputException - $result['data'] = $error->getData(); $statusCode = ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS; } diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php index b9722d276975a..0360d9ccf5476 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php @@ -13,7 +13,7 @@ use Magento\Quote\Model\Quote; /** - * Add products to cart + * Adding products to cart using GraphQL */ class AddProductsToCart { @@ -44,29 +44,16 @@ public function __construct( * * @param Quote $cart * @param array $cartItems - * @return \Magento\Framework\GraphQl\Exception\GraphQlCartInputException * @throws GraphQlInputException * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException */ - public function execute(Quote $cart, array $cartItems): \Magento\Framework\GraphQl\Exception\GraphQlCartInputException + public function execute(Quote $cart, array $cartItems): void { foreach ($cartItems as $cartItemData) { $this->addProductToCart->execute($cart, $cartItemData); } - if ($cart->getData('has_error')) { - $e = new \Magento\Framework\GraphQl\Exception\GraphQlCartInputException(__('Shopping cart errors')); - $errors = $cart->getErrors(); - foreach ($errors as $error) { - /** @var MessageInterface $error */ - $e->addError(new GraphQlInputException(__($error->getText()))); - } - $e->addData($cartItems); - - throw $e; - } - $this->cartRepository->save($cart); } } diff --git a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItems.php b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItems.php index 2674b3728619a..8017a91b5cfd2 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItems.php +++ b/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItems.php @@ -9,6 +9,7 @@ use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Quote\Model\Quote\Item as QuoteItem; @@ -29,6 +30,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value $cart = $value['model']; $itemsData = []; + if ($cart->getData('has_error')) { + $errors = $cart->getErrors(); + foreach ($errors as $error) { + $itemsData[] = new GraphQlInputException(__($error->getText())); + } + } foreach ($cart->getAllVisibleItems() as $cartItem) { /** * @var QuoteItem $cartItem diff --git a/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlCartInputException.php b/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlCartInputException.php deleted file mode 100644 index 2dbbc1c20476e..0000000000000 --- a/lib/internal/Magento/Framework/GraphQl/Exception/GraphQlCartInputException.php +++ /dev/null @@ -1,97 +0,0 @@ -isSafe = $isSafe; - parent::__construct($phrase, $cause, $code); - } - - /** - * @inheritdoc - */ - public function isClientSafe() : bool - { - return $this->isSafe; - } - - /** - * @inheritdoc - */ - public function getCategory() : string - { - return self::EXCEPTION_CATEGORY; - } - - /** - * Add child error if used as aggregate exception - * - * @param LocalizedException $exception - * @return $this - */ - public function addError(LocalizedException $exception): self - { - $this->errors[] = $exception; - return $this; - } - - /** - * Get child errors if used as aggregate exception - * - * @return LocalizedException[] - */ - public function getErrors(): array - { - return $this->errors; - } - - /** - * @param array $data - * @return GraphQlInputException - */ - public function addData(array $data): self - { - $this->data = $data; - return $this; - } -} From 39ade1f9791606927a37386d08a96105e4d18613 Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Thu, 12 Mar 2020 09:48:15 +0200 Subject: [PATCH 5/8] added webapi test --- .../Guest/AddSimpleProductToCartTest.php | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php index 01ae565f00bf6..3bdce6ea98b30 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php @@ -10,6 +10,7 @@ use Exception; use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; use Magento\TestFramework\Helper\Bootstrap; +use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException; use Magento\TestFramework\TestCase\GraphQlAbstract; /** @@ -79,6 +80,30 @@ public function testAddSimpleProductToCart() self::assertEquals('USD', $rowTotalIncludingTax['currency']); } + /** + * Add out of stock product to cart + * + * @@magentoApiDataFixture Magento/Catalog/_files/multiple_products.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @return void + */ + public function testAddProductToCartWithError(): void + { + $disabledProductSku = 'simple3'; + $quantity = 2; + + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId, $disabledProductSku, $quantity); + + $this->expectException(ResponseContainsErrorsException::class); + $this->expectExceptionMessage( + 'Could not add the product with SKU simple3 to the shopping cart: ' . + 'Product that you are trying to add is not available.' + ); + + $this->graphQlMutation($query); + } + /** * @expectedException Exception * @expectedExceptionMessage Required parameter "cart_id" is missing @@ -191,7 +216,7 @@ public function testAddSimpleProductToCustomerCart() private function getQuery(string $maskedQuoteId, string $sku, float $quantity): string { return << Date: Thu, 12 Mar 2020 18:41:35 +0200 Subject: [PATCH 6/8] changes requested; add out stock product to cart test --- .../Guest/AddSimpleProductToCartTest.php | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php index 3bdce6ea98b30..e5953e7b7ad72 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php @@ -80,24 +80,48 @@ public function testAddSimpleProductToCart() self::assertEquals('USD', $rowTotalIncludingTax['currency']); } + /** + * Add disabled product to cart + * + * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @return void + */ + public function testAddDisabledProductToCart(): void + { + $sku = 'simple3'; + $quantity = 2; + + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); + $query = $this->getQuery($maskedQuoteId, $sku, $quantity); + + $this->expectException(ResponseContainsErrorsException::class); + $this->expectExceptionMessage( + 'Could not add the product with SKU ' . $sku . ' to the shopping cart: ' . + 'Product that you are trying to add is not available.' + ); + + $this->graphQlMutation($query); + } + /** * Add out of stock product to cart * - * @@magentoApiDataFixture Magento/Catalog/_files/multiple_products.php + * @magentoApiDataFixture Magento/Catalog/_files/product_virtual_out_of_stock.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php * @return void */ - public function testAddProductToCartWithError(): void + public function testAddOutOfStockProductToCart(): void { - $disabledProductSku = 'simple3'; + $sku = 'virtual-product-out'; $quantity = 2; $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); - $query = $this->getQuery($maskedQuoteId, $disabledProductSku, $quantity); + $query = $this->getQuery($maskedQuoteId, $sku, $quantity); $this->expectException(ResponseContainsErrorsException::class); $this->expectExceptionMessage( - 'Could not add the product with SKU simple3 to the shopping cart: ' . + 'Could not add the product with SKU ' . $sku . ' to the shopping cart: ' . 'Product that you are trying to add is not available.' ); From 158f788ec5a49557104fe1722c7e9551721b5932 Mon Sep 17 00:00:00 2001 From: AleksLi Date: Fri, 13 Mar 2020 08:22:22 +0100 Subject: [PATCH 7/8] MC-26683: Added test case for 'Some of the products are out of stock.' case --- .../Quote/Guest/AddSimpleProductToCartTest.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php index e5953e7b7ad72..986de59ff7bd2 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php @@ -8,6 +8,7 @@ namespace Magento\GraphQl\Quote\Guest; use Exception; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException; @@ -107,22 +108,26 @@ public function testAddDisabledProductToCart(): void /** * Add out of stock product to cart * - * @magentoApiDataFixture Magento/Catalog/_files/product_virtual_out_of_stock.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php + * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_out_of_stock.php * @return void + * @throws NoSuchEntityException */ public function testAddOutOfStockProductToCart(): void { - $sku = 'virtual-product-out'; - $quantity = 2; + $sku = 'simple1'; + $quantity = 1; $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = $this->getQuery($maskedQuoteId, $sku, $quantity); $this->expectException(ResponseContainsErrorsException::class); $this->expectExceptionMessage( - 'Could not add the product with SKU ' . $sku . ' to the shopping cart: ' . - 'Product that you are trying to add is not available.' + 'Some of the products are out of stock.' ); $this->graphQlMutation($query); From fff5ce9c09b3d0e9650d7754dff384aad74af7f1 Mon Sep 17 00:00:00 2001 From: AleksLi Date: Fri, 13 Mar 2020 20:35:55 +0100 Subject: [PATCH 8/8] MC-26683: Removed unused set_guest_email.php fixture for test --- .../Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php index 986de59ff7bd2..3ee27acfa2418 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php @@ -111,7 +111,6 @@ public function testAddDisabledProductToCart(): void * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php - * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/set_guest_email.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/set_simple_product_out_of_stock.php * @return void