Skip to content

Commit 9804cb1

Browse files
Merge branch '2.3-develop' of https://github.com/lisovyievhenii/magento2 into 2.3-develop
2 parents 544c7b6 + 70c5c51 commit 9804cb1

File tree

3 files changed

+106
-53
lines changed

3 files changed

+106
-53
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,7 @@ protected function initializeProductData(array $productData, $createNew)
334334
unset($productData['media_gallery']);
335335
if ($createNew) {
336336
$product = $this->productFactory->create();
337-
if ($this->storeManager->hasSingleStore()) {
338-
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsiteId()]);
339-
}
337+
$this->assignProductToWebsites($product);
340338
} else {
341339
$this->removeProductFromLocalCache($productData['sku']);
342340
$product = $this->get($productData['sku']);
@@ -345,31 +343,20 @@ protected function initializeProductData(array $productData, $createNew)
345343
foreach ($productData as $key => $value) {
346344
$product->setData($key, $value);
347345
}
348-
$this->assignProductToWebsites($product, $createNew);
349346

350347
return $product;
351348
}
352349

353350
/**
354351
* @param \Magento\Catalog\Model\Product $product
355-
* @param bool $createNew
356352
* @return void
357353
*/
358-
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product, $createNew)
354+
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product)
359355
{
360-
$websiteIds = $product->getWebsiteIds();
361-
362-
if (!$this->storeManager->hasSingleStore()) {
363-
$websiteIds = array_unique(
364-
array_merge(
365-
$websiteIds,
366-
[$this->storeManager->getStore()->getWebsiteId()]
367-
)
368-
);
369-
}
370-
371-
if ($createNew && $this->storeManager->getStore(true)->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) {
356+
if ($this->storeManager->getStore(true)->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) {
372357
$websiteIds = array_keys($this->storeManager->getWebsites());
358+
} else {
359+
$websiteIds = [$this->storeManager->getStore()->getWebsiteId()];
373360
}
374361

375362
$product->setWebsiteIds($websiteIds);

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,6 @@ public function testGetBySkuFromCacheInitializedInGetById()
553553

554554
public function testSaveExisting()
555555
{
556-
$this->storeManagerMock->expects($this->any())->method('getWebsites')->willReturn([1 => 'default']);
557556
$this->resourceModelMock->expects($this->any())->method('getIdBySku')->will($this->returnValue(100));
558557
$this->productFactoryMock->expects($this->any())
559558
->method('create')
@@ -566,7 +565,6 @@ public function testSaveExisting()
566565
->expects($this->once())
567566
->method('toNestedArray')
568567
->will($this->returnValue($this->productData));
569-
$this->productMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
570568
$this->productMock->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);
571569

572570
$this->assertEquals($this->productMock, $this->model->save($this->productMock));
@@ -588,7 +586,6 @@ public function testSaveNew()
588586
->expects($this->once())
589587
->method('toNestedArray')
590588
->will($this->returnValue($this->productData));
591-
$this->productMock->method('getWebsiteIds')->willReturn([]);
592589
$this->productMock->method('getSku')->willReturn('simple');
593590

594591
$this->assertEquals($this->productMock, $this->model->save($this->productMock));
@@ -615,7 +612,6 @@ public function testSaveUnableToSaveException()
615612
->expects($this->once())
616613
->method('toNestedArray')
617614
->will($this->returnValue($this->productData));
618-
$this->productMock->method('getWebsiteIds')->willReturn([]);
619615
$this->productMock->method('getSku')->willReturn('simple');
620616

621617
$this->model->save($this->productMock);
@@ -642,7 +638,6 @@ public function testSaveException()
642638
->expects($this->once())
643639
->method('toNestedArray')
644640
->will($this->returnValue($this->productData));
645-
$this->productMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
646641
$this->productMock->method('getSku')->willReturn('simple');
647642

648643
$this->model->save($this->productMock);
@@ -667,7 +662,6 @@ public function testSaveInvalidProductException()
667662
->expects($this->once())
668663
->method('toNestedArray')
669664
->will($this->returnValue($this->productData));
670-
$this->productMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
671665
$this->productMock->method('getSku')->willReturn('simple');
672666

673667
$this->model->save($this->productMock);
@@ -697,9 +691,6 @@ public function testSaveThrowsTemporaryStateExceptionIfDatabaseConnectionErrorOc
697691
->expects($this->once())
698692
->method('toNestedArray')
699693
->will($this->returnValue($this->productData));
700-
$this->productMock->expects($this->once())
701-
->method('getWebsiteIds')
702-
->willReturn([]);
703694
$this->productMock->method('getSku')->willReturn('simple');
704695

705696
$this->model->save($this->productMock);
@@ -847,7 +838,6 @@ public function testSaveExistingWithOptions(array $newOptions, array $existingOp
847838
->method('toNestedArray')
848839
->will($this->returnValue($this->productData));
849840

850-
$this->initializedProductMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
851841
$this->initializedProductMock->expects($this->atLeastOnce())
852842
->method('getSku')->willReturn($this->productData['sku']);
853843
$this->productMock->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);
@@ -1083,7 +1073,6 @@ public function testSaveWithLinks(array $newLinks, array $existingLinks, array $
10831073
$outputLinks[] = $outputLink;
10841074
}
10851075
}
1086-
$this->initializedProductMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
10871076

10881077
if (!empty($outputLinks)) {
10891078
$this->initializedProductMock->expects($this->once())
@@ -1264,7 +1253,6 @@ public function testSaveExistingWithNewMediaGalleryEntries()
12641253
'media_type' => 'media_type',
12651254
]
12661255
);
1267-
$this->initializedProductMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
12681256
$this->initializedProductMock->expects($this->atLeastOnce())
12691257
->method('getSku')->willReturn($this->productData['sku']);
12701258
$this->productMock->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);
@@ -1305,7 +1293,6 @@ public function testSaveWithDifferentWebsites()
13051293
2 => ['second'],
13061294
3 => ['third']
13071295
]);
1308-
$this->productMock->expects($this->once())->method('getWebsiteIds')->willReturn([1,2,3]);
13091296
$this->productMock->expects($this->once())->method('setWebsiteIds')->willReturn([2,3]);
13101297
$this->productMock->method('getSku')->willReturn('simple');
13111298

