Skip to content

Commit 46bf790

Browse files
author
Magento CICD
authored
merge magento/2.2.6-develop into magento-chaika/chaika_226
2 parents 53ba739 + 04113a9 commit 46bf790

File tree

5 files changed

+122
-79
lines changed

5 files changed

+122
-79
lines changed

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

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,39 +56,36 @@ public function __construct(
5656
* @return \Magento\Catalog\Model\Indexer\Product\Flat
5757
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
5858
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
59+
* @SuppressWarnings(PHPMD.NPathComplexity)
5960
*/
6061
public function write($storeId, $productId, $valueFieldSuffix = '')
6162
{
6263
$flatTable = $this->_productIndexerHelper->getFlatTableName($storeId);
64+
$entityTableName = $this->_productIndexerHelper->getTable('catalog_product_entity');
6365

6466
$attributes = $this->_productIndexerHelper->getAttributes();
6567
$eavAttributes = $this->_productIndexerHelper->getTablesStructure($attributes);
6668
$updateData = [];
6769
$describe = $this->_connection->describeTable($flatTable);
70+
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
71+
$linkField = $metadata->getLinkField();
6872

6973
foreach ($eavAttributes as $tableName => $tableColumns) {
7074
$columnsChunks = array_chunk($tableColumns, self::ATTRIBUTES_CHUNK_SIZE, true);
7175

7276
foreach ($columnsChunks as $columns) {
7377
$select = $this->_connection->select();
74-
$selectValue = $this->_connection->select();
75-
$keyColumns = [
76-
'entity_id' => 'e.entity_id',
77-
'attribute_id' => 't.attribute_id',
78-
'value' => $this->_connection->getIfNullSql('`t2`.`value`', '`t`.`value`'),
79-
];
80-
81-
if ($tableName != $this->_productIndexerHelper->getTable('catalog_product_entity')) {
78+
79+
if ($tableName != $entityTableName) {
8280
$valueColumns = [];
8381
$ids = [];
8482
$select->from(
85-
['e' => $this->_productIndexerHelper->getTable('catalog_product_entity')],
86-
$keyColumns
87-
);
88-
89-
$selectValue->from(
90-
['e' => $this->_productIndexerHelper->getTable('catalog_product_entity')],
91-
$keyColumns
83+
['e' => $entityTableName],
84+
[
85+
'entity_id' => 'e.entity_id',
86+
'attribute_id' => 't.attribute_id',
87+
'value' => $this->_connection->getIfNullSql('`t2`.`value`', '`t`.`value`'),
88+
]
9289
);
9390

9491
/** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
@@ -97,8 +94,7 @@ public function write($storeId, $productId, $valueFieldSuffix = '')
9794
$ids[$attribute->getId()] = $columnName;
9895
}
9996
}
100-
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
101-
$select->joinLeft(
97+
$select->joinInner(
10298
['t' => $tableName],
10399
sprintf('e.%s = t.%s ', $linkField, $linkField) . $this->_connection->quoteInto(
104100
' AND t.attribute_id IN (?)',
@@ -116,8 +112,6 @@ public function write($storeId, $productId, $valueFieldSuffix = '')
116112
[]
117113
)->where(
118114
'e.entity_id = ' . $productId
119-
)->where(
120-
't.attribute_id IS NOT NULL'
121115
);
122116
$cursor = $this->_connection->query($select);
123117
while ($row = $cursor->fetch(\Zend_Db::FETCH_ASSOC)) {
@@ -157,7 +151,7 @@ public function write($storeId, $productId, $valueFieldSuffix = '')
157151
$columnNames[] = 'attribute_set_id';
158152
$columnNames[] = 'type_id';
159153
$select->from(
160-
['e' => $this->_productIndexerHelper->getTable('catalog_product_entity')],
154+
['e' => $entityTableName],
161155
$columnNames
162156
)->where(
163157
'e.entity_id = ' . $productId
@@ -175,7 +169,9 @@ public function write($storeId, $productId, $valueFieldSuffix = '')
175169

176170
if (!empty($updateData)) {
177171
$updateData += ['entity_id' => $productId];
178-
$updateData += ['row_id' => $productId];
172+
if ($linkField !== $metadata->getIdentifierField()) {
173+
$updateData += [$linkField => $productId];
174+
}
179175
$updateFields = [];
180176
foreach ($updateData as $key => $value) {
181177
$updateFields[$key] = $key;

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

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
*/
66
namespace Magento\Catalog\Model\Indexer\Product\Flat\Action;
77

8+
use Magento\Catalog\Api\Data\ProductInterface;
89
use Magento\Catalog\Model\Indexer\Product\Flat\FlatTableBuilder;
910
use Magento\Catalog\Model\Indexer\Product\Flat\TableBuilder;
11+
use Magento\Framework\EntityManager\MetadataPool;
1012

1113
/**
12-
* Class Row reindex action
14+
* Class Row reindex action.
15+
*
16+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1317
*/
1418
class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
1519
{
1620
/**
17-
* @var \Magento\Catalog\Model\Indexer\Product\Flat\Action\Indexer
21+
* @var Indexer
1822
*/
1923
protected $flatItemWriter;
2024

@@ -23,6 +27,11 @@ class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
2327
*/
2428
protected $flatItemEraser;
2529

30+
/**
31+
* @var MetadataPool
32+
*/
33+
private $metadataPool;
34+
2635
/**
2736
* @param \Magento\Framework\App\ResourceConnection $resource
2837
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -32,6 +41,7 @@ class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
3241
* @param FlatTableBuilder $flatTableBuilder
3342
* @param Indexer $flatItemWriter
3443
* @param Eraser $flatItemEraser
44+
* @param MetadataPool|null $metadataPool
3545
*/
3646
public function __construct(
3747
\Magento\Framework\App\ResourceConnection $resource,
@@ -41,7 +51,8 @@ public function __construct(
4151
TableBuilder $tableBuilder,
4252
FlatTableBuilder $flatTableBuilder,
4353
Indexer $flatItemWriter,
44-
Eraser $flatItemEraser
54+
Eraser $flatItemEraser,
55+
MetadataPool $metadataPool = null
4556
) {
4657
parent::__construct(
4758
$resource,
@@ -53,6 +64,8 @@ public function __construct(
5364
);
5465
$this->flatItemWriter = $flatItemWriter;
5566
$this->flatItemEraser = $flatItemEraser;
67+
$this->metadataPool = $metadataPool ?:
68+
\Magento\Framework\App\ObjectManager::getInstance()->get(MetadataPool::class);
5669
}
5770

5871
/**
@@ -61,7 +74,6 @@ public function __construct(
6174
* @param int|null $id
6275
* @return \Magento\Catalog\Model\Indexer\Product\Flat\Action\Row
6376
* @throws \Magento\Framework\Exception\LocalizedException
64-
* @throws \Zend_Db_Statement_Exception
6577
*/
6678
public function execute($id = null)
6779
{
@@ -71,50 +83,47 @@ public function execute($id = null)
7183
);
7284
}
7385
$ids = [$id];
74-
foreach ($this->_storeManager->getStores() as $store) {
86+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
87+
88+
$stores = $this->_storeManager->getStores();
89+
foreach ($stores as $store) {
7590
$tableExists = $this->_isFlatTableExists($store->getId());
7691
if ($tableExists) {
7792
$this->flatItemEraser->removeDeletedProducts($ids, $store->getId());
7893
}
7994

8095
/* @var $status \Magento\Eav\Model\Entity\Attribute */
81-
$status = $this->_productIndexerHelper->getAttribute('status');
96+
$status = $this->_productIndexerHelper->getAttribute(ProductInterface::STATUS);
8297
$statusTable = $status->getBackend()->getTable();
8398
$statusConditions = [
8499
'store_id IN(0,' . (int)$store->getId() . ')',
85100
'attribute_id = ' . (int)$status->getId(),
86-
'entity_id = ' . (int)$id
101+
$linkField . ' = ' . (int)$id,
87102
];
88103
$select = $this->_connection->select();
89-
$select->from(
90-
$statusTable,
91-
['value']
92-
)->where(
93-
implode(' AND ', $statusConditions)
94-
)->order(
95-
'store_id DESC'
96-
);
104+
$select->from($statusTable, ['value'])
105+
->where(implode(' AND ', $statusConditions))
106+
->order('store_id DESC')
107+
->limit(1);
97108
$result = $this->_connection->query($select);
98-
$status = $result->fetch(1);
109+
$status = $result->fetchColumn(0);
99110

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);
111+
if ($status == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) {
112+
if (!$tableExists) {
113+
$this->_flatTableBuilder->build(
114+
$store->getId(),
115+
$ids,
116+
$this->_valueFieldSuffix,
117+
$this->_tableDropSuffix,
118+
false
119+
);
112120
}
113-
}
114-
if ($status['value'] == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED) {
121+
$this->flatItemWriter->write($store->getId(), $id, $this->_valueFieldSuffix);
122+
} else {
115123
$this->flatItemEraser->deleteProductsFromStore($id, $store->getId());
116124
}
117125
}
126+
118127
return $this;
119128
}
120129
}

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

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
namespace Magento\Catalog\Test\Unit\Model\Indexer\Product\Flat\Action;
1010

11+
use Magento\Catalog\Api\Data\ProductInterface;
1112
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1214

1315
/**
1416
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -21,45 +23,48 @@ class RowTest extends \PHPUnit\Framework\TestCase
2123
protected $model;
2224

2325
/**
24-
* @var \PHPUnit_Framework_MockObject_MockObject
26+
* @var MockObject
2527
*/
2628
protected $storeManager;
2729

2830
/**
29-
* @var \PHPUnit_Framework_MockObject_MockObject
31+
* @var MockObject
3032
*/
3133
protected $store;
3234

3335
/**
34-
* @var \PHPUnit_Framework_MockObject_MockObject
36+
* @var MockObject
3537
*/
3638
protected $productIndexerHelper;
3739

3840
/**
39-
* @var \PHPUnit_Framework_MockObject_MockObject
41+
* @var MockObject
4042
*/
4143
protected $resource;
4244

4345
/**
44-
* @var \PHPUnit_Framework_MockObject_MockObject
46+
* @var MockObject
4547
*/
4648
protected $connection;
4749

4850
/**
49-
* @var \PHPUnit_Framework_MockObject_MockObject
51+
* @var MockObject
5052
*/
5153
protected $flatItemWriter;
5254

5355
/**
54-
* @var \PHPUnit_Framework_MockObject_MockObject
56+
* @var MockObject
5557
*/
5658
protected $flatItemEraser;
5759

5860
/**
59-
* @var \PHPUnit_Framework_MockObject_MockObject
61+
* @var MockObject
6062
*/
6163
protected $flatTableBuilder;
6264

65+
/**
66+
* @inheritdoc
67+
*/
6368
protected function setUp()
6469
{
6570
$objectManager = new ObjectManager($this);
@@ -70,11 +75,11 @@ protected function setUp()
7075
$this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
7176
$this->resource->expects($this->any())->method('getConnection')
7277
->with('default')
73-
->will($this->returnValue($this->connection));
78+
->willReturn($this->connection);
7479
$this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
7580
$this->store = $this->createMock(\Magento\Store\Model\Store::class);
76-
$this->store->expects($this->any())->method('getId')->will($this->returnValue('store_id_1'));
77-
$this->storeManager->expects($this->any())->method('getStores')->will($this->returnValue([$this->store]));
81+
$this->store->expects($this->any())->method('getId')->willReturn('store_id_1');
82+
$this->storeManager->expects($this->any())->method('getStores')->willReturn([$this->store]);
7883
$this->flatItemEraser = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\Action\Eraser::class);
7984
$this->flatItemWriter = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\Action\Indexer::class);
8085
$this->flatTableBuilder = $this->createMock(\Magento\Catalog\Model\Indexer\Product\Flat\FlatTableBuilder::class);
@@ -89,9 +94,7 @@ protected function setUp()
8994
->disableOriginalConstructor()
9095
->getMock();
9196
$backendMock->expects($this->any())->method('getTable')->willReturn($attributeTable);
92-
$statusAttributeMock->expects($this->any())->method('getBackend')->willReturn(
93-
$backendMock
94-
);
97+
$statusAttributeMock->expects($this->any())->method('getBackend')->willReturn($backendMock);
9598
$statusAttributeMock->expects($this->any())->method('getId')->willReturn($statusId);
9699
$selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
97100
->disableOriginalConstructor()
@@ -102,19 +105,30 @@ protected function setUp()
102105
['value']
103106
)->willReturnSelf();
104107
$selectMock->expects($this->any())->method('where')->willReturnSelf();
108+
$selectMock->expects($this->any())->method('order')->willReturnSelf();
109+
$selectMock->expects($this->any())->method('limit')->willReturnSelf();
105110
$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]));
111+
$this->connection->expects($this->any())->method('query')->with($selectMock)->willReturn($pdoMock);
112+
$pdoMock->expects($this->any())->method('fetchColumn')->willReturn('1');
113+
114+
$metadataPool = $this->createMock(\Magento\Framework\EntityManager\MetadataPool::class);
115+
$productMetadata = $this->getMockBuilder(\Magento\Framework\EntityManager\EntityMetadataInterface::class)
116+
->getMockForAbstractClass();
117+
$metadataPool->expects($this->any())->method('getMetadata')->with(ProductInterface::class)
118+
->willReturn($productMetadata);
119+
$productMetadata->expects($this->any())->method('getLinkField')->willReturn('entity_id');
108120

109121
$this->model = $objectManager->getObject(
110122
\Magento\Catalog\Model\Indexer\Product\Flat\Action\Row::class, [
111-
'resource' => $this->resource,
112-
'storeManager' => $this->storeManager,
113-
'productHelper' => $this->productIndexerHelper,
114-
'flatItemEraser' => $this->flatItemEraser,
115-
'flatItemWriter' => $this->flatItemWriter,
123+
'resource' => $this->resource,
124+
'storeManager' => $this->storeManager,
125+
'productHelper' => $this->productIndexerHelper,
126+
'flatItemEraser' => $this->flatItemEraser,
127+
'flatItemWriter' => $this->flatItemWriter,
116128
'flatTableBuilder' => $this->flatTableBuilder,
117129
]);
130+
131+
$objectManager->setBackwardCompatibleProperty($this->model, 'metadataPool', $metadataPool);
118132
}
119133

