Skip to content

Commit 71e57a4

Browse files
author
vnayda
committed
MAGETWO-57963: The product page doesn't show all the fields for Product Details - for mainline
1 parent 33f0757 commit 71e57a4

File tree

16 files changed

+473
-57
lines changed

16 files changed

+473
-57
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Set/Save.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ public function execute()
102102
$model->save();
103103
$this->messageManager->addSuccess(__('You saved the attribute set.'));
104104
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
105-
$this->messageManager->addExceptionMessage(
106-
$e,
107-
__('Attribute group with same code is already exist. Please enter other Group name')
108-
);
105+
$this->messageManager->addErrorMessage($e->getMessage());
109106
$hasError = true;
110107
} catch (\Magento\Framework\Exception\LocalizedException $e) {
111108
$this->messageManager->addError($e->getMessage());

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
use Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend;
1414
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
1515
use Magento\Framework\App\Config\Element;
16-
use Magento\Framework\App\ResourceConnection\Config;
16+
use Magento\Framework\DB\Adapter\DuplicateException;
17+
use Magento\Framework\Exception\AlreadyExistsException;
1718
use Magento\Framework\Exception\LocalizedException;
1819
use Magento\Framework\Model\AbstractModel;
1920
use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
@@ -1108,6 +1109,7 @@ protected function _setAttributeValue($object, $valueRow)
11081109
* @param \Magento\Framework\Model\AbstractModel $object
11091110
* @return $this
11101111
* @throws \Exception
1112+
* @throws AlreadyExistsException
11111113
*/
11121114
public function save(\Magento\Framework\Model\AbstractModel $object)
11131115
{
@@ -1147,6 +1149,10 @@ public function save(\Magento\Framework\Model\AbstractModel $object)
11471149
}
11481150
$this->addCommitCallback([$object, 'afterCommitCallback'])->commit();
11491151
$object->setHasDataChanges(false);
1152+
} catch (DuplicateException $e) {
1153+
$this->rollBack();
1154+
$object->setHasDataChanges(true);
1155+
throw new AlreadyExistsException();
11501156
} catch (\Exception $e) {
11511157
$this->rollBack();
11521158
$object->setHasDataChanges(true);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Eav\Model\Entity\Attribute;
7+
8+
use Magento\Framework\Exception\AlreadyExistsException;
9+
use Magento\Framework\Phrase;
10+
11+
/**
12+
* Class AlreadyExistsException
13+
*/
14+
class AttributeGroupAlreadyExistsException extends AlreadyExistsException
15+
{
16+
/**
17+
* @param Phrase $phrase
18+
* @param \Exception $cause
19+
*/
20+
public function __construct(Phrase $phrase = null, \Exception $cause = null)
21+
{
22+
if ($phrase === null) {
23+
$phrase = new Phrase('Attribute group with same code is already exist. Please enter other Group name');
24+
}
25+
parent::__construct($phrase, $cause);
26+
}
27+
}

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* See COPYING.txt for license details.
55
*/
66
namespace Magento\Eav\Model\ResourceModel\Entity\Attribute;
7+
use Magento\Eav\Model\Entity\Attribute\AttributeGroupAlreadyExistsException;
8+
use Magento\Framework\DB\Adapter\DuplicateException;
9+
use Magento\Framework\Model\AbstractModel;
710

811
/**
912
* Eav Resource Entity Attribute Group
@@ -50,10 +53,10 @@ public function itemExists($object)
5053
/**
5154
* Perform actions before object save
5255
*
53-
* @param \Magento\Framework\Model\AbstractModel $object
56+
* @param AbstractModel $object
5457
* @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
5558
*/
56-
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
59+
protected function _beforeSave(AbstractModel $object)
5760
{
5861
if (!$object->getSortOrder()) {
5962
$object->setSortOrder($this->_getMaxSortOrder($object) + 1);
@@ -64,10 +67,10 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
6467
/**
6568
* Perform actions after object save
6669
*
67-
* @param \Magento\Framework\Model\AbstractModel $object
70+
* @param AbstractModel $object
6871
* @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
6972
*/
70-
protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
73+
protected function _afterSave(AbstractModel $object)
7174
{
7275
if ($object->getAttributes()) {
7376
foreach ($object->getAttributes() as $attribute) {
@@ -82,7 +85,7 @@ protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
8285
/**
8386
* Retrieve max sort order
8487
*
85-
* @param \Magento\Framework\Model\AbstractModel $object
88+
* @param AbstractModel $object
8689
* @return int
8790
*/
8891
protected function _getMaxSortOrder($object)
@@ -130,4 +133,28 @@ public function updateDefaultGroup($attributeSetId)
130133

131134
return $this;
132135
}
136+
137+
/**
138+
* {@inheritdoc}
139+
*/
140+
protected function saveNewObject(AbstractModel $object)
141+
{
142+
try {
143+
return parent::saveNewObject($object);
144+
} catch (DuplicateException $e) {
145+
throw new AttributeGroupAlreadyExistsException();
146+
}
147+
}
148+
149+
/**
150+
* {@inheritdoc}
151+
*/
152+
protected function updateObject(AbstractModel $object)
153+
{
154+
try {
155+
return parent::updateObject($object);
156+
} catch (DuplicateException $e) {
157+
throw new AttributeGroupAlreadyExistsException();
158+
}
159+
}
133160
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
2525
if (version_compare($context->getVersion(), '2.1.0', '<')) {
2626
$this->addUniqueKeyToEavAttributeGroupTable($setup);
2727
}
28-
2928
$setup->endSetup();
3029
}
3130

3231
/**
3332
* @param SchemaSetupInterface $setup
3433
* @return void
3534
*/
36-
protected function addUniqueKeyToEavAttributeGroupTable(SchemaSetupInterface $setup)
35+
private function addUniqueKeyToEavAttributeGroupTable(SchemaSetupInterface $setup)
3736
{
3837
$setup->getConnection()->addIndex(
3938
$setup->getTable('eav_attribute_group'),

app/code/Magento/Eav/Test/Unit/Model/Entity/AbstractEntityTest.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
*/
66
namespace Magento\Eav\Test\Unit\Model\Entity;
77

8+
use Magento\Eav\Model\Entity\AbstractEntity;
9+
use Magento\Framework\DB\Adapter\AdapterInterface;
10+
use Magento\Framework\DB\Adapter\DuplicateException;
11+
use Magento\Framework\Model\AbstractModel;
812
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
913

1014
class AbstractEntityTest extends \PHPUnit_Framework_TestCase
1115
{
1216
/**
1317
* Entity model to be tested
14-
* @var \Magento\Eav\Model\Entity\AbstractEntity|\PHPUnit_Framework_MockObject_MockObject
18+
* @var AbstractEntity|\PHPUnit_Framework_MockObject_MockObject
1519
*/
1620
protected $_model;
1721

@@ -23,11 +27,11 @@ protected function setUp()
2327
$objectManager = new ObjectManager($this);
2428
$this->eavConfig = $this->getMock(\Magento\Eav\Model\Config::class, [], [], '', false);
2529
$arguments = $objectManager->getConstructArguments(
26-
\Magento\Eav\Model\Entity\AbstractEntity::class,
30+
AbstractEntity::class,
2731
['eavConfig' => $this->eavConfig]
2832
);
2933
$this->_model = $this->getMockForAbstractClass(
30-
\Magento\Eav\Model\Entity\AbstractEntity::class,
34+
AbstractEntity::class,
3135
$arguments
3236
);
3337
}
@@ -113,7 +117,7 @@ protected function _getAttributes()
113117
/**
114118
* Get adapter mock
115119
*
116-
* @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Adapter\AdapterInterface
120+
* @return \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\DB\Adapter\Pdo\Mysql
117121
*/
118122
protected function _getConnectionMock()
119123
{
@@ -300,7 +304,7 @@ public function testSave($attributeCode, $attributeSetId, $productData, $product
300304
$objectManager = new ObjectManager($this);
301305
$this->eavConfig = $this->getMock(\Magento\Eav\Model\Config::class, [], [], '', false);
302306
$arguments = $objectManager->getConstructArguments(
303-
\Magento\Eav\Model\Entity\AbstractEntity::class,
307+
AbstractEntity::class,
304308
[
305309
'eavConfig' => $eavConfig,
306310
'data' => [
@@ -310,8 +314,8 @@ public function testSave($attributeCode, $attributeSetId, $productData, $product
310314
]
311315
]
312316
);
313-
/** @var $model \Magento\Framework\Model\AbstractModel|\PHPUnit_Framework_MockObject_MockObject */
314-
$model = $this->getMockBuilder(\Magento\Eav\Model\Entity\AbstractEntity::class)
317+
/** @var $model AbstractEntity|\PHPUnit_Framework_MockObject_MockObject */
318+
$model = $this->getMockBuilder(AbstractEntity::class)
315319
->setConstructorArgs($arguments)
316320
->setMethods(['_getValue', 'beginTransaction', 'commit', 'rollback', 'getConnection'])
317321
->getMock();
@@ -353,4 +357,30 @@ public function productAttributesDataProvider()
353357
]
354358
];
355359
}
360+
361+
/**
362+
* @expectedException \Magento\Framework\Exception\AlreadyExistsException
363+
*/
364+
public function testDuplicateExceptionProcessingOnSave()
365+
{
366+
$connection = $this->getMock(AdapterInterface::class);
367+
$connection->expects($this->once())->method('rollback');
368+
369+
/** @var AbstractEntity|\PHPUnit_Framework_MockObject_MockObject $model */
370+
$model = $this->getMockBuilder(AbstractEntity::class)
371+
->disableOriginalConstructor()
372+
->setMethods(['getConnection'])
373+
->getMockForAbstractClass();
374+
$model->expects($this->any())->method('getConnection')->willReturn($connection);
375+
376+
/** @var AbstractModel|\PHPUnit_Framework_MockObject_MockObject $object */
377+
$object = $this->getMockBuilder(AbstractModel::class)
378+
->disableOriginalConstructor()
379+
->getMock();
380+
$object->expects($this->once())->method('hasDataChanges')->willReturn(true);
381+
$object->expects($this->once())->method('beforeSave')->willThrowException(new DuplicateException());
382+
$object->expects($this->once())->method('setHasDataChanges')->with(true);
383+
384+
$model->save($object);
385+
}
356386
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Controller\Adminhtml\Product\Set;
7+
8+
use Magento\Eav\Api\AttributeSetRepositoryInterface;
9+
use Magento\Eav\Api\Data\AttributeSetInterface;
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
class SaveTest extends \Magento\TestFramework\TestCase\AbstractBackendController
14+
{
15+
/**
16+
* @magentoDataFixture Magento/Catalog/_files/attribute_set_with_renamed_group.php
17+
*/
18+
public function testAlreadyExistsExceptionProcessingWhenGroupCodeIsDuplicated()
19+
{
20+
$attributeSet = $this->getAttributeSetByName('attribute_set_test');
21+
$this->assertNotEmpty($attributeSet, 'Attribute set with name "attribute_set_test" is missed');
22+
23+
$this->getRequest()->setPostValue('data', json_encode([
24+
'attribute_set_name' => 'attribute_set_test',
25+
'groups' => [
26+
['ynode-418', 'attribute-group-name', 1],
27+
],
28+
'attributes' => [
29+
['9999', 'ynode-418', 1, null]
30+
],
31+
'not_attributes' => [],
32+
'removeGroups' => [],
33+
]));
34+
$this->dispatch('backend/catalog/product_set/save/id/' . $attributeSet->getAttributeSetId());
35+
36+
$jsonResponse = json_decode($this->getResponse()->getBody());
37+
$this->assertNotNull($jsonResponse);
38+
$this->assertEquals(1, $jsonResponse->error);
39+
$this->assertContains(
40+
'Attribute group with same code is already exist. Please enter other Group name',
41+
$jsonResponse->message
42+
);
43+
}
44+
45+
/**
46+
* @param string $attributeSetName
47+
* @return AttributeSetInterface|null
48+
*/
49+
protected function getAttributeSetByName($attributeSetName)
50+
{
51+
$objectManager = Bootstrap::getObjectManager();
52+
53+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
54+
$searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
55+
$searchCriteriaBuilder->addFilter('attribute_set_name', $attributeSetName);
56+
57+
/** @var AttributeSetRepositoryInterface $attributeSetRepository */
58+
$attributeSetRepository = $objectManager->get(AttributeSetRepositoryInterface::class);
59+
$result = $attributeSetRepository->getList($searchCriteriaBuilder->create());
60+
61+
$items = $result->getItems();
62+
return $result->getTotalCount() ? array_pop($items) : null;
63+
}
64+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
use Magento\Catalog\Api\AttributeSetRepositoryInterface;
7+
use Magento\Eav\Api\AttributeGroupRepositoryInterface;
8+
use Magento\Eav\Api\Data\AttributeGroupInterface;
9+
use Magento\Eav\Api\Data\AttributeGroupInterfaceFactory;
10+
use Magento\Eav\Api\Data\AttributeSetInterface;
11+
use Magento\Eav\Api\Data\AttributeSetInterfaceFactory;
12+
use Magento\Eav\Model\Entity\Type;
13+
use Magento\Framework\Api\DataObjectHelper;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
16+
$objectManager = Bootstrap::getObjectManager();
17+
$attributeSetFactory = $objectManager->get(AttributeSetInterfaceFactory::class);
18+
$attributeGroupFactory = $objectManager->get(AttributeGroupInterfaceFactory::class);
19+
/** @var DataObjectHelper $dataObjectHelper */
20+
$dataObjectHelper = $objectManager->get(DataObjectHelper::class);
21+
/** @var AttributeGroupRepositoryInterface $attributeGroupRepository */
22+
$attributeGroupRepository = $objectManager->get(AttributeGroupRepositoryInterface::class);
23+
/** @var AttributeSetRepositoryInterface $attributeSetRepository */
24+
$attributeSetRepository = $objectManager->get(AttributeSetRepositoryInterface::class);
25+
26+
/** @var AttributeSetInterface $attributeSet */
27+
$attributeSet = $attributeSetFactory->create();
28+
$entityTypeId = $objectManager->create(Type::class)->loadByCode('catalog_product')->getId();
29+
$dataObjectHelper->populateWithArray(
30+
$attributeSet,
31+
[
32+
'attribute_set_name' => 'attribute_set_test',
33+
'entity_type_id' => $entityTypeId,
34+
],
35+
AttributeSetInterface::class
36+
);
37+
$attributeSetRepository->save($attributeSet);
38+
39+
/** @var AttributeGroupInterface $attributeGroup */
40+
$attributeGroup = $attributeGroupFactory->create();
41+
$dataObjectHelper->populateWithArray(
42+
$attributeGroup,
43+
[
44+
'attribute_set_id' => $attributeSet->getAttributeSetId(),
45+
'attribute_group_name' => 'attribute-group-name',
46+
'default_id' => 1,
47+
],
48+
AttributeGroupInterface::class
49+
);
50+
$attributeGroupRepository->save($attributeGroup);
51+
52+
// during renaming group code is not changed
53+
$attributeGroup->setAttributeGroupName('attribute-group-renamed');
54+
$attributeGroupRepository->save($attributeGroup);

lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ protected function _checkDdlTransaction($sql)
468468
* @param mixed $bind An array of data or data itself to bind to the placeholders.
469469
* @return \Zend_Db_Statement_Pdo|void
470470
* @throws \Zend_Db_Adapter_Exception To re-throw \PDOException.
471-
* @throws LocalizedException In case multiple queries are attempted at once, to protect from SQL injection
471+
* @throws \Zend_Db_Statement_Exception
472472
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
473473
*/
474474
protected function _query($sql, $bind = [])

0 commit comments

Comments
 (0)