88namespace Magento \GraphQl \Wishlist ;
99
1010use Exception ;
11+ use Magento \Bundle \Model \Selection ;
1112use Magento \Framework \Exception \AuthenticationException ;
1213use Magento \Integration \Api \CustomerTokenServiceInterface ;
1314use Magento \TestFramework \Helper \Bootstrap ;
@@ -51,47 +52,67 @@ protected function setUp(): void
5152 }
5253
5354 /**
55+ * Test that a wishlist item bundle product is properly updated.
56+ *
57+ * This includes the selected options for the bundle product.
58+ *
5459 * @magentoConfigFixture default_store wishlist/general/active 1
5560 * @magentoApiDataFixture Magento/Customer/_files/customer.php
56- * @magentoApiDataFixture Magento/Bundle/_files/product_1 .php
61+ * @magentoApiDataFixture Magento/Bundle/_files/bundle_product_dropdown_options .php
5762 *
5863 * @throws Exception
5964 */
6065 public function testUpdateBundleProductWithOptions (): void
6166 {
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
7868 $ 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+ );
8280
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+ );
8489 $ response = $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeaderMap ());
8590
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 ' ]);
95116 }
96117
97118 /**
@@ -116,14 +137,16 @@ private function getHeaderMap(string $username = 'customer@example.com', string
116137 *
117138 * @param int $wishlistItemId
118139 * @param int $qty
140+ * @param string $description
119141 * @param string $bundleOptions
120142 * @param int $wishlistId
121143 *
122144 * @return string
123145 */
124- private function getBundleQuery (
146+ private function getUpdateQuery (
125147 int $ wishlistItemId ,
126148 int $ qty ,
149+ string $ description ,
127150 string $ bundleOptions ,
128151 int $ wishlistId = 0
129152 ): string {
@@ -135,6 +158,7 @@ private function getBundleQuery(
135158 {
136159 wishlist_item_id: " {$ wishlistItemId }"
137160 quantity: {$ qty }
161+ description: " {$ description }"
138162 selected_options: [
139163 " {$ bundleOptions }"
140164 ]
@@ -154,6 +178,7 @@ private function getBundleQuery(
154178 items{
155179 id
156180 quantity
181+ description
157182 ... on BundleWishlistItem {
158183 bundle_options {
159184 id
@@ -176,15 +201,35 @@ private function getBundleQuery(
176201 }
177202
178203 /**
179- * @param int $optionId
180- * @param int $selectionId
181- * @param int $quantity
204+ * Generate the uid for the specified bundle option selection.
182205 *
206+ * @param string $bundleProductSku
207+ * @param bool $useFirstSelection
183208 * @return string
184209 */
185- private function generateBundleOptionUid (int $ optionId , int $ selectionId , int $ quantity ): string
210+ private function generateBundleOptionUid (string $ bundleProductSku , bool $ useFirstSelection ): string
186211 {
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 " );
188233 }
189234
190235 /**
@@ -197,23 +242,14 @@ private function generateBundleOptionUid(int $optionId, int $selectionId, int $q
197242 */
198243 private function addProductToWishlist (): array
199244 {
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+ );
215251
216- $ query = $ this ->addQuery ( $ sku , $ qty , $ bundleOptions );
252+ $ query = $ this ->getAddQuery ( $ bundleProductSku , $ initialQuantity , $ initialBundleOptionUid );
217253 return $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeaderMap ());
218254 }
219255
@@ -227,7 +263,7 @@ private function addProductToWishlist(): array
227263 *
228264 * @return string
229265 */
230- private function addQuery (
266+ private function getAddQuery (
231267 string $ sku ,
232268 int $ qty ,
233269 string $ bundleOptions ,
0 commit comments