Skip to content

Commit

Permalink
12535: Product change sku via repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmalevanec committed Dec 8, 2017
1 parent 321278b commit 4996ea2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
11 changes: 8 additions & 3 deletions app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,13 @@ protected function initializeProductData(array $productData, $createNew)
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsiteId()]);
}
} else {
unset($this->instances[$productData['sku']]);
$product = $this->get($productData['sku']);
if (!empty($productData['id'])) {
unset($this->instancesById[$productData['id']]);
$product = $this->getById($productData['id']);
} else {
unset($this->instances[$productData['sku']]);
$product = $this->get($productData['sku']);
}
}

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

try {
$existingProduct = $this->get($product->getSku());
$existingProduct = $product->getId() ? $this->getById($product->getId()) : $this->get($product->getSku());

$product->setData(
$this->resourceModel->getLinkField(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ public function testSaveException()
->willReturn(true);
$this->resourceModelMock->expects($this->once())->method('save')->with($this->productMock)
->willThrowException(new \Magento\Eav\Model\Entity\Attribute\Exception(__('123')));
$this->productMock->expects($this->once())->method('getId')->willReturn(null);
$this->productMock->expects($this->exactly(2))->method('getId')->willReturn(null);
$this->extensibleDataObjectConverterMock
->expects($this->once())
->method('toNestedArray')
Expand All @@ -634,7 +634,7 @@ public function testSaveInvalidProductException()
$this->initializationHelperMock->expects($this->never())->method('initialize');
$this->resourceModelMock->expects($this->once())->method('validate')->with($this->productMock)
->willReturn(['error1', 'error2']);
$this->productMock->expects($this->never())->method('getId');
$this->productMock->expects($this->once())->method('getId')->willReturn(null);
$this->extensibleDataObjectConverterMock
->expects($this->once())
->method('toNestedArray')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Model;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
use Magento\TestFramework\Helper\Bootstrap;

/**
* Provide tests for ProductRepository model.
*/
class ProductRepositoryTest extends \PHPUnit\Framework\TestCase
{
/**
* Test subject.
*
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
}

/**
* Test Product Repository can change(update) "sku" for given product.
*
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDbIsolation enabled
* @magentoAppArea adminhtml
*/
public function testUpdateProductSku()
{
$newSku = 'simple-edited';
$productId = Bootstrap::getObjectManager()->get(ProductResource::class)->getIdBySku('simple');
$initialProduct = Bootstrap::getObjectManager()->create(Product::class)->load($productId);

$initialProduct->setSku($newSku);
$this->productRepository->save($initialProduct);

$updatedProduct = Bootstrap::getObjectManager()->create(Product::class);
$updatedProduct->load($productId);
self::assertSame($newSku, $updatedProduct->getSku());

//clean up.
$this->productRepository->delete($updatedProduct);
}
}

0 comments on commit 4996ea2

Please sign in to comment.