Skip to content

Commit 7665a11

Browse files
ENGCOM-2413: [Forwardport] [TASK] Solve issue #14966 - Disabling product does not remove it from #16791
2 parents 1e0767f + 2245aac commit 7665a11

File tree

2 files changed

+72
-11
lines changed
  • app/code/Magento/Catalog
    • Model/Indexer/Product/Flat/Action
    • Test/Unit/Model/Indexer/Product/Flat/Action

2 files changed

+72
-11
lines changed

app/code/Magento/Catalog/Model/Indexer/Product/Flat/Action/Row.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function __construct(
6161
* @param int|null $id
6262
* @return \Magento\Catalog\Model\Indexer\Product\Flat\Action\Row
6363
* @throws \Magento\Framework\Exception\LocalizedException
64+
* @throws \Zend_Db_Statement_Exception
6465
*/
6566
public function execute($id = null)
6667
{
@@ -75,17 +76,43 @@ public function execute($id = null)
7576
if ($tableExists) {
7677
$this->flatItemEraser->removeDeletedProducts($ids, $store->getId());
7778
}
78-
if (isset($ids[0])) {
79-
if (!$tableExists) {
80-
$this->_flatTableBuilder->build(
81-
$store->getId(),
82-
[$ids[0]],
83-
$this->_valueFieldSuffix,
84-
$this->_tableDropSuffix,
85-
false
86-
);
79+
80+
/* @var $status \Magento\Eav\Model\Entity\Attribute */
81+
$status = $this->_productIndexerHelper->getAttribute('status');
82+
$statusTable = $status->getBackend()->getTable();
83+
$statusConditions = [
84+
'store_id IN(0,' . (int)$store->getId() . ')',
85+
'attribute_id = ' . (int)$status->getId(),
86+
'entity_id = ' . (int)$id
87+
];
88+
$select = $this->_connection->select();
89+
$select->from(
90+
$statusTable,
91+
['value']
92+
)->where(
93+
implode(' AND ', $statusConditions)
94+
)->order(
95+
'store_id DESC'
96+
);
97+
$result = $this->_connection->query($select);
98+
$status = $result->fetch(1);
99+
100+
if ($status['value'] == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) {
101+
if (isset($ids[0])) {
102+
if (!$tableExists) {
103+
$this->_flatTableBuilder->build(
104+
$store->getId(),
105+
[$ids[0]],
106+
$this->_valueFieldSuffix,
107+
$this->_tableDropSuffix,
108+
false
109+
);
110+
}
111+
$this->flatItemWriter->write($store->getId(), $ids[0], $this->_valueFieldSuffix);
87112
}
88-
$this->flatItemWriter->write($store->getId(), $ids[0], $this->_valueFieldSuffix);
113+
}
114+
if ($status['value'] == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED) {
115+
$this->flatItemEraser->deleteProductsFromStore($id, $store->getId());
89116
}
90117
}
91118
return $this;

app/code/Magento/Catalog/Test/Unit/Model/Indexer/Product/Flat/Action/RowTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1010

11+
/**
12+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13+
*/
1114
class RowTest extends \PHPUnit\Framework\TestCase
1215
{
1316
/**
@@ -59,6 +62,8 @@ protected function setUp()
5962
{
6063
$objectManager = new ObjectManager($this);
6164

65+
$attributeTable = 'catalog_product_entity_int';
66+
$statusId = 22;
6267
$this->connection = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
6368
$this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
6469
$this->resource->expects($this->any())->method('getConnection')
@@ -68,12 +73,41 @@ protected function setUp()
6873
$this->store = $this->createMock(\Magento\Store\Model\Store::class);
6974
$this->store->expects($this->any())->method('getId')->will($this->returnValue('store_id_1'));
7075
$this->storeManager->expects($this->any())->method('getStores')->will($this->returnValue([$this->store]));
71-
$this->productIndexerHelper = $this->createMock(\Magento\Catalog\Helper\Product\Flat\Indexer::class);
7276
$this->flatItemEraser = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\Action\Eraser::class);
7377
$this->flatItemWriter = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\Action\Indexer::class);
7478
$this->flatTableBuilder = $this->createMock(
7579
\Magento\Catalog\Model\Indexer\Product\Flat\FlatTableBuilder::class
7680
);
81+
$this->productIndexerHelper = $this->createMock(\Magento\Catalog\Helper\Product\Flat\Indexer::class);
82+
$statusAttributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
83+
->disableOriginalConstructor()
84+
->getMock();
85+
$this->productIndexerHelper->expects($this->any())->method('getAttribute')
86+
->with('status')
87+
->willReturn($statusAttributeMock);
88+
$backendMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class)
89+
->disableOriginalConstructor()
90+
->getMock();
91+
$backendMock->expects($this->any())->method('getTable')->willReturn($attributeTable);
92+
$statusAttributeMock->expects($this->any())->method('getBackend')->willReturn(
93+
$backendMock
94+
);
95+
$statusAttributeMock->expects($this->any())->method('getId')->willReturn($statusId);
96+
$selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
97+
->disableOriginalConstructor()
98+
->getMock();
99+
$this->connection->expects($this->any())->method('select')->willReturn($selectMock);
100+
$selectMock->expects($this->any())->method('from')->with(
101+
$attributeTable,
102+
['value']
103+
)->willReturnSelf();
104+
$selectMock->expects($this->any())->method('where')->willReturnSelf();
105+
$pdoMock = $this->createMock(\Zend_Db_Statement_Pdo::class);
106+
$this->connection->expects($this->any())
107+
->method('query')
108+
->with($selectMock)
109+
->will($this->returnValue($pdoMock));
110+
$pdoMock->expects($this->any())->method('fetch')->will($this->returnValue(['value' => 1]));
77111

78112
$this->model = $objectManager->getObject(
79113
\Magento\Catalog\Model\Indexer\Product\Flat\Action\Row::class,

0 commit comments

Comments
 (0)