66namespace Magento \Catalog \Api ;
77
88use Magento \Catalog \Api \Data \ProductInterface ;
9- use Magento \Framework \Webapi \Rest \Request ;
109use 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 ;
1116use Magento \TestFramework \Helper \Bootstrap ;
1217use Magento \TestFramework \ObjectManager ;
1318use Magento \TestFramework \TestCase \WebapiAbstract ;
1419
1520/**
1621 * SpecialPriceStorage API operations test
22+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1723 */
1824class SpecialPriceStorageTest extends WebapiAbstract
1925{
@@ -33,13 +39,19 @@ class SpecialPriceStorageTest extends WebapiAbstract
3339 */
3440 private $ productResource ;
3541
42+ /**
43+ * @var StoreManagerInterface
44+ */
45+ private $ storeManager ;
46+
3647 /**
3748 * @ingeritdoc
3849 */
3950 protected function setUp (): void
4051 {
4152 $ this ->objectManager = Bootstrap::getObjectManager ();
4253 $ this ->productResource = $ this ->objectManager ->get (ProductResource::class);
54+ $ this ->storeManager = $ this ->objectManager ->get (StoreManagerInterface::class);
4355 }
4456
4557 /**
@@ -174,31 +186,25 @@ public function testDeleteWhenPriceIsGlobal(): void
174186 /**
175187 * Test delete method.
176188 *
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
179190 * @dataProvider deleteData
180191 * @param array $data
181- * @return void
192+ * @throws CouldNotSaveException
193+ * @throws InputException
194+ * @throws NoSuchEntityException
195+ * @throws StateException
182196 */
183- public function testDelete (array $ data ): void
197+ public function testDelete (array $ data )
184198 {
185199 /** @var ProductRepositoryInterface $productRepository */
186200 $ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
187- $ product = $ productRepository ->get ($ data ['sku ' ], true , $ data [ ' store_id ' ], true );
201+ $ product = $ productRepository ->get ($ data ['sku ' ], true );
188202 $ product ->setData ('special_price ' , $ data ['price ' ]);
189203 $ product ->setData ('special_from_date ' , $ data ['price_from ' ]);
190204 if ($ data ['price_to ' ]) {
191205 $ product ->setData ('special_to_date ' , $ data ['price_to ' ]);
192206 }
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 );
202208 $ serviceInfo = [
203209 'rest ' => [
204210 'resourcePath ' => '/V1/products/special-price-delete ' ,
@@ -210,21 +216,17 @@ public function testDelete(array $data): void
210216 'operation ' => self ::SERVICE_NAME . 'Delete ' ,
211217 ],
212218 ];
213-
214219 $ response = $ this ->_webApiCall (
215220 $ serviceInfo ,
216221 [
217- 'prices ' => [$ data ]
222+ 'prices ' => [
223+ $ data
224+ ]
218225 ]
219226 );
220-
227+ $ product = $ productRepository -> get ( $ data [ ' sku ' ], false , null , true );
221228 $ this ->assertEmpty ($ response );
222-
223- $ product = $ productRepository ->get ($ data ['sku ' ], false , $ data ['store_id ' ], true );
224229 $ this ->assertNull ($ product ->getSpecialPrice ());
225-
226- $ product = $ productRepository ->get ($ data ['sku ' ], false , 1 , true );
227- $ this ->assertNotNull ($ product ->getSpecialPrice ());
228230 }
229231
230232 /**
@@ -272,24 +274,82 @@ public function deleteData(): array
272274 $ toDate = '2038-01-19 03:14:07 ' ;
273275
274276 return [
275- 'data set with price_to specified ' => [
277+ [
278+ // data set with 'price_to' specified
276279 [
277280 'price ' => 3057 ,
278281 'store_id ' => 0 ,
279- 'sku ' => self ::PRODUCT_SKU_TWO_WEBSITES ,
282+ 'sku ' => self ::SIMPLE_PRODUCT_SKU ,
280283 'price_from ' => $ fromDate ,
281284 'price_to ' => $ toDate
282285 ]
283286 ],
284- 'data set without price_to specified ' => [
287+ [
288+ // data set without 'price_to' specified
285289 [
286290 'price ' => 3057 ,
287291 'store_id ' => 0 ,
288- 'sku ' => self ::PRODUCT_SKU_TWO_WEBSITES ,
292+ 'sku ' => self ::SIMPLE_PRODUCT_SKU ,
289293 'price_from ' => $ fromDate ,
290294 'price_to ' => false
291295 ]
292296 ],
293297 ];
294298 }
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+ }
295355}
0 commit comments