Skip to content

Commit 30ae9f2

Browse files
authored
ENGCOM-6698: #7720 #26470
2 parents 0e90a71 + c594093 commit 30ae9f2

File tree

4 files changed

+142
-3
lines changed

4 files changed

+142
-3
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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,20 @@
4848
use PHPUnit\Framework\TestCase;
4949

5050
/**
51+
* Test for \Magento\Catalog\Model\ProductRepository.
52+
*
5153
* @SuppressWarnings(PHPMD.TooManyFields)
5254
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
5355
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
5456
*/
5557
class ProductRepositoryTest extends TestCase
5658
{
59+
private const STUB_STORE_ID = 1;
60+
private const STUB_STORE_ID_GLOBAL = 0;
61+
private const STUB_PRODUCT_ID = 100;
62+
private const STUB_PRODUCT_NAME = 'name';
63+
private const STUB_PRODUCT_SKU = 'sku';
64+
5765
/**
5866
* @var Product|MockObject
5967
*/
@@ -298,6 +306,7 @@ protected function setUp(): void
298306
->disableOriginalConstructor()
299307
->setMethods([])
300308
->getMockForAbstractClass();
309+
$storeMock->method('getId')->willReturn(self::STUB_STORE_ID);
301310
$storeMock->expects($this->any())->method('getWebsiteId')->willReturn('1');
302311
$storeMock->expects($this->any())->method('getCode')->willReturn(Store::ADMIN_CODE);
303312
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeMock);
@@ -351,6 +360,72 @@ function ($value) {
351360
$this->objectManager->setBackwardCompatibleProperty($this->model, 'mediaProcessor', $mediaProcessor);
352361
}
353362

363+
/**
364+
* Test save product with global store id
365+
*
366+
* @param array $productData
367+
* @return void
368+
* @dataProvider getProductData
369+
*/
370+
public function testSaveForAllStoreViewScope(array $productData): void
371+
{
372+
$this->productFactory->method('create')->willReturn($this->product);
373+
$this->product->method('getSku')->willReturn($productData['sku']);
374+
$this->extensibleDataObjectConverter
375+
->expects($this->once())
376+
->method('toNestedArray')
377+
->willReturn($productData);
378+
$this->resourceModel->method('getIdBySku')->willReturn(self::STUB_PRODUCT_ID);
379+
$this->resourceModel->expects($this->once())->method('validate')->willReturn(true);
380+
$this->product->expects($this->at(14))->method('setData')
381+
->with('store_id', $productData['store_id']);
382+
383+
$this->model->save($this->product);
384+
}
385+
386+
/**
387+
* Product data provider
388+
*
389+
* @return array
390+
*/
391+
public function getProductData(): array
392+
{
393+
return [
394+
[
395+
[
396+
'sku' => self::STUB_PRODUCT_SKU,
397+
'name' => self::STUB_PRODUCT_NAME,
398+
'store_id' => self::STUB_STORE_ID_GLOBAL,
399+
],
400+
],
401+
];
402+
}
403+
404+
/**
405+
* Test save product without store
406+
*
407+
* @return void
408+
*/
409+
public function testSaveWithoutStoreId(): void
410+
{
411+
$this->productFactory->method('create')->willReturn($this->product);
412+
$this->product->method('getSku')->willReturn($this->productData['sku']);
413+
$this->extensibleDataObjectConverter
414+
->expects($this->once())
415+
->method('toNestedArray')
416+
->willReturn($this->productData);
417+
$this->resourceModel->method('getIdBySku')->willReturn(self::STUB_PRODUCT_ID);
418+
$this->resourceModel->expects($this->once())->method('validate')->willReturn(true);
419+
$this->product->expects($this->at(15))->method('setData')
420+
->with('store_id', self::STUB_STORE_ID);
421+
422+
$this->model->save($this->product);
423+
}
424+
425+
/**
426+
* @expectedException \Magento\Framework\Exception\NoSuchEntityException
427+
* @expectedExceptionMessage The product that was requested doesn't exist. Verify the product and try again.
428+
*/
354429
public function testGetAbsentProduct()
355430
{
356431
$this->expectException('Magento\Framework\Exception\NoSuchEntityException');

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
*/
3434
class ProductRepositoryTest extends TestCase
3535
{
36+
private const STUB_STORE_ID = 1;
37+
private const STUB_STORE_ID_GLOBAL = 0;
38+
private const STUB_PRODUCT_NAME = 'Simple Product';
39+
private const STUB_UPDATED_PRODUCT_NAME = 'updated';
40+
private const STUB_PRODUCT_SKU = 'simple';
41+
3642
/**
3743
* @var ObjectManagerInterface
3844
*/
@@ -273,4 +279,55 @@ private function assertProductNotExist(string $sku): void
273279
));
274280
$this->productRepository->get($sku);
275281
}
282+
283+
/**
284+
* Tests product repository update
285+
*
286+
* @dataProvider productUpdateDataProvider
287+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
288+
* @param int $storeId
289+
* @param int $checkStoreId
290+
* @param string $expectedNameStore
291+
* @param string $expectedNameCheckedStore
292+
*/
293+
public function testProductUpdate(
294+
int $storeId,
295+
int $checkStoreId,
296+
string $expectedNameStore,
297+
string $expectedNameCheckedStore
298+
): void {
299+
$sku = self::STUB_PRODUCT_SKU;
300+
301+
$product = $this->productRepository->get($sku, false, $storeId);
302+
$product->setName(self::STUB_UPDATED_PRODUCT_NAME);
303+
$this->productRepository->save($product);
304+
$productNameStoreId = $this->productRepository->get($sku, false, $storeId)->getName();
305+
$productNameCheckedStoreId = $this->productRepository->get($sku, false, $checkStoreId)->getName();
306+
307+
$this->assertEquals($expectedNameStore, $productNameStoreId);
308+
$this->assertEquals($expectedNameCheckedStore, $productNameCheckedStoreId);
309+
}
310+
311+
/**
312+
* Product update data provider
313+
*
314+
* @return array
315+
*/
316+
public function productUpdateDataProvider(): array
317+
{
318+
return [
319+
'Updating for global store' => [
320+
self::STUB_STORE_ID_GLOBAL,
321+
self::STUB_STORE_ID,
322+
self::STUB_UPDATED_PRODUCT_NAME,
323+
self::STUB_UPDATED_PRODUCT_NAME,
324+
],
325+
'Updating for store' => [
326+
self::STUB_STORE_ID,
327+
self::STUB_STORE_ID_GLOBAL,
328+
self::STUB_UPDATED_PRODUCT_NAME,
329+
self::STUB_PRODUCT_NAME,
330+
],
331+
];
332+
}
276333
}

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Block/Product/View/Type/MultiStoreConfigurableViewOnProductPageTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ private function prepareConfigurableProduct(string $sku, string $storeCode): voi
184184
{
185185
$product = $this->productRepository->get($sku, false, null, true);
186186
$productToUpdate = $product->getTypeInstance()->getUsedProductCollection($product)
187-
->setPageSize(1)->getFirstItem();
187+
->addStoreFilter($storeCode)
188+
->setPageSize(1)
189+
->getFirstItem();
190+
188191
$this->assertNotEmpty($productToUpdate->getData(), 'Configurable product does not have a child');
189192
$this->executeInStoreContext->execute($storeCode, [$this, 'setProductDisabled'], $productToUpdate);
190193
}

0 commit comments

Comments
 (0)