120134
/**
@@ -129,9 +143,9 @@ public function testExecuteWithEmptyId()
129143
public function testExecuteWithNonExistingFlatTablesCreatesTables()
130144
{
131145
$this->productIndexerHelper->expects($this->any())->method('getFlatTableName')
132-
->will($this->returnValue('store_flat_table'));
146+
->willReturn('store_flat_table');
133147
$this->connection->expects($this->any())->method('isTableExists')->with('store_flat_table')
134-
->will($this->returnValue(false));
148+
->willReturn(false);
135149
$this->flatItemEraser->expects($this->never())->method('removeDeletedProducts');
136150
$this->flatTableBuilder->expects($this->once())->method('build')->with('store_id_1', ['product_id_1']);
137151
$this->flatItemWriter->expects($this->once())->method('write')->with('store_id_1', 'product_id_1');
@@ -141,12 +155,11 @@ public function testExecuteWithNonExistingFlatTablesCreatesTables()
141155
public function testExecuteWithExistingFlatTablesCreatesTables()
142156
{
143157
$this->productIndexerHelper->expects($this->any())->method('getFlatTableName')
144-
->will($this->returnValue('store_flat_table'));
158+
->willReturn('store_flat_table');
145159
$this->connection->expects($this->any())->method('isTableExists')->with('store_flat_table')
146-
->will($this->returnValue(true));
160+
->willReturn(true);
147161
$this->flatItemEraser->expects($this->once())->method('removeDeletedProducts');
148162
$this->flatTableBuilder->expects($this->never())->method('build')->with('store_id_1', ['product_id_1']);
149163
$this->model->execute('product_id_1');
150164
}
151165
}
152-

0 commit comments

Comments
 (0)