Skip to content

Commit 18598c5

Browse files
[Solve issue magento#14966 - Disabling product does not remove it from… magento#15019]
1 parent c41ffe5 commit 18598c5

File tree

2 files changed

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

2 files changed

+72
-14
lines changed

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

Lines changed: 37 additions & 11 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,19 +76,44 @@ 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+
if ($status['value'] == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) {
100+
if (isset($ids[0])) {
101+
if (!$tableExists) {
102+
$this->_flatTableBuilder->build(
103+
$store->getId(),
104+
[$ids[0]],
105+
$this->_valueFieldSuffix,
106+
$this->_tableDropSuffix,
107+
false
108+
);
109+
}
110+
$this->flatItemWriter->write($store->getId(), $ids[0], $this->_valueFieldSuffix);
87111
}
88-
$this->flatItemWriter->write($store->getId(), $ids[0], $this->_valueFieldSuffix);
112+
}
113+
if ($status['value'] == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED) {
114+
$this->flatItemEraser->deleteProductsFromStore($id, $store->getId());
89115
}
90116
}
91117
return $this;
92118
}
93-
}
119+
}

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

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

11+
12+
/**
13+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14+
*/
1115
class RowTest extends \PHPUnit\Framework\TestCase
1216
{
1317
/**
@@ -59,6 +63,8 @@ protected function setUp()
5963
{
6064
$objectManager = new ObjectManager($this);
6165

66+
$attributeTable = 'catalog_product_entity_int';
67+
$statusId = 22;
6268
$this->connection = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
6369
$this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
6470
$this->resource->expects($this->any())->method('getConnection')
@@ -68,12 +74,38 @@ protected function setUp()
6874
$this->store = $this->createMock(\Magento\Store\Model\Store::class);
6975
$this->store->expects($this->any())->method('getId')->will($this->returnValue('store_id_1'));
7076
$this->storeManager->expects($this->any())->method('getStores')->will($this->returnValue([$this->store]));
71-
$this->productIndexerHelper = $this->createMock(\Magento\Catalog\Helper\Product\Flat\Indexer::class);
7277
$this->flatItemEraser = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\Action\Eraser::class);
7378
$this->flatItemWriter = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\Action\Indexer::class);
7479
$this->flatTableBuilder = $this->createMock(
7580
\Magento\Catalog\Model\Indexer\Product\Flat\FlatTableBuilder::class
7681
);
82+
$this->productIndexerHelper = $this->createMock(\Magento\Catalog\Helper\Product\Flat\Indexer::class);
83+
$statusAttributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
84+
->disableOriginalConstructor()
85+
->getMock();
86+
$this->productIndexerHelper->expects($this->any())->method('getAttribute')
87+
->with('status')
88+
->willReturn($statusAttributeMock);
89+
$backendMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend::class)
90+
->disableOriginalConstructor()
91+
->getMock();
92+
$backendMock->expects($this->any())->method('getTable')->willReturn($attributeTable);
93+
$statusAttributeMock->expects($this->any())->method('getBackend')->willReturn(
94+
$backendMock
95+
);
96+
$statusAttributeMock->expects($this->any())->method('getId')->willReturn($statusId);
97+
$selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
98+
->disableOriginalConstructor()
99+
->getMock();
100+
$this->connection->expects($this->any())->method('select')->willReturn($selectMock);
101+
$selectMock->expects($this->any())->method('from')->with(
102+
$attributeTable,
103+
['value']
104+
)->willReturnSelf();
105+
$selectMock->expects($this->any())->method('where')->willReturnSelf();
106+
$pdoMock = $this->createMock(\Zend_Db_Statement_Pdo::class);
107+
$this->connection->expects($this->any())->method('query')->with($selectMock)->will($this->returnValue($pdoMock));
108+
$pdoMock->expects($this->any())->method('fetch')->will($this->returnValue(['value' => 1]));
77109

78110
$this->model = $objectManager->getObject(
79111
\Magento\Catalog\Model\Indexer\Product\Flat\Action\Row::class,
@@ -83,7 +115,7 @@ protected function setUp()
83115
'productHelper' => $this->productIndexerHelper,
84116
'flatItemEraser' => $this->flatItemEraser,
85117
'flatItemWriter' => $this->flatItemWriter,
86-
'flatTableBuilder' => $this->flatTableBuilder
118+
'flatTableBuilder' => $this->flatTableBuilder,
87119
]
88120
);
89121
}
@@ -119,4 +151,4 @@ public function testExecuteWithExistingFlatTablesCreatesTables()
119151
$this->flatTableBuilder->expects($this->never())->method('build')->with('store_id_1', ['product_id_1']);
120152
$this->model->execute('product_id_1');
121153
}
122-
}
154+
}

0 commit comments

Comments
 (0)