Skip to content

Module catalog, Attribute Repository code validation regex #29015

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

Merged
merged 7 commits into from
Sep 25, 2020
Merged
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
14 changes: 11 additions & 3 deletions app/code/Magento/Catalog/Model/Product/Attribute/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\Catalog\Model\Product\Attribute;

use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;

Expand Down Expand Up @@ -218,11 +219,16 @@ public function getCustomAttributesMetadata($dataObjectClassName = null)
*/
protected function generateCode($label)
{
$code = substr(preg_replace('/[^a-z_0-9]/', '_', $this->filterManager->translitUrl($label)), 0, 30);
$code = substr(
preg_replace('/[^a-z_0-9]/', '_', $this->filterManager->translitUrl($label)),
0,
Attribute::ATTRIBUTE_CODE_MAX_LENGTH
);
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,29}[a-z0-9]$/']);
if (!$validatorAttrCode->isValid($code)) {
$code = 'attr_' . ($code ?: substr(md5(time()), 0, 8));
$code = 'attr_' . ($code ?: substr(hash('sha256', time()), 0, 8));
}

return $code;
}

Expand All @@ -235,7 +241,9 @@ protected function generateCode($label)
*/
protected function validateCode($code)
{
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,30}$/']);
$validatorAttrCode = new \Zend_Validate_Regex(
['pattern' => '/^[a-z][a-z_0-9]{0,' . Attribute::ATTRIBUTE_CODE_MAX_LENGTH . '}$/']
);
if (!$validatorAttrCode->isValid($code)) {
throw InputException::invalidFieldValue('attribute_code', $code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,15 @@ public function testGetList()
}

/**
* Test create attribute
*
* @dataProvider attributeCodeDataProvider
* @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/create_attribute_service.php
* @param string $attributeCode
* @return void
*/
public function testCreate()
public function testCreate(string $attributeCode): void
{
$attributeCode = uniqid('label_attr_code');
$attribute = $this->createAttribute($attributeCode);

$expectedData = [
Expand Down Expand Up @@ -121,6 +124,17 @@ public function testCreate()
$this->assertEquals('Default Red', $attribute['options'][2]['label']);
}

/**
* @return array
*/
public function attributeCodeDataProvider(): array
{
return [
[str_repeat('az_7', 15)],
[uniqid('label_attr_code')],
];
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_attribute.php
* @return void
Expand Down