Skip to content

Commit 011b3e2

Browse files
committed
MC-38168: Manual Indexer after Merchandising - Empty Catalog/ Number of products incorrect
1 parent f65a214 commit 011b3e2

File tree

2 files changed

+254
-1
lines changed
  • app/code/Magento/Catalog
    • Model/Indexer/Product/Category/Action
    • Test/Unit/Model/Indexer/Product/Category/Action

2 files changed

+254
-1
lines changed

app/code/Magento/Catalog/Model/Indexer/Product/Category/Action/Rows.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Magento\Framework\DB\Adapter\AdapterInterface;
1919
use Magento\Framework\Indexer\IndexerRegistry;
2020
use Magento\Catalog\Model\Indexer\Category\Product as CategoryProductIndexer;
21+
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
2122
use Magento\Indexer\Model\WorkingStateProvider;
2223

2324
/**
@@ -60,6 +61,7 @@ class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractActio
6061
* @param Config $config
6162
* @param QueryGenerator|null $queryGenerator
6263
* @param MetadataPool|null $metadataPool
64+
* @param TableMaintainer|null $tableMaintainer
6365
* @param CacheContext|null $cacheContext
6466
* @param EventManagerInterface|null $eventManager
6567
* @param IndexerRegistry|null $indexerRegistry
@@ -71,12 +73,13 @@ public function __construct(
7173
Config $config,
7274
QueryGenerator $queryGenerator = null,
7375
MetadataPool $metadataPool = null,
76+
?TableMaintainer $tableMaintainer = null,
7477
CacheContext $cacheContext = null,
7578
EventManagerInterface $eventManager = null,
7679
IndexerRegistry $indexerRegistry = null,
7780
?WorkingStateProvider $workingStateProvider = null
7881
) {
79-
parent::__construct($resource, $storeManager, $config, $queryGenerator, $metadataPool);
82+
parent::__construct($resource, $storeManager, $config, $queryGenerator, $metadataPool, $tableMaintainer);
8083
$this->cacheContext = $cacheContext ?: ObjectManager::getInstance()->get(CacheContext::class);
8184
$this->eventManager = $eventManager ?: ObjectManager::getInstance()->get(EventManagerInterface::class);
8285
$this->indexerRegistry = $indexerRegistry ?: ObjectManager::getInstance()->get(IndexerRegistry::class);
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
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\Product\Category\Action;
9+
10+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\Store\Model\Store;
13+
use Magento\Catalog\Model\Config;
14+
use Magento\Catalog\Model\Indexer\Product\Category\Action\Rows;
15+
use Magento\Catalog\Model\Indexer\Category\Product as CategoryProductIndexer;
16+
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
17+
use Magento\Indexer\Model\WorkingStateProvider;
18+
use Magento\Framework\EntityManager\EntityMetadataInterface;
19+
use Magento\Framework\App\ResourceConnection;
20+
use Magento\Framework\DB\Query\Generator as QueryGenerator;
21+
use Magento\Framework\DB\Adapter\AdapterInterface;
22+
use Magento\Framework\DB\Select;
23+
use Magento\Framework\DB\Ddl\Table;
24+
use Magento\Framework\EntityManager\MetadataPool;
25+
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
26+
use Magento\Framework\Indexer\IndexerRegistry;
27+
use Magento\Framework\Indexer\CacheContext;
28+
use Magento\Framework\Indexer\IndexerInterface;
29+
use PHPUnit\Framework\MockObject\MockObject;
30+
use PHPUnit\Framework\TestCase;
31+
32+
/**
33+
* Test for Rows action
34+
*/
35+
class RowsTest extends TestCase
36+
{
37+
/**
38+
* @var WorkingStateProvider|MockObject
39+
*/
40+
private $workingStateProvider;
41+
42+
/**
43+
* @var ResourceConnection|MockObject
44+
*/
45+
private $resource;
46+
47+
/**
48+
* @var StoreManagerInterface|MockObject
49+
*/
50+
private $storeManager;
51+
52+
/**
53+
* @var Config|MockObject
54+
*/
55+
private $config;
56+
57+
/**
58+
* @var QueryGenerator|MockObject
59+
*/
60+
private $queryGenerator;
61+
62+
/**
63+
* @var MetadataPool|MockObject
64+
*/
65+
private $metadataPool;
66+
67+
/**
68+
* @var CacheContext|MockObject
69+
*/
70+
private $cacheContext;
71+
72+
/**
73+
* @var EventManagerInterface|MockObject
74+
*/
75+
private $eventManager;
76+
77+
/**
78+
* @var IndexerRegistry|MockObject
79+
*/
80+
private $indexerRegistry;
81+
82+
/**
83+
* @var TableMaintainer|MockObject
84+
*/
85+
private $tableMaintainer;
86+
87+
/**
88+
* @var IndexerInterface|MockObject
89+
*/
90+
private $indexer;
91+
92+
/**
93+
* @var AdapterInterface|MockObject
94+
*/
95+
private $connection;
96+
97+
/**
98+
* @var Select|MockObject
99+
*/
100+
private $select;
101+
102+
/**
103+
* @var Rows
104+
*/
105+
private $rowsModel;
106+
107+
protected function setUp() : void
108+
{
109+
$this->workingStateProvider = $this->getMockBuilder(WorkingStateProvider::class)
110+
->disableOriginalConstructor()
111+
->getMock();
112+
$this->resource = $this->getMockBuilder(ResourceConnection::class)
113+
->disableOriginalConstructor()
114+
->getMock();
115+
$this->connection = $this->getMockBuilder(AdapterInterface::class)
116+
->getMockForAbstractClass();
117+
$this->resource->expects($this->any())
118+
->method('getConnection')
119+
->willReturn($this->connection);
120+
$this->select = $this->getMockBuilder(Select::class)
121+
->disableOriginalConstructor()
122+
->getMock();
123+
$this->select->expects($this->any())
124+
->method('from')
125+
->willReturnSelf();
126+
$this->select->expects($this->any())
127+
->method('where')
128+
->willReturnSelf();
129+
$this->select->expects($this->any())
130+
->method('distinct')
131+
->willReturnSelf();
132+
$this->select->expects($this->any())
133+
->method('joinInner')
134+
->willReturnSelf();
135+
$this->select->expects($this->any())
136+
->method('group')
137+
->willReturnSelf();
138+
$this->select->expects($this->any())
139+
->method('joinLeft')
140+
->willReturnSelf();
141+
$this->select->expects($this->any())
142+
->method('columns')
143+
->willReturnSelf();
144+
$this->connection->expects($this->any())
145+
->method('select')
146+
->willReturn($this->select);
147+
$this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)
148+
->getMockForAbstractClass();
149+
$this->config = $this->getMockBuilder(Config::class)
150+
->disableOriginalConstructor()
151+
->getMock();
152+
$this->queryGenerator = $this->getMockBuilder(QueryGenerator::class)
153+
->disableOriginalConstructor()
154+
->getMock();
155+
$this->metadataPool = $this->getMockBuilder(MetadataPool::class)
156+
->disableOriginalConstructor()
157+
->getMock();
158+
$this->cacheContext = $this->getMockBuilder(CacheContext::class)
159+
->disableOriginalConstructor()
160+
->getMock();
161+
$this->eventManager = $this->getMockBuilder(EventManagerInterface::class)
162+
->getMockForAbstractClass();
163+
$this->indexerRegistry = $this->getMockBuilder(IndexerRegistry::class)
164+
->disableOriginalConstructor()
165+
->getMock();
166+
$this->indexer = $this->getMockBuilder(IndexerInterface::class)
167+
->getMockForAbstractClass();
168+
$this->tableMaintainer = $this->getMockBuilder(TableMaintainer::class)
169+
->disableOriginalConstructor()
170+
->getMock();
171+
172+
$this->rowsModel = new Rows(
173+
$this->resource,
174+
$this->storeManager,
175+
$this->config,
176+
$this->queryGenerator,
177+
$this->metadataPool,
178+
$this->tableMaintainer,
179+
$this->cacheContext,
180+
$this->eventManager,
181+
$this->indexerRegistry,
182+
$this->workingStateProvider
183+
);
184+
}
185+
186+
public function testExecuteWithIndexerWorking() : void
187+
{
188+
$categoryId = '1';
189+
$store = $this->getMockBuilder(Store::class)
190+
->disableOriginalConstructor()
191+
->getMock();
192+
$store->expects($this->any())
193+
->method('getRootCategoryId')
194+
->willReturn($categoryId);
195+
$store->expects($this->any())
196+
->method('getId')
197+
->willReturn(1);
198+
199+
$attribute = $this->getMockBuilder(AbstractAttribute::class)
200+
->disableOriginalConstructor()
201+
->getMockForAbstractClass();
202+
$this->config->expects($this->any())
203+
->method('getAttribute')
204+
->willReturn($attribute);
205+
206+
$table = $this->getMockBuilder(Table::class)
207+
->disableOriginalConstructor()
208+
->getMock();
209+
$this->connection->expects($this->any())
210+
->method('newTable')
211+
->willReturn($table);
212+
213+
$metadata = $this->getMockBuilder(EntityMetadataInterface::class)
214+
->getMockForAbstractClass();
215+
$this->metadataPool->expects($this->any())
216+
->method('getMetadata')
217+
->willReturn($metadata);
218+
219+
$this->connection->expects($this->any())
220+
->method('fetchAll')
221+
->willReturn([]);
222+
$this->connection->expects($this->any())
223+
->method('fetchCol')
224+
->willReturn([]);
225+
226+
$this->connection->expects($this->any())
227+
->method('fetchOne')
228+
->willReturn($categoryId);
229+
$this->indexerRegistry->expects($this->any())
230+
->method('get')
231+
->with(CategoryProductIndexer::INDEXER_ID)
232+
->willReturn($this->indexer);
233+
$this->indexer->expects($this->any())
234+
->method('getId')
235+
->willReturn(CategoryProductIndexer::INDEXER_ID);
236+
$this->workingStateProvider->expects($this->any())
237+
->method('isWorking')
238+
->with(CategoryProductIndexer::INDEXER_ID)
239+
->willReturn(true);
240+
$this->storeManager->expects($this->any())
241+
->method('getStores')
242+
->willReturn([$store]);
243+
244+
$this->connection->expects($this->once())
245+
->method('delete');
246+
247+
$result = $this->rowsModel->execute([1, 2, 3]);
248+
$this->assertInstanceOf(Rows::class, $result);
249+
}
250+
}

0 commit comments

Comments
 (0)