Skip to content

Commit 496d36a

Browse files
committed
GraphQL-530: [Cart Operations] Update Cart Items validation messages
1 parent a081ebb commit 496d36a

File tree

5 files changed

+93
-51
lines changed

5 files changed

+93
-51
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ private function processCartItems(Quote $cart, array $items): void
115115
if ($cartItem->getHasError()) {
116116
$errors = [];
117117
foreach ($cartItem->getMessage(false) as $message) {
118-
if (!in_array($message, $errors)) {
119-
$errors[] = $message;
120-
}
118+
$errors[] = $message;
121119
}
122120

123121
if (!empty($errors)) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ public function testAddSimpleProductToCartWithNegativeQty()
8282
}
8383

8484
/**
85-
* @magentoApiDataFixture Magento/Catalog/_files/products.php
86-
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
85+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
86+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
8787
*/
8888
public function testAddProductIfQuantityIsDecimal()
8989
{
90-
$sku = 'simple';
90+
$sku = 'simple_product';
9191
$qty = 0.2;
9292

93-
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
93+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
9494
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
9595

9696
$this->expectExceptionMessage(
@@ -102,10 +102,10 @@ public function testAddProductIfQuantityIsDecimal()
102102
/**
103103
* @param string $maskedQuoteId
104104
* @param string $sku
105-
* @param int $qty
105+
* @param float $qty
106106
* @return string
107107
*/
108-
private function getQuery(string $maskedQuoteId, string $sku, int $qty) : string
108+
private function getQuery(string $maskedQuoteId, string $sku, float $qty) : string
109109
{
110110
return <<<QUERY
111111
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+
}

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,6 @@ public function testUpdateCartItemQty()
7979
$this->assertEquals($qty, $item['qty']);
8080
}
8181

82-
/**
83-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
84-
*/
85-
public function testUpdateCartItemDecimalQty()
86-
{
87-
$quote = $this->quoteFactory->create();
88-
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
89-
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
90-
$product = $this->productRepository->get('simple');
91-
92-
$itemId = (int)$quote->getItemByProduct($product)->getId();
93-
$qty = 0.5;
94-
95-
$this->expectExceptionMessage(
96-
"Could not update the product with SKU " . $product->getSku() . ": The fewest you may purchase is 1."
97-
);
98-
99-
$query = $this->getQuery($maskedQuoteId, $itemId, $qty);
100-
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
101-
}
102-
10382
/**
10483
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
10584
*/

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,6 @@ public function testUpdateCartItemQty()
7272
$this->assertEquals($qty, $item['qty']);
7373
}
7474

75-
/**
76-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
77-
*/
78-
public function testUpdateCartItemDecimalQty()
79-
{
80-
$quote = $this->quoteFactory->create();
81-
$this->quoteResource->load($quote, 'test_order_with_simple_product_without_address', 'reserved_order_id');
82-
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());
83-
$product = $this->productRepository->get('simple');
84-
85-
$itemId = (int)$quote->getItemByProduct($product)->getId();
86-
$qty = 0.5;
87-
88-
$this->expectExceptionMessage(
89-
"Could not update the product with SKU " . $product->getSku() . ": The fewest you may purchase is 1."
90-
);
91-
92-
$query = $this->getQuery($maskedQuoteId, $itemId, $qty);
93-
$this->graphQlQuery($query);
94-
}
95-
9675
/**
9776
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
9877
*/

0 commit comments

Comments
 (0)