99
1010use Exception ;
1111use Magento \Framework \Exception \AuthenticationException ;
12- use Magento \Framework \ObjectManagerInterface ;
1312use Magento \Integration \Api \CustomerTokenServiceInterface ;
1413use Magento \TestFramework \Helper \Bootstrap ;
1514use Magento \TestFramework \TestCase \GraphQlAbstract ;
@@ -53,13 +52,12 @@ public function testAddConfigurableProductWithOptions(): void
5352 $ product = $ this ->getConfigurableProductInfo ();
5453 $ customerId = 1 ;
5554 $ qty = 2 ;
56- $ attributeId = (int ) $ product ['configurable_options ' ][0 ]['attribute_id ' ];
57- $ valueIndex = $ product ['configurable_options ' ][0 ]['values ' ][0 ]['value_index ' ];
5855 $ childSku = $ product ['variants ' ][0 ]['product ' ]['sku ' ];
56+ $ selectedOptions = array_column ($ product ['variants ' ][0 ]['attributes ' ], 'uid ' );
5957 $ parentSku = $ product ['sku ' ];
60- $ selectedConfigurableOptionsQuery = $ this ->generateSuperAttributesUidQuery ( $ attributeId , $ valueIndex );
58+ $ additionalInput = $ this ->getSelectedOptionsQuery ( $ selectedOptions );
6159
62- $ query = $ this ->getQuery ($ parentSku , $ childSku , $ qty , $ selectedConfigurableOptionsQuery );
60+ $ query = $ this ->getQuery ($ parentSku , $ childSku , $ qty , $ additionalInput );
6361
6462 $ response = $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeadersMap ());
6563 $ wishlist = $ this ->wishlistFactory ->create ()->loadByCustomerId ($ customerId , true );
@@ -81,6 +79,78 @@ public function testAddConfigurableProductWithOptions(): void
8179 $ configurableOptions = $ wishlistResponse ['items_v2 ' ]['items ' ][0 ]['configurable_options ' ];
8280 $ this ->assertEquals ('Test Configurable ' , $ configurableOptions [0 ]['option_label ' ]);
8381 $ this ->assertEquals ('Option 1 ' , $ configurableOptions [0 ]['value_label ' ]);
82+ $ this ->assertEquals ($ childSku , $ wishlistResponse ['items_v2 ' ]['items ' ][0 ]['configured_variant ' ]['sku ' ]);
83+ }
84+
85+ /**
86+ * @magentoConfigFixture default_store wishlist/general/active 1
87+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
88+ * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
89+ *
90+ * @throws Exception
91+ */
92+ public function testAddConfigurableProductWithoutOption (): void
93+ {
94+ $ product = $ this ->getConfigurableProductInfo ();
95+ $ query = $ this ->getQuery ($ product ['sku ' ], $ product ['sku ' ], 2 );
96+ $ response = $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeadersMap ());
97+
98+ $ this ->assertArrayHasKey ('addProductsToWishlist ' , $ response );
99+ $ this ->assertArrayHasKey ('wishlist ' , $ response ['addProductsToWishlist ' ]);
100+ $ this ->assertEmpty (
101+ $ response ['addProductsToWishlist ' ]['user_errors ' ],
102+ json_encode ($ response ['addProductsToWishlist ' ]['user_errors ' ])
103+ );
104+ $ wishlistResponse = $ response ['addProductsToWishlist ' ]['wishlist ' ];
105+
106+ $ this ->assertCount (1 , $ wishlistResponse ['items_v2 ' ]['items ' ]);
107+ $ this ->assertEquals ($ product ['sku ' ], $ wishlistResponse ['items_v2 ' ]['items ' ][0 ]['product ' ]['sku ' ]);
108+ $ this ->assertEmpty ($ wishlistResponse ['items_v2 ' ]['items ' ][0 ]['configurable_options ' ]);
109+ $ this ->assertNull ($ wishlistResponse ['items_v2 ' ]['items ' ][0 ]['configured_variant ' ]);
110+ }
111+
112+ /**
113+ * @magentoConfigFixture default_store wishlist/general/active 1
114+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
115+ * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_with_custom_option_dropdown.php
116+ *
117+ * @throws Exception
118+ */
119+ public function testAddConfigurableProductWithCustomOptions (): void
120+ {
121+ $ product = $ this ->getConfigurableProductInfo ();
122+ $ qty = 2 ;
123+ $ childSku = $ product ['variants ' ][0 ]['product ' ]['sku ' ];
124+ $ parentSku = $ product ['sku ' ];
125+ $ selectedOptions = array_column ($ product ['variants ' ][0 ]['attributes ' ], 'uid ' );
126+ $ optionId = $ product ['options ' ][0 ]['uid ' ];
127+ $ optionValue = $ product ['options ' ][0 ]['value ' ][0 ]['option_type_id ' ];
128+ $ customOptions = [$ optionId => $ optionValue ];
129+ $ additionalInput = $ this ->getSelectedOptionsQuery ($ selectedOptions )
130+ . PHP_EOL
131+ . $ this ->getCustomOptionsQuery ($ customOptions );
132+
133+ $ query = $ this ->getQuery ($ parentSku , $ childSku , $ qty , $ additionalInput );
134+ $ response = $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeadersMap ());
135+
136+ $ this ->assertArrayHasKey ('addProductsToWishlist ' , $ response );
137+ $ this ->assertArrayHasKey ('wishlist ' , $ response ['addProductsToWishlist ' ]);
138+ $ this ->assertEmpty ($ response ['addProductsToWishlist ' ]['user_errors ' ]);
139+
140+ $ wishlistResponse = $ response ['addProductsToWishlist ' ]['wishlist ' ];
141+ $ this ->assertCount (1 , $ wishlistResponse ['items_v2 ' ]['items ' ]);
142+
143+ $ this ->assertEquals ($ childSku , $ wishlistResponse ['items_v2 ' ]['items ' ][0 ]['configured_variant ' ]['sku ' ]);
144+
145+ $ this ->assertNotEmpty ($ wishlistResponse ['items_v2 ' ]['items ' ][0 ]['configurable_options ' ]);
146+ $ configurableOptions = $ wishlistResponse ['items_v2 ' ]['items ' ][0 ]['configurable_options ' ];
147+ $ this ->assertEquals ('Test Configurable ' , $ configurableOptions [0 ]['option_label ' ]);
148+ $ this ->assertEquals ('Option 1 ' , $ configurableOptions [0 ]['value_label ' ]);
149+
150+ $ this ->assertNotEmpty ($ wishlistResponse ['items_v2 ' ]['items ' ][0 ]['customizable_options ' ]);
151+ $ customizableOptions = $ wishlistResponse ['items_v2 ' ]['items ' ][0 ]['customizable_options ' ];
152+ $ this ->assertEquals ($ optionId , $ customizableOptions [0 ]['customizable_option_uid ' ]);
153+ $ this ->assertEquals ($ optionValue , $ customizableOptions [0 ]['values ' ][0 ]['value ' ]);
84154 }
85155
86156 /**
@@ -103,10 +173,10 @@ private function getHeadersMap(string $username = 'customer@example.com', string
103173 /**
104174 * Returns GraphQl mutation string
105175 *
106- * @param string $parentSku
176+ * @param string|null $parentSku
107177 * @param string $childSku
108178 * @param int $qty
109- * @param string $customizableOptions
179+ * @param string $additionalInput
110180 * @param int $wishlistId
111181 *
112182 * @return string
@@ -115,7 +185,7 @@ private function getQuery(
115185 string $ parentSku ,
116186 string $ childSku ,
117187 int $ qty ,
118- string $ customizableOptions ,
188+ string $ additionalInput = '' ,
119189 int $ wishlistId = 0
120190 ): string {
121191 return <<<MUTATION
@@ -127,7 +197,7 @@ private function getQuery(
127197 sku: " {$ childSku }"
128198 parent_sku: " {$ parentSku }"
129199 quantity: {$ qty }
130- {$ customizableOptions }
200+ {$ additionalInput }
131201 }
132202 ]
133203) {
@@ -147,13 +217,34 @@ private function getQuery(
147217 quantity
148218 added_at
149219 ... on ConfigurableWishlistItem {
150- child_sku
151220 configurable_options {
152221 id
222+ configurable_product_option_uid
153223 option_label
154224 value_id
225+ configurable_product_option_value_uid
155226 value_label
156227 }
228+ configured_variant {
229+ sku
230+ }
231+ }
232+ customizable_options {
233+ customizable_option_uid
234+ is_required
235+ label
236+ type
237+ values {
238+ customizable_option_value_uid
239+ label
240+ value
241+ price {
242+ value
243+ }
244+ }
245+ }
246+ product {
247+ sku
157248 }
158249 }
159250 }
@@ -164,16 +255,32 @@ private function getQuery(
164255 }
165256
166257 /**
167- * Generates uid for super configurable product super attributes
258+ * Generates GQL for selected_options
168259 *
169- * @param int $attributeId
170- * @param int $valueIndex
260+ * @param array $options
261+ * @return string
262+ */
263+ private function getSelectedOptionsQuery (array $ options ): string
264+ {
265+ return 'selected_options: ' . json_encode ($ options );
266+ }
267+
268+ /**
269+ * Generates GQL for entered_options
171270 *
271+ * @param array $options
172272 * @return string
173273 */
174- private function generateSuperAttributesUidQuery ( int $ attributeId , int $ valueIndex ): string
274+ private function getCustomOptionsQuery ( array $ options ): string
175275 {
176- return 'selected_options: [" ' . base64_encode ("configurable/ $ attributeId/ $ valueIndex " ) . '"] ' ;
276+ $ output = [];
277+ foreach ($ options as $ id => $ value ) {
278+ $ output [] = [
279+ 'uid ' => $ id ,
280+ 'value ' => $ value
281+ ];
282+ }
283+ return 'entered_options: ' . preg_replace ('/"([^"]+)"\s*:\s*/ ' , '$1: ' , json_encode ($ output ));
177284 }
178285
179286 /**
@@ -207,8 +314,21 @@ private function getFetchProductQuery(string $term): string
207314 ) {
208315 items {
209316 sku
317+ ... on CustomizableProductInterface {
318+ options {
319+ uid
320+ ... on CustomizableDropDownOption {
321+ value {
322+ option_type_id
323+ }
324+ }
325+ }
326+ }
210327 ... on ConfigurableProduct {
211328 variants {
329+ attributes {
330+ uid
331+ }
212332 product {
213333 sku
214334 }
0 commit comments