Skip to content

Commit 1720e66

Browse files
committed
Merge branch 'graphql-api-enhancements' of https://github.com/magento-lynx/magento2ce into LYNX-892
2 parents d43895f + 22bb1d6 commit 1720e66

File tree

4 files changed

+176
-5
lines changed

4 files changed

+176
-5
lines changed

app/code/Magento/Quote/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
<item name="The requested qty is not available" xsi:type="string">INSUFFICIENT_STOCK</item>
182182
<item name="Not enough items for sale" xsi:type="string">INSUFFICIENT_STOCK</item>
183183
<item name="Only %s of %s available" xsi:type="string">INSUFFICIENT_STOCK</item>
184+
<item name="You need to choose options for your item." xsi:type="string">REQUIRED_PARAMETER_MISSING</item>
184185
</argument>
185186
</arguments>
186187
</type>

app/code/Magento/WishlistGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type WishlistCartUserInputError @doc(description: "Contains details about errors
8282

8383
enum WishlistCartUserInputErrorType @doc(description: "A list of possible error types.") {
8484
PRODUCT_NOT_FOUND
85+
REQUIRED_PARAMETER_MISSING
8586
NOT_SALABLE
8687
INSUFFICIENT_STOCK
8788
UNDEFINED
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Quote;
9+
10+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
11+
use Magento\ConfigurableProduct\Test\Fixture\Attribute as AttributeFixture;
12+
use Magento\ConfigurableProduct\Test\Fixture\Product as ConfigurableProductFixture;
13+
use Magento\Customer\Test\Fixture\Customer;
14+
use Magento\Framework\Exception\AuthenticationException;
15+
use Magento\Integration\Api\CustomerTokenServiceInterface;
16+
use Magento\Quote\Test\Fixture\CustomerCart;
17+
use Magento\Quote\Test\Fixture\QuoteIdMask;
18+
use Magento\TestFramework\Fixture\DataFixture;
19+
use Magento\TestFramework\Fixture\DataFixtureStorage;
20+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
21+
use Magento\TestFramework\Helper\Bootstrap;
22+
use Magento\TestFramework\TestCase\GraphQlAbstract;
23+
24+
class AddConfigurableProductToCartTest extends GraphQlAbstract
25+
{
26+
/**
27+
* @var DataFixtureStorage
28+
*/
29+
private $fixtures;
30+
31+
/**
32+
* @var CustomerTokenServiceInterface
33+
*/
34+
private $customerTokenService;
35+
36+
protected function setUp(): void
37+
{
38+
$this->fixtures = DataFixtureStorageManager::getStorage();
39+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
40+
}
41+
42+
#[
43+
DataFixture(AttributeFixture::class, [
44+
'frontend_input' => 'select',
45+
'options' => ['40', '42'],
46+
'is_configurable' => true,
47+
'is_global' => true
48+
], as: 'attribute'),
49+
DataFixture(
50+
ProductFixture::class,
51+
[
52+
'price' => 100,
53+
'custom_attributes' => [
54+
['attribute_code' => '$attribute.attribute_code$', 'value' => '40']
55+
]
56+
],
57+
as: 'product1'
58+
),
59+
DataFixture(
60+
ProductFixture::class,
61+
[
62+
'price' => 100,
63+
'custom_attributes' => [
64+
['attribute_code' => '$attribute.attribute_code$', 'value' => '42']
65+
]
66+
],
67+
as: 'product2'
68+
),
69+
DataFixture(
70+
ConfigurableProductFixture::class,
71+
[
72+
'_options' => ['$attribute$'],
73+
'_links' => ['$product1$', '$product2$'],
74+
'custom_attributes' => [
75+
['attribute_code' => '$attribute.attribute_code$', 'value' => '40']
76+
]
77+
],
78+
'configurable_product'
79+
),
80+
DataFixture(Customer::class, as: 'customer'),
81+
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'customerCart'),
82+
DataFixture(QuoteIdMask::class, ['cart_id' => '$customerCart.id$'], 'quoteIdMask'),
83+
]
84+
public function testAddToCartForConfigurableProductWithoutOptions(): void
85+
{
86+
$maskedQuoteId = $this->fixtures->get('quoteIdMask')->getMaskedId();
87+
88+
$this->assertEquals(
89+
[
90+
'addProductsToCart' => [
91+
'cart' => [
92+
'id' => $maskedQuoteId,
93+
'itemsV2' => [
94+
'items' => []
95+
]
96+
],
97+
'user_errors' => [
98+
[
99+
'code' => 'REQUIRED_PARAMETER_MISSING',
100+
'message' => 'You need to choose options for your item.'
101+
]
102+
]
103+
]
104+
],
105+
$this->graphQlMutation(
106+
$this->getAddToCartMutation(
107+
$maskedQuoteId,
108+
$this->fixtures->get('configurable_product')->getSku(),
109+
2
110+
),
111+
[],
112+
"",
113+
$this->getCustomerAuthHeaders($this->fixtures->get('customer')->getEmail())
114+
)
115+
);
116+
}
117+
118+
/**
119+
* Get addToCart mutation for a configurable product without specifying options
120+
*
121+
* @param string $cartId
122+
* @param string $sku
123+
* @param int $quantity
124+
* @return string
125+
*/
126+
private function getAddToCartMutation(string $cartId, string $sku, int $quantity): string
127+
{
128+
return <<<MUTATION
129+
mutation{
130+
addProductsToCart(cartId: "{$cartId}",
131+
cartItems:[
132+
{
133+
sku:"{$sku}"
134+
quantity:{$quantity}
135+
}
136+
]
137+
)
138+
{
139+
cart {
140+
id
141+
itemsV2 {
142+
items {
143+
quantity
144+
product {
145+
sku
146+
}
147+
}
148+
}
149+
}
150+
user_errors{
151+
code
152+
message
153+
}
154+
}
155+
}
156+
MUTATION;
157+
}
158+
159+
/**
160+
* Returns the header with customer token for GQL Mutation
161+
*
162+
* @param string $email
163+
* @return array
164+
* @throws AuthenticationException
165+
*/
166+
private function getCustomerAuthHeaders(string $email): array
167+
{
168+
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, 'password');
169+
return ['Authorization' => 'Bearer ' . $customerToken];
170+
}
171+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddWishlistItemsToCartTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,11 @@ public function testAddIncompleteItemsToCart(): void
6868

