Skip to content

Commit c3b4dff

Browse files
committed
Merge branch 'ACP2E-2219' of https://github.com/magento-l3/magento2ce into PR-VK-2023-09-29
2 parents 42fe02b + eb858b6 commit c3b4dff

File tree

5 files changed

+161
-59
lines changed
  • app/code/Magento
    • CatalogSearch
      • Model/Indexer/Fulltext/Plugin/Product/Category/Action
      • etc
    • Elasticsearch
      • Model/Indexer/Fulltext/Plugin/Category/Product/Action
      • Test/Unit/Model/Indexer/Fulltext/Plugin/Category/Product/Action
  • lib/internal/Magento/Framework/Mview/View

5 files changed

+161
-59
lines changed

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Plugin/Product/Category/Action/Rows.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

app/code/Magento/CatalogSearch/etc/di.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@
3838
<type name="Magento\Catalog\Model\Product\Action">
3939
<plugin name="catalogsearchFulltextMassAction" type="Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product\Action"/>
4040
</type>
41-
<type name="Magento\Catalog\Model\Indexer\Product\Category\Action\Rows">
42-
<plugin name="catalogsearchFulltextCategoryAssignment" type="Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Product\Category\Action\Rows"/>
43-
</type>
4441
<type name="Magento\Store\Model\ResourceModel\Store">
4542
<plugin name="catalogsearchFulltextIndexerStoreView" type="Magento\CatalogSearch\Model\Indexer\Fulltext\Plugin\Store\View" />
4643
</type>

