Skip to content

Commit 61cb299

Browse files
committed
MC-36830: [Issue] Fix for empty category field values in REST calls
1 parent d31cd74 commit 61cb299

File tree

4 files changed

+126
-1
lines changed

4 files changed

+126
-1
lines changed

app/code/Magento/Catalog/Model/Indexer/Category/Product/Plugin/TableResolver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ public function afterGetTableName(
5555
string $result,
5656
$modelEntity
5757
) {
58-
if (!is_array($modelEntity) && $modelEntity === AbstractAction::MAIN_INDEX_TABLE) {
58+
if (!is_array($modelEntity) &&
59+
$modelEntity === AbstractAction::MAIN_INDEX_TABLE &&
60+
$this->storeManager->getStore()->getId()
61+
) {
5962
$catalogCategoryProductDimension = new Dimension(
6063
\Magento\Store\Model\Store::ENTITY,
6164
$this->storeManager->getStore()->getId()
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Test\Unit\Model\Indexer\Category\Product\Plugin;
9+
10+
use Magento\Catalog\Model\Indexer\Category\Product\Plugin\TableResolver;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver;
13+
use Magento\Store\Model\Store;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class TableResolverTest extends TestCase
18+
{
19+
/**
20+
* Tests replacing catalog_category_product_index table name
21+
*
22+
* @param int $storeId
23+
* @param string $tableName
24+
* @param string $expected
25+
* @dataProvider afterGetTableNameDataProvider
26+
*/
27+
public function testAfterGetTableName(int $storeId, string $tableName, string $expected): void
28+
{
29+
$storeManagerMock = $this->getMockForAbstractClass(StoreManagerInterface::class);
30+
31+
$storeMock = $this->getMockBuilder(Store::class)
32+
->onlyMethods(['getId'])
33+
->disableOriginalConstructor()
34+
->getMock();
35+
$storeMock->method('getId')
36+
->willReturn($storeId);
37+
38+
$storeManagerMock->method('getStore')->willReturn($storeMock);
39+
40+
$tableResolverMock = $this->getMockBuilder(IndexScopeResolver::class)
41+
->disableOriginalConstructor()
42+
->getMock();
43+
$tableResolverMock->method('resolve')->willReturn('catalog_category_product_index_store1');
44+
45+
$subjectMock = $this->getMockBuilder(ResourceConnection::class)
46+
->disableOriginalConstructor()
47+
->getMock();
48+
49+
$model = new TableResolver($storeManagerMock, $tableResolverMock);
50+
51+
$this->assertEquals(
52+
$expected,
53+
$model->afterGetTableName($subjectMock, $tableName, 'catalog_category_product_index')
54+
);
55+
}
56+
57+
/**
58+
* Data provider for testAfterGetTableName
59+
*
60+
* @return array
61+
*/
62+
public function afterGetTableNameDataProvider(): array
63+
{
64+
return [
65+
[
66+
'storeId' => 1,
67+
'tableName' => 'catalog_category_product_index',
68+
'expected' => 'catalog_category_product_index_store1'
69+
],
70+
[
71+
'storeId' => 0,
72+
'tableName' => 'catalog_category_product_index',
73+
'expected' => 'catalog_category_product_index'
74+
],
75+
];
76+
}
77+
}

app/code/Magento/Elasticsearch/Test/Unit/Model/ResourceModel/IndexTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,48 @@ public function testGetCategoryProductIndexData()
417417
);
418418
}
419419

420+
/**
421+
* Test getCategoryProductIndexData method for all stores
422+
*/
423+
public function testGetCategoryProductIndexDataForAllStores()
424+
{
425+
$connection = $this->connection;
426+
$select = $this->select;
427+
428+
$connection->expects($this->any())
429+
->method('select')
430+
->willReturn($select);
431+
432+
$select->expects($this->any())
433+
->method('from')
434+
->with(
435+
['catalog_category_product_index'],
436+
['category_id', 'product_id', 'position', 'store_id']
437+
)->willReturnSelf();
438+
439+
$select->expects($this->any())
440+
->method('where')
441+
->willReturnSelf();
442+
443+
$connection->expects($this->once())
444+
->method('fetchAll')
445+
->with($select)
446+
->willReturn([[
447+
'product_id' => 1,
448+
'category_id' => 1,
449+
'position' => 1,
450+
]]);
451+
452+
$this->assertEquals(
453+
[
454+
1 => [
455+
1 => 1,
456+
],
457+
],
458+
$this->model->getCategoryProductIndexData(0, [1])
459+
);
460+
}
461+
420462
/**
421463
* Test getMovedCategoryProductIds method
422464
*/

lib/internal/Magento/Framework/Indexer/ScopeResolver/IndexScopeResolver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function resolve($index, array $dimensions)
4444
{
4545
$tableNameParts = [];
4646
foreach ($dimensions as $dimension) {
47+
if (!$dimension->getValue()) {
48+
continue;
49+
}
4750
switch ($dimension->getName()) {
4851
case 'scope':
4952
$tableNameParts[$dimension->getName()] = $dimension->getName() . $this->getScopeId($dimension);

0 commit comments

Comments
 (0)