Skip to content

Disabled flat: cast $attributeId as (int) in selects #26790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
abstract class AbstractCollection extends AbstractDb implements SourceProviderInterface
{
/**
* Attribute table alias prefix
* Define default prefix for attribute table alias
*/
const ATTRIBUTE_TABLE_ALIAS_PREFIX = 'at_';

Expand Down Expand Up @@ -495,7 +495,7 @@ public function addAttributeToSelect($attribute, $joinType = false)
$entity = clone $this->getEntity();
$attributes = $entity->loadAllAttributes()->getAttributesByCode();
foreach ($attributes as $attrCode => $attr) {
$this->_selectAttributes[$attrCode] = $attr->getId();
$this->_selectAttributes[$attrCode] = (int) $attr->getId();
}
} else {
if (isset($this->_joinAttributes[$attribute])) {
Expand All @@ -511,7 +511,7 @@ public function addAttributeToSelect($attribute, $joinType = false)
)
);
}
$this->_selectAttributes[$attrInstance->getAttributeCode()] = $attrInstance->getId();
$this->_selectAttributes[$attrInstance->getAttributeCode()] = (int) $attrInstance->getId();
}
return $this;
}
Expand Down Expand Up @@ -1173,7 +1173,7 @@ public function _loadAttributes($printQuery = false, $logQuery = false)
}
$attribute = $this->_eavConfig->getAttribute($entity->getType(), $attributeCode);
if ($attribute && !$attribute->isStatic()) {
$tableAttributes[$attribute->getBackendTable()][] = $attributeId;
$tableAttributes[$attribute->getBackendTable()][] = (int) $attributeId;
if (!isset($attributeTypes[$attribute->getBackendTable()])) {
$attributeTypes[$attribute->getBackendTable()] = $attribute->getBackendType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ protected function _construct()
{
return $this->_init(\Magento\Framework\DataObject::class, 'test_entity_model');
}

/**
* Retrieve collection empty item
*
* @return \Magento\Framework\DataObject
*/
public function getNewEmptyItem()
{
return new \Magento\Framework\DataObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
class AbstractCollectionTest extends \PHPUnit\Framework\TestCase
{
const ATTRIBUTE_CODE = 'any_attribute';
const ATTRIBUTE_ID_STRING = '15';
const ATTRIBUTE_ID_INT = 15;

/**
* @var AbstractCollectionStub|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -105,6 +109,26 @@ protected function setUp()
$entityMock = $this->createMock(\Magento\Eav\Model\Entity\AbstractEntity::class);
$entityMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock));
$entityMock->expects($this->any())->method('getDefaultAttributes')->will($this->returnValue([]));
$entityMock->expects($this->any())->method('getLinkField')->willReturn('entity_id');

$attributeMock = $this->createMock(\Magento\Eav\Model\Attribute::class);
$attributeMock->expects($this->any())->method('isStatic')->willReturn(false);
$attributeMock->expects($this->any())->method('getAttributeCode')->willReturn(self::ATTRIBUTE_CODE);
$attributeMock->expects($this->any())->method('getBackendTable')->willReturn('eav_entity_int');
$attributeMock->expects($this->any())->method('getBackendType')->willReturn('int');
$attributeMock->expects($this->any())->method('getId')->willReturn(self::ATTRIBUTE_ID_STRING);

$entityMock
->expects($this->any())
->method('getAttribute')
->with(self::ATTRIBUTE_CODE)
->willReturn($attributeMock);

$this->configMock
->expects($this->any())
->method('getAttribute')
->with(null, self::ATTRIBUTE_CODE)
->willReturn($attributeMock);

$this->validatorFactoryMock->expects(
$this->any()
Expand Down Expand Up @@ -193,6 +217,30 @@ public function testRemoveItemByKey($values, $count)
$this->assertNull($this->model->getItemById($testId));
}

/**
* @dataProvider getItemsDataProvider
*/
public function testAttributeIdIsInt($values)
{
$this->resourceHelperMock->expects($this->any())->method('getLoadAttributesSelectGroups')->willReturn([]);
$this->fetchStrategyMock->expects($this->any())->method('fetchAll')->will($this->returnValue($values));
$selectMock = $this->coreResourceMock->getConnection()->select();
$selectMock->expects($this->any())->method('from')->willReturn($selectMock);
$selectMock->expects($this->any())->method('join')->willReturn($selectMock);
$selectMock->expects($this->any())->method('where')->willReturn($selectMock);
$selectMock->expects($this->any())->method('columns')->willReturn($selectMock);

$this->model
->addAttributeToSelect(self::ATTRIBUTE_CODE)
->_loadEntities()
->_loadAttributes();

$_selectAttributesActualValue = $this->readAttribute($this->model, '_selectAttributes');

$this->assertAttributeEquals([self::ATTRIBUTE_CODE => self::ATTRIBUTE_ID_STRING], '_selectAttributes', $this->model);
$this->assertSame($_selectAttributesActualValue[self::ATTRIBUTE_CODE], self::ATTRIBUTE_ID_INT);
}

/**
* @return array
*/
Expand Down