4
4
* Copyright © Magento, Inc. All rights reserved.
5
5
* See COPYING.txt for license details.
6
6
*/
7
+
7
8
namespace Magento \Bundle \Api ;
8
9
10
+ use Magento \Framework \Webapi \Rest \Request ;
9
11
use Magento \TestFramework \Helper \Bootstrap ;
12
+ use Magento \TestFramework \TestCase \WebapiAbstract ;
10
13
11
- class ProductLinkManagementTest extends \ Magento \ TestFramework \ TestCase \ WebapiAbstract
14
+ class ProductLinkManagementTest extends WebapiAbstract
12
15
{
13
16
const SERVICE_NAME = 'bundleProductLinkManagementV1 ' ;
14
17
const SERVICE_VERSION = 'V1 ' ;
@@ -84,6 +87,59 @@ public function testAddChild()
84
87
$ this ->assertGreaterThan (0 , $ childId );
85
88
}
86
89
90
+ /**
91
+ * Verify empty out of stock bundle product is in stock after child has been added.
92
+ *
93
+ * @magentoApiDataFixture Magento/Bundle/_files/empty_bundle_product.php
94
+ * @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
95
+ *
96
+ * @return void
97
+ */
98
+ public function testBundleProductIsInStockAfterAddChild (): void
99
+ {
100
+ $ productSku = 'bundle-product ' ;
101
+ $ option = [
102
+ 'required ' => 1 ,
103
+ 'position ' => 0 ,
104
+ 'type ' => 'select ' ,
105
+ 'title ' => 'option 1 ' ,
106
+ 'sku ' => $ productSku ,
107
+ ];
108
+ self ::assertFalse ($ this ->isProductInStock ($ productSku ));
109
+ $ optionId = $ this ->addOption ($ option );
110
+ $ linkedProduct = [
111
+ 'sku ' => 'virtual-product ' ,
112
+ 'option_id ' => $ optionId ,
113
+ 'position ' => '1 ' ,
114
+ 'is_default ' => 1 ,
115
+ 'priceType ' => 2 ,
116
+ 'price ' => 151.34 ,
117
+ 'qty ' => 8 ,
118
+ 'can_change_quantity ' => 1 ,
119
+ ];
120
+
121
+ $ this ->addChild ($ productSku , $ optionId , $ linkedProduct );
122
+ self ::assertTrue ($ this ->isProductInStock ($ productSku ));
123
+ }
124
+
125
+ /**
126
+ * Verify in stock bundle product is out stock after child has been removed.
127
+ *
128
+ * @magentoApiDataFixture Magento/Bundle/_files/product.php
129
+ *
130
+ * @return void
131
+ */
132
+ public function testBundleProductIsOutOfStockAfterRemoveChild (): void
133
+ {
134
+ $ productSku = 'bundle-product ' ;
135
+ $ childSku = 'simple ' ;
136
+ $ optionIds = $ this ->getProductOptions (3 );
137
+ $ optionId = array_shift ($ optionIds );
138
+ self ::assertTrue ($ this ->isProductInStock ($ productSku ));
139
+ $ this ->removeChild ($ productSku , $ optionId , $ childSku );
140
+ self ::assertFalse ($ this ->isProductInStock ($ productSku ));
141
+ }
142
+
87
143
/**
88
144
* @magentoApiDataFixture Magento/Bundle/_files/product.php
89
145
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
@@ -119,7 +175,7 @@ private function saveChild($productSku, $linkedProduct)
119
175
[$ productSku , $ linkedProduct ['id ' ]],
120
176
$ resourcePath
121
177
),
122
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_PUT ,
178
+ 'httpMethod ' => Request::HTTP_METHOD_PUT ,
123
179
],
124
180
'soap ' => [
125
181
'service ' => self ::SERVICE_NAME ,
@@ -149,7 +205,7 @@ private function addChild($productSku, $optionId, $linkedProduct)
149
205
[$ productSku , $ optionId ],
150
206
$ resourcePath
151
207
),
152
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_POST ,
208
+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
153
209
],
154
210
'soap ' => [
155
211
'service ' => self ::SERVICE_NAME ,
@@ -179,7 +235,7 @@ protected function removeChild($productSku, $optionId, $childSku)
179
235
$ serviceInfo = [
180
236
'rest ' => [
181
237
'resourcePath ' => sprintf ($ resourcePath , $ productSku , $ optionId , $ childSku ),
182
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_DELETE ,
238
+ 'httpMethod ' => Request::HTTP_METHOD_DELETE ,
183
239
],
184
240
'soap ' => [
185
241
'service ' => self ::SERVICE_NAME ,
@@ -200,7 +256,7 @@ protected function getChildren($productSku)
200
256
$ serviceInfo = [
201
257
'rest ' => [
202
258
'resourcePath ' => self ::RESOURCE_PATH . '/ ' . $ productSku . '/children ' ,
203
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_GET ,
259
+ 'httpMethod ' => Request::HTTP_METHOD_GET ,
204
260
],
205
261
'soap ' => [
206
262
'service ' => self ::SERVICE_NAME ,
@@ -210,4 +266,50 @@ protected function getChildren($productSku)
210
266
];
211
267
return $ this ->_webApiCall ($ serviceInfo , ['productSku ' => $ productSku ]);
212
268
}
269
+
270
+ /**
271
+ * Check product stock status.
272
+ *
273
+ * @param string $productSku
274
+ * @return bool
275
+ */
276
+ private function isProductInStock (string $ productSku ): bool
277
+ {
278
+ $ serviceInfo = [
279
+ 'rest ' => [
280
+ 'resourcePath ' => '/V1/stockStatuses/ ' . $ productSku ,
281
+ 'httpMethod ' => Request::HTTP_METHOD_GET ,
282
+ ],
283
+ 'soap ' => [
284
+ 'service ' => 'catalogInventoryStockRegistryV1 ' ,
285
+ 'serviceVersion ' => self ::SERVICE_VERSION ,
286
+ 'operation ' => 'catalogInventoryStockRegistryV1getStockStatusBySku ' ,
287
+ ],
288
+ ];
289
+ $ result = $ this ->_webApiCall ($ serviceInfo , ['productSku ' => $ productSku ]);
290
+
291
+ return (bool )$ result ['stock_status ' ];
292
+ }
293
+
294
+ /**
295
+ * Add option to bundle product.
296
+ *
297
+ * @param array $option
298
+ * @return int
299
+ */
300
+ private function addOption (array $ option ): int
301
+ {
302
+ $ serviceInfo = [
303
+ 'rest ' => [
304
+ 'resourcePath ' => '/V1/bundle-products/options/add ' ,
305
+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
306
+ ],
307
+ 'soap ' => [
308
+ 'service ' => 'bundleProductOptionManagementV1 ' ,
309
+ 'serviceVersion ' => self ::SERVICE_VERSION ,
310
+ 'operation ' => 'bundleProductOptionManagementV1Save ' ,
311
+ ],
312
+ ];
313
+ return $ this ->_webApiCall ($ serviceInfo , ['option ' => $ option ]);
314
+ }
213
315
}
0 commit comments