Skip to content

Commit e5d6bca

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-67198' into 2.2-develop-pr11
2 parents 493956a + ee30094 commit e5d6bca

File tree

5 files changed

+165
-11
lines changed

5 files changed

+165
-11
lines changed

app/code/Magento/Eav/Model/Entity/Attribute.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
2626
*/
2727
const ATTRIBUTE_CODE_MAX_LENGTH = 30;
2828

29+
/**
30+
* Attribute code min length.
31+
*/
32+
const ATTRIBUTE_CODE_MIN_LENGTH = 1;
33+
2934
/**
3035
* Cache tag
3136
*/

app/code/Magento/Eav/Setup/EavSetup.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -771,19 +771,24 @@ private function _getValue($array, $key, $default = null)
771771
*/
772772
private function _validateAttributeData($data)
773773
{
774-
$attributeCodeMaxLength = \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MAX_LENGTH;
774+
$minLength = \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MIN_LENGTH;
775+
$maxLength = \Magento\Eav\Model\Entity\Attribute::ATTRIBUTE_CODE_MAX_LENGTH;
776+
$attributeCode = isset($data['attribute_code']) ? $data['attribute_code'] : '';
775777

776-
if (isset(
777-
$data['attribute_code']
778-
) && !\Zend_Validate::is(
779-
$data['attribute_code'],
778+
$isAllowedLength = \Zend_Validate::is(
779+
trim($attributeCode),
780780
'StringLength',
781-
['max' => $attributeCodeMaxLength]
782-
)
783-
) {
784-
throw new LocalizedException(
785-
__('An attribute code must not be more than %1 characters.', $attributeCodeMaxLength)
781+
['min' => $minLength, 'max' => $maxLength]
782+
);
783+
784+
if (!$isAllowedLength) {
785+
$errorMessage = __(
786+
'An attribute code must not be less than %1 and more than %2 characters.',
787+
$minLength,
788+
$maxLength
786789
);
790+
791+
throw new LocalizedException($errorMessage);
787792
}
788793

789794
return true;

app/code/Magento/Eav/Setup/UpgradeSchema.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
2525
if (version_compare($context->getVersion(), '2.1.0', '<')) {
2626
$this->addUniqueKeyToEavAttributeGroupTable($setup);
2727
}
28+
29+
if (version_compare($context->getVersion(), '2.1.1', '<')) {
30+
$this->modifyAttributeCodeColumnForNotNullable($setup);
31+
}
2832
$setup->endSetup();
2933
}
3034

@@ -45,4 +49,23 @@ private function addUniqueKeyToEavAttributeGroupTable(SchemaSetupInterface $setu
4549
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
4650
);
4751
}
52+
53+
/**
54+
* Column attribute_code from eav_attribute should not be nullable.
55+
*
56+
* @param SchemaSetupInterface $setup
57+
*/
58+
private function modifyAttributeCodeColumnForNotNullable(SchemaSetupInterface $setup)
59+
{
60+
$setup->getConnection()->modifyColumn(
61+
$setup->getTable('eav_attribute'),
62+
'attribute_code',
63+
[
64+
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
65+
'length' => 255,
66+
'nullable' => false,
67+
'comment' => 'Attribute Code',
68+
]
69+
);
70+
}
4871
}

app/code/Magento/Eav/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_Eav" setup_version="2.1.0">
9+
<module name="Magento_Eav" setup_version="2.1.1">
1010
<sequence>
1111
<module name="Magento_Store"/>
1212
</sequence>
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Eav\Setup;
8+
9+
/**
10+
* Test class for Magento\Eav\Setup\EavSetup.
11+
* @magentoDbIsolation enabled
12+
*/
13+
class EavSetupTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* Eav setup.
17+
*
18+
* @var \Magento\Eav\Setup\EavSetup
19+
*/
20+
private $eavSetup;
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
protected function setUp()
26+
{
27+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
28+
$this->eavSetup = $objectManager->create(\Magento\Eav\Setup\EavSetup::class);
29+
}
30+
31+
/**
32+
* Verify that add attribute work correct attribute_code.
33+
*
34+
* @param string $attributeCode
35+
*
36+
* @dataProvider addAttributeDataProvider
37+
*/
38+
public function testAddAttribute($attributeCode)
39+
{
40+
$attributeData = $this->getAttributeData();
41+
42+
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $attributeData);
43+
44+
$attribute = $this->eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode);
45+
46+
$this->assertEmpty(array_diff($attributeData, $attribute));
47+
}
48+
49+
/**
50+
* Data provider for testAddAttributeThrowException().
51+
*
52+
* @return array
53+
*/
54+
public function addAttributeDataProvider()
55+
{
56+
return [
57+
['eav_setup_test'],
58+
['_29_characters_29_characters_'],
59+
];
60+
}
61+
62+
/**
63+
* Verify that add attribute throw exception if attribute_code is not valid.
64+
*
65+
* @param string|null $attributeCode
66+
*
67+
* @dataProvider addAttributeThrowExceptionDataProvider
68+
* @expectedException \Magento\Framework\Exception\LocalizedException
69+
* @expectedExceptionMessage An attribute code must not be less than 1 and more than 30 characters.
70+
*/
71+
public function testAddAttributeThrowException($attributeCode)
72+
{
73+
$attributeData = $this->getAttributeData();
74+
75+
$this->eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $attributeData);
76+
}
77+
78+
/**
79+
* Data provider for testAddAttributeThrowException().
80+
*
81+
* @return array
82+
*/
83+
public function addAttributeThrowExceptionDataProvider()
84+
{
85+
return [
86+
[null],
87+
[''],
88+
[' '],
89+
['more_than_30_characters_more_than'],
90+
];
91+
}
92+
93+
/**
94+
* Get simple attribute data.
95+
*/
96+
private function getAttributeData()
97+
{
98+
$attributeData = [
99+
'type' => 'varchar',
100+
'backend' => '',
101+
'frontend' => '',
102+
'label' => 'Eav Setup Test',
103+
'input' => 'text',
104+
'class' => '',
105+
'source' => '',
106+
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
107+
'visible' => 0,
108+
'required' => 0,
109+
'user_defined' => 1,
110+
'default' => 'none',
111+
'searchable' => 0,
112+
'filterable' => 0,
113+
'comparable' => 0,
114+
'visible_on_front' => 0,
115+
'unique' => 0,
116+
'apply_to' => 'category',
117+
];
118+
119+
return $attributeData;
120+
}
121+
}

0 commit comments

Comments
 (0)