app/code/Magento/Elasticsearch/Model/Indexer/Fulltext/Plugin/Category/Product/Action/Rows.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function afterExecute(
8686

8787
$productIds = array_merge([], ...$productIds);
8888
if (!empty($productIds)) {
89-
$indexer->reindexList(array_unique($productIds));
89+
$indexer->getView()->getChangelog()->addList($productIds);
9090
}
9191
}
9292

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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\Elasticsearch\Test\Unit\Model\Indexer\Fulltext\Plugin\Category\Product\Action;
9+
10+
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
11+
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\DB\Select;
13+
use Magento\Store\Model\Store;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
use Magento\Framework\Indexer\IndexerRegistry;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
use Magento\Elasticsearch\Model\Indexer\Fulltext\Plugin\Category\Product\Action\Rows;
19+
use Magento\Catalog\Model\Indexer\Category\Product\Action\Rows as ActionRows;
20+
use Magento\Framework\App\ResourceConnection;
21+
use Magento\CatalogSearch\Model\Indexer\Fulltext as FulltextIndexer;
22+
23+
/**
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25+
*/
26+
class RowsTest extends TestCase
27+
{
28+
/**
29+
* @var IndexerRegistry|MockObject
30+
*/
31+
private $indexerRegistryMock;
32+
33+
/**
34+
* @var StoreManagerInterface|MockObject
35+
*/
36+
private $storeManagerMock;
37+
38+
/**
39+
* @var Select|MockObject
40+
*/
41+
private $selectMock;
42+
43+
/**
44+
* @var AdapterInterface|MockObject
45+
*/
46+
private $connectionMock;
47+
48+
/**
49+
* @var ResourceConnection|MockObject
50+
*/
51+
private $resourceMock;
52+
53+
/**
54+
* @var TableMaintainer|MockObject
55+
*/
56+
private $tableMaintainerMock;
57+
58+
/**
59+
* @var Rows
60+
*/
61+
private $plugin;
62+
public function setUp(): void
63+
{
64+
parent::setUp();
65+
$this->indexerRegistryMock = $this->createMock(IndexerRegistry::class);
66+
$this->storeManagerMock =
67+
$this->getMockBuilder(StoreManagerInterface::class)->getMockForAbstractClass();
68+
$this->connectionMock =
69+
$this->getMockBuilder(AdapterInterface::class)->getMockForAbstractClass();
70+
$this->selectMock = $this->createMock(Select::class);
71+
$this->connectionMock->expects($this->any())->method('select')->willReturn($this->selectMock);
72+
$this->tableMaintainerMock = $this->createMock(TableMaintainer::class);
73+
$this->resourceMock = $this->getMockBuilder(ResourceConnection::class)
74+
->disableOriginalConstructor()
75+
->getMock();
76+
$this->resourceMock->expects($this->any())
77+
->method('getConnection')
78+
->willReturn($this->connectionMock);
79+
$this->plugin = new Rows(
80+
$this->indexerRegistryMock,
81+
$this->storeManagerMock,
82+
$this->resourceMock,
83+
$this->tableMaintainerMock
84+
);
85+
}
86+
87+
/**
88+
* Test afterExecute method.
89+
*
90+
* @return void
91+
*/
92+
public function testAfterExecute(): void
93+
{
94+
$productToReindex = [1];
95+
$storeId = 1;
96+
$categoryIds = [4];
97+
$actionMock = $this->createMock(ActionRows::class);
98+
$storeMock = $this->createMock(Store::class);
99+
$storeMock->expects($this->once())->method('getId')->willReturn($storeId);
100+
$this->storeManagerMock->expects($this->once())->method('getStores')->willReturn([$storeMock]);
101+
$this->tableMaintainerMock->expects($this->once())->method('getMainTable')->with($storeId)->willReturn('table');
102+
103+
$this->getProductIdsFromIndex($productToReindex);
104+
$this->createIndexerMock($productToReindex);
105+
106+
$this->plugin->afterExecute($actionMock, $actionMock, $categoryIds);
107+
}
108+
109+
/**
110+
* Creates a mock for the indexer registry to add given ids to changelog.
111+
*
112+
* @param array $ids
113+
* @return void
114+
*/
115+
private function createIndexerMock(array $ids): void
116+
{
117+
//schedule catalogsearch indexer changes to improve row index performance instead of executing them right away
118+
$changelogMock = $this->createMock(\Magento\Framework\Mview\View\Changelog::class);
119+
$changelogMock->expects($this->once())->method('addList')->with($ids);
120+
$viewMock = $this->createMock(\Magento\Framework\Mview\ViewInterface::class);
121+
$viewMock->expects($this->once())->method('getChangelog')->willReturn($changelogMock);
122+
123+
$indexerMock = $this->createMock(\Magento\Framework\Indexer\IndexerInterface::class);
124+
$indexerMock->expects($this->once())->method('isScheduled')->willReturn(true);
125+
$indexerMock->expects($this->once())->method('getView')->willReturn($viewMock);
126+
127+
$this->indexerRegistryMock->expects($this->once())
128+
->method('get')
129+
->with(FulltextIndexer::INDEXER_ID)
130+
->willReturn($indexerMock);
131+
}
132+
133+
/**
134+
* Mocks the connection to return the given ids.
135+
*
136+
* @param array $ids
137+
* @return void
138+
*/
139+
private function getProductIdsFromIndex(array $ids): void
140+
{
141+
$this->selectMock->expects($this->any())->method('from')->with()->willReturnSelf();
142+
$this->selectMock->expects($this->any())->method('where')->willReturnSelf();
143+
$this->connectionMock->expects($this->any())->method('fetchCol')->willReturn($ids);
144+
}
145+
}

lib/internal/Magento/Framework/Mview/View/Changelog.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ class Changelog implements ChangelogInterface
2222
/**
2323
* Suffix for changelog table
2424
*/
25-
const NAME_SUFFIX = 'cl';
25+
public const NAME_SUFFIX = 'cl';
2626

2727
/**
2828
* Column name of changelog entity
2929
*/
30-
const COLUMN_NAME = 'entity_id';
30+
public const COLUMN_NAME = 'entity_id';
3131

3232
/**
3333
* Column name for Version ID
3434
*/
35-
const VERSION_ID_COLUMN_NAME = 'version_id';
35+
public const VERSION_ID_COLUMN_NAME = 'version_id';
3636

3737
/**
3838
* Database connection
@@ -304,4 +304,16 @@ public function getViewId()
304304
{
305305
return $this->viewId;
306306
}
307+
308+
/**
309+
* Add list of ids to changelog
310+
*
311+
* @param array $ids
312+
* @return void
313+
*/
314+
public function addList(array $ids): void
315+
{
316+
$changelogTableName = $this->resource->getTableName($this->getName());
317+
$this->connection->insertArray($changelogTableName, ['entity_id'], $ids);
318+
}
307319
}

0 commit comments

Comments
 (0)