Skip to content

Commit ac70547

Browse files
author
Alexander Akimov
authored
Merge pull request #1422 from magento-plankton/MAGETWO-71136
[Plankton] Bugs
2 parents e55bc07 + dfc3507 commit ac70547

File tree

5 files changed

+66
-8
lines changed

5 files changed

+66
-8
lines changed

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/AbstractTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ public function testWalkAttributes()
4747

4848
$code = 'test_attr';
4949
$set = 10;
50+
$storeId = 100;
5051

5152
$object = $this->getMock('Magento\Catalog\Model\Product', ['__wakeup'], [], '', false);
5253

5354
$object->setData('test_attr', 'test_attr');
5455
$object->setData('attribute_set_id', $set);
56+
$object->setData('store_id', $storeId);
5557

5658
$entityType = new \Magento\Framework\DataObject();
5759
$entityType->setEntityTypeCode('test');

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

+49-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
1515
use Magento\Framework\App\Config\Element;
1616
use Magento\Framework\App\ResourceConnection\Config;
17+
use Magento\Framework\DataObject;
1718
use Magento\Framework\Exception\LocalizedException;
1819
use Magento\Framework\Model\AbstractModel;
1920
use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
@@ -204,6 +205,13 @@ abstract class AbstractEntity extends AbstractResource implements EntityInterfac
204205
*/
205206
protected $objectRelationProcessor;
206207

208+
/**
209+
* Attributes stored by scope (store id and attribute set id).
210+
*
211+
* @var array
212+
*/
213+
private $attributesByScope;
214+
207215
/**
208216
* @param Context $context
209217
* @param array $data
@@ -447,6 +455,20 @@ public function getAttribute($attribute)
447455
return $attribute;
448456
}
449457

458+
/**
459+
* Adding attribute to entity by scope.
460+
*
461+
* @param AbstractAttribute $attribute
462+
* @param string $suffix
463+
* @return $this
464+
*/
465+
public function addAttributeByScope(AbstractAttribute $attribute, $suffix)
466+
{
467+
$attributeCode = $attribute->getAttributeCode();
468+
$this->attributesByScope[$suffix][$attributeCode] = $attribute;
469+
return $this->addAttribute($attribute);
470+
}
471+
450472
/**
451473
* Adding attribute to entity
452474
*
@@ -572,6 +594,31 @@ protected function _isApplicableAttribute($object, $attribute)
572594
return true;
573595
}
574596

597+
/**
598+
* Get attributes by scope
599+
*
600+
* @return array
601+
*/
602+
private function getAttributesByScope($suffix)
603+
{
604+
return !empty($this->attributesByScope[$suffix])
605+
? $this->attributesByScope[$suffix]
606+
: $this->getAttributesByCode();
607+
}
608+
609+
/**
610+
* Get attributes cache suffix.
611+
*
612+
* @param DataObject $object
613+
* @return string
614+
*/
615+
private function getAttributesCacheSuffix(DataObject $object)
616+
{
617+
$attributeSetId = $object->getAttributeSetId() ?: 0;
618+
$storeId = $object->getStoreId() ?: 0;
619+
return $storeId . '-' . $attributeSetId;
620+
}
621+
575622
/**
576623
* Walk through the attributes and run method with optional arguments
577624
*
@@ -607,7 +654,8 @@ public function walkAttributes($partMethod, array $args = [], $collectExceptionM
607654
break;
608655
}
609656
$results = [];
610-
foreach ($this->getAttributesByCode() as $attrCode => $attribute) {
657+
$suffix = $this->getAttributesCacheSuffix($args[0]);
658+
foreach ($this->getAttributesByScope($suffix) as $attrCode => $attribute) {
611659
if (isset($args[0]) && is_object($args[0]) && !$this->_isApplicableAttribute($args[0], $attribute)) {
612660
continue;
613661
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function loadAllAttributes(AbstractEntity $resource, DataObject $object =
7070
$attributes = $this->cache->getAttributes($typeCode, $suffix);
7171
if ($attributes) {
7272
foreach ($attributes as $attribute) {
73-
$resource->addAttribute($attribute);
73+
$resource->addAttributeByScope($attribute, $suffix);
7474
}
7575
return $resource;
7676
}

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,6 @@ public function testSave($attributeCode, $attributeSetId, $productData, $product
317317
->getMock();
318318
$model->expects($this->any())->method('_getValue')->will($this->returnValue($eavConfig));
319319
$model->expects($this->any())->method('getConnection')->will($this->returnValue($this->_getConnectionMock()));
320-
321-
322320
$eavConfig->expects($this->any())->method('getAttribute')->will(
323321
$this->returnCallback(
324322
function ($entityType, $attributeCode) use ($attributes) {
@@ -337,19 +335,29 @@ public function productAttributesDataProvider()
337335
[
338336
'test_attr',
339337
$attributeSetId,
340-
['test_attr' => 'test_attr', 'attribute_set_id' => $attributeSetId, 'entity_id' => null],
338+
[
339+
'test_attr' => 'test_attr',
340+
'attribute_set_id' => $attributeSetId,
341+
'entity_id' => null,
342+
'store_id' => 1
343+
],
341344
null,
342345
],
343346
[
344347
'test_attr',
345348
$attributeSetId,
346-
['test_attr' => 'test_attr', 'attribute_set_id' => $attributeSetId, 'entity_id' => 12345],
349+
[
350+
'test_attr' => 'test_attr',
351+
'attribute_set_id' => $attributeSetId,
352+
'entity_id' => 12345,
353+
'store_id' => 1
354+
],
347355
['test_attr' => 'test_attr']
348356
],
349357
[
350358
'test_attr',
351359
$attributeSetId,
352-
['test_attr' => '99.99', 'attribute_set_id' => $attributeSetId, 'entity_id' => 12345],
360+
['test_attr' => '99.99', 'attribute_set_id' => $attributeSetId, 'entity_id' => 12345, 'store_id' => 1],
353361
['test_attr' => '99.9900']
354362
]
355363
];

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/CategoryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public function testSaveActionCategoryWithDangerRequest()
339339
);
340340
$this->dispatch('backend/catalog/category/save');
341341
$this->assertSessionMessages(
342-
$this->equalTo(['The value of attribute "is_active" must be set']),
342+
$this->equalTo(['The value of attribute "name" must be set']),
343343
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
344344
);
345345
}

0 commit comments

Comments
 (0)