Skip to content

Commit 4f98ae4

Browse files
committed
MAGETWO-45142: product API with "all" store code does not work in RC 2.0 after all dependent tickets are fixed
1 parent c38de7c commit 4f98ae4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,29 @@ protected function initializeProductData(array $productData, $createNew)
301301
foreach ($productData as $key => $value) {
302302
$product->setData($key, $value);
303303
}
304+
$this->assignProductToWebsites($product);
304305

305306
return $product;
306307
}
307308

309+
/**
310+
* @param \Magento\Catalog\Model\Product $product
311+
* @return void
312+
*/
313+
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product)
314+
{
315+
if (!$this->storeManager->hasSingleStore()) {
316+
317+
if ($this->storeManager->getStore()->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) {
318+
$websiteIds = array_keys($this->storeManager->getWebsites());
319+
} else {
320+
$websiteIds = [$this->storeManager->getStore()->getWebsiteId()];
321+
}
322+
323+
$product->setWebsiteIds(array_unique(array_merge($product->getWebsiteIds(), $websiteIds)));
324+
}
325+
}
326+
308327
/**
309328
* Process product options, creating new options, updating and deleting existing options
310329
*

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ protected function setUp()
146146
$this->initializedProductMock = $this->getMock(
147147
'Magento\Catalog\Model\Product',
148148
[
149+
'getWebsiteIds',
149150
'setProductOptions',
150151
'load',
151152
'getOptions',
@@ -444,6 +445,8 @@ public function testSaveExisting()
444445
->expects($this->once())
445446
->method('toNestedArray')
446447
->will($this->returnValue($this->productData));
448+
$this->productMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
449+
447450
$this->assertEquals($this->productMock, $this->model->save($this->productMock));
448451
}
449452

@@ -462,6 +465,8 @@ public function testSaveNew()
462465
->expects($this->once())
463466
->method('toNestedArray')
464467
->will($this->returnValue($this->productData));
468+
$this->productMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
469+
465470
$this->assertEquals($this->productMock, $this->model->save($this->productMock));
466471
}
467472

@@ -484,6 +489,8 @@ public function testSaveUnableToSaveException()
484489
->expects($this->once())
485490
->method('toNestedArray')
486491
->will($this->returnValue($this->productData));
492+
$this->productMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
493+
487494
$this->model->save($this->productMock);
488495
}
489496

@@ -507,6 +514,8 @@ public function testSaveException()
507514
->expects($this->once())
508515
->method('toNestedArray')
509516
->will($this->returnValue($this->productData));
517+
$this->productMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
518+
510519
$this->model->save($this->productMock);
511520
}
512521

@@ -528,6 +537,8 @@ public function testSaveInvalidProductException()
528537
->expects($this->once())
529538
->method('toNestedArray')
530539
->will($this->returnValue($this->productData));
540+
$this->productMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
541+
531542
$this->model->save($this->productMock);
532543
}
533544

@@ -732,6 +743,8 @@ public function testSaveExistingWithOptions(array $newOptions, array $existingOp
732743
->method('setProductOptions')
733744
->with($expectedData);
734745

746+
$this->initializedProductMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
747+
735748
$this->assertEquals($this->initializedProductMock, $this->model->save($this->productMock));
736749
}
737750

@@ -963,6 +976,7 @@ public function testSaveWithLinks(array $newLinks, array $existingLinks, array $
963976
$outputLinks[] = $outputLink;
964977
}
965978
}
979+
$this->initializedProductMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
966980

967981
$results = $this->model->save($this->initializedProductMock);
968982
$this->assertEquals($this->initializedProductMock, $results);
@@ -1131,6 +1145,7 @@ public function testSaveExistingWithNewMediaGalleryEntries()
11311145
'media_type' => 'media_type',
11321146
]
11331147
);
1148+
$this->initializedProductMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
11341149

11351150
$this->model->save($this->productMock);
11361151
}
@@ -1206,6 +1221,7 @@ public function testSaveExistingWithMediaGalleryEntries()
12061221
$galleryAttributeBackendMock->expects($this->once())
12071222
->method('setMediaAttribute')
12081223
->with($this->initializedProductMock, ['image', 'small_image'], 'filename1');
1224+
$this->initializedProductMock->expects($this->once())->method('getWebsiteIds')->willReturn([]);
12091225

12101226
$this->model->save($this->productMock);
12111227
$this->assertEquals($expectedResult, $this->initializedProductMock->getMediaGallery('images'));

0 commit comments

Comments
 (0)