Skip to content

Commit 6250fd7

Browse files
authored
ENGCOM-8229: Module catalog, Attribute Repository code validation regex #29015
2 parents 9a1cab1 + 58b60ef commit 6250fd7

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Repository.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Catalog\Model\Product\Attribute;
88

99
use Magento\Eav\Api\Data\AttributeInterface;
10+
use Magento\Eav\Model\Entity\Attribute;
1011
use Magento\Framework\Exception\InputException;
1112
use Magento\Framework\Exception\NoSuchEntityException;
1213

@@ -218,11 +219,16 @@ public function getCustomAttributesMetadata($dataObjectClassName = null)
218219
*/
219220
protected function generateCode($label)
220221
{
221-
$code = substr(preg_replace('/[^a-z_0-9]/', '_', $this->filterManager->translitUrl($label)), 0, 30);
222+
$code = substr(
223+
preg_replace('/[^a-z_0-9]/', '_', $this->filterManager->translitUrl($label)),
224+
0,
225+
Attribute::ATTRIBUTE_CODE_MAX_LENGTH
226+
);
222227
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,29}[a-z0-9]$/']);
223228
if (!$validatorAttrCode->isValid($code)) {
224-
$code = 'attr_' . ($code ?: substr(md5(time()), 0, 8));
229+
$code = 'attr_' . ($code ?: substr(hash('sha256', time()), 0, 8));
225230
}
231+
226232
return $code;
227233
}
228234

@@ -235,7 +241,9 @@ protected function generateCode($label)
235241
*/
236242
protected function validateCode($code)
237243
{
238-
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,30}$/']);
244+
$validatorAttrCode = new \Zend_Validate_Regex(
245+
['pattern' => '/^[a-z][a-z_0-9]{0,' . Attribute::ATTRIBUTE_CODE_MAX_LENGTH . '}$/']
246+
);
239247
if (!$validatorAttrCode->isValid($code)) {
240248
throw InputException::invalidFieldValue('attribute_code', $code);
241249
}

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductAttributeRepositoryTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,15 @@ public function testGetList()
8787
}
8888

8989
/**
90+
* Test create attribute
91+
*
92+
* @dataProvider attributeCodeDataProvider
9093
* @magentoApiDataFixture Magento/Catalog/Model/Product/Attribute/_files/create_attribute_service.php
94+
* @param string $attributeCode
9195
* @return void
9296
*/
93-
public function testCreate()
97+
public function testCreate(string $attributeCode): void
9498
{
95-
$attributeCode = uniqid('label_attr_code');
9699
$attribute = $this->createAttribute($attributeCode);
97100

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

127+
/**
128+
* @return array
129+
*/
130+
public function attributeCodeDataProvider(): array
131+
{
132+
return [
133+
[str_repeat('az_7', 15)],
134+
[uniqid('label_attr_code')],
135+
];
136+
}
137+
124138
/**
125139
* @magentoApiDataFixture Magento/Catalog/_files/product_attribute.php
126140
* @return void

0 commit comments

Comments
 (0)