Skip to content

Commit 4db921f

Browse files
committed
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"
1 parent 373adf2 commit 4db921f

File tree

3 files changed

+143
-55
lines changed

3 files changed

+143
-55
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,20 @@ public function __construct(
4646
* @param array $cartItems
4747
* @throws GraphQlInputException
4848
* @throws \Magento\Framework\Exception\LocalizedException
49+
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
4950
*/
5051
public function execute(Quote $cart, array $cartItems): void
5152
{
5253
foreach ($cartItems as $cartItemData) {
5354
$this->addProductToCart->execute($cart, $cartItemData);
5455
}
5556

57+
if ($cart->getData('has_error')) {
58+
throw new GraphQlInputException(
59+
__('Shopping cart error: %message', ['message' => $this->getCartErrors($cart)])
60+
);
61+
}
62+
5663
$this->cartRepository->save($cart);
5764
}
5865

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\CatalogInventory;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\TestCase\GraphQlAbstract;
12+
use Magento\Quote\Model\QuoteFactory;
13+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
14+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
15+
16+
class AddProductToCartTest extends GraphQlAbstract
17+
{
18+
/**
19+
* @var QuoteResource
20+
*/
21+
private $quoteResource;
22+
23+
/**
24+
* @var QuoteFactory
25+
*/
26+
private $quoteFactory;
27+
28+
/**
29+
* @var QuoteIdToMaskedQuoteIdInterface
30+
*/
31+
private $quoteIdToMaskedId;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp()
37+
{
38+
$objectManager = Bootstrap::getObjectManager();
39+
$this->quoteResource = $objectManager->get(QuoteResource::class);
40+
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
41+
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
42+
}
43+
44+
/**
45+
* @magentoApiDataFixture Magento/Catalog/_files/products.php
46+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
47+
* @expectedException \Exception
48+
* @expectedExceptionMessage The requested qty is not available
49+
*/
50+
public function testAddProductIfQuantityIsNotAvailable()
51+
{
52+
$sku = 'simple';
53+
$qty = 200;
54+
55+
$maskedQuoteId = $this->getMaskedQuoteId();
56+
$query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty);
57+
$this->graphQlQuery($query);
58+
}
59+
60+
/**
61+
* @magentoApiDataFixture Magento/Catalog/_files/products.php
62+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
63+
* @magentoConfigFixture default cataloginventory/item_options/max_sale_qty 5
64+
* @expectedException \Exception
65+
* @expectedExceptionMessage The most you may purchase is 5.
66+
*/
67+
public function testAddMoreProductsThatAllowed()
68+
{
69+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/167');
70+
71+
$sku = 'custom-design-simple-product';
72+
$qty = 7;
73+
74+
$maskedQuoteId = $this->getMaskedQuoteId();
75+
$query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty);
76+
$this->graphQlQuery($query);
77+
}
78+
79+
/**
80+
* @return string
81+
*/
82+
public function getMaskedQuoteId() : string
83+
{
84+
$quote = $this->quoteFactory->create();
85+
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
86+
87+
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
88+
}
89+
90+
/**
91+
* @param string $maskedQuoteId
92+
* @param string $sku
93+
* @param int $qty
94+
* @return string
95+
*/
96+
public function getAddSimpleProductQuery(string $maskedQuoteId, string $sku, int $qty) : string
97+
{
98+
return <<<QUERY
99+
mutation {
100+
addSimpleProductsToCart(
101+
input: {
102+
cart_id: "{$maskedQuoteId}",
103+
cartItems: [
104+
{
105+
data: {
106+
qty: $qty
107+
sku: "$sku"
108+
}
109+
}
110+
]
111+
}
112+
) {
113+
cart {
114+
cart_id
115+
items {
116+
qty
117+
}
118+
}
119+
}
120+
}
121+
QUERY;
122+
}
123+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductToCartTest.php

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
use Magento\TestFramework\Helper\Bootstrap;
1111
use Magento\TestFramework\TestCase\GraphQlAbstract;
12-
use Magento\Quote\Model\Quote;
12+
use Magento\Quote\Model\QuoteFactory;
1313
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
1414
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
15-
use Magento\Config\Model\ResourceModel\Config;
1615

1716
class AddSimpleProductToCartTest extends GraphQlAbstract
1817
{
@@ -22,30 +21,24 @@ class AddSimpleProductToCartTest extends GraphQlAbstract
2221
private $quoteResource;
2322

2423
/**
25-
* @var Quote
24+
* @var QuoteFactory
2625
*/
27-
private $quote;
26+
private $quoteFactory;
2827

2928
/**
3029
* @var QuoteIdToMaskedQuoteIdInterface
3130
*/
3231
private $quoteIdToMaskedId;
3332

34-
/**
35-
* @var Config
36-
*/
37-
private $config;
38-
3933
/**
4034
* @inheritdoc
4135
*/
4236
protected function setUp()
4337
{
4438
$objectManager = Bootstrap::getObjectManager();
4539
$this->quoteResource = $objectManager->get(QuoteResource::class);
46-
$this->quote = $objectManager->create(Quote::class);
40+
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
4741
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
48-
$this->config = $objectManager->get(Config::class);
4942
}
5043

5144
/**
@@ -57,68 +50,33 @@ public function testAddSimpleProductsToCart()
5750
$sku = 'simple';
5851
$qty = 2;
5952
$maskedQuoteId = $this->getMaskedQuoteId();
60-
$query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty);
53+
54+
$query = $this->geAddSimpleProducttQuery($maskedQuoteId, $sku, $qty);
6155
$response = $this->graphQlQuery($query);
6256
self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);
63-
$cartQty = $response['addSimpleProductsToCart']['cart']['items'][0]['qty'];
64-
65-
$this->assertEquals($qty, $cartQty);
66-
}
67-
68-
/**
69-
* @magentoApiDataFixture Magento/Catalog/_files/products.php
70-
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
71-
* @expectedException \Exception
72-
* @expectedExceptionMessage The requested qty is not available
73-
*/
74-
public function testAddProductIfQuantityIsNotAvailable()
75-
{
76-
$sku = 'simple';
77-
$qty = 200;
78-
79-
$maskedQuoteId = $this->getMaskedQuoteId();
80-
$query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty);
81-
$this->graphQlQuery($query);
82-
}
8357

84-
/**
85-
* @magentoApiDataFixture Magento/Catalog/_files/products.php
86-
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
87-
* @expectedExceptionMessage The most you may purchase is 5.
88-
*/
89-
public function testAddMoreProductsThatAllowed()
90-
{
91-
$sku = 'custom-design-simple-product';
92-
$qty = 7;
93-
$maxQty = 5;
94-
95-
$this->config->saveConfig('cataloginventory/item_options/max_sale_qty', $maxQty, 'default', 0);
96-
$maskedQuoteId = $this->getMaskedQuoteId();
97-
$query = $this->getQueryAddSimpleProduct($maskedQuoteId, $sku, $qty);
98-
$this->graphQlQuery($query);
58+
$cartQty = $response['addSimpleProductsToCart']['cart']['items'][0]['qty'];
59+
self::assertEquals($qty, $cartQty);
9960
}
10061

10162
/**
10263
* @return string
10364
*/
10465
public function getMaskedQuoteId() : string
10566
{
106-
$this->quoteResource->load(
107-
$this->quote,
108-
'test_order_1',
109-
'reserved_order_id'
110-
);
111-
return $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
67+
$quote = $this->quoteFactory->create();
68+
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
69+
70+
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
11271
}
11372

11473
/**
11574
* @param string $maskedQuoteId
11675
* @param string $sku
11776
* @param int $qty
118-
*
11977
* @return string
12078
*/
121-
public function getQueryAddSimpleProduct(string $maskedQuoteId, string $sku, int $qty) : string
79+
public function geAddSimpleProducttQuery(string $maskedQuoteId, string $sku, int $qty) : string
12280
{
12381
return <<<QUERY
12482
mutation {

0 commit comments

Comments
 (0)