@@ -50,6 +50,52 @@ protected function setUp(): void
50
50
$ this ->productRepository = $ objectManager ->get (ProductRepositoryInterface::class);
51
51
}
52
52
53
+ /**
54
+ * @magentoConfigFixture default_store wishlist/general/active 1
55
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
56
+ * @magentoApiDataFixture Magento/Bundle/_files/product_1.php
57
+ *
58
+ * @throws Exception
59
+ */
60
+ public function testUpdateBundleProductWithOptions (): void
61
+ {
62
+ $ qty = 2 ;
63
+ $ optionQty = 1 ;
64
+ $ sku = 'bundle-product ' ;
65
+ $ product = $ this ->productRepository ->get ($ sku );
66
+ /** @var Type $typeInstance */
67
+ $ typeInstance = $ product ->getTypeInstance ();
68
+ $ typeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
69
+ /** @var Option $option */
70
+ $ option = $ typeInstance ->getOptionsCollection ($ product )->getLastItem ();
71
+ /** @var Product $selection */
72
+ $ selection = $ typeInstance ->getSelectionsCollection ([$ option ->getId ()], $ product )->getLastItem ();
73
+ $ optionId = $ option ->getId ();
74
+ $ selectionId = $ selection ->getSelectionId ();
75
+ $ bundleOptions = $ this ->generateBundleOptionUid ((int ) $ optionId , (int ) $ selectionId , $ optionQty );
76
+
77
+ // Add product to wishlist
78
+ $ this ->addProductToWishlist ();
79
+
80
+ $ wishlist = $ this ->getBundleWishlist ();
81
+ $ wishlistId = $ wishlist ['customer ' ]['wishlists ' ][0 ]['id ' ];
82
+ $ wishlistItemId = $ wishlist ['customer ' ]['wishlists ' ][0 ]['items_v2 ' ][0 ]['id ' ];
83
+ $ itemsCount = $ wishlist ['customer ' ]['wishlists ' ][0 ]['items_count ' ];
84
+
85
+ $ query = $ this ->getBundleQuery ((int )$ wishlistItemId , $ qty , $ bundleOptions , (int )$ wishlistId );
86
+ $ response = $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeaderMap ());
87
+
88
+ $ this ->assertArrayHasKey ('updateProductsInWishlist ' , $ response );
89
+ $ this ->assertArrayHasKey ('wishlist ' , $ response ['updateProductsInWishlist ' ]);
90
+ $ response = $ response ['updateProductsInWishlist ' ]['wishlist ' ];
91
+ $ this ->assertEquals ($ itemsCount , $ response ['items_count ' ]);
92
+ $ this ->assertEquals ($ qty , $ response ['items_v2 ' ][0 ]['quantity ' ]);
93
+ $ this ->assertNotEmpty ($ response ['items_v2 ' ][0 ]['bundle_options ' ]);
94
+ $ bundleOptions = $ response ['items_v2 ' ][0 ]['bundle_options ' ];
95
+ $ this ->assertEquals ('Bundle Product Items ' , $ bundleOptions [0 ]['label ' ]);
96
+ $ this ->assertEquals (Select::NAME , $ bundleOptions [0 ]['type ' ]);
97
+ }
98
+
53
99
/**
54
100
* Authentication header map
55
101
*
@@ -111,73 +157,134 @@ private function getCustomerBundleWishlistQuery(): string
111
157
QUERY ;
112
158
}
113
159
160
+ /**
161
+ * Returns GraphQl mutation string
162
+ *
163
+ * @param int $wishlistItemId
164
+ * @param int $qty
165
+ * @param string $bundleOptions
166
+ * @param int $wishlistId
167
+ *
168
+ * @return string
169
+ */
170
+ private function getBundleQuery (
171
+ int $ wishlistItemId ,
172
+ int $ qty ,
173
+ string $ bundleOptions ,
174
+ int $ wishlistId = 0
175
+ ): string {
176
+ return <<<MUTATION
177
+ mutation {
178
+ updateProductsInWishlist(
179
+ wishlistId: {$ wishlistId },
180
+ wishlistItems: [
181
+ {
182
+ wishlist_item_id: " {$ wishlistItemId }"
183
+ quantity: {$ qty }
184
+ selected_options: [
185
+ " {$ bundleOptions }"
186
+ ]
187
+ }
188
+ ]
189
+ ) {
190
+ user_errors {
191
+ code
192
+ message
193
+ }
194
+ wishlist {
195
+ id
196
+ sharing_code
197
+ items_count
198
+ updated_at
199
+ items_v2 {
200
+ id
201
+ description
202
+ quantity
203
+ ... on BundleWishlistItem {
204
+ bundle_options {
205
+ id
206
+ label
207
+ type
208
+ values {
209
+ id
210
+ label
211
+ quantity
212
+ price
213
+ }
214
+ }
215
+ }
216
+ }
217
+ }
218
+ }
219
+ }
220
+ MUTATION ;
221
+ }
222
+
223
+ /**
224
+ * @param int $optionId
225
+ * @param int $selectionId
226
+ *
227
+ * @param int $quantity
228
+ *
229
+ * @return string
230
+ */
231
+ private function generateBundleOptionUid (int $ optionId , int $ selectionId , int $ quantity ): string
232
+ {
233
+ return base64_encode ("bundle/ $ optionId/ $ selectionId/ $ quantity " );
234
+ }
235
+
114
236
/**
115
237
* @magentoConfigFixture default_store wishlist/general/active 1
116
238
* @magentoApiDataFixture Magento/Customer/_files/customer.php
117
239
* @magentoApiDataFixture Magento/Bundle/_files/product_1.php
118
240
*
119
241
* @throws Exception
120
242
*/
121
- public function testUpdateBundleProductWithOptions (): void
243
+ private function addProductToWishlist (): void
122
244
{
123
- $ wishlist = $ this ->getBundleWishlist ();
245
+ $ sku = 'bundle-product ' ;
246
+ $ product = $ this ->productRepository ->get ($ sku );
124
247
$ qty = 2 ;
125
248
$ optionQty = 1 ;
126
- $ optionId = $ wishlist ['customer ' ]['wishlists ' ][0 ]['items_v2 ' ][0 ]['bundle_options ' ][0 ]['id ' ];
127
249
128
- $ sku = 'bundle-product ' ;
129
- $ product = $ this ->productRepository ->get ($ sku );
130
250
/** @var Type $typeInstance */
131
251
$ typeInstance = $ product ->getTypeInstance ();
132
252
$ typeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
133
253
/** @var Option $option */
134
- $ option = $ typeInstance ->getOptionsCollection ($ product )->getLastItem ();
254
+ $ option = $ typeInstance ->getOptionsCollection ($ product )->getFirstItem ();
135
255
/** @var Product $selection */
136
- $ selection = $ typeInstance ->getSelectionsCollection ([$ option ->getId ()], $ product )->getLastItem ();
256
+ $ selection = $ typeInstance ->getSelectionsCollection ([$ option ->getId ()], $ product )->getFirstItem ();
137
257
$ optionId = $ option ->getId ();
138
258
$ selectionId = $ selection ->getSelectionId ();
139
259
$ bundleOptions = $ this ->generateBundleOptionUid ((int ) $ optionId , (int ) $ selectionId , $ optionQty );
140
260
141
- $ wishlistId = $ wishlist ['customer ' ]['wishlists ' ][0 ]['id ' ];
142
- $ wishlistItemId = $ wishlist ['customer ' ]['wishlists ' ][0 ]['items_v2 ' ][0 ]['id ' ];
143
- $ itemsCount = $ wishlist ['customer ' ]['wishlists ' ][0 ]['items_count ' ];
144
- $ query = $ this ->getBundleQuery ((int )$ wishlistItemId , $ qty , $ bundleOptions , (int )$ wishlistId );
145
- $ response = $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeaderMap ());
146
-
147
- $ this ->assertArrayHasKey ('updateProductsInWishlist ' , $ response );
148
- $ this ->assertArrayHasKey ('wishlist ' , $ response ['updateProductsInWishlist ' ]);
149
- $ response = $ response ['updateProductsInWishlist ' ]['wishlist ' ];
150
- $ this ->assertEquals ($ itemsCount , $ response ['items_count ' ]);
151
- $ this ->assertEquals ($ qty , $ response ['items_v2 ' ][0 ]['quantity ' ]);
152
- $ this ->assertNotEmpty ($ response ['items_v2 ' ][0 ]['bundle_options ' ]);
153
- $ bundleOptions = $ response ['items_v2 ' ][0 ]['bundle_options ' ];
154
- $ this ->assertEquals ('Bundle Product Items ' , $ bundleOptions [0 ]['label ' ]);
155
- $ this ->assertEquals (Select::NAME , $ bundleOptions [0 ]['type ' ]);
261
+ $ query = $ this ->addQuery ($ sku , $ qty , $ bundleOptions );
262
+ $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeaderMap ());
156
263
}
157
264
158
265
/**
159
- * Returns GraphQl mutation string
266
+ * Returns GraphQl add mutation string
160
267
*
161
- * @param int $wishlistItemId
268
+ * @param string $sku
162
269
* @param int $qty
163
270
* @param string $bundleOptions
164
271
* @param int $wishlistId
165
272
*
166
273
* @return string
167
274
*/
168
- private function getBundleQuery (
169
- int $ wishlistItemId ,
275
+ private function addQuery (
276
+ string $ sku ,
170
277
int $ qty ,
171
278
string $ bundleOptions ,
172
279
int $ wishlistId = 0
173
280
): string {
174
281
return <<<MUTATION
175
282
mutation {
176
- updateProductsInWishlist (
283
+ addProductsToWishlist (
177
284
wishlistId: {$ wishlistId },
178
285
wishlistItems: [
179
286
{
180
- wishlist_item_id : " {$ wishlistItemId }"
287
+ sku : " {$ sku }"
181
288
quantity: {$ qty }
182
289
selected_options: [
183
290
" {$ bundleOptions }"
@@ -198,6 +305,7 @@ private function getBundleQuery(
198
305
id
199
306
description
200
307
quantity
308
+ added_at
201
309
... on BundleWishlistItem {
202
310
bundle_options {
203
311
id
@@ -218,16 +326,4 @@ private function getBundleQuery(
218
326
MUTATION ;
219
327
}
220
328
221
- /**
222
- * @param int $optionId
223
- * @param int $selectionId
224
- *
225
- * @param int $quantity
226
- *
227
- * @return string
228
- */
229
- private function generateBundleOptionUid (int $ optionId , int $ selectionId , int $ quantity ): string
230
- {
231
- return base64_encode ("bundle/ $ optionId/ $ selectionId/ $ quantity " );
232
- }
233
329
}
0 commit comments