Skip to content

Commit 46ce8f6

Browse files
ENGCOM-6908: Disabled flat: cast $attributeId as (int) in selects #26790
- Merge Pull Request #26790 from ilnytskyi/magento2:disable-flat-eav-int-select - Merged commits: 1. 4853eea 2. 2397bec
2 parents 5c5b60c + 2397bec commit 46ce8f6

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
abstract class AbstractCollection extends AbstractDb implements SourceProviderInterface
2525
{
2626
/**
27-
* Attribute table alias prefix
27+
* Define default prefix for attribute table alias
2828
*/
2929
const ATTRIBUTE_TABLE_ALIAS_PREFIX = 'at_';
3030

@@ -495,7 +495,7 @@ public function addAttributeToSelect($attribute, $joinType = false)
495495
$entity = clone $this->getEntity();
496496
$attributes = $entity->loadAllAttributes()->getAttributesByCode();
497497
foreach ($attributes as $attrCode => $attr) {
498-
$this->_selectAttributes[$attrCode] = $attr->getId();
498+
$this->_selectAttributes[$attrCode] = (int) $attr->getId();
499499
}
500500
} else {
501501
if (isset($this->_joinAttributes[$attribute])) {
@@ -511,7 +511,7 @@ public function addAttributeToSelect($attribute, $joinType = false)
511511
)
512512
);
513513
}
514-
$this->_selectAttributes[$attrInstance->getAttributeCode()] = $attrInstance->getId();
514+
$this->_selectAttributes[$attrInstance->getAttributeCode()] = (int) $attrInstance->getId();
515515
}
516516
return $this;
517517
}
@@ -1173,7 +1173,7 @@ public function _loadAttributes($printQuery = false, $logQuery = false)
11731173
}
11741174
$attribute = $this->_eavConfig->getAttribute($entity->getType(), $attributeCode);
11751175
if ($attribute && !$attribute->isStatic()) {
1176-
$tableAttributes[$attribute->getBackendTable()][] = $attributeId;
1176+
$tableAttributes[$attribute->getBackendTable()][] = (int) $attributeId;
11771177
if (!isset($attributeTypes[$attribute->getBackendTable()])) {
11781178
$attributeTypes[$attribute->getBackendTable()] = $attribute->getBackendType();
11791179
}

app/code/Magento/Eav/Test/Unit/Model/Entity/Collection/AbstractCollectionStub.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ protected function _construct()
3030
{
3131
return $this->_init(\Magento\Framework\DataObject::class, 'test_entity_model');
3232
}
33+
34+
/**
35+
* Retrieve collection empty item
36+
*
37+
* @return \Magento\Framework\DataObject
38+
*/
39+
public function getNewEmptyItem()
40+
{
41+
return new \Magento\Framework\DataObject();
42+
}
3343
}

app/code/Magento/Eav/Test/Unit/Model/Entity/Collection/AbstractCollectionTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
*/
1313
class AbstractCollectionTest extends \PHPUnit\Framework\TestCase
1414
{
15+
const ATTRIBUTE_CODE = 'any_attribute';
16+
const ATTRIBUTE_ID_STRING = '15';
17+
const ATTRIBUTE_ID_INT = 15;
18+
1519
/**
1620
* @var AbstractCollectionStub|\PHPUnit_Framework_MockObject_MockObject
1721
*/
@@ -105,6 +109,26 @@ protected function setUp()
105109
$entityMock = $this->createMock(\Magento\Eav\Model\Entity\AbstractEntity::class);
106110
$entityMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock));
107111
$entityMock->expects($this->any())->method('getDefaultAttributes')->will($this->returnValue([]));
112+
$entityMock->expects($this->any())->method('getLinkField')->willReturn('entity_id');
113+
114+
$attributeMock = $this->createMock(\Magento\Eav\Model\Attribute::class);
115+
$attributeMock->expects($this->any())->method('isStatic')->willReturn(false);
116+
$attributeMock->expects($this->any())->method('getAttributeCode')->willReturn(self::ATTRIBUTE_CODE);
117+
$attributeMock->expects($this->any())->method('getBackendTable')->willReturn('eav_entity_int');
118+
$attributeMock->expects($this->any())->method('getBackendType')->willReturn('int');
119+
$attributeMock->expects($this->any())->method('getId')->willReturn(self::ATTRIBUTE_ID_STRING);
120+
121+
$entityMock
122+
->expects($this->any())
123+
->method('getAttribute')
124+
->with(self::ATTRIBUTE_CODE)
125+
->willReturn($attributeMock);
126+
127+
$this->configMock
128+
->expects($this->any())
129+
->method('getAttribute')
130+
->with(null, self::ATTRIBUTE_CODE)
131+
->willReturn($attributeMock);
108132

109133
$this->validatorFactoryMock->expects(
110134
$this->any()
@@ -193,6 +217,30 @@ public function testRemoveItemByKey($values, $count)
193217
$this->assertNull($this->model->getItemById($testId));
194218
}
195219

220+
/**
221+
* @dataProvider getItemsDataProvider
222+
*/
223+
public function testAttributeIdIsInt($values)
224+
{
225+
$this->resourceHelperMock->expects($this->any())->method('getLoadAttributesSelectGroups')->willReturn([]);
226+
$this->fetchStrategyMock->expects($this->any())->method('fetchAll')->will($this->returnValue($values));
227+
$selectMock = $this->coreResourceMock->getConnection()->select();
228+
$selectMock->expects($this->any())->method('from')->willReturn($selectMock);
229+
$selectMock->expects($this->any())->method('join')->willReturn($selectMock);
230+
$selectMock->expects($this->any())->method('where')->willReturn($selectMock);
231+
$selectMock->expects($this->any())->method('columns')->willReturn($selectMock);
232+
233+
$this->model
234+
->addAttributeToSelect(self::ATTRIBUTE_CODE)
235+
->_loadEntities()
236+
->_loadAttributes();
237+
238+
$_selectAttributesActualValue = $this->readAttribute($this->model, '_selectAttributes');
239+
240+
$this->assertAttributeEquals([self::ATTRIBUTE_CODE => self::ATTRIBUTE_ID_STRING], '_selectAttributes', $this->model);
241+
$this->assertSame($_selectAttributesActualValue[self::ATTRIBUTE_CODE], self::ATTRIBUTE_ID_INT);
242+
}
243+
196244
/**
197245
* @return array
198246
*/

0 commit comments

Comments
 (0)