Skip to content

Commit 96ca5e0

Browse files
committed
Merge branch 'MAGETWO-80502-2' into 2.1.13-PR-0.4
2 parents ec3eeb5 + 6b92366 commit 96ca5e0

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,27 +292,34 @@ protected function initializeProductData(array $productData, $createNew)
292292
foreach ($productData as $key => $value) {
293293
$product->setData($key, $value);
294294
}
295-
$this->assignProductToWebsites($product);
295+
$this->assignProductToWebsites($product, $createNew);
296296

297297
return $product;
298298
}
299299

300300
/**
301301
* @param \Magento\Catalog\Model\Product $product
302+
* @param bool $createNew
302303
* @return void
303304
*/
304-
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product)
305+
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product, $createNew)
305306
{
306-
if (!$this->storeManager->hasSingleStore()) {
307-
308-
if ($this->storeManager->getStore()->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) {
309-
$websiteIds = array_keys($this->storeManager->getWebsites());
310-
} else {
311-
$websiteIds = [$this->storeManager->getStore()->getWebsiteId()];
312-
}
307+
$websiteIds = $product->getWebsiteIds();
308+
309+
if ($createNew && !$this->storeManager->hasSingleStore()) {
310+
$websiteIds = array_unique(
311+
array_merge(
312+
$websiteIds,
313+
[$this->storeManager->getStore()->getWebsiteId()]
314+
)
315+
);
316+
}
313317

314-
$product->setWebsiteIds(array_unique(array_merge($product->getWebsiteIds(), $websiteIds)));
318+
if ($createNew && $this->storeManager->getStore(true)->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) {
319+
$websiteIds = array_keys($this->storeManager->getWebsites());
315320
}
321+
322+
$product->setWebsiteIds($websiteIds);
316323
}
317324

318325
/**

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
1717
use Magento\Framework\Api\SortOrder;
1818
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
19+
use Magento\Store\Api\Data\StoreInterface;
1920

2021
/**
2122
* Tests \Magento\Catalog\Model\ProductRepositoryTest
@@ -1290,6 +1291,45 @@ public function testSaveExistingWithNewMediaGalleryEntries()
12901291
$this->model->save($this->productMock);
12911292
}
12921293

1294+
public function testSaveWithDifferentWebsites()
1295+
{
1296+
$getWebsitesResultData = [
1297+
1 => ['first'],
1298+
2 => ['second'],
1299+
3 => ['third']
1300+
];
1301+
$getWebsiteIdsResultData = [1,2,3];
1302+
$setWebsiteIdsResultData = [2,3];
1303+
$getIdBySkuResultData = 100;
1304+
$storeMock = $this->getMock(StoreInterface::class);
1305+
$this->resourceModelMock->expects($this->at(0))->method('getIdBySku')->will($this->returnValue(null));
1306+
$this->resourceModelMock
1307+
->expects($this->at(3))
1308+
->method('getIdBySku')
1309+
->will($this->returnValue($getIdBySkuResultData));
1310+
$this->productFactoryMock->expects($this->any())
1311+
->method('create')
1312+
->will($this->returnValue($this->productMock));
1313+
$this->initializationHelperMock->expects($this->never())->method('initialize');
1314+
$this->resourceModelMock->expects($this->once())->method('validate')->with($this->productMock)
1315+
->willReturn(true);
1316+
$this->resourceModelMock->expects($this->once())->method('save')->with($this->productMock)->willReturn(true);
1317+
$this->extensibleDataObjectConverterMock
1318+
->expects($this->once())
1319+
->method('toNestedArray')
1320+
->will($this->returnValue($this->productData));
1321+
$this->storeManagerMock->expects($this->any())
1322+
->method('getStore')
1323+
->willReturn($storeMock);
1324+
$this->storeManagerMock->expects($this->once())
1325+
->method('getWebsites')
1326+
->willReturn($getWebsitesResultData);
1327+
$this->productMock->expects($this->once())->method('getWebsiteIds')->willReturn($getWebsiteIdsResultData);
1328+
$this->productMock->expects($this->once())->method('setWebsiteIds')->willReturn($setWebsiteIdsResultData);
1329+
1330+
$this->assertEquals($this->productMock, $this->model->save($this->productMock));
1331+
}
1332+
12931333
public function testSaveExistingWithMediaGalleryEntries()
12941334
{
12951335
//update one entry, delete one entry

0 commit comments

Comments
 (0)