Skip to content

Commit 965dc7e

Browse files
ENGCOM-1463: [TASK] Solve issue #14966 - Disabling product does not remove it from… #15019
- Merge Pull Request #15019 from magento/magento2:experius-2.2-patch-issue-14966 - Merged commits: 1. 0d35081 2. c6196d7 3. 2d6fb62 4. 38bd9d3 5. 219024c
2 parents ffac6ee + 219024c commit 965dc7e

File tree

2 files changed

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

2 files changed

+70
-12
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: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1212

13+
/**
14+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15+
*/
1316
class RowTest extends \PHPUnit\Framework\TestCase
1417
{
1518
/**
@@ -61,6 +64,8 @@ protected function setUp()
6164
{
6265
$objectManager = new ObjectManager($this);
6366

67+
$attributeTable = 'catalog_product_entity_int';
68+
$statusId = 22;
6469
$this->connection = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
6570
$this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
6671
$this->resource->expects($this->any())->method('getConnection')
@@ -70,10 +75,36 @@ protected function setUp()
7075
$this->store = $this->createMock(\Magento\Store\Model\Store::class);
7176
$this->store->expects($this->any())->method('getId')->will($this->returnValue('store_id_1'));
7277
$this->storeManager->expects($this->any())->method('getStores')->will($this->returnValue([$this->store]));
73-
$this->productIndexerHelper = $this->createMock(\Magento\Catalog\Helper\Product\Flat\Indexer::class);
7478
$this->flatItemEraser = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\Action\Eraser::class);
7579
$this->flatItemWriter = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\Action\Indexer::class);
7680
$this->flatTableBuilder = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\FlatTableBuilder::class);
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())->method('query')->with($selectMock)->will($this->returnValue($pdoMock));
107+
$pdoMock->expects($this->any())->method('fetch')->will($this->returnValue(['value' => 1]));
77108

78109
$this->model = $objectManager->getObject(
79110
\Magento\Catalog\Model\Indexer\Product\Flat\Action\Row::class, [
@@ -82,7 +113,7 @@ protected function setUp()
82113
'productHelper' => $this->productIndexerHelper,
83114
'flatItemEraser' => $this->flatItemEraser,
84115
'flatItemWriter' => $this->flatItemWriter,
85-
'flatTableBuilder' => $this->flatTableBuilder
116+
'flatTableBuilder' => $this->flatTableBuilder,
86117
]);
87118
}
88119

0 commit comments

Comments
 (0)