Skip to content

Commit

Permalink
Merge pull request #1422 from magento-plankton/MAGETWO-71136
Browse files Browse the repository at this point in the history
[Plankton] Bugs
  • Loading branch information
Alexander Akimov authored Aug 18, 2017
2 parents e55bc07 + dfc3507 commit ac70547
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ public function testWalkAttributes()

$code = 'test_attr';
$set = 10;
$storeId = 100;

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

$object->setData('test_attr', 'test_attr');
$object->setData('attribute_set_id', $set);
$object->setData('store_id', $storeId);

$entityType = new \Magento\Framework\DataObject();
$entityType->setEntityTypeCode('test');
Expand Down
50 changes: 49 additions & 1 deletion app/code/Magento/Eav/Model/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Magento\Framework\App\Config\Element;
use Magento\Framework\App\ResourceConnection\Config;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
Expand Down Expand Up @@ -204,6 +205,13 @@ abstract class AbstractEntity extends AbstractResource implements EntityInterfac
*/
protected $objectRelationProcessor;

/**
* Attributes stored by scope (store id and attribute set id).
*
* @var array
*/
private $attributesByScope;

/**
* @param Context $context
* @param array $data
Expand Down Expand Up @@ -447,6 +455,20 @@ public function getAttribute($attribute)
return $attribute;
}

/**
* Adding attribute to entity by scope.
*
* @param AbstractAttribute $attribute
* @param string $suffix
* @return $this
*/
public function addAttributeByScope(AbstractAttribute $attribute, $suffix)
{
$attributeCode = $attribute->getAttributeCode();
$this->attributesByScope[$suffix][$attributeCode] = $attribute;
return $this->addAttribute($attribute);
}

/**
* Adding attribute to entity
*
Expand Down Expand Up @@ -572,6 +594,31 @@ protected function _isApplicableAttribute($object, $attribute)
return true;
}

/**
* Get attributes by scope
*
* @return array
*/
private function getAttributesByScope($suffix)
{
return !empty($this->attributesByScope[$suffix])
? $this->attributesByScope[$suffix]
: $this->getAttributesByCode();
}

/**
* Get attributes cache suffix.
*
* @param DataObject $object
* @return string
*/
private function getAttributesCacheSuffix(DataObject $object)
{
$attributeSetId = $object->getAttributeSetId() ?: 0;
$storeId = $object->getStoreId() ?: 0;
return $storeId . '-' . $attributeSetId;
}

/**
* Walk through the attributes and run method with optional arguments
*
Expand Down Expand Up @@ -607,7 +654,8 @@ public function walkAttributes($partMethod, array $args = [], $collectExceptionM
break;
}
$results = [];
foreach ($this->getAttributesByCode() as $attrCode => $attribute) {
$suffix = $this->getAttributesCacheSuffix($args[0]);
foreach ($this->getAttributesByScope($suffix) as $attrCode => $attribute) {
if (isset($args[0]) && is_object($args[0]) && !$this->_isApplicableAttribute($args[0], $attribute)) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Eav/Model/Entity/AttributeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function loadAllAttributes(AbstractEntity $resource, DataObject $object =
$attributes = $this->cache->getAttributes($typeCode, $suffix);
if ($attributes) {
foreach ($attributes as $attribute) {
$resource->addAttribute($attribute);
$resource->addAttributeByScope($attribute, $suffix);
}
return $resource;
}
Expand Down
18 changes: 13 additions & 5 deletions app/code/Magento/Eav/Test/Unit/Model/Entity/AbstractEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,6 @@ public function testSave($attributeCode, $attributeSetId, $productData, $product
->getMock();
$model->expects($this->any())->method('_getValue')->will($this->returnValue($eavConfig));
$model->expects($this->any())->method('getConnection')->will($this->returnValue($this->_getConnectionMock()));


$eavConfig->expects($this->any())->method('getAttribute')->will(
$this->returnCallback(
function ($entityType, $attributeCode) use ($attributes) {
Expand All @@ -337,19 +335,29 @@ public function productAttributesDataProvider()
[
'test_attr',
$attributeSetId,
['test_attr' => 'test_attr', 'attribute_set_id' => $attributeSetId, 'entity_id' => null],
[
'test_attr' => 'test_attr',
'attribute_set_id' => $attributeSetId,
'entity_id' => null,
'store_id' => 1
],
null,
],
[
'test_attr',
$attributeSetId,
['test_attr' => 'test_attr', 'attribute_set_id' => $attributeSetId, 'entity_id' => 12345],
[
'test_attr' => 'test_attr',
'attribute_set_id' => $attributeSetId,
'entity_id' => 12345,
'store_id' => 1
],
['test_attr' => 'test_attr']
],
[
'test_attr',
$attributeSetId,
['test_attr' => '99.99', 'attribute_set_id' => $attributeSetId, 'entity_id' => 12345],
['test_attr' => '99.99', 'attribute_set_id' => $attributeSetId, 'entity_id' => 12345, 'store_id' => 1],
['test_attr' => '99.9900']
]
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public function testSaveActionCategoryWithDangerRequest()
);
$this->dispatch('backend/catalog/category/save');
$this->assertSessionMessages(
$this->equalTo(['The value of attribute "is_active" must be set']),
$this->equalTo(['The value of attribute "name" must be set']),
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
);
}
Expand Down

0 comments on commit ac70547

Please sign in to comment.