8
8
namespace Magento \GraphQl \Wishlist ;
9
9
10
10
use Exception ;
11
- use Magento \Bundle \Model \Selection ;
12
11
use Magento \Framework \Exception \AuthenticationException ;
13
12
use Magento \Integration \Api \CustomerTokenServiceInterface ;
14
13
use Magento \TestFramework \Helper \Bootstrap ;
15
14
use Magento \TestFramework \TestCase \GraphQlAbstract ;
16
15
use Magento \Wishlist \Model \WishlistFactory ;
17
- use Magento \Bundle \Model \Option ;
18
- use Magento \Bundle \Model \Product \Type ;
19
16
use Magento \Catalog \Api \ProductRepositoryInterface ;
20
- use Magento \Catalog \Model \Product ;
21
17
use Magento \Ui \Component \Form \Element \Select ;
22
18
23
19
/**
@@ -59,7 +55,6 @@ protected function setUp(): void
59
55
* @magentoConfigFixture default_store wishlist/general/active 1
60
56
* @magentoApiDataFixture Magento/Customer/_files/customer.php
61
57
* @magentoApiDataFixture Magento/Bundle/_files/bundle_product_dropdown_options.php
62
- *
63
58
* @throws Exception
64
59
*/
65
60
public function testUpdateBundleProductWithOptions (): void
@@ -73,10 +68,8 @@ public function testUpdateBundleProductWithOptions(): void
73
68
// Set the new values to update the wishlist item with
74
69
$ newQuantity = 5 ;
75
70
$ newDescription = 'This is a test. ' ;
76
- $ newBundleOptionUid = $ this ->generateBundleOptionUid (
77
- 'bundle-product-dropdown-options ' ,
78
- false
79
- );
71
+ $ bundleProductOptions = $ this ->getBundleProductOptions ('bundle-product-dropdown-options ' );
72
+ $ newBundleOptionUid = $ bundleProductOptions [1 ]["uid " ];
80
73
81
74
// Update the newly added wishlist item as the fixture customer
82
75
$ query = $ this ->getUpdateQuery (
@@ -110,28 +103,12 @@ public function testUpdateBundleProductWithOptions(): void
110
103
// Assert that the selected value for this bundle option is updated
111
104
self ::assertNotEmpty ($ responseBundleOption ['values ' ]);
112
105
$ responseOptionSelection = $ responseBundleOption ['values ' ][0 ];
106
+ self ::assertEquals ($ newBundleOptionUid , $ responseOptionSelection ['uid ' ]);
113
107
self ::assertEquals ('Simple Product2 ' , $ responseOptionSelection ['label ' ]);
114
108
self ::assertEquals (1 , $ responseOptionSelection ['quantity ' ]);
115
109
self ::assertEquals (10 , $ responseOptionSelection ['price ' ]);
116
110
}
117
111
118
- /**
119
- * Authentication header map
120
- *
121
- * @param string $username
122
- * @param string $password
123
- *
124
- * @return array
125
- *
126
- * @throws AuthenticationException
127
- */
128
- private function getHeaderMap (string $ username = 'customer@example.com ' , string $ password = 'password ' ): array
129
- {
130
- $ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ($ username , $ password );
131
-
132
- return ['Authorization ' => 'Bearer ' . $ customerToken ];
133
- }
134
-
135
112
/**
136
113
* Returns GraphQl mutation string
137
114
*
@@ -140,7 +117,6 @@ private function getHeaderMap(string $username = 'customer@example.com', string
140
117
* @param string $description
141
118
* @param string $bundleOptions
142
119
* @param int $wishlistId
143
- *
144
120
* @return string
145
121
*/
146
122
private function getUpdateQuery (
@@ -182,10 +158,12 @@ private function getUpdateQuery(
182
158
... on BundleWishlistItem {
183
159
bundle_options {
184
160
id
161
+ uid
185
162
label
186
163
type
187
164
values {
188
165
id
166
+ uid
189
167
label
190
168
quantity
191
169
price
@@ -201,66 +179,29 @@ private function getUpdateQuery(
201
179
}
202
180
203
181
/**
204
- * Generate the uid for the specified bundle option selection .
182
+ * Add a product to the to the wishlist .
205
183
*
206
- * @param string $bundleProductSku
207
- * @param bool $useFirstSelection
208
- * @return string
209
- */
210
- private function generateBundleOptionUid (string $ bundleProductSku , bool $ useFirstSelection ): string
211
- {
212
- $ product = $ this ->productRepository ->get ($ bundleProductSku );
213
-
214
- /** @var Type $typeInstance */
215
- $ typeInstance = $ product ->getTypeInstance ();
216
- $ typeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
217
-
218
- /** @var Option $option */
219
- $ option = $ typeInstance ->getOptionsCollection ($ product )->getLastItem ();
220
- $ optionId = (int ) $ option ->getId ();
221
-
222
- /** @var Selection $selection */
223
- $ selections = $ typeInstance ->getSelectionsCollection ([$ option ->getId ()], $ product );
224
- if ($ useFirstSelection ) {
225
- $ selection = $ selections ->getFirstItem ();
226
- } else {
227
- $ selection = $ selections ->getLastItem ();
228
- }
229
-
230
- $ selectionId = (int ) $ selection ->getSelectionId ();
231
-
232
- return base64_encode ("bundle/ $ optionId/ $ selectionId/1 " );
233
- }
234
-
235
- /**
236
- * @magentoConfigFixture default_store wishlist/general/active 1
237
- * @magentoApiDataFixture Magento/Customer/_files/customer.php
238
- * @magentoApiDataFixture Magento/Bundle/_files/product_1.php
239
- *
240
- * @throws Exception
241
- * return array
184
+ * @return array
185
+ * @throws AuthenticationException
242
186
*/
243
187
private function addProductToWishlist (): array
244
188
{
245
189
$ bundleProductSku = 'bundle-product-dropdown-options ' ;
190
+ $ bundleProductOptions = $ this ->getBundleProductOptions ($ bundleProductSku );
191
+ $ initialBundleOptionUid = $ bundleProductOptions [0 ]["uid " ];
246
192
$ initialQuantity = 2 ;
247
- $ initialBundleOptionUid = $ this ->generateBundleOptionUid (
248
- $ bundleProductSku ,
249
- true
250
- );
251
193
252
194
$ query = $ this ->getAddQuery ($ bundleProductSku , $ initialQuantity , $ initialBundleOptionUid );
253
195
return $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeaderMap ());
254
196
}
255
197
256
198
/**
257
- * Returns GraphQl add mutation string
199
+ * Returns the GraphQl mutation for adding an item to the wishlist.
258
200
*
259
201
* @param string $sku
260
202
* @param int $qty
261
203
* @param string $bundleOptions
262
204
* @param int $wishlistId
263
- *
264
205
* @return string
265
206
*/
266
207
private function getAddQuery (
@@ -317,5 +258,68 @@ private function getAddQuery(
317
258
}
318
259
MUTATION ;
319
260
}
261
+
262
+ /**
263
+ * Get the available options for the specified bundle product.
264
+ *
265
+ * @param string $bundleProductSku
266
+ * @return array
267
+ */
268
+ private function getBundleProductOptions (string $ bundleProductSku )
269
+ {
270
+ $ query = $ this ->getBundleProductSearchQuery ($ bundleProductSku );
271
+ $ response = $ this ->graphQlQuery ($ query );
272
+
273
+ $ bundleProduct = $ response ["products " ]["items " ][0 ];
274
+ $ bundleProductOptions = $ bundleProduct ["items " ][0 ]["options " ];
275
+
276
+ return $ bundleProductOptions ;
277
+ }
278
+
279
+ /**
280
+ * Returns the GraphQl product search query for a bundle product.
281
+ *
282
+ * @param string $bundleProductSku
283
+ * @return string
284
+ */
285
+ private function getBundleProductSearchQuery (string $ bundleProductSku ): string
286
+ {
287
+ return <<<QUERY
288
+ query {
289
+ products(search: " {$ bundleProductSku }"){
290
+ items {
291
+ uid
292
+ sku
293
+ name
294
+ ... on BundleProduct {
295
+ items {
296
+ uid
297
+ title
298
+ type
299
+ options {
300
+ id
301
+ uid
302
+ }
303
+ }
304
+ }
305
+ }
306
+ }
320
307
}
308
+ QUERY ;
309
+ }
321
310
311
+ /**
312
+ * Authentication header map
313
+ *
314
+ * @param string $username
315
+ * @param string $password
316
+ * @return array
317
+ * @throws AuthenticationException
318
+ */
319
+ private function getHeaderMap (string $ username = 'customer@example.com ' , string $ password = 'password ' ): array
320
+ {
321
+ $ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ($ username , $ password );
322
+
323
+ return ['Authorization ' => 'Bearer ' . $ customerToken ];
324
+ }
325
+ }
0 commit comments