From 4db921f0dbc31f0a1d1bd6dc8937593c9951ecb6 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Wed, 27 Feb 2019 11:40:54 -0600 Subject: [PATCH] GraphQL-293: When maxSaleQty is set and qty is more than maxSaleQty the "The most you may purchase is %1." message should be sent instead of "Internal server error" --- .../Model/Cart/AddProductsToCart.php | 7 + .../CatalogInventory/AddProductToCartTest.php | 123 ++++++++++++++++++ .../Quote/AddSimpleProductToCartTest.php | 68 ++-------- 3 files changed, 143 insertions(+), 55 deletions(-) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/AddProductToCartTest.php diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php index 6deb387476d5f..005cf3a10ca80 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php @@ -46,6 +46,7 @@ public function __construct( * @param array $cartItems * @throws GraphQlInputException * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException */ public function execute(Quote $cart, array $cartItems): void { @@ -53,6 +54,12 @@ public function execute(Quote $cart, array $cartItems): void $this->addProductToCart->execute($cart, $cartItemData); } + if ($cart->getData('has_error')) { + throw new GraphQlInputException( + __('Shopping cart error: %message', ['message' => $this->getCartErrors($cart)]) + ); + } + $this->cartRepository->save($cart); } diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/AddProductToCartTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/AddProductToCartTest.php new file mode 100644 index 0000000000000..947d48f8e15c8 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/AddProductToCartTest.php @@ -0,0 +1,123 @@ +quoteResource = $objectManager->get(QuoteResource::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); + $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); + } + + /** + * @magentoApiDataFixture Magento/Catalog/_files/products.php + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php + * @expectedException \Exception + * @expectedExceptionMessage The requested qty is not available + */ + public function testAddProductIfQuantityIsNotAvailable() + { + $sku = 'simple'; + $qty = 200; + + $maskedQuoteId = $this->getMaskedQuoteId(); + $query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty); + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/Catalog/_files/products.php + * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php + * @magentoConfigFixture default cataloginventory/item_options/max_sale_qty 5 + * @expectedException \Exception + * @expectedExceptionMessage The most you may purchase is 5. + */ + public function testAddMoreProductsThatAllowed() + { + $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/167'); + + $sku = 'custom-design-simple-product'; + $qty = 7; + + $maskedQuoteId = $this->getMaskedQuoteId(); + $query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty); + $this->graphQlQuery($query); + } + + /** + * @return string + */ + public function getMaskedQuoteId() : string + { + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); + } + + /** + * @param string $maskedQuoteId + * @param string $sku + * @param int $qty + * @return string + */ + public function getAddSimpleProductQuery(string $maskedQuoteId, string $sku, int $qty) : string + { + return <<quoteResource = $objectManager->get(QuoteResource::class); - $this->quote = $objectManager->create(Quote::class); + $this->quoteFactory = $objectManager->get(QuoteFactory::class); $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); - $this->config = $objectManager->get(Config::class); } /** @@ -57,45 +50,13 @@ public function testAddSimpleProductsToCart() $sku = 'simple'; $qty = 2; $maskedQuoteId = $this->getMaskedQuoteId(); - $query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty); + + $query = $this->geAddSimpleProducttQuery($maskedQuoteId, $sku, $qty); $response = $this->graphQlQuery($query); self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']); - $cartQty = $response['addSimpleProductsToCart']['cart']['items'][0]['qty']; - - $this->assertEquals($qty, $cartQty); - } - - /** - * @magentoApiDataFixture Magento/Catalog/_files/products.php - * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php - * @expectedException \Exception - * @expectedExceptionMessage The requested qty is not available - */ - public function testAddProductIfQuantityIsNotAvailable() - { - $sku = 'simple'; - $qty = 200; - - $maskedQuoteId = $this->getMaskedQuoteId(); - $query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty); - $this->graphQlQuery($query); - } - /** - * @magentoApiDataFixture Magento/Catalog/_files/products.php - * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php - * @expectedExceptionMessage The most you may purchase is 5. - */ - public function testAddMoreProductsThatAllowed() - { - $sku = 'custom-design-simple-product'; - $qty = 7; - $maxQty = 5; - - $this->config->saveConfig('cataloginventory/item_options/max_sale_qty', $maxQty, 'default', 0); - $maskedQuoteId = $this->getMaskedQuoteId(); - $query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty); - $this->graphQlQuery($query); + $cartQty = $response['addSimpleProductsToCart']['cart']['items'][0]['qty']; + self::assertEquals($qty, $cartQty); } /** @@ -103,22 +64,19 @@ public function testAddMoreProductsThatAllowed() */ public function getMaskedQuoteId() : string { - $this->quoteResource->load( - $this->quote, - 'test_order_1', - 'reserved_order_id' - ); - return $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); + $quote = $this->quoteFactory->create(); + $this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id'); + + return $this->quoteIdToMaskedId->execute((int)$quote->getId()); } /** * @param string $maskedQuoteId * @param string $sku * @param int $qty - * * @return string */ - public function getQueryAddSimpleProduct(string $maskedQuoteId, string $sku, int $qty) : string + public function geAddSimpleProducttQuery(string $maskedQuoteId, string $sku, int $qty) : string { return <<