@@ -1377,7 +1364,6 @@ public function testSaveExistingWithMediaGalleryEntries()
13771364
$this->mediaGalleryProcessor->expects($this->once())
13781365
->method('setMediaAttribute')
13791366
->with($this->initializedProductMock, ['image', 'small_image'], 'filename1');
1380-
$this->initializedProductMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
13811367
$this->initializedProductMock->expects($this->atLeastOnce())
13821368
->method('getSku')->willReturn($this->productData['sku']);
13831369
$this->productMock->expects($this->atLeastOnce())->method('getSku')->willReturn($this->productData['sku']);

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php

Lines changed: 101 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
use Magento\Store\Model\Store;
1111
use Magento\CatalogInventory\Api\Data\StockItemInterface;
1212
use Magento\Store\Model\Website;
13+
use Magento\Store\Model\WebsiteRepository;
1314
use Magento\TestFramework\Helper\Bootstrap;
1415
use Magento\TestFramework\TestCase\WebapiAbstract;
1516
use Magento\Framework\Api\FilterBuilder;
1617
use Magento\Framework\Api\SearchCriteriaBuilder;
1718
use Magento\Framework\Api\SortOrder;
1819
use Magento\Framework\Api\SortOrderBuilder;
1920
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
21+
use Magento\Framework\Exception\NoSuchEntityException;
2022

2123
/**
2224
* @magentoAppIsolation enabled
@@ -136,6 +138,24 @@ public function productCreationProvider()
136138
];
137139
}
138140

141+
/**
142+
* Load website by website code
143+
*
144+
* @param $websiteCode
145+
* @return Website
146+
*/
147+
private function loadWebsiteByCode($websiteCode)
148+
{
149+
$websiteRepository = Bootstrap::getObjectManager()->get(WebsiteRepository::class);
150+
try {
151+
$website = $websiteRepository->get($websiteCode);
152+
} catch (NoSuchEntityException $e) {
153+
$this->fail("Couldn`t load website: {$websiteCode}");
154+
}
155+
156+
return $website;
157+
}
158+
139159
/**
140160
* Test removing association between product and website 1
141161
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
@@ -144,12 +164,7 @@ public function testUpdateWithDeleteWebsites()
144164
{
145165
$productBuilder[ProductInterface::SKU] = 'unique-simple-azaza';
146166
/** @var Website $website */
147-
$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(Website::class);
148-
$website->load('second_website', 'code');
149-
150-
if (!$website->getId()) {
151-
$this->fail("Couldn`t load website");
152-
}
167+
$website = $this->loadWebsiteByCode('second_website');
153168

