Skip to content

Commit a3aef9d

Browse files
ENGCOM-6128: Fix the problem 'Attribute with the same code already exists' #25134
2 parents b8ffb11 + ee53e2f commit a3aef9d

File tree

2 files changed

+38
-55
lines changed

2 files changed

+38
-55
lines changed

dev/tests/integration/testsuite/Magento/Customer/_files/attribute_user_defined_address_custom_attribute.php

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,37 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
$model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Attribute::class);
8-
$model->setName(
9-
'custom_attribute1'
10-
)->setEntityTypeId(
11-
2
12-
)->setAttributeSetId(
13-
2
14-
)->setAttributeGroupId(
15-
1
16-
)->setFrontendInput(
17-
'text'
18-
)->setFrontendLabel(
19-
'custom_attribute_frontend_label'
20-
)->setIsUserDefined(
21-
1
22-
);
23-
$model->save();
24-
25-
$model2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Attribute::class);
26-
$model2->setName(
27-
'custom_attribute2'
28-
)->setEntityTypeId(
29-
2
30-
)->setAttributeSetId(
31-
2
32-
)->setAttributeGroupId(
33-
1
34-
)->setFrontendInput(
35-
'text'
36-
)->setFrontendLabel(
37-
'custom_attribute_frontend_label'
38-
)->setIsUserDefined(
39-
1
40-
);
41-
$model2->save();
7+
/** @var \Magento\Framework\ObjectManagerInterface $objectManager */
8+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
9+
10+
/** @var \Magento\Customer\Model\AttributeFactory $attributeFactory */
11+
$attributeFactory = $objectManager->create(\Magento\Customer\Model\AttributeFactory::class);
12+
13+
/** @var \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository */
14+
$attributeRepository = $objectManager->create(\Magento\Eav\Api\AttributeRepositoryInterface::class);
4215

4316
/** @var \Magento\Customer\Setup\CustomerSetup $setupResource */
44-
$setupResource = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
45-
\Magento\Customer\Setup\CustomerSetup::class
46-
);
47-
48-
$data = [['form_code' => 'customer_address_edit', 'attribute_id' => $model->getAttributeId()]];
49-
$setupResource->getSetup()->getConnection()->insertMultiple(
50-
$setupResource->getSetup()->getTable('customer_form_attribute'),
51-
$data
52-
);
53-
54-
$data2 = [['form_code' => 'customer_address_edit', 'attribute_id' => $model2->getAttributeId()]];
55-
$setupResource->getSetup()->getConnection()->insertMultiple(
56-
$setupResource->getSetup()->getTable('customer_form_attribute'),
57-
$data2
58-
);
17+
$setupResource = $objectManager->create(\Magento\Customer\Setup\CustomerSetup::class);
18+
19+
$attributeNames = ['custom_attribute1', 'custom_attribute2'];
20+
foreach ($attributeNames as $attributeName) {
21+
/** @var \Magento\Customer\Model\Attribute $attribute */
22+
$attribute = $attributeFactory->create();
23+
24+
$attribute->setName($attributeName)
25+
->setEntityTypeId(2)
26+
->setAttributeSetId(2)
27+
->setAttributeGroupId(1)
28+
->setFrontendInput('text')
29+
->setFrontendLabel('custom_attribute_frontend_label')
30+
->setIsUserDefined(true);
31+
32+
$attributeRepository->save($attribute);
33+
34+
$setupResource->getSetup()
35+
->getConnection()
36+
->insertMultiple(
37+
$setupResource->getSetup()->getTable('customer_form_attribute'),
38+
[['form_code' => 'customer_address_edit', 'attribute_id' => $attribute->getAttributeId()]]
39+
);
40+
}

dev/tests/integration/testsuite/Magento/Customer/_files/attribute_user_defined_address_custom_attribute_rollback.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
* See COPYING.txt for license details.
66
*/
77

8-
$model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Attribute::class);
9-
$model->load('custom_attribute_test', 'attribute_code')->delete();
10-
11-
$model2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Attribute::class);
12-
$model2->load('custom_attributes_test', 'attribute_code')->delete();
8+
/** @var \Magento\Customer\Model\Attribute $attributeModel */
9+
$attributeModel = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
10+
\Magento\Customer\Model\Attribute::class
11+
);
12+
$attributeModel->load('custom_attribute1', 'attribute_code')->delete();
13+
$attributeModel->load('custom_attribute2', 'attribute_code')->delete();

0 commit comments

Comments
 (0)