Skip to content

Commit 6381c50

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-49185' into Troll_S53
2 parents 303839d + a691b35 commit 6381c50

File tree

4 files changed

+108
-4
lines changed

4 files changed

+108
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -931,13 +931,13 @@ public function checkAttributeUniqueValue(AbstractAttribute $attribute, $object)
931931
$select = $connection->select();
932932
if ($attribute->getBackend()->getType() === 'static') {
933933
$value = $object->getData($attribute->getAttributeCode());
934-
$bind = ['attribute_code' => trim($value)];
934+
$bind = ['value' => trim($value)];
935935

936936
$select->from(
937937
$this->getEntityTable(),
938938
$this->getEntityIdField()
939939
)->where(
940-
$attribute->getAttributeCode() . ' = :attribute_code'
940+
$attribute->getAttributeCode() . ' = :value'
941941
);
942942
} else {
943943
$value = $object->getData($attribute->getAttributeCode());
@@ -950,7 +950,7 @@ public function checkAttributeUniqueValue(AbstractAttribute $attribute, $object)
950950
];
951951
$select->from(
952952
$attribute->getBackend()->getTable(),
953-
$attribute->getBackend()->getEntityIdField()
953+
$object->getResource()->getLinkField()
954954
)->where(
955955
'attribute_id = :attribute_id'
956956
)->where(

dev/tests/integration/testsuite/Magento/Catalog/Model/ProductTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ protected function setUp()
3737
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
3838
'Magento\Catalog\Model\Product'
3939
);
40-
$this->markTestSkipped('Test skipped due to changes that appear after MAGETWO-45654');
4140
}
4241

4342
public static function tearDownAfterClass()
@@ -434,6 +433,47 @@ public function testValidate()
434433
}
435434
}
436435

436+
/**
437+
* @magentoDbIsolation enabled
438+
* @magentoDataFixture Magento/Catalog/_files/products_with_unique_input_attribute.php
439+
*/
440+
public function testValidateUniqueInputAttributeValue()
441+
{
442+
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
443+
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
444+
->get('Magento\Catalog\Model\ResourceModel\Eav\Attribute')
445+
->loadByCode(\Magento\Catalog\Model\Product::ENTITY, 'unique_input_attribute');
446+
$this->_model->setTypeId(
447+
'simple'
448+
)->setAttributeSetId(
449+
4
450+
)->setName(
451+
'Simple Product with non-unique value'
452+
)->setSku(
453+
'some product SKU'
454+
)->setPrice(
455+
10
456+
)->setMetaTitle(
457+
'meta title'
458+
)->setData(
459+
$attribute->getAttributeCode(),
460+
'unique value'
461+
)->setVisibility(
462+
\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH
463+
)->setStatus(
464+
\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
465+
)->setCollectExceptionMessages(
466+
true
467+
);
468+
469+
$validationResult = $this->_model->validate();
470+
$this->assertCount(1, $validationResult);
471+
$this->assertContains(
472+
'The value of attribute "' . $attribute->getDefaultFrontendLabel() . '" must be unique',
473+
$validationResult
474+
);
475+
}
476+
437477
/**
438478
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
439479
* @magentoAppIsolation enabled
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
require __DIR__ . '/unique_input_attribute.php';
7+
8+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
9+
10+
/** @var $product \Magento\Catalog\Model\Product */
11+
$product = $objectManager->create('Magento\Catalog\Model\Product');
12+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
13+
->setAttributeSetId(4)
14+
->setName('Simple Product with unique input attribute')
15+
->setSku('simple product with unique input attribute')
16+
->setPrice(10)
17+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
18+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
19+
->setWebsiteIds([1])
20+
->setStockData(['qty' => 100, 'is_in_stock' => 1])
21+
->setData($attribute->getAttributeCode(), 'unique value')
22+
->save();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* "Input" fixture of product EAV attribute.
4+
*
5+
* Copyright © 2016 Magento. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
9+
/** @var \Magento\Eav\Model\Entity\Type $entityType */
10+
$entityType = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Eav\Model\Entity\Type');
11+
$entityType->loadByCode('catalog_product');
12+
$defaultSetId = $entityType->getDefaultAttributeSetId();
13+
/** @var \Magento\Eav\Model\Entity\Attribute\Set $defaultSet */
14+
$defaultSet = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
15+
'Magento\Eav\Model\Entity\Attribute\Set'
16+
);
17+
$defaultSet->load($defaultSetId);
18+
$defaultGroupId = $defaultSet->getDefaultGroupId();
19+
20+
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
21+
$attribute = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
22+
'Magento\Catalog\Model\ResourceModel\Eav\Attribute'
23+
);
24+
$attribute->setAttributeCode(
25+
'unique_input_attribute'
26+
)->setEntityTypeId(
27+
$entityType->getEntityTypeId()
28+
)->setAttributeGroupId(
29+
$defaultGroupId
30+
)->setAttributeSetId(
31+
$defaultSetId
32+
)->setFrontendInput(
33+
'text'
34+
)->setFrontendLabel(
35+
'Unique Input Attribute'
36+
)->setBackendType(
37+
'varchar'
38+
)->setIsUserDefined(
39+
1
40+
)->setIsUnique(
41+
1
42+
)->save();

0 commit comments

Comments
 (0)