Skip to content

Sku uniqueness #22746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
34 changes: 15 additions & 19 deletions app/code/Magento/Catalog/Model/Product/Attribute/Backend/Sku.php
100644 → 100755
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,8 +104,9 @@ protected function _generateUniqueSku($object)
*/
public function beforeSave($object)
{
$this->_generateUniqueSku($object);
$this->trimValue($object);
if ($object->getIsDuplicate()) {
$this->_generateUniqueSku($object);
}
return parent::beforeSave($object);
}

Expand Down Expand Up @@ -126,19 +137,4 @@ protected function _getLastSimilarAttributeValueIncrement($attribute, $object)
$data = $connection->fetchOne($select, $bind);
return abs((int)str_replace($value, '', $data));
}

/**
* Remove extra spaces from attribute value before save.
*
* @param Product $object
* @return void
*/
private function trimValue($object)
{
$attrCode = $this->getAttribute()->getAttributeCode();
$value = $object->getData($attrCode);
if ($value) {
$object->setData($attrCode, trim($value));
}
}
}
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 @@ -8,13 +8,19 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../vendor/magento/mtf/etc/variations.xsd">
<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="tag" xsi:type="string">test_type:acceptance_test, test_type:extended_acceptance_test, severity:S1</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