6969
$query = $this->getQuery($wishlistId, $itemId);
7070
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
71-
7271
$this->assertArrayHasKey('addWishlistItemsToCart', $response);
7372
$wishlistAfterAddingToCart = $response['addWishlistItemsToCart']['wishlist'];
7473
$userErrors = $response['addWishlistItemsToCart']['add_wishlist_items_to_cart_user_errors'];
7574
$this->assertEquals($userErrors[0]['message'], 'You need to choose options for your item.');
76-
$this->assertEquals($userErrors[0]['code'], 'UNDEFINED');
75+
$this->assertEquals($userErrors[0]['code'], 'REQUIRED_PARAMETER_MISSING');
7776
$this->assertEquals($userErrors[0]['wishlistId'], $wishlistId);
7877
$this->assertEquals($userErrors[0]['wishlistItemId'], $itemId);
7978
$wishlistItems = $wishlistAfterAddingToCart['items_v2']['items'];
@@ -296,7 +295,6 @@ private function getQuery(
296295
* Returns GraphQl mutation string
297296
*
298297
* @param string $wishlistId
299-
* @param string $itemId
300298
* @return string
301299
*/
302300
private function getAddAllItemsToCartQuery(
@@ -385,8 +383,8 @@ private function getCustomerWishlistQuery(): string
385383
* Returns the GraphQl mutation string for products added to wishlist
386384
*
387385
* @param string $wishlistId
388-
* @param string $sku2
389-
* @param int $quantity2
386+
* @param string $sku
387+
* @param int $quantity
390388
* @return string
391389
*/
392390
private function addSecondProductToWishlist(

0 commit comments

Comments
 (0)