Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

magento-engcom/import-export-improvements#101 Remove sku autogeneration on new products, keep on duplication #119

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions app/code/Magento/Catalog/Model/Product/Attribute/Backend/Sku.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Model\Product\Attribute\Backend;

use Magento\Catalog\Model\Product;

/**
* Catalog product SKU backend attribute model.
* Catalog product SKU backend attribute model
*
* @author Magento Core Team <[email protected]>
*/
class Sku extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
Expand Down Expand Up @@ -58,6 +59,15 @@ public function validate($object)
__('SKU length should be %1 characters maximum.', self::SKU_MAX_LENGTH)
);
}

$attribute = $this->getAttribute();
$entity = $attribute->getEntity();
if (!$entity->checkAttributeUniqueValue($attribute, $object)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('SKU is already in use.')
);
}

return true;
}

Expand Down Expand Up @@ -94,7 +104,9 @@ protected function _generateUniqueSku($object)
*/
public function beforeSave($object)
{
$this->_generateUniqueSku($object);
if ($object->getIsDuplicate()) {
$this->_generateUniqueSku($object);
}
$this->trimValue($object);
return parent::beforeSave($object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function __prepare(Category $category)
* Run create product simple entity test.
*
* @param CatalogProductSimple $product
* @param CatalogProductSimple $product2
* @param Category $category
* @param CatalogProductIndex $productGrid
* @param CatalogProductNew $newProductPage
Expand All @@ -77,6 +78,7 @@ public function __prepare(Category $category)
*/
public function testCreate(
CatalogProductSimple $product,
CatalogProductSimple $product2,
Category $category,
CatalogProductIndex $productGrid,
CatalogProductNew $newProductPage,
Expand All @@ -92,13 +94,15 @@ public function testCreate(
['configData' => $this->configData, 'flushCache' => $this->flushCache]
)->run();

for ($index = 0; $index < 2; $index++) {
// Duplicate product
$productGrid->open();
$productGrid->getGridPageActionBlock()->addProduct('simple');
$newProductPage->getProductForm()->fill($product, null, $category);
$newProductPage->getFormPageActions()->save();
}
$productGrid->open();
$productGrid->getGridPageActionBlock()->addProduct('simple');
$newProductPage->getProductForm()->fill($product, null, $category);
$newProductPage->getFormPageActions()->save();

$productGrid->open();
$productGrid->getGridPageActionBlock()->addProduct('simple');
$newProductPage->getProductForm()->fill($product2, null, $category);
$newProductPage->getFormPageActions()->save();

return ['product' => $product];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
<testCase name="Magento\CatalogUrlRewrite\Test\TestCase\CreateDuplicateUrlProductEntity" summary="Create Simple Product" ticketId="MAGETWO-69427">
<variation name="CreateDuplicateUrlProductEntityTestVariation1" summary="Create Duplicate Url Product">
<data name="tag" xsi:type="string">test_type:acceptance_test, test_type:extended_acceptance_test, severity:S1, mftf_migrated:yes</data>
<data name="product/data/url_key" xsi:type="string">simple-product-%isolation%</data>
<data name="product/data/url_key" xsi:type="string">simple-product-samekey</data>
<data name="product/data/name" xsi:type="string">Simple Product %isolation%</data>
<data name="product/data/sku" xsi:type="string">simple_sku_%isolation%</data>
<data name="product/data/price/value" xsi:type="string">10000</data>
<data name="product/data/weight" xsi:type="string">50</data>
<data name="product/data/quantity_and_stock_status/qty" xsi:type="string">657</data>
<data name="product2/data/url_key" xsi:type="string">simple-product-samekey</data>
<data name="product2/data/name" xsi:type="string">Simple Product %isolation%</data>
<data name="product2/data/sku" xsi:type="string">simple_sku_%isolation%</data>
<data name="product2/data/price/value" xsi:type="string">10000</data>
<data name="product2/data/weight" xsi:type="string">50</data>
<data name="product2/data/quantity_and_stock_status/qty" xsi:type="string">657</data>
<constraint name="Magento\CatalogUrlRewrite\Test\Constraint\AssertProductUrlDuplicateErrorMessage" />
</variation>
</testCase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,34 @@ class SkuTest extends \PHPUnit\Framework\TestCase
/**
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testGenerateUniqueSkuExistingProduct()
public function testGenerateUniqueSkuExistingProductDuplication()
{
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\ProductRepository::class
);
$product = $repository->get('simple');
$product->setId(null);
$product->setIsDuplicate(true);
$this->assertEquals('simple', $product->getSku());
$product->getResource()->getAttribute('sku')->getBackend()->beforeSave($product);
$this->assertEquals('simple-1', $product->getSku());
}

/**
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testGenerateUniqueSkuExistingProductNoDuplication()
{
$repository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\ProductRepository::class
);
$product = $repository->get('simple');
$product->setId(null);
$this->assertEquals('simple', $product->getSku());
$product->getResource()->getAttribute('sku')->getBackend()->beforeSave($product);
$this->assertEquals('simple', $product->getSku());
}

/**
* @param $product \Magento\Catalog\Model\Product
* @dataProvider uniqueSkuDataProvider
Expand All @@ -48,16 +64,15 @@ public function testGenerateUniqueLongSku()
\Magento\Catalog\Model\ProductRepository::class
);
$product = $repository->get('simple');
$product->setSku('0123456789012345678901234567890123456789012345678901234567890123');
$product->setSku('0123456789012345678901234567890123456789012345678901234567890123')->save();

/** @var \Magento\Catalog\Model\Product\Copier $copier */
$copier = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Catalog\Model\Product\Copier::class
);
$copier->copy($product);
$copy = $copier->copy($product);
$this->assertEquals('0123456789012345678901234567890123456789012345678901234567890123', $product->getSku());
$product->getResource()->getAttribute('sku')->getBackend()->beforeSave($product);
$this->assertEquals('01234567890123456789012345678901234567890123456789012345678901-1', $product->getSku());
$this->assertEquals('01234567890123456789012345678901234567890123456789012345678901-1', $copy->getSku());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ protected function prepareProducts()

$this->product->setStoreId(0)->setData('test_attribute', 'test_attribute_value')->save();
$this->productSecond = clone $this->product;
$this->productSecond->setId(null)->setUrlKey('product-second')->save();
$this->productSecond->setId(null)->setUrlKey('product-second')->setIsDuplicate(true)->save();
$this->productThird = clone $this->product;
$this->productThird->setId(null)
->setUrlKey('product-third')
->setIsDuplicate(true)
->setData('test_attribute', 'NO_test_attribute_value')
->save();
}
Expand Down