Skip to content

Commit 45030f3

Browse files
Merge remote-tracking branch 'remotes/github/MAGETWO-59400' into EPAM-PR-59
2 parents b70d2ec + c4f1e33 commit 45030f3

File tree

3 files changed

+173
-18
lines changed
  • app/code/Magento/Catalog/Model/Indexer/Product/Flat
  • dev/tests/integration
    • framework/Magento/TestFramework/Catalog/Model/Indexer/Product/Flat/Action
    • testsuite/Magento/Catalog/Model/Indexer/Product/Flat/Action

3 files changed

+173
-18
lines changed

app/code/Magento/Catalog/Model/Indexer/Product/Flat/AbstractAction.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ protected function _reindex($storeId, array $changedIds = [])
169169
}
170170

171171
/**
172-
* Retrieve Product Type Instances
173-
* as key - type code, value - instance model
172+
* Retrieve Product Type Instances as key - type code, value - instance model
174173
*
175174
* @return array
176175
*/
@@ -213,17 +212,19 @@ protected function _updateRelationProducts($storeId, $productIds = null)
213212
) {
214213
$columns = $this->_productIndexerHelper->getFlatColumns();
215214
$fieldList = array_keys($columns);
216-
unset($columns['entity_id']);
217-
unset($columns['child_id']);
218-
unset($columns['is_child']);
215+
unset(
216+
$columns['entity_id'],
217+
$columns['child_id'],
218+
$columns['is_child']
219+
);
219220
/** @var $select \Magento\Framework\DB\Select */
220221
$select = $this->_connection->select()->from(
221222
['t' => $this->_productIndexerHelper->getTable($relation->getTable())],
222-
[$relation->getChildFieldName(), new \Zend_Db_Expr('1')]
223+
['entity_table.entity_id', $relation->getChildFieldName(), new \Zend_Db_Expr('1')]
223224
)->join(
224225
['entity_table' => $this->_connection->getTableName('catalog_product_entity')],
225-
'entity_table.' . $metadata->getLinkField() . 't.' . $relation->getParentFieldName(),
226-
[$relation->getParentFieldName() => 'entity_table.entity_id']
226+
"entity_table.{$metadata->getLinkField()} = t.{$relation->getParentFieldName()}",
227+
[]
227228
)->join(
228229
['e' => $this->_productIndexerHelper->getFlatTableName($storeId)],
229230
"e.entity_id = t.{$relation->getChildFieldName()}",
@@ -232,10 +233,10 @@ protected function _updateRelationProducts($storeId, $productIds = null)
232233
if ($relation->getWhere() !== null) {
233234
$select->where($relation->getWhere());
234235
}
235-
if ($productIds !== null) {
236+
if (!empty($productIds)) {
236237
$cond = [
237238
$this->_connection->quoteInto("{$relation->getChildFieldName()} IN(?)", $productIds),
238-
$this->_connection->quoteInto("entity_table.entity_id IN(?)", $productIds),
239+
$this->_connection->quoteInto('entity_table.entity_id IN(?)', $productIds),
239240
];
240241

241242
$select->where(implode(' OR ', $cond));
@@ -273,15 +274,11 @@ protected function _cleanRelationProducts($storeId)
273274
$select = $this->_connection->select()->distinct(
274275
true
275276
)->from(
276-
['t' => $this->_productIndexerHelper->getTable($relation->getTable())],
277-
[]
278-
)->join(
279-
['entity_table' => $this->_connection->getTableName('catalog_product_entity')],
280-
'entity_table.' . $metadata->getLinkField() . 't.' . $relation->getParentFieldName(),
281-
[$relation->getParentFieldName() => 'entity_table.entity_id']
277+
$this->_productIndexerHelper->getTable($relation->getTable()),
278+
$relation->getParentFieldName()
282279
);
283280
$joinLeftCond = [
284-
"e.entity_id = entity_table.entity_id",
281+
"e.{$metadata->getLinkField()} = t.{$relation->getParentFieldName()}",
285282
"e.child_id = t.{$relation->getChildFieldName()}",
286283
];
287284
if ($relation->getWhere() !== null) {
@@ -302,7 +299,7 @@ protected function _cleanRelationProducts($storeId)
302299
'e.is_child = ?',
303300
1
304301
)->where(
305-
'e.entity_id IN(?)',
302+
"e.{$metadata->getLinkField()} IN(?)",
306303
$entitySelect
307304
)->where(
308305
"t.{$relation->getChildFieldName()} IS NULL"
@@ -335,6 +332,8 @@ protected function _isFlatTableExists($storeId)
335332
}
336333

337334
/**
335+
* Get Metadata Pool
336+
*
338337
* @return \Magento\Framework\EntityManager\MetadataPool
339338
*/
340339
private function getMetadataPool()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\TestFramework\Catalog\Model\Indexer\Product\Flat\Action;
10+
11+
/**
12+
* Class Full reindex action
13+
*/
14+
class Full extends \Magento\Catalog\Model\Indexer\Product\Flat\Action\Full
15+
{
16+
/**
17+
* List of product types available in installation
18+
*
19+
* @var array
20+
*/
21+
protected $_productTypes;
22+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Catalog\Model\Indexer\Product\Flat\Action;
10+
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\TestFramework\Catalog\Model\Indexer\Product\Flat\Action\Full as FlatIndexerFull;
15+
16+
/**
17+
* Test relation customization
18+
*/
19+
class RelationTest extends \Magento\TestFramework\Indexer\TestCase
20+
{
21+
/**
22+
* @var FlatIndexerFull
23+
*/
24+
private $indexer;
25+
26+
/**
27+
* @var StoreManagerInterface
28+
*/
29+
private $storeManager;
30+
31+
/**
32+
* @var AdapterInterface
33+
*/
34+
private $connection;
35+
36+
/**
37+
* Updated flat tables
38+
*
39+
* @var array
40+
*/
41+
private $flatUpdated = [];
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function setUp()
47+
{
48+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
49+
50+
$tableBuilderMock = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\TableBuilder::class);
51+
$flatTableBuilderMock = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\FlatTableBuilder::class);
52+
53+
$productIndexerHelper = $objectManager->create(
54+
\Magento\Catalog\Helper\Product\Flat\Indexer::class,
55+
['addChildData' => 1]
56+
);
57+
$this->indexer = $objectManager->create(
58+
FlatIndexerFull::class,
59+
[
60+
'productHelper' => $productIndexerHelper,
61+
'tableBuilder' => $tableBuilderMock,
62+
'flatTableBuilder' => $flatTableBuilderMock
63+
]
64+
);
65+
$this->storeManager = $objectManager->create(StoreManagerInterface::class);
66+
$this->connection = $objectManager->get(ResourceConnection::class)->getConnection();
67+
68+
foreach ($this->storeManager->getStores() as $store) {
69+
$flatTable = $productIndexerHelper->getFlatTableName($store->getId());
70+
if ($this->connection->isTableExists($flatTable) &&
71+
!$this->connection->tableColumnExists($flatTable, 'child_id') &&
72+
!$this->connection->tableColumnExists($flatTable, 'is_child')
73+
) {
74+
$this->connection->addColumn(
75+
$flatTable,
76+
'child_id',
77+
[
78+
'type' => 'integer',
79+
'length' => null,
80+
'unsigned' => true,
81+
'nullable' => true,
82+
'default' => null,
83+
'unique' => true,
84+
'comment' => 'Child Id',
85+
]
86+
);
87+
$this->connection->addColumn(
88+
$flatTable,
89+
'is_child',
90+
[
91+
'type' => 'smallint',
92+
'length' => 1,
93+
'unsigned' => true,
94+
'nullable' => false,
95+
'default' => '0',
96+
'comment' => 'Checks If Entity Is Child',
97+
]
98+
);
99+
100+
$this->flatUpdated[] = $flatTable;
101+
}
102+
}
103+
}
104+
105+
/**
106+
* @inheritdoc
107+
*/
108+
protected function tearDown()
109+
{
110+
foreach ($this->flatUpdated as $flatTable) {
111+
$this->connection->dropColumn($flatTable, 'child_id');
112+
$this->connection->dropColumn($flatTable, 'is_child');
113+
}
114+
}
115+
116+
/**
117+
* Test that SQL generated for relation customization is valid
118+
*
119+
* @return void
120+
* @throws \Magento\Framework\Exception\LocalizedException
121+
* @throws \Exception
122+
*/
123+
public function testExecute() : void
124+
{
125+
try {
126+
$this->indexer->execute();
127+
} catch (\Magento\Framework\Exception\LocalizedException $e) {
128+
if ($e->getPrevious() instanceof \Zend_Db_Statement_Exception) {
129+
$this->fail($e->getMessage());
130+
}
131+
throw $e;
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)