9
9
use Magento \Catalog \Model \ResourceModel \Product ;
10
10
use Magento \Catalog \Observer \SwitchPriceAttributeScopeOnConfigChange ;
11
11
use Magento \Framework \App \Config \ReinitableConfigInterface ;
12
+ use Magento \Store \Model \Store ;
12
13
use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
13
14
14
15
/**
@@ -49,7 +50,7 @@ protected function setUp(): void
49
50
$ reinitiableConfig = $ this ->objectManager ->get (ReinitableConfigInterface::class);
50
51
$ reinitiableConfig ->setValue (
51
52
'catalog/price/scope ' ,
52
- \ Magento \ Store \ Model \ Store::PRICE_SCOPE_WEBSITE
53
+ Store::PRICE_SCOPE_WEBSITE
53
54
);
54
55
$ observer = $ this ->objectManager ->get (\Magento \Framework \Event \Observer::class);
55
56
$ this ->objectManager ->get (SwitchPriceAttributeScopeOnConfigChange::class)
@@ -113,8 +114,8 @@ public function testSetScope()
113
114
*/
114
115
public function testAfterSave ()
115
116
{
116
- /** @var \Magento\Store\Model\ Store $store */
117
- $ store = $ this ->objectManager ->create (\ Magento \ Store \ Model \ Store::class);
117
+ /** @var Store $store */
118
+ $ store = $ this ->objectManager ->create (Store::class);
118
119
$ globalStoreId = $ store ->load ('admin ' )->getId ();
119
120
$ product = $ this ->productRepository ->get ('simple ' );
120
121
$ product ->setPrice ('9.99 ' );
@@ -136,9 +137,9 @@ public function testAfterSave()
136
137
*/
137
138
public function testAfterSaveWithDifferentStores ()
138
139
{
139
- /** @var \Magento\Store\Model\ Store $store */
140
+ /** @var Store $store */
140
141
$ store = $ this ->objectManager ->create (
141
- \ Magento \ Store \ Model \ Store::class
142
+ Store::class
142
143
);
143
144
$ globalStoreId = $ store ->load ('admin ' )->getId ();
144
145
$ secondStoreId = $ this ->fixtures ->get ('store2 ' )->getId ();
@@ -175,9 +176,9 @@ public function testAfterSaveWithDifferentStores()
175
176
*/
176
177
public function testAfterSaveWithSameCurrency ()
177
178
{
178
- /** @var \Magento\Store\Model\ Store $store */
179
+ /** @var Store $store */
179
180
$ store = $ this ->objectManager ->create (
180
- \ Magento \ Store \ Model \ Store::class
181
+ Store::class
181
182
);
182
183
$ globalStoreId = $ store ->load ('admin ' )->getId ();
183
184
$ secondStoreId = $ store ->load ('fixture_second_store ' )->getId ();
@@ -214,9 +215,9 @@ public function testAfterSaveWithSameCurrency()
214
215
*/
215
216
public function testAfterSaveWithUseDefault ()
216
217
{
217
- /** @var \Magento\Store\Model\ Store $store */
218
+ /** @var Store $store */
218
219
$ store = $ this ->objectManager ->create (
219
- \ Magento \ Store \ Model \ Store::class
220
+ Store::class
220
221
);
221
222
$ globalStoreId = $ store ->load ('admin ' )->getId ();
222
223
$ secondStoreId = $ store ->load ('fixture_second_store ' )->getId ();
@@ -266,9 +267,9 @@ public function testAfterSaveWithUseDefault()
266
267
*/
267
268
public function testAfterSaveForWebsitesWithDifferentCurrencies ()
268
269
{
269
- /** @var \Magento\Store\Model\ Store $store */
270
+ /** @var Store $store */
270
271
$ store = $ this ->objectManager ->create (
271
- \ Magento \ Store \ Model \ Store::class
272
+ Store::class
272
273
);
273
274
274
275
/** @var \Magento\Directory\Model\ResourceModel\Currency $rate */
@@ -322,12 +323,153 @@ public static function tearDownAfterClass(): void
322
323
);
323
324
$ reinitiableConfig ->setValue (
324
325
'catalog/price/scope ' ,
325
- \ Magento \ Store \ Model \ Store::PRICE_SCOPE_GLOBAL
326
+ Store::PRICE_SCOPE_GLOBAL
326
327
);
327
328
$ observer = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (
328
329
\Magento \Framework \Event \Observer::class
329
330
);
330
331
\Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (SwitchPriceAttributeScopeOnConfigChange::class)
331
332
->execute ($ observer );
332
333
}
334
+
335
+ /**
336
+ * @magentoDataFixture Magento\Store\Test\Fixture\Website as:website2
337
+ * @magentoDataFixture Magento\Store\Test\Fixture\Group with:{"website_id":"$website2.id$"} as:store_group2
338
+ * @magentoDataFixture Magento\Store\Test\Fixture\Store with:{"store_group_id":"$store_group2.id$"} as:store2
339
+ * @magentoDataFixture Magento\Store\Test\Fixture\Store with:{"store_group_id":"$store_group2.id$"} as:store3
340
+ * @magentoDataFixture Magento\Catalog\Test\Fixture\Attribute as:attr1
341
+ * @magentoDataFixture Magento\Catalog\Test\Fixture\Attribute as:attr2
342
+ * @magentoDataFixture Magento\Catalog\Test\Fixture\Attribute as:attr3
343
+ * @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"website_ids":[1, "$website2.id"]} as:product
344
+ * @magentoDataFixtureDataProvider {"attr1":{"frontend_input":"price","is_filterable":1}}
345
+ * @magentoDataFixtureDataProvider {"attr2":{"frontend_input":"price","is_filterable":1}}
346
+ * @magentoDataFixtureDataProvider {"attr3":{"frontend_input":"price","is_filterable":1}}
347
+ * @magentoConfigFixture current_store catalog/price/scope 1
348
+ * @magentoDbIsolation disabled
349
+ * @magentoAppArea adminhtml
350
+ * @dataProvider saveCustomPriceAttributeDataProvider
351
+ */
352
+ public function testSaveCustomPriceAttribute (
353
+ array $ attributes ,
354
+ array $ updates ,
355
+ array $ expectedValues ,
356
+ array $ expectedIndexValues
357
+ ) {
358
+ $ storeIds ['admin ' ] = $ this ->objectManager ->create (Store::class)->load ('admin ' )->getId ();
359
+ $ storeIds ['default ' ] = $ this ->objectManager ->create (Store::class)->load ('default ' )->getId ();
360
+ $ storeIds ['store2 ' ] = $ this ->fixtures ->get ('store2 ' )->getId ();
361
+ $ storeIds ['store3 ' ] = $ this ->fixtures ->get ('store3 ' )->getId ();
362
+ $ storeNames = array_flip ($ storeIds );
363
+ $ productSku = $ this ->fixtures ->get ('product ' )->getSku ();
364
+ $ productId = $ this ->fixtures ->get ('product ' )->getId ();
365
+
366
+ foreach ($ updates as $ name => $ scopes ) {
367
+ $ attributeCode = $ this ->fixtures ->get ($ name )->getAttributeCode ();
368
+ foreach ($ scopes as $ storeName => $ storeValue ) {
369
+ $ product = $ this ->productRepository ->get ($ productSku , true , $ storeIds [$ storeName ], true );
370
+ $ product ->setData ($ attributeCode , $ storeValue );
371
+ $ this ->productResource ->save ($ product );
372
+ }
373
+ }
374
+
375
+ $ actualValues = [];
376
+ foreach ($ attributes as $ name ) {
377
+ $ attributeCode = $ this ->fixtures ->get ($ name )->getAttributeCode ();
378
+ foreach ($ storeIds as $ storeName => $ storeId ) {
379
+ $ product = $ this ->productRepository ->get ($ productSku , false , $ storeId , true );
380
+ $ actualValues [$ name ][$ storeName ] = $ product ->getData ($ attributeCode );
381
+ }
382
+ }
383
+
384
+ $ this ->assertEquals ($ expectedValues , $ actualValues );
385
+
386
+ $ connection = $ this ->productResource ->getConnection ();
387
+
388
+ $ actualIndexValues = [];
389
+ foreach ($ attributes as $ name ) {
390
+ $ attributeId = $ this ->fixtures ->get ($ name )->getId ();
391
+ $ select = $ connection ->select ()
392
+ ->from (
393
+ $ connection ->getTableName ('catalog_product_index_eav_decimal ' ),
394
+ [
395
+ 'store_id ' ,
396
+ 'value '
397
+ ]
398
+ )
399
+ ->where (
400
+ 'entity_id = ? ' ,
401
+ $ productId ,
402
+ )
403
+ ->where (
404
+ 'attribute_id = ? ' ,
405
+ $ attributeId
406
+ );
407
+ $ actualIndexValues [$ name ] = [];
408
+ foreach ($ connection ->fetchPairs ($ select ) as $ storeId => $ storeValue ) {
409
+ $ actualIndexValues [$ name ][$ storeNames [$ storeId ]] = $ storeValue ;
410
+ }
411
+ }
412
+
413
+ $ this ->assertEquals ($ expectedIndexValues , $ actualIndexValues );
414
+ }
415
+
416
+ /**
417
+ * @return array[]
418
+ */
419
+ public function saveCustomPriceAttributeDataProvider (): array
420
+ {
421
+ return [
422
+ [
423
+ 'attributes ' => ['attr1 ' , 'attr2 ' , 'attr3 ' ],
424
+ 'set ' => [
425
+ 'attr1 ' => [
426
+ 'admin ' => 9 ,
427
+ ],
428
+ 'attr2 ' => [
429
+ 'admin ' => 7 ,
430
+ 'store2 ' => 3.5 ,
431
+ ],
432
+ 'attr3 ' => [
433
+ 'store3 ' => 15 ,
434
+ ]
435
+ ],
436
+ 'expectedValues ' =>[
437
+ 'attr1 ' => [
438
+ 'admin ' => 9 ,
439
+ 'default ' => 9 ,
440
+ 'store2 ' => 9 ,
441
+ 'store3 ' => 9 ,
442
+ ],
443
+ 'attr2 ' => [
444
+ 'admin ' => 7 ,
445
+ 'default ' => 7 ,
446
+ 'store2 ' => 3.5 ,
447
+ 'store3 ' => 3.5 ,
448
+ ],
449
+ 'attr3 ' => [
450
+ 'admin ' => null ,
451
+ 'default ' => null ,
452
+ 'store2 ' => 15 ,
453
+ 'store3 ' => 15 ,
454
+ ]
455
+ ],
456
+ 'expectedIndexValues ' => [
457
+ 'attr1 ' => [
458
+ 'default ' => 9 ,
459
+ 'store2 ' => 9 ,
460
+ 'store3 ' => 9 ,
461
+ ],
462
+ 'attr2 ' => [
463
+ 'default ' => 7 ,
464
+ 'store2 ' => 3.5 ,
465
+ 'store3 ' => 3.5 ,
466
+ ],
467
+ 'attr3 ' => [
468
+ 'store2 ' => 15 ,
469
+ 'store3 ' => 15 ,
470
+ ]
471
+ ]
472
+ ]
473
+ ];
474
+ }
333
475
}
0 commit comments