Skip to content

Commit 81498ff

Browse files
AnujNehraAnujNehra
authored andcommitted
ACP2E-1975: GraphQL cannot add product with customizable option to cart
1 parent 8f8d228 commit 81498ff

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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\Quote;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\TestCase\GraphQlAbstract;
12+
13+
/**
14+
* Test GraphQL can add product with customizable option to cart
15+
*/
16+
class AddProductWithCustomizableOptionToCartTest extends GraphQlAbstract
17+
{
18+
/**
19+
* @var GetCustomOptionsWithUIDForQueryBySku
20+
*/
21+
private $getCustomOptionsWithIDV2ForQueryBySku;
22+
23+
/**
24+
* @var GetMaskedQuoteIdByReservedOrderId
25+
*/
26+
private $getMaskedQuoteIdByReservedOrderId;
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
protected function setUp(): void
32+
{
33+
$objectManager = Bootstrap::getObjectManager();
34+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
35+
$this->getCustomOptionsWithIDV2ForQueryBySku = $objectManager->get(
36+
GetCustomOptionsWithUIDForQueryBySku::class
37+
);
38+
}
39+
40+
/**
41+
* Test adding a simple product to the shopping cart with all supported
42+
* customizable options assigned
43+
*
44+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_options.php
45+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
46+
*/
47+
public function testAddSimpleProductWithOptions()
48+
{
49+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
50+
51+
$sku = 'simple';
52+
$qty = 1;
53+
54+
$itemOptions = $this->getCustomOptionsWithIDV2ForQueryBySku->execute($sku);
55+
56+
/* The type field is only required for assertions, it should not be present in query */
57+
foreach ($itemOptions['entered_options'] as &$enteredOption) {
58+
if (isset($enteredOption['type'])) {
59+
unset($enteredOption['type']);
60+
}
61+
}
62+
63+
$productOptionsQuery = preg_replace(
64+
'/"([^"]+)"\s*:\s*/',
65+
'$1:',
66+
json_encode($itemOptions)
67+
);
68+
69+
$query = $this->getAddToCartMutation($maskedQuoteId, $qty, $sku, trim($productOptionsQuery, '{}'));
70+
$response = $this->graphQlMutation($query);
71+
self::assertArrayHasKey('items', $response['addProductsToCart']['cart']);
72+
self::assertCount($qty, $response['addProductsToCart']['cart']['items']);
73+
self::assertNotEmpty($response['addProductsToCart']['cart']['items'][0]['customizable_options']);
74+
}
75+
76+
/**
77+
* Returns GraphQl query string
78+
*
79+
* @param string $maskedQuoteId
80+
* @param int $qty
81+
* @param string $sku
82+
* @param string $customizableOptions
83+
* @return string
84+
*/
85+
private function getAddToCartMutation(
86+
string $maskedQuoteId,
87+
int $qty,
88+
string $sku,
89+
string $customizableOptions = ''
90+
): string {
91+
return <<<MUTATION
92+
mutation {
93+
addProductsToCart(
94+
cartId: "{$maskedQuoteId}",
95+
cartItems: [
96+
{
97+
sku: "{$sku}"
98+
quantity: {$qty}
99+
{$customizableOptions}
100+
}
101+
]
102+
) {
103+
cart {
104+
items {
105+
product {
106+
name
107+
sku
108+
}
109+
... on SimpleCartItem {
110+
customizable_options {
111+
label
112+
customizable_option_uid
113+
values {
114+
value
115+
customizable_option_value_uid
116+
}
117+
}
118+
}
119+
}
120+
}
121+
user_errors {
122+
code
123+
message
124+
}
125+
}
126+
}
127+
MUTATION;
128+
}
129+
}

0 commit comments

Comments
 (0)