Skip to content

Commit e59fbea

Browse files
authored
Merge pull request #1731 from algolia/feat/MAGE-1131-indexing-manager-tests
MAGE-1131: Added tests for IndexingManager
2 parents 296a7d8 + 960da02 commit e59fbea

File tree

6 files changed

+342
-0
lines changed

6 files changed

+342
-0
lines changed

Test/Integration/Indexing/Category/CategoriesIndexingTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Test\Integration\Indexing\Category;
44

55
use Algolia\AlgoliaSearch\Console\Command\Indexer\IndexCategoriesCommand;
6+
use Algolia\AlgoliaSearch\Model\Indexer\Category as CategoryIndexer;
67
use Algolia\AlgoliaSearch\Service\Category\BatchQueueProcessor as CategoryBatchQueueProcessor;
78
use Algolia\AlgoliaSearch\Test\Integration\Indexing\IndexingTestCase;
89

@@ -59,4 +60,22 @@ public function testIndexingCategoriesCommand()
5960
$indexCategoriesCmd = $this->objectManager->get(IndexCategoriesCommand::class);
6061
$this->processCommandTest($indexCategoriesCmd,'categories', $this->assertValues->expectedCategory);
6162
}
63+
64+
/**
65+
* @magentoConfigFixture current_store algoliasearch_indexing_manager/full_indexing/categories 0
66+
*/
67+
public function testDisabledOldIndexer()
68+
{
69+
$categoriesIndexer = $this->objectManager->create(CategoryIndexer::class);
70+
$this->processOldIndexerTest($categoriesIndexer, 'categories', 0);
71+
}
72+
73+
/**
74+
* @magentoConfigFixture current_store algoliasearch_indexing_manager/full_indexing/categories 1
75+
*/
76+
public function testEnabledOldIndexer()
77+
{
78+
$categoriesIndexer = $this->objectManager->create(CategoryIndexer::class);
79+
$this->processOldIndexerTest($categoriesIndexer, 'categories', $this->assertValues->expectedCategory);
80+
}
6281
}

Test/Integration/Indexing/IndexingTestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Algolia\AlgoliaSearch\Console\Command\Indexer\AbstractIndexerCommand;
77
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
88
use Algolia\AlgoliaSearch\Test\Integration\TestCase;
9+
use Magento\Framework\Indexer\ActionInterface;
910
use Symfony\Component\Console\Input\InputInterface;
1011
use Symfony\Component\Console\Output\OutputInterface;
1112

@@ -31,6 +32,16 @@ protected function processTest(
3132
$this->assertNumberofHits($indexSuffix, $expectedNbHits);
3233
}
3334

35+
protected function processOldIndexerTest(ActionInterface $indexer, $indexSuffix, $expectedNbHits)
36+
{
37+
$this->algoliaHelper->clearIndex($this->indexPrefix . 'default_' . $indexSuffix);
38+
39+
$indexer->executeFull();
40+
$this->algoliaHelper->waitLastTask();
41+
42+
$this->assertNumberofHits($indexSuffix, $expectedNbHits);
43+
}
44+
3445
protected function processCommandTest(
3546
AbstractIndexerCommand $command,
3647
$indexSuffix,

Test/Integration/Indexing/Page/PagesIndexingTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Algolia\AlgoliaSearch\Console\Command\Indexer\IndexPagesCommand;
66
use Algolia\AlgoliaSearch\Helper\Entity\PageHelper;
7+
use Algolia\AlgoliaSearch\Model\Indexer\Page as PageIndexer;
78
use Algolia\AlgoliaSearch\Service\Page\BatchQueueProcessor as PageBatchQueueProcessor;
89
use Algolia\AlgoliaSearch\Test\Integration\Indexing\IndexingTestCase;
910
use Magento\Cms\Model\PageFactory;
@@ -159,4 +160,27 @@ public function testIndexingPagesCommand()
159160
$indexPagesCmd = $this->objectManager->get(IndexPagesCommand::class);
160161
$this->processCommandTest($indexPagesCmd, 'pages', $this->assertValues->expectedPages);
161162
}
163+
164+
/**
165+
* @magentoConfigFixture current_store algoliasearch_indexing_manager/full_indexing/pages 0
166+
*/
167+
public function testDisabledOldIndexer()
168+
{
169+
$pagesIndexer = $this->objectManager->create(PageIndexer::class);
170+
$this->processOldIndexerTest($pagesIndexer, 'pages', 0);
171+
}
172+
173+
/**
174+
* @magentoConfigFixture current_store algoliasearch_indexing_manager/full_indexing/pages 1
175+
*/
176+
public function testEnabledOldIndexer()
177+
{
178+
$this->setConfig(
179+
'algoliasearch_autocomplete/autocomplete/excluded_pages',
180+
$this->getSerializer()->serialize([])
181+
);
182+
183+
$pagesIndexer = $this->objectManager->create(PageIndexer::class);
184+
$this->processOldIndexerTest($pagesIndexer, 'pages', $this->assertValues->expectedPages);
185+
}
162186
}

Test/Integration/Indexing/Product/ProductsIndexingTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
77
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
88
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
9+
use Algolia\AlgoliaSearch\Model\Indexer\Product as ProductIndexer;
910
use Magento\Catalog\Model\Product;
1011
use Magento\Framework\Exception\NoSuchEntityException;
1112
use Magento\Framework\Indexer\IndexerRegistry;
@@ -97,6 +98,28 @@ public function testIndexingProductsCommand()
9798
$this->processCommandTest($indexProductsCmd, 'products', $this->assertValues->productsOnStockCount);
9899
}
99100

101+
/**
102+
* @magentoConfigFixture current_store algoliasearch_indexing_manager/full_indexing/products 0
103+
*/
104+
public function testDisabledOldIndexer()
105+
{
106+
$productsIndexer = $this->objectManager->create(ProductIndexer::class);
107+
$this->processOldIndexerTest($productsIndexer, 'products', 0);
108+
}
109+
110+
/**
111+
* @magentoConfigFixture current_store algoliasearch_indexing_manager/full_indexing/products 1
112+
*/
113+
public function testEnabledOldIndexer()
114+
{
115+
$this->setConfig(ConfigHelper::SHOW_OUT_OF_STOCK, 0);
116+
117+
$this->updateStockItem(self::OUT_OF_STOCK_PRODUCT_SKU, false);
118+
119+
$productsIndexer = $this->objectManager->create(ProductIndexer::class);
120+
$this->processOldIndexerTest($productsIndexer, 'products', $this->assertValues->productsOnStockCount);
121+
}
122+
100123
private function getValidTestProduct()
101124
{
102125
if (!$this->testProductId) {
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)