Skip to content

Commit a039ff4

Browse files
author
vadim
committed
7720
1 parent c2e2646 commit a039ff4

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
use Magento\Framework\Exception\ValidatorException;
3131

3232
/**
33-
* Product Repository.
33+
* @inheritdoc
34+
*
3435
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3536
* @SuppressWarnings(PHPMD.TooManyFields)
3637
*/
@@ -543,7 +544,9 @@ public function save(ProductInterface $product, $saveOptions = false)
543544
if (!$ignoreLinksFlag && $ignoreLinksFlag !== null) {
544545
$productLinks = $product->getProductLinks();
545546
}
546-
$productDataArray['store_id'] = (int)$this->storeManager->getStore()->getId();
547+
if (!isset($productDataArray['store_id'])) {
548+
$productDataArray['store_id'] = (int) $this->storeManager->getStore()->getId();
549+
}
547550
$product = $this->initializeProductData($productDataArray, empty($existingProduct));
548551

549552
$this->processLinks($product, $productLinks);
@@ -735,6 +738,7 @@ private function getCollectionProcessor()
735738
{
736739
if (!$this->collectionProcessor) {
737740
$this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
741+
// phpstan:ignore "Class Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor not found."
738742
\Magento\Catalog\Model\Api\SearchCriteria\ProductCollectionProcessor::class
739743
);
740744
}

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

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
use PHPUnit_Framework_MockObject_MockObject as MockObject;
4747

4848
/**
49-
* Class ProductRepositoryTest
50-
* @package Magento\Catalog\Test\Unit\Model
49+
* Test for \Magento\Catalog\Model\ProductRepository.
50+
*
5151
* @SuppressWarnings(PHPMD.TooManyFields)
5252
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
5353
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -291,6 +291,7 @@ protected function setUp()
291291
->disableOriginalConstructor()
292292
->setMethods([])
293293
->getMockForAbstractClass();
294+
$storeMock->method('getId')->willReturn('1');
294295
$storeMock->expects($this->any())->method('getWebsiteId')->willReturn('1');
295296
$storeMock->expects($this->any())->method('getCode')->willReturn(Store::ADMIN_CODE);
296297
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeMock);
@@ -345,6 +346,66 @@ function ($value) {
345346
$this->objectManager->setBackwardCompatibleProperty($this->model, 'mediaProcessor', $mediaProcessor);
346347
}
347348

349+
/**
350+
* Test save product with store id 0
351+
*
352+
* @param array $productData
353+
* @return void
354+
* @dataProvider getProductData
355+
*/
356+
public function testSaveForAllStoreViewScope(array $productData): void
357+
{
358+
$this->productFactory->method('create')->willReturn($this->product);
359+
$this->product->method('getSku')->willReturn($productData['sku']);
360+
$this->extensibleDataObjectConverter
361+
->expects($this->once())
362+
->method('toNestedArray')
363+
->willReturn($productData);
364+
$this->resourceModel->method('getIdBySku')->willReturn(100);
365+
$this->resourceModel->expects($this->once())->method('validate')->willReturn(true);
366+
$this->product->expects($this->at(14))->method('setData')->with('store_id', $productData['store_id']);
367+
368+
$this->model->save($this->product);
369+
}
370+
371+
/**
372+
* Product data provider
373+
*
374+
* @return array
375+
*/
376+
public function getProductData(): array
377+
{
378+
return [
379+
[
380+
[
381+
'sku' => 'sku',
382+
'name' => 'product',
383+
'store_id' => 0,
384+
],
385+
],
386+
];
387+
}
388+
389+
/**
390+
* Test save product without store
391+
*
392+
* @return void
393+
*/
394+
public function testSaveWithoutStoreId(): void
395+
{
396+
$this->productFactory->method('create')->willReturn($this->product);
397+
$this->product->method('getSku')->willReturn($this->productData['sku']);
398+
$this->extensibleDataObjectConverter
399+
->expects($this->once())
400+
->method('toNestedArray')
401+
->willReturn($this->productData);
402+
$this->resourceModel->method('getIdBySku')->willReturn(100);
403+
$this->resourceModel->expects($this->once())->method('validate')->willReturn(true);
404+
$this->product->expects($this->at(15))->method('setData')->with('store_id', 1);
405+
406+
$this->model->save($this->product);
407+
}
408+
348409
/**
349410
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
350411
* @expectedExceptionMessage The product that was requested doesn't exist. Verify the product and try again.

dev/tests/integration/testsuite/Magento/Catalog/Model/ProductRepositoryTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,24 @@ public function testCustomLayout(): void
233233
}
234234
$this->assertTrue($caughtException);
235235
}
236+
237+
/**
238+
* Tests product repository update should use provided store code.
239+
*
240+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
241+
*/
242+
public function testProductUpdate(): void
243+
{
244+
$sku = 'simple';
245+
$nameUpdated = 'updated';
246+
247+
$product = $this->productRepository->get($sku, false, 0);
248+
$product->setName($nameUpdated);
249+
$this->productRepository->save($product);
250+
$product = $this->productRepository->get($sku, false, 0);
251+
$this->assertEquals(
252+
$nameUpdated,
253+
$product->getName()
254+
);
255+
}
236256
}

0 commit comments

Comments
 (0)