154169
$websitesData = [
155170
'website_ids' => [
@@ -171,13 +186,6 @@ public function testUpdateWithDeleteWebsites()
171186
public function testDeleteAllWebsiteAssociations()
172187
{
173188
$productBuilder[ProductInterface::SKU] = 'unique-simple-azaza';
174-
/** @var Website $website */
175-
$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(Website::class);
176-
$website->load('second_website', 'code');
177-
178-
if (!$website->getId()) {
179-
$this->fail("Couldn`t load website");
180-
}
181189

182190
$websitesData = [
183191
'website_ids' => []
@@ -198,14 +206,9 @@ public function testCreateWithMultipleWebsites()
198206
$productBuilder = $this->getSimpleProductData();
199207
$productBuilder[ProductInterface::SKU] = 'test-test-sku';
200208
$productBuilder[ProductInterface::TYPE_ID] = 'simple';
201-
202209
/** @var Website $website */
203-
$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(Website::class);
204-
$website->load('test_website', 'code');
210+
$website = $this->loadWebsiteByCode('test_website');
205211

206-
if (!$website->getId()) {
207-
$this->fail("Couldn`t load website");
208-
}
209212
$websitesData = [
210213
'website_ids' => [
211214
1,
@@ -218,6 +221,84 @@ public function testCreateWithMultipleWebsites()
218221
$response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]["website_ids"],
219222
$websitesData["website_ids"]
220223
);
224+
$this->deleteProduct($productBuilder[ProductInterface::SKU]);
225+
}
226+
227+
/**
228+
* Add product associated with website that is not associated with default store
229+
*
230+
* @magentoApiDataFixture Magento/Store/_files/second_website_with_two_stores.php
231+
*/
232+
public function testCreateWithNonDefaultStoreWebsite()
233+
{
234+
$productBuilder = $this->getSimpleProductData();
235+
$productBuilder[ProductInterface::SKU] = 'test-sku-second-site-123';
236+
$productBuilder[ProductInterface::TYPE_ID] = 'simple';
237+
/** @var Website $website */
238+
$website = $this->loadWebsiteByCode('test');
239+
240+
$websitesData = [
241+
'website_ids' => [
242+
$website->getId(),
243+
]
244+
];
245+
$productBuilder[ProductInterface::EXTENSION_ATTRIBUTES_KEY] = $websitesData;
246+
$response = $this->saveProduct($productBuilder);
247+
$this->assertEquals(
248+
$websitesData["website_ids"],
249+
$response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]["website_ids"]
250+
);
251+
$this->deleteProduct($productBuilder[ProductInterface::SKU]);
252+
}
253+
254+
/**
255+
* Update product to be associated with website that is not associated with default store
256+
*
257+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
258+
* @magentoApiDataFixture Magento/Store/_files/second_website_with_two_stores.php
259+
*/
260+
public function testUpdateWithNonDefaultStoreWebsite()
261+
{
262+
$productBuilder[ProductInterface::SKU] = 'unique-simple-azaza';
263+
/** @var Website $website */
264+
$website = $this->loadWebsiteByCode('test');
265+
266+
$this->assertNotContains(Store::SCOPE_DEFAULT, $website->getStoreCodes());
267+
268+
$websitesData = [
269+
'website_ids' => [
270+
$website->getId(),
271+
]
272+
];
273+
$productBuilder[ProductInterface::EXTENSION_ATTRIBUTES_KEY] = $websitesData;
274+
$response = $this->updateProduct($productBuilder);
275+
$this->assertEquals(
276+
$websitesData["website_ids"],
277+
$response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]["website_ids"]
278+
);
279+
}
280+
281+
/**
282+
* Update product without specifying websites
283+
*
284+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
285+
*/
286+
public function testUpdateWithoutWebsiteIds()
287+
{
288+
$productBuilder[ProductInterface::SKU] = 'unique-simple-azaza';
289+
$originalProduct = $this->getProduct($productBuilder[ProductInterface::SKU]);
290+
$newName = 'Updated Product';
291+
292+
$productBuilder[ProductInterface::NAME] = $newName;
293+
$response = $this->updateProduct($productBuilder);
294+
$this->assertEquals(
295+
$newName,
296+
$response[ProductInterface::NAME]
297+
);
298+
$this->assertEquals(
299+
$originalProduct[ProductInterface::EXTENSION_ATTRIBUTES_KEY]["website_ids"],
300+
$response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]["website_ids"]
301+
);
221302
}
222303

223304
/**
@@ -727,8 +808,7 @@ public function testGetList()
727808
*/
728809
public function testGetListWithFilteringByWebsite()
729810
{
730-
$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(Website::class);
731-
$website->load('test', 'code');
811+
$website = $this->loadWebsiteByCode('test');
732812
$searchCriteria = [
733813
'searchCriteria' => [
734814
'filter_groups' => [

0 commit comments

Comments
 (0)