8
8
namespace Magento \GraphQl \Wishlist ;
9
9
10
10
use Exception ;
11
+ use Magento \Bundle \Model \Selection ;
11
12
use Magento \Framework \Exception \AuthenticationException ;
12
13
use Magento \Integration \Api \CustomerTokenServiceInterface ;
13
14
use Magento \TestFramework \Helper \Bootstrap ;
@@ -51,47 +52,67 @@ protected function setUp(): void
51
52
}
52
53
53
54
/**
55
+ * Test that a wishlist item bundle product is properly updated.
56
+ *
57
+ * This includes the selected options for the bundle product.
58
+ *
54
59
* @magentoConfigFixture default_store wishlist/general/active 1
55
60
* @magentoApiDataFixture Magento/Customer/_files/customer.php
56
- * @magentoApiDataFixture Magento/Bundle/_files/product_1 .php
61
+ * @magentoApiDataFixture Magento/Bundle/_files/bundle_product_dropdown_options .php
57
62
*
58
63
* @throws Exception
59
64
*/
60
65
public function testUpdateBundleProductWithOptions (): void
61
66
{
62
- $ qty = 5 ;
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
67
+ // Add the fixture bundle product to the fixture customer's wishlist
78
68
$ wishlist = $ this ->addProductToWishlist ();
79
- $ wishlistId = $ wishlist ['addProductsToWishlist ' ]['wishlist ' ]['id ' ];
80
- $ wishlistItemId = $ wishlist ['addProductsToWishlist ' ]['wishlist ' ]['items_v2 ' ]['items ' ][0 ]['id ' ];
81
- $ itemsCount = $ wishlist ['addProductsToWishlist ' ]['wishlist ' ]['items_count ' ];
69
+ $ wishlistId = (int ) $ wishlist ['addProductsToWishlist ' ]['wishlist ' ]['id ' ];
70
+ $ wishlistItemId = (int ) $ wishlist ['addProductsToWishlist ' ]['wishlist ' ]['items_v2 ' ]['items ' ][0 ]['id ' ];
71
+ $ previousItemsCount = $ wishlist ['addProductsToWishlist ' ]['wishlist ' ]['items_count ' ];
72
+
73
+ // Set the new values to update the wishlist item with
74
+ $ newQuantity = 5 ;
75
+ $ newDescription = 'This is a test. ' ;
76
+ $ newBundleOptionUid = $ this ->generateBundleOptionUid (
77
+ 'bundle-product-dropdown-options ' ,
78
+ false
79
+ );
82
80
83
- $ query = $ this ->getBundleQuery ((int )$ wishlistItemId , $ qty , $ bundleOptions , (int )$ wishlistId );
81
+ // Update the newly added wishlist item as the fixture customer
82
+ $ query = $ this ->getUpdateQuery (
83
+ $ wishlistItemId ,
84
+ $ newQuantity ,
85
+ $ newDescription ,
86
+ $ newBundleOptionUid ,
87
+ $ wishlistId
88
+ );
84
89
$ response = $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeaderMap ());
85
90
86
- $ this ->assertArrayHasKey ('updateProductsInWishlist ' , $ response );
87
- $ this ->assertArrayHasKey ('wishlist ' , $ response ['updateProductsInWishlist ' ]);
88
- $ response = $ response ['updateProductsInWishlist ' ]['wishlist ' ];
89
- $ this ->assertEquals ($ itemsCount , $ response ['items_count ' ]);
90
- $ this ->assertEquals ($ qty , $ response ['items_v2 ' ]['items ' ][0 ]['quantity ' ]);
91
- $ this ->assertNotEmpty ($ response ['items_v2 ' ]['items ' ][0 ]['bundle_options ' ]);
92
- $ bundleOptions = $ response ['items_v2 ' ]['items ' ][0 ]['bundle_options ' ];
93
- $ this ->assertEquals ('Bundle Product Items ' , $ bundleOptions [0 ]['label ' ]);
94
- $ this ->assertEquals (Select::NAME , $ bundleOptions [0 ]['type ' ]);
91
+ // Assert that the response has the expected base properties
92
+ self ::assertArrayHasKey ('updateProductsInWishlist ' , $ response );
93
+ self ::assertArrayHasKey ('wishlist ' , $ response ['updateProductsInWishlist ' ]);
94
+
95
+ // Assert that the wishlist item count is unchanged
96
+ $ responseWishlist = $ response ['updateProductsInWishlist ' ]['wishlist ' ];
97
+ self ::assertEquals ($ previousItemsCount , $ responseWishlist ['items_count ' ]);
98
+
99
+ // Assert that the wishlist item quantity and description are updated
100
+ $ responseWishlistItem = $ responseWishlist ['items_v2 ' ]['items ' ][0 ];
101
+ self ::assertEquals ($ newQuantity , $ responseWishlistItem ['quantity ' ]);
102
+ self ::assertEquals ($ newDescription , $ responseWishlistItem ['description ' ]);
103
+
104
+ // Assert that the bundle option for this wishlist item is accurate
105
+ self ::assertNotEmpty ($ responseWishlistItem ['bundle_options ' ]);
106
+ $ responseBundleOption = $ responseWishlistItem ['bundle_options ' ][0 ];
107
+ self ::assertEquals ('Dropdown Options ' , $ responseBundleOption ['label ' ]);
108
+ self ::assertEquals (Select::NAME , $ responseBundleOption ['type ' ]);
109
+
110
+ // Assert that the selected value for this bundle option is updated
111
+ self ::assertNotEmpty ($ responseBundleOption ['values ' ]);
112
+ $ responseOptionSelection = $ responseBundleOption ['values ' ][0 ];
113
+ self ::assertEquals ('Simple Product2 ' , $ responseOptionSelection ['label ' ]);
114
+ self ::assertEquals (1 , $ responseOptionSelection ['quantity ' ]);
115
+ self ::assertEquals (10 , $ responseOptionSelection ['price ' ]);
95
116
}
96
117
97
118
/**
@@ -116,14 +137,16 @@ private function getHeaderMap(string $username = 'customer@example.com', string
116
137
*
117
138
* @param int $wishlistItemId
118
139
* @param int $qty
140
+ * @param string $description
119
141
* @param string $bundleOptions
120
142
* @param int $wishlistId
121
143
*
122
144
* @return string
123
145
*/
124
- private function getBundleQuery (
146
+ private function getUpdateQuery (
125
147
int $ wishlistItemId ,
126
148
int $ qty ,
149
+ string $ description ,
127
150
string $ bundleOptions ,
128
151
int $ wishlistId = 0
129
152
): string {
@@ -135,6 +158,7 @@ private function getBundleQuery(
135
158
{
136
159
wishlist_item_id: " {$ wishlistItemId }"
137
160
quantity: {$ qty }
161
+ description: " {$ description }"
138
162
selected_options: [
139
163
" {$ bundleOptions }"
140
164
]
@@ -154,6 +178,7 @@ private function getBundleQuery(
154
178
items{
155
179
id
156
180
quantity
181
+ description
157
182
... on BundleWishlistItem {
158
183
bundle_options {
159
184
id
@@ -176,15 +201,35 @@ private function getBundleQuery(
176
201
}
177
202
178
203
/**
179
- * @param int $optionId
180
- * @param int $selectionId
181
- * @param int $quantity
204
+ * Generate the uid for the specified bundle option selection.
182
205
*
206
+ * @param string $bundleProductSku
207
+ * @param bool $useFirstSelection
183
208
* @return string
184
209
*/
185
- private function generateBundleOptionUid (int $ optionId , int $ selectionId , int $ quantity ): string
210
+ private function generateBundleOptionUid (string $ bundleProductSku , bool $ useFirstSelection ): string
186
211
{
187
- return base64_encode ("bundle/ $ optionId/ $ selectionId/ $ quantity " );
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 " );
188
233
}
189
234
190
235
/**
@@ -197,23 +242,14 @@ private function generateBundleOptionUid(int $optionId, int $selectionId, int $q
197
242
*/
198
243
private function addProductToWishlist (): array
199
244
{
200
- $ sku = 'bundle-product ' ;
201
- $ product = $ this ->productRepository ->get ($ sku );
202
- $ qty = 2 ;
203
- $ optionQty = 1 ;
204
-
205
- /** @var Type $typeInstance */
206
- $ typeInstance = $ product ->getTypeInstance ();
207
- $ typeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
208
- /** @var Option $option */
209
- $ option = $ typeInstance ->getOptionsCollection ($ product )->getFirstItem ();
210
- /** @var Product $selection */
211
- $ selection = $ typeInstance ->getSelectionsCollection ([$ option ->getId ()], $ product )->getFirstItem ();
212
- $ optionId = $ option ->getId ();
213
- $ selectionId = $ selection ->getSelectionId ();
214
- $ bundleOptions = $ this ->generateBundleOptionUid ((int ) $ optionId , (int ) $ selectionId , $ optionQty );
245
+ $ bundleProductSku = 'bundle-product-dropdown-options ' ;
246
+ $ initialQuantity = 2 ;
247
+ $ initialBundleOptionUid = $ this ->generateBundleOptionUid (
248
+ $ bundleProductSku ,
249
+ true
250
+ );
215
251
216
- $ query = $ this ->addQuery ( $ sku , $ qty , $ bundleOptions );
252
+ $ query = $ this ->getAddQuery ( $ bundleProductSku , $ initialQuantity , $ initialBundleOptionUid );
217
253
return $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeaderMap ());
218
254
}
219
255
@@ -227,7 +263,7 @@ private function addProductToWishlist(): array
227
263
*
228
264
* @return string
229
265
*/
230
- private function addQuery (
266
+ private function getAddQuery (
231
267
string $ sku ,
232
268
int $ qty ,
233
269
string $ bundleOptions ,
0 commit comments