Skip to content

Commit b7849a5

Browse files
ENGCOM-4810: magento/graphql-ce#530: [Cart Operations] Update Cart Items validation messages #531
- Merge Pull Request magento/graphql-ce#531 from magento/graphql-ce:530-update-cart-items-validation-messages - Merged commits: 1. 1267436 2. e627992 3. 357caac 4. a081ebb 5. 496d36a 6. 3469559 7. afa6c57
2 parents 932acf9 + afa6c57 commit b7849a5

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1717
use Magento\Quote\Api\CartItemRepositoryInterface;
1818
use Magento\Quote\Model\Quote;
19+
use Magento\Quote\Model\Quote\Item;
1920
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
2021

2122
/**
@@ -111,8 +112,35 @@ private function processCartItems(Quote $cart, array $items): void
111112
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
112113
} else {
113114
$cartItem->setQty($qty);
115+
$this->validateCartItem($cartItem);
114116
$this->cartItemRepository->save($cartItem);
115117
}
116118
}
117119
}
120+
121+
/**
122+
* Validate cart item
123+
*
124+
* @param Item $cartItem
125+
* @return void
126+
* @throws GraphQlInputException
127+
*/
128+
private function validateCartItem(Item $cartItem): void
129+
{
130+
if ($cartItem->getHasError()) {
131+
$errors = [];
132+
foreach ($cartItem->getMessage(false) as $message) {
133+
$errors[] = $message;
134+
}
135+
136+
if (!empty($errors)) {
137+
throw new GraphQlInputException(
138+
__(
139+
'Could not update the product with SKU %sku: %message',
140+
['sku' => $cartItem->getSku(), 'message' => __(implode("\n", $errors))]
141+
)
142+
);
143+
}
144+
}
145+
}
118146
}

dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/AddProductToCartTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,31 @@ public function testAddSimpleProductToCartWithNegativeQty()
8181
$this->graphQlMutation($query);
8282
}
8383

84+
/**
85+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
86+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
87+
*/
88+
public function testAddProductIfQuantityIsDecimal()
89+
{
90+
$sku = 'simple_product';
91+
$qty = 0.2;
92+
93+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
94+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
95+
96+
$this->expectExceptionMessage(
97+
"Could not add the product with SKU {$sku} to the shopping cart: The fewest you may purchase is 1."
98+
);
99+
$this->graphQlMutation($query);
100+
}
101+
84102
/**
85103
* @param string $maskedQuoteId
86104
* @param string $sku
87-
* @param int $qty
105+
* @param float $qty
88106
* @return string
89107
*/
90-
private function getQuery(string $maskedQuoteId, string $sku, int $qty) : string
108+
private function getQuery(string $maskedQuoteId, string $sku, float $qty) : string
91109
{
92110
return <<<QUERY
93111
mutation {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\GraphQl\Quote\GetQuoteItemIdByReservedQuoteIdAndSku;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
15+
/**
16+
* Test for updating/removing shopping cart items
17+
*/
18+
class UpdateCartItemsTest extends GraphQlAbstract
19+
{
20+
/**
21+
* @var GetMaskedQuoteIdByReservedOrderId
22+
*/
23+
private $getMaskedQuoteIdByReservedOrderId;
24+
25+
/**
26+
* @var GetQuoteItemIdByReservedQuoteIdAndSku
27+
*/
28+
private $getQuoteItemIdByReservedQuoteIdAndSku;
29+
30+
protected function setUp()
31+
{
32+
$objectManager = Bootstrap::getObjectManager();
33+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
34+
$this->getQuoteItemIdByReservedQuoteIdAndSku = $objectManager->get(
35+
GetQuoteItemIdByReservedQuoteIdAndSku::class
36+
);
37+
}
38+
39+
/**
40+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
41+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
42+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
43+
*/
44+
public function testUpdateCartItemDecimalQty()
45+
{
46+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
47+
$itemId = $this->getQuoteItemIdByReservedQuoteIdAndSku->execute('test_quote', 'simple_product');
48+
49+
$qty = 0.5;
50+
$this->expectExceptionMessage(
51+
"Could not update the product with SKU simple_product: The fewest you may purchase is 1."
52+
);
53+
$query = $this->getQuery($maskedQuoteId, $itemId, $qty);
54+
$this->graphQlMutation($query);
55+
}
56+
57+
/**
58+
* @param string $maskedQuoteId
59+
* @param int $itemId
60+
* @param float $qty
61+
* @return string
62+
*/
63+
private function getQuery(string $maskedQuoteId, int $itemId, float $qty): string
64+
{
65+
return <<<QUERY
66+
mutation {
67+
updateCartItems(input: {
68+
cart_id: "{$maskedQuoteId}"
69+
cart_items:[
70+
{
71+
cart_item_id: {$itemId}
72+
quantity: {$qty}
73+
}
74+
]
75+
}) {
76+
cart {
77+
items {
78+
id
79+
qty
80+
}
81+
}
82+
}
83+
}
84+
QUERY;
85+
}
86+
}

0 commit comments

Comments
 (0)