Skip to content

Commit ec1f9cb

Browse files
author
Oleksii Korshenko
committed
MAGETWO-85294: 12535: Product change sku via repository. #984
- Merge Pull Request magento-engcom/magento2ce#984 from nmalevanec/magento2:12535 - Merged commits: 1. 4996ea2 2. 390d751
2 parents a1ee98a + 390d751 commit ec1f9cb

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,13 @@ protected function initializeProductData(array $productData, $createNew)
333333
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsiteId()]);
334334
}
335335
} else {
336-
unset($this->instances[$productData['sku']]);
337-
$product = $this->get($productData['sku']);
336+
if (!empty($productData['id'])) {
337+
unset($this->instancesById[$productData['id']]);
338+
$product = $this->getById($productData['id']);
339+
} else {
340+
unset($this->instances[$productData['sku']]);
341+
$product = $this->get($productData['sku']);
342+
}
338343
}
339344

340345
foreach ($productData as $key => $value) {
@@ -562,7 +567,7 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
562567
$tierPrices = $product->getData('tier_price');
563568

564569
try {
565-
$existingProduct = $this->get($product->getSku());
570+
$existingProduct = $product->getId() ? $this->getById($product->getId()) : $this->get($product->getSku());
566571

567572
$product->setData(
568573
$this->resourceModel->getLinkField(),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ public function testSaveException()
610610
->willReturn(true);
611611
$this->resourceModelMock->expects($this->once())->method('save')->with($this->productMock)
612612
->willThrowException(new \Magento\Eav\Model\Entity\Attribute\Exception(__('123')));
613-
$this->productMock->expects($this->once())->method('getId')->willReturn(null);
613+
$this->productMock->expects($this->exactly(2))->method('getId')->willReturn(null);
614614
$this->extensibleDataObjectConverterMock
615615
->expects($this->once())
616616
->method('toNestedArray')
@@ -634,7 +634,7 @@ public function testSaveInvalidProductException()
634634
$this->initializationHelperMock->expects($this->never())->method('initialize');
635635
$this->resourceModelMock->expects($this->once())->method('validate')->with($this->productMock)
636636
->willReturn(['error1', 'error2']);
637-
$this->productMock->expects($this->never())->method('getId');
637+
$this->productMock->expects($this->once())->method('getId')->willReturn(null);
638638
$this->extensibleDataObjectConverterMock
639639
->expects($this->once())
640640
->method('toNestedArray')
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model;
8+
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
/**
14+
* Provide tests for ProductRepository model.
15+
*/
16+
class ProductRepositoryTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* Test subject.
20+
*
21+
* @var ProductRepositoryInterface
22+
*/
23+
private $productRepository;
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
protected function setUp()
29+
{
30+
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
31+
}
32+
33+
/**
34+
* Test Product Repository can change(update) "sku" for given product.
35+
*
36+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
37+
* @magentoAppArea adminhtml
38+
*/
39+
public function testUpdateProductSku()
40+
{
41+
$newSku = 'simple-edited';
42+
$productId = Bootstrap::getObjectManager()->get(ProductResource::class)->getIdBySku('simple');
43+
$initialProduct = Bootstrap::getObjectManager()->create(Product::class)->load($productId);
44+
45+
$initialProduct->setSku($newSku);
46+
$this->productRepository->save($initialProduct);
47+
48+
$updatedProduct = Bootstrap::getObjectManager()->create(Product::class);
49+
$updatedProduct->load($productId);
50+
self::assertSame($newSku, $updatedProduct->getSku());
51+
}
52+
}

0 commit comments

Comments
 (0)