Skip to content

Commit 8f01cbb

Browse files
committed
MAGE-1131: Added unit tests for IndexingManager/Reindex controller
1 parent ef1c3ad commit 8f01cbb

File tree

2 files changed

+265
-0
lines changed

2 files changed

+265
-0
lines changed
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Unit\Controller\Adminhtml\IndexingManager;
4+
5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Algolia\AlgoliaSearch\Service\Category\BatchQueueProcessor as CategoryBatchQueueProcessor;
7+
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
8+
use Algolia\AlgoliaSearch\Service\Page\BatchQueueProcessor as PageBatchQueueProcessor;
9+
use Algolia\AlgoliaSearch\Service\Product\BatchQueueProcessor as ProductBatchQueueProcessor;
10+
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
11+
use Magento\Backend\App\Action\Context;
12+
use Magento\Backend\Model\View\Result\Redirect;
13+
use Magento\Framework\App\RequestInterface;
14+
use Magento\Framework\Controller\ResultFactory;
15+
use Magento\Framework\Message\ManagerInterface;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class ReindexTest extends TestCase
20+
{
21+
protected ?ReindexTestable $reindexController = null;
22+
23+
protected ?Context $context = null;
24+
protected ?RequestInterface $request = null;
25+
protected ?ManagerInterface $messageManager = null;
26+
protected ?ResultFactory $resultFactory = null;
27+
28+
protected ?StoreManagerInterface $storeManager = null;
29+
protected ?StoreNameFetcher $storeNameFetcher = null;
30+
protected ?IndexNameFetcher $indexNameFetcher = null;
31+
protected ?ConfigHelper $configHelper = null;
32+
protected ?ProductBatchQueueProcessor $productBatchQueueProcessor = null;
33+
protected ?CategoryBatchQueueProcessor $categoryBatchQueueProcessor = null;
34+
protected ?PageBatchQueueProcessor $pageBatchQueueProcessor = null;
35+
36+
protected ?array $stores = ["1" => "foo", "2" => "bar"];
37+
38+
protected function setUp(): void
39+
{
40+
$this->request = $this->createMock(RequestInterface::class);
41+
$this->messageManager = $this->createMock(ManagerInterface::class);
42+
$this->resultFactory = $this->createMock(ResultFactory::class);
43+
$resultInstance = $this->createMock(Redirect::class);
44+
$resultInstance->method('setPath')->willReturn('');
45+
$this->resultFactory->method('create')
46+
->with(ResultFactory::TYPE_REDIRECT)
47+
->willReturn($resultInstance);
48+
49+
$this->context = $this->createMock(Context::class);
50+
$this->context->method('getRequest')->willReturn($this->request);
51+
$this->context->method('getMessageManager')->willReturn($this->messageManager);
52+
$this->context->method('getResultFactory')->willReturn($this->resultFactory);
53+
54+
$this->storeManager = $this->createMock(StoreManagerInterface::class);
55+
$this->storeManager->method('getStores')->willReturn($this->stores);
56+
57+
$this->storeNameFetcher = $this->createMock(StoreNameFetcher::class);
58+
$this->indexNameFetcher = $this->createMock(IndexNameFetcher::class);
59+
$this->configHelper = $this->createMock(ConfigHelper::class);
60+
$this->productBatchQueueProcessor = $this->createMock(ProductBatchQueueProcessor::class);
61+
$this->categoryBatchQueueProcessor = $this->createMock(CategoryBatchQueueProcessor::class);
62+
$this->pageBatchQueueProcessor = $this->createMock(PageBatchQueueProcessor::class);
63+
64+
$this->reindexController = new ReindexTestable(
65+
$this->context,
66+
$this->storeManager,
67+
$this->storeNameFetcher,
68+
$this->indexNameFetcher,
69+
$this->configHelper,
70+
$this->productBatchQueueProcessor,
71+
$this->categoryBatchQueueProcessor,
72+
$this->pageBatchQueueProcessor
73+
);
74+
}
75+
76+
public function testExecuteFullIndexingAllEntitiesAllStores()
77+
{
78+
$this->request
79+
->expects($this->once())
80+
->method('getParams')
81+
->willReturn([
82+
"store_id" => null,
83+
"entity" => "all",
84+
]);
85+
86+
$this->productBatchQueueProcessor
87+
->expects($this->exactly(2))
88+
->method('processBatch');
89+
90+
$this->categoryBatchQueueProcessor
91+
->expects($this->exactly(2))
92+
->method('processBatch');
93+
94+
$this->pageBatchQueueProcessor
95+
->expects($this->exactly(2))
96+
->method('processBatch');
97+
98+
$this->reindexController->execute();
99+
}
100+
101+
public function testExecuteFullIndexingPagesAllStores()
102+
{
103+
$this->request
104+
->expects($this->once())
105+
->method('getParams')
106+
->willReturn([
107+
"store_id" => null,
108+
"entity" => "pages",
109+
]);
110+
111+
$this->productBatchQueueProcessor
112+
->expects($this->never())
113+
->method('processBatch');
114+
115+
$this->categoryBatchQueueProcessor
116+
->expects($this->never())
117+
->method('processBatch');
118+
119+
$this->pageBatchQueueProcessor
120+
->expects($this->exactly(2))
121+
->method('processBatch');
122+
123+
$this->reindexController->execute();
124+
}
125+
126+
public function testExecuteFullIndexingProductsOneStore()
127+
{
128+
$this->request
129+
->expects($this->once())
130+
->method('getParams')
131+
->willReturn([
132+
"store_id" => "1",
133+
"entity" => "products",
134+
]);
135+
136+
$this->productBatchQueueProcessor
137+
->expects($this->once())
138+
->method('processBatch');
139+
140+
$this->categoryBatchQueueProcessor
141+
->expects($this->never())
142+
->method('processBatch');
143+
144+
$this->pageBatchQueueProcessor
145+
->expects($this->never())
146+
->method('processBatch');
147+
148+
$this->reindexController->execute();
149+
}
150+
151+
public function testExecuteProductsMassAction()
152+
{
153+
$selectedProducts = [2, 3, 4];
154+
155+
$this->request
156+
->expects($this->once())
157+
->method('getParams')
158+
->willReturn([
159+
"store_id" => null,
160+
"namespace" => "product_listing",
161+
"selected" => $selectedProducts,
162+
]);
163+
164+
$this->productBatchQueueProcessor
165+
->expects($this->exactly(2))
166+
->method('processBatch')
167+
->with(1 || 2, $selectedProducts);
168+
169+
$this->categoryBatchQueueProcessor
170+
->expects($this->never())
171+
->method('processBatch');
172+
173+
$this->pageBatchQueueProcessor
174+
->expects($this->never())
175+
->method('processBatch');
176+
177+
$this->reindexController->execute();
178+
}
179+
180+
/**
181+
* @dataProvider entityParamsProvider
182+
*/
183+
public function testEntityToIndex($params, $result)
184+
{
185+
$this->assertEquals($result, $this->reindexController->defineEntitiesToIndex($params));
186+
}
187+
188+
public function entityParamsProvider()
189+
{
190+
return [
191+
[
192+
'params' => [],
193+
'result' => [],
194+
],
195+
[
196+
'params' => ['entity' => 'all'],
197+
'result' => ['products', 'categories', 'pages'],
198+
],
199+
[
200+
'params' => ['entity' => 'categories'],
201+
'result' => ['categories'],
202+
],
203+
[
204+
'params' => ['namespace' => 'product_listing'],
205+
'result' => ['products'],
206+
],
207+
[
208+
'params' => ['namespace' => 'cms_page_listing'],
209+
'result' => ['pages'],
210+
],
211+
];
212+
}
213+
214+
/**
215+
* @dataProvider redirectParamsProvider
216+
*/
217+
public function testRedirectPath($params, $result)
218+
{
219+
$this->assertEquals($result, $this->reindexController->defineRedirectPath($params));
220+
}
221+
222+
public function redirectParamsProvider()
223+
{
224+
return [
225+
[
226+
'params' => [],
227+
'result' => '*/*/',
228+
],
229+
[
230+
'params' => ['foo' => 'bar'],
231+
'result' => '*/*/',
232+
],
233+
[
234+
'params' => ['redirect' => 'my/custom/url'],
235+
'result' => 'my/custom/url',
236+
],
237+
[
238+
'params' => ['namespace' => 'product_listing'],
239+
'result' => 'catalog/product/index',
240+
],
241+
[
242+
'params' => ['namespace' => 'cms_page_listing'],
243+
'result' => 'cms/page/index',
244+
],
245+
];
246+
}
247+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Test\Unit\Controller\Adminhtml\IndexingManager;
4+
5+
use Algolia\AlgoliaSearch\Controller\Adminhtml\IndexingManager\Reindex;
6+
7+
class ReindexTestable extends Reindex
8+
{
9+
public function defineEntitiesToIndex(array $params): array
10+
{
11+
return parent::defineEntitiesToIndex($params);
12+
}
13+
14+
public function defineRedirectPath(array $params): string
15+
{
16+
return parent::defineRedirectPath($params);
17+
}
18+
}

0 commit comments

Comments
 (0)