10
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
11
use Magento \Eav \Model \Entity \Attribute \ScopedAttributeInterface ;
12
12
use Magento \Store \Model \Store ;
13
+ use Magento \TestFramework \Fixture \DataFixtureStorage ;
14
+ use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
13
15
use Magento \TestFramework \Helper \Bootstrap ;
14
16
use Magento \TestFramework \TestCase \WebapiAbstract ;
15
17
use Magento \Catalog \Model \Attribute \ScopeOverriddenValue ;
26
28
*/
27
29
class ProductRepositoryMultiWebsiteTest extends WebapiAbstract
28
30
{
29
- const SERVICE_NAME = 'catalogProductRepositoryV1 ' ;
30
- const SERVICE_VERSION = 'V1 ' ;
31
- const RESOURCE_PATH = '/V1/products ' ;
31
+ private const SERVICE_NAME = 'catalogProductRepositoryV1 ' ;
32
+ private const SERVICE_VERSION = 'V1 ' ;
33
+ private const RESOURCE_PATH = '/V1/products ' ;
32
34
33
35
/**
34
36
* @var array
@@ -40,13 +42,19 @@ class ProductRepositoryMultiWebsiteTest extends WebapiAbstract
40
42
*/
41
43
private $ objectManager ;
42
44
45
+ /**
46
+ * @var DataFixtureStorage
47
+ */
48
+ private $ fixtures ;
49
+
43
50
/**
44
51
* @inheritDoc
45
52
*/
46
53
protected function setUp (): void
47
54
{
48
55
parent ::setUp ();
49
56
$ this ->objectManager = Bootstrap::getObjectManager ();
57
+ $ this ->fixtures = $ this ->objectManager ->get (DataFixtureStorageManager::class)->getStorage ();
50
58
}
51
59
52
60
/**
@@ -329,6 +337,52 @@ public function testPartialUpdate(): void
329
337
);
330
338
}
331
339
340
+ /**
341
+ * @magentoApiDataFixture Magento\Store\Test\Fixture\Website as:website2
342
+ * @magentoApiDataFixture Magento\Store\Test\Fixture\Group with:{"website_id":"$website2.id$"} as:store_group2
343
+ * @magentoApiDataFixture Magento\Store\Test\Fixture\Store with:{"store_group_id":"$store_group2.id$"} as:store2
344
+ * @magentoApiDataFixture Magento\Store\Test\Fixture\Store with:{"store_group_id":"$store_group2.id$"} as:store3
345
+ * @magentoApiDataFixture Magento\Catalog\Test\Fixture\Product with:{"website_ids":[1,"$website2.id$"]} as:product
346
+ * @magentoConfigFixture catalog/price/scope 1
347
+ */
348
+ public function testUpdatePrice (): void
349
+ {
350
+ $ store = $ this ->objectManager ->get (Store::class);
351
+ $ defaultWebsiteStore1 = $ store ->load ('default ' , 'code ' )->getCode ();
352
+ $ secondWebsiteStore1 = $ this ->fixtures ->get ('store2 ' )->getCode ();
353
+ $ secondWebsiteStore2 = $ this ->fixtures ->get ('store3 ' )->getCode ();
354
+ $ sku = $ this ->fixtures ->get ('product ' )->getSku ();
355
+
356
+ // change any attribute value in second store
357
+ $ request = [
358
+ ProductInterface::SKU => $ sku ,
359
+ 'name ' => 'updated product name for storeview '
360
+ ];
361
+ $ this ->saveProduct ($ request , $ secondWebsiteStore1 );
362
+
363
+ // now update prices in second website
364
+ $ request = [
365
+ ProductInterface::SKU => $ sku ,
366
+ 'price ' => 9 ,
367
+ ProductInterface::CUSTOM_ATTRIBUTES => [
368
+ [
369
+ 'attribute_code ' => 'special_price ' ,
370
+ 'value ' => 8 ,
371
+ ]
372
+ ],
373
+ ];
374
+ $ this ->saveProduct ($ request , $ secondWebsiteStore1 );
375
+ $ defaultWebsiteStore1Response = $ this ->flattenCustomAttributes ($ this ->getProduct ($ sku , $ defaultWebsiteStore1 ));
376
+ $ this ->assertEquals (10 , $ defaultWebsiteStore1Response ['price ' ]);
377
+ $ this ->assertArrayNotHasKey ('special_price ' , $ defaultWebsiteStore1Response );
378
+ $ secondWebsiteStore1Response = $ this ->flattenCustomAttributes ($ this ->getProduct ($ sku , $ secondWebsiteStore1 ));
379
+ $ this ->assertEquals (9 , $ secondWebsiteStore1Response ['price ' ]);
380
+ $ this ->assertEquals (8 , $ secondWebsiteStore1Response ['special_price ' ]);
381
+ $ secondWebsiteStore2Response = $ this ->flattenCustomAttributes ($ this ->getProduct ($ sku , $ secondWebsiteStore2 ));
382
+ $ this ->assertEquals (9 , $ secondWebsiteStore2Response ['price ' ]);
383
+ $ this ->assertEquals (8 , $ secondWebsiteStore2Response ['special_price ' ]);
384
+ }
385
+
332
386
/**
333
387
* @param array $expected
334
388
* @param array $actual
@@ -381,4 +435,39 @@ private function updateAttribute(string $code, array $data): void
381
435
$ attribute ->addData ($ data );
382
436
$ attributeRepository ->save ($ attribute );
383
437
}
438
+
439
+ /**
440
+ * @param array $data
441
+ * @return array
442
+ */
443
+ private function flattenCustomAttributes (array $ data ): array
444
+ {
445
+ $ customAttributes = $ data [ProductInterface::CUSTOM_ATTRIBUTES ] ?? [];
446
+ unset($ data [ProductInterface::CUSTOM_ATTRIBUTES ]);
447
+ return array_merge (array_column ($ customAttributes , 'value ' , 'attribute_code ' ), $ data );
448
+ }
449
+
450
+ /**
451
+ * Get product
452
+ *
453
+ * @param string $sku
454
+ * @param string|null $storeCode
455
+ * @return array|bool|float|int|string
456
+ */
457
+ private function getProduct ($ sku , $ storeCode = null )
458
+ {
459
+ $ serviceInfo = [
460
+ 'rest ' => [
461
+ 'resourcePath ' => self ::RESOURCE_PATH . '/ ' . $ sku ,
462
+ 'httpMethod ' => Request::HTTP_METHOD_GET ,
463
+ ],
464
+ 'soap ' => [
465
+ 'service ' => self ::SERVICE_NAME ,
466
+ 'serviceVersion ' => self ::SERVICE_VERSION ,
467
+ 'operation ' => self ::SERVICE_NAME . 'Get ' ,
468
+ ],
469
+ ];
470
+
471
+ return $ this ->_webApiCall ($ serviceInfo , ['sku ' => $ sku ], null , $ storeCode );
472
+ }
384
473
}
0 commit comments