7
7
8
8
namespace Magento \Catalog \Model ;
9
9
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
10
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
12
use Magento \Catalog \Model \Product \Media \ConfigInterface ;
12
13
use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
13
14
use Magento \Framework \Api \SearchCriteriaBuilder ;
15
+ use Magento \Framework \App \Config \ReinitableConfigInterface ;
16
+ use Magento \Framework \App \Config \Value ;
14
17
use Magento \Framework \App \Filesystem \DirectoryList ;
15
18
use Magento \Framework \Exception \CouldNotSaveException ;
16
19
use Magento \Framework \Exception \InputException ;
20
23
use Magento \Framework \Filesystem ;
21
24
use Magento \Framework \Filesystem \Directory \WriteInterface ;
22
25
use Magento \Framework \ObjectManagerInterface ;
26
+ use Magento \Store \Model \ScopeInterface ;
27
+ use Magento \Store \Model \Store ;
28
+ use Magento \Store \Model \StoreManagerInterface ;
23
29
use Magento \TestFramework \Catalog \Model \ProductLayoutUpdateManager ;
30
+ use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
24
31
use Magento \TestFramework \Helper \Bootstrap ;
25
32
use PHPUnit \Framework \TestCase ;
26
33
@@ -86,6 +93,21 @@ class ProductRepositoryTest extends TestCase
86
93
*/
87
94
private $ productSkusToDelete = [];
88
95
96
+ /**
97
+ * @var \Magento\TestFramework\Fixture\DataFixtureStorage
98
+ */
99
+ private $ fixtures ;
100
+
101
+ /**
102
+ * @var StoreManagerInterface
103
+ */
104
+ private $ storeManager ;
105
+
106
+ /**
107
+ * @var int
108
+ */
109
+ private $ currentStore ;
110
+
89
111
/**
90
112
* @inheritdoc
91
113
*/
@@ -109,6 +131,9 @@ protected function setUp(): void
109
131
$ this ->mediaConfig = $ this ->objectManager ->get (ConfigInterface::class);
110
132
$ this ->mediaDirectory = $ this ->objectManager ->get (Filesystem::class)
111
133
->getDirectoryWrite (DirectoryList::MEDIA );
134
+ $ this ->fixtures = $ this ->objectManager ->get (DataFixtureStorageManager::class)->getStorage ();
135
+ $ this ->storeManager = $ this ->objectManager ->create (StoreManagerInterface::class);
136
+ $ this ->currentStore = $ this ->storeManager ->getStore ()->getId ();
112
137
}
113
138
114
139
/**
@@ -124,6 +149,7 @@ protected function tearDown(): void
124
149
}
125
150
}
126
151
152
+ $ this ->storeManager ->setCurrentStore ($ this ->currentStore );
127
153
parent ::tearDown ();
128
154
}
129
155
@@ -330,4 +356,126 @@ public function productUpdateDataProvider(): array
330
356
],
331
357
];
332
358
}
359
+
360
+ /**
361
+ * @magentoDataFixture Magento\Store\Test\Fixture\Website as:website2
362
+ * @magentoDataFixture Magento\Store\Test\Fixture\Group with:{"website_id":"$website2.id$"} as:store_group2
363
+ * @magentoDataFixture Magento\Store\Test\Fixture\Store with:{"store_group_id":"$store_group2.id$"} as:store2
364
+ * @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"website_ids":[1,"$website2.id$"]} as:product1
365
+ * @magentoDataFixture Magento\Catalog\Test\Fixture\Product with:{"website_ids":[1,"$website2.id$"]} as:product2
366
+ * @magentoDataFixture setPriceScopeToWebsite
367
+ * @magentoDbIsolation disabled
368
+ * @magentoAppArea adminhtml
369
+ */
370
+ public function testConsecutivePartialProductsUpdateInStoreView (): void
371
+ {
372
+ $ store1 = $ this ->storeManager ->getStore ('default ' )->getId ();
373
+ $ store2 = $ this ->fixtures ->get ('store2 ' )->getId ();
374
+ $ product1 = $ this ->fixtures ->get ('product1 ' );
375
+ $ product2 = $ this ->fixtures ->get ('product2 ' );
376
+ $ product1Store1Name = $ product1 ->getName ();
377
+ $ product2Store1Name = $ product2 ->getName ();
378
+ $ product2Store1Price = 10 ;
379
+
380
+ $ product1Store2Name = $ product1 ->getName () . ' Store View Value ' ;
381
+ $ product2Store2Name = $ product2 ->getName () . ' Store View Value ' ;
382
+ $ product2Store2Price = 9 ;
383
+
384
+ $ this ->storeManager ->setCurrentStore ($ store2 );
385
+ $ this ->productRepository ->save (
386
+ $ this ->getProductInstance (
387
+ [
388
+ 'sku ' => $ product2 ->getSku (),
389
+ 'price ' => $ product2Store2Price ,
390
+ ]
391
+ )
392
+ );
393
+ $ this ->productRepository ->save (
394
+ $ this ->getProductInstance (
395
+ [
396
+ 'sku ' => $ product1 ->getSku (),
397
+ 'name ' => $ product1Store2Name ,
398
+ ]
399
+ )
400
+ );
401
+ $ this ->productRepository ->save (
402
+ $ this ->getProductInstance (
403
+ [
404
+ 'sku ' => $ product2 ->getSku (),
405
+ 'name ' => $ product2Store2Name ,
406
+ ]
407
+ )
408
+ );
409
+ $ product1 = $ this ->productRepository ->get ($ product1 ->getSku (), true , $ store2 , true );
410
+ $ product2 = $ this ->productRepository ->get ($ product2 ->getSku (), true , $ store2 , true );
411
+ $ this ->assertEquals ($ product1Store2Name , $ product1 ->getName ());
412
+ $ this ->assertEquals ($ product2Store2Name , $ product2 ->getName ());
413
+ $ this ->assertEquals ($ product2Store2Price , $ product2 ->getPrice ());
414
+
415
+ $ this ->storeManager ->setCurrentStore ($ store1 );
416
+
417
+ $ product1 = $ this ->productRepository ->get ($ product1 ->getSku (), true , $ store1 , true );
418
+ $ product2 = $ this ->productRepository ->get ($ product2 ->getSku (), true , $ store1 , true );
419
+ $ this ->assertEquals ($ product1Store1Name , $ product1 ->getName ());
420
+ $ this ->assertEquals ($ product2Store1Name , $ product2 ->getName ());
421
+ $ this ->assertEquals ($ product2Store1Price , $ product2 ->getPrice ());
422
+ }
423
+
424
+ /**
425
+ * Get Simple Product Data
426
+ *
427
+ * @param array $data
428
+ * @return ProductInterface
429
+ */
430
+ private function getProductInstance (array $ data = []): ProductInterface
431
+ {
432
+ return $ this ->objectManager ->create (
433
+ ProductInterface::class,
434
+ [
435
+ 'data ' => $ data
436
+ ]
437
+ );
438
+ }
439
+
440
+ public static function setPriceScopeToWebsite (): void
441
+ {
442
+ self ::setConfig (['catalog/price/scope ' => 1 ]);
443
+ }
444
+
445
+ public static function setPriceScopeToWebsiteRollback (): void
446
+ {
447
+ self ::setConfig (['catalog/price/scope ' => null ]);
448
+ }
449
+
450
+ /**
451
+ * @param array $config
452
+ * @return void
453
+ */
454
+ private static function setConfig (array $ config ): void
455
+ {
456
+ $ objectManager = Bootstrap::getObjectManager ();
457
+ $ configFactory = $ objectManager ->create (\Magento \Config \Model \Config \Factory::class);
458
+ foreach ($ config as $ path => $ value ) {
459
+ $ inherit = $ value === null ;
460
+ $ pathParts = explode ('/ ' , $ path );
461
+ $ store = 0 ;
462
+ $ configData = [
463
+ 'section ' => $ pathParts [0 ],
464
+ 'website ' => '' ,
465
+ 'store ' => $ store ,
466
+ 'groups ' => [
467
+ $ pathParts [1 ] => [
468
+ 'fields ' => [
469
+ $ pathParts [2 ] => [
470
+ 'value ' => $ value ,
471
+ 'inherit ' => $ inherit
472
+ ]
473
+ ]
474
+ ]
475
+ ]
476
+ ];
477
+
478
+ $ configFactory ->create (['data ' => $ configData ])->save ();
479
+ }
480
+ }
333
481
}
0 commit comments