6
6
namespace Magento \Catalog \Api ;
7
7
8
8
use Magento \Catalog \Api \Data \ProductInterface ;
9
- use Magento \Framework \Webapi \Rest \Request ;
10
9
use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
10
+ use Magento \Framework \Exception \CouldNotSaveException ;
11
+ use Magento \Framework \Exception \InputException ;
12
+ use Magento \Framework \Exception \NoSuchEntityException ;
13
+ use Magento \Framework \Exception \StateException ;
14
+ use Magento \Framework \Webapi \Rest \Request ;
15
+ use Magento \Store \Model \StoreManagerInterface ;
11
16
use Magento \TestFramework \Helper \Bootstrap ;
12
17
use Magento \TestFramework \ObjectManager ;
13
18
use Magento \TestFramework \TestCase \WebapiAbstract ;
14
19
15
20
/**
16
21
* SpecialPriceStorage API operations test
22
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17
23
*/
18
24
class SpecialPriceStorageTest extends WebapiAbstract
19
25
{
@@ -33,13 +39,19 @@ class SpecialPriceStorageTest extends WebapiAbstract
33
39
*/
34
40
private $ productResource ;
35
41
42
+ /**
43
+ * @var StoreManagerInterface
44
+ */
45
+ private $ storeManager ;
46
+
36
47
/**
37
48
* @ingeritdoc
38
49
*/
39
50
protected function setUp (): void
40
51
{
41
52
$ this ->objectManager = Bootstrap::getObjectManager ();
42
53
$ this ->productResource = $ this ->objectManager ->get (ProductResource::class);
54
+ $ this ->storeManager = $ this ->objectManager ->get (StoreManagerInterface::class);
43
55
}
44
56
45
57
/**
@@ -174,31 +186,25 @@ public function testDeleteWhenPriceIsGlobal(): void
174
186
/**
175
187
* Test delete method.
176
188
*
177
- * @magentoApiDataFixture Magento/Catalog/_files/product_two_websites.php
178
- * @magentoConfigFixture default_store catalog/price/scope 1
189
+ * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
179
190
* @dataProvider deleteData
180
191
* @param array $data
181
- * @return void
192
+ * @throws CouldNotSaveException
193
+ * @throws InputException
194
+ * @throws NoSuchEntityException
195
+ * @throws StateException
182
196
*/
183
- public function testDelete (array $ data ): void
197
+ public function testDelete (array $ data )
184
198
{
185
199
/** @var ProductRepositoryInterface $productRepository */
186
200
$ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
187
- $ product = $ productRepository ->get ($ data ['sku ' ], true , $ data [ ' store_id ' ], true );
201
+ $ product = $ productRepository ->get ($ data ['sku ' ], true );
188
202
$ product ->setData ('special_price ' , $ data ['price ' ]);
189
203
$ product ->setData ('special_from_date ' , $ data ['price_from ' ]);
190
204
if ($ data ['price_to ' ]) {
191
205
$ product ->setData ('special_to_date ' , $ data ['price_to ' ]);
192
206
}
193
- $ this ->productResource ->saveAttribute ($ product , 'special_price ' );
194
- $ this ->productResource ->saveAttribute ($ product , 'special_from_date ' );
195
- $ this ->productResource ->saveAttribute ($ product , 'special_to_date ' );
196
-
197
- $ product ->setData ('store_id ' , 1 );
198
- $ this ->productResource ->saveAttribute ($ product , 'special_price ' );
199
- $ this ->productResource ->saveAttribute ($ product , 'special_from_date ' );
200
- $ this ->productResource ->saveAttribute ($ product , 'special_to_date ' );
201
-
207
+ $ productRepository ->save ($ product );
202
208
$ serviceInfo = [
203
209
'rest ' => [
204
210
'resourcePath ' => '/V1/products/special-price-delete ' ,
@@ -210,21 +216,17 @@ public function testDelete(array $data): void
210
216
'operation ' => self ::SERVICE_NAME . 'Delete ' ,
211
217
],
212
218
];
213
-
214
219
$ response = $ this ->_webApiCall (
215
220
$ serviceInfo ,
216
221
[
217
- 'prices ' => [$ data ]
222
+ 'prices ' => [
223
+ $ data
224
+ ]
218
225
]
219
226
);
220
-
227
+ $ product = $ productRepository -> get ( $ data [ ' sku ' ], false , null , true );
221
228
$ this ->assertEmpty ($ response );
222
-
223
- $ product = $ productRepository ->get ($ data ['sku ' ], false , $ data ['store_id ' ], true );
224
229
$ this ->assertNull ($ product ->getSpecialPrice ());
225
-
226
- $ product = $ productRepository ->get ($ data ['sku ' ], false , 1 , true );
227
- $ this ->assertNotNull ($ product ->getSpecialPrice ());
228
230
}
229
231
230
232
/**
@@ -272,24 +274,82 @@ public function deleteData(): array
272
274
$ toDate = '2038-01-19 03:14:07 ' ;
273
275
274
276
return [
275
- 'data set with price_to specified ' => [
277
+ [
278
+ // data set with 'price_to' specified
276
279
[
277
280
'price ' => 3057 ,
278
281
'store_id ' => 0 ,
279
- 'sku ' => self ::PRODUCT_SKU_TWO_WEBSITES ,
282
+ 'sku ' => self ::SIMPLE_PRODUCT_SKU ,
280
283
'price_from ' => $ fromDate ,
281
284
'price_to ' => $ toDate
282
285
]
283
286
],
284
- 'data set without price_to specified ' => [
287
+ [
288
+ // data set without 'price_to' specified
285
289
[
286
290
'price ' => 3057 ,
287
291
'store_id ' => 0 ,
288
- 'sku ' => self ::PRODUCT_SKU_TWO_WEBSITES ,
292
+ 'sku ' => self ::SIMPLE_PRODUCT_SKU ,
289
293
'price_from ' => $ fromDate ,
290
294
'price_to ' => false
291
295
]
292
296
],
293
297
];
294
298
}
299
+
300
+ /**
301
+ * Test delete method for specific store.
302
+ *
303
+ * @magentoApiDataFixture Magento/Catalog/_files/product_two_websites.php
304
+ * @magentoConfigFixture default_store catalog/price/scope 1
305
+ * @return void
306
+ */
307
+ public function testDeleteDataForSpecificStore (): void
308
+ {
309
+ $ secondStoreViewId = $ this ->storeManager ->getStore ('fixture_second_store ' )
310
+ ->getId ();
311
+
312
+ $ data = [
313
+ 'price ' => 777 ,
314
+ 'store_id ' => $ secondStoreViewId ,
315
+ 'sku ' => self ::PRODUCT_SKU_TWO_WEBSITES ,
316
+ 'price_from ' => '1970-01-01 00:00:01 ' ,
317
+ 'price_to ' => false
318
+ ];
319
+
320
+ /** @var ProductRepositoryInterface $productRepository */
321
+ $ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
322
+ $ product = $ productRepository ->get ($ data ['sku ' ], true , 1 , true );
323
+ $ product ->setData ('special_price ' , $ data ['price ' ]);
324
+ $ product ->setData ('special_from_date ' , $ data ['price_from ' ]);
325
+
326
+ $ this ->productResource ->saveAttribute ($ product , 'special_price ' );
327
+ $ this ->productResource ->saveAttribute ($ product , 'special_from_date ' );
328
+ $ this ->productResource ->saveAttribute ($ product , 'special_to_date ' );
329
+
330
+ $ product ->setData ('store_id ' , $ secondStoreViewId );
331
+ $ this ->productResource ->saveAttribute ($ product , 'special_price ' );
332
+ $ this ->productResource ->saveAttribute ($ product , 'special_from_date ' );
333
+ $ this ->productResource ->saveAttribute ($ product , 'special_to_date ' );
334
+
335
+ $ serviceInfo = [
336
+ 'rest ' => [
337
+ 'resourcePath ' => '/V1/products/special-price-delete ' ,
338
+ 'httpMethod ' => Request::HTTP_METHOD_POST
339
+ ],
340
+ 'soap ' => [
341
+ 'service ' => self ::SERVICE_NAME ,
342
+ 'serviceVersion ' => self ::SERVICE_VERSION ,
343
+ 'operation ' => self ::SERVICE_NAME . 'Delete ' ,
344
+ ],
345
+ ];
346
+
347
+ $ this ->_webApiCall ($ serviceInfo , ['prices ' => [$ data ]]);
348
+
349
+ $ product = $ productRepository ->get ($ data ['sku ' ], false , 1 , true );
350
+ $ this ->assertNotNull ($ product ->getSpecialPrice ());
351
+
352
+ $ product = $ productRepository ->get ($ data ['sku ' ], false , $ secondStoreViewId , true );
353
+ $ this ->assertNull ($ product ->getSpecialPrice ());
354
+ }
295
355
}
0 commit comments