Skip to content

Commit 428e3d8

Browse files
fix static, test coverage
1 parent 65a67aa commit 428e3d8

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,16 @@ public function getCustomAttributesMetadata($dataObjectClassName = null)
219219
*/
220220
protected function generateCode($label)
221221
{
222-
$code = substr(preg_replace('/[^a-z_0-9]/', '_', $this->filterManager->translitUrl($label)), 0, Attribute::ATTRIBUTE_CODE_MAX_LENGTH);
222+
$code = substr(
223+
preg_replace('/[^a-z_0-9]/', '_', $this->filterManager->translitUrl($label)),
224+
0,
225+
Attribute::ATTRIBUTE_CODE_MAX_LENGTH
226+
);
223227
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,29}[a-z0-9]$/']);
224228
if (!$validatorAttrCode->isValid($code)) {
225-
$code = 'attr_' . ($code ?: substr(md5(time()), 0, 8));
229+
$code = 'attr_' . ($code ?: substr(hash('sha256', time()), 0, 8));
226230
}
231+
227232
return $code;
228233
}
229234

@@ -236,7 +241,9 @@ protected function generateCode($label)
236241
*/
237242
protected function validateCode($code)
238243
{
239-
$validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,' . Attribute::ATTRIBUTE_CODE_MAX_LENGTH . '}$/']);
244+
$validatorAttrCode = new \Zend_Validate_Regex(
245+
['pattern' => '/^[a-z][a-z_0-9]{0,' . Attribute::ATTRIBUTE_CODE_MAX_LENGTH . '}$/']
246+
);
240247
if (!$validatorAttrCode->isValid($code)) {
241248
throw InputException::invalidFieldValue('attribute_code', $code);
242249
}

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)