IndexHandlerTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Model\Indexer;
  7. use Magento\Catalog\Model\Product\Action as ProductAction;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\CatalogInventory\Api\StockRegistryInterface;
  10. use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
  11. use Magento\CatalogSearch\Model\Indexer\Fulltext as CatalogSearchFulltextIndexer;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
  15. use Magento\Elasticsearch6\Model\Client\Elasticsearch as ElasticsearchClient;
  16. use Magento\Elasticsearch\Model\Config;
  17. use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
  18. use Magento\Indexer\Model\Indexer;
  19. /**
  20. * Important: Please make sure that each integration test file works with unique elastic search index. In order to
  21. * achieve this, use @magentoConfigFixture to pass unique value for 'elasticsearch_index_prefix' for every test
  22. * method. E.g. '@magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix indexerhandlertest'
  23. *
  24. * @magentoDbIsolation disabled
  25. * @magentoDataFixture Magento/Elasticsearch/_files/indexer.php
  26. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  27. */
  28. class IndexHandlerTest extends \PHPUnit\Framework\TestCase
  29. {
  30. /**
  31. * @var ProductRepositoryInterface
  32. */
  33. private $productRepository;
  34. /**
  35. * @var ElasticsearchClient
  36. */
  37. private $client;
  38. /**
  39. * @var StoreManagerInterface
  40. */
  41. private $storeManager;
  42. /**
  43. * @var int[]
  44. */
  45. private $storeIds;
  46. /**
  47. * @var string
  48. */
  49. private $entityType;
  50. /**
  51. * @var Indexer
  52. */
  53. private $indexer;
  54. /**
  55. * @var SearchIndexNameResolver
  56. */
  57. private $searchIndexNameResolver;
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function setUp()
  62. {
  63. $connectionManager = Bootstrap::getObjectManager()->create(ConnectionManager::class);
  64. $this->client = $connectionManager->getConnection();
  65. $this->storeManager = Bootstrap::getObjectManager()->create(StoreManagerInterface::class);
  66. $this->storeIds = array_keys($this->storeManager->getStores());
  67. $clientConfig = Bootstrap::getObjectManager()->create(Config::class);
  68. $this->entityType = $clientConfig->getEntityType();
  69. $this->indexer = Bootstrap::getObjectManager()->create(Indexer::class);
  70. $this->indexer->load(CatalogSearchFulltextIndexer::INDEXER_ID);
  71. $this->indexer->reindexAll();
  72. $this->searchIndexNameResolver = Bootstrap::getObjectManager()->create(SearchIndexNameResolver::class);
  73. $this->productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
  74. }
  75. /**
  76. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  77. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix indexerhandlertest
  78. * @return void
  79. */
  80. public function testReindexAll(): void
  81. {
  82. $productApple = $this->productRepository->get('fulltext-1');
  83. foreach ($this->storeIds as $storeId) {
  84. $products = $this->searchByName('Apple', $storeId);
  85. $this->assertCount(1, $products);
  86. $this->assertEquals($productApple->getId(), $products[0]['_id']);
  87. $products = $this->searchByName('Simple Product', $storeId);
  88. $this->assertCount(5, $products);
  89. }
  90. }
  91. /**
  92. * @magentoAppIsolation enabled
  93. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  94. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix indexerhandlertest
  95. * @return void
  96. */
  97. public function testReindexRowAfterEdit(): void
  98. {
  99. $this->storeManager->setCurrentStore('admin');
  100. $productApple = $this->productRepository->get('fulltext-1');
  101. $productApple->setName('Simple Product Cucumber');
  102. $this->productRepository->save($productApple);
  103. foreach ($this->storeIds as $storeId) {
  104. $products = $this->searchByName('Apple', $storeId);
  105. $this->assertCount(0, $products);
  106. $products = $this->searchByName('Cucumber', $storeId);
  107. $this->assertCount(1, $products);
  108. $this->assertEquals($productApple->getId(), $products[0]['_id']);
  109. $products = $this->searchByName('Simple Product', $storeId);
  110. $this->assertCount(5, $products);
  111. }
  112. }
  113. /**
  114. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  115. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix indexerhandlertest
  116. * @return void
  117. */
  118. public function testReindexRowAfterMassAction(): void
  119. {
  120. $productApple = $this->productRepository->get('fulltext-1');
  121. $productBanana = $this->productRepository->get('fulltext-2');
  122. $productIds = [
  123. $productApple->getId(),
  124. $productBanana->getId(),
  125. ];
  126. $attrData = [
  127. 'name' => 'Simple Product Common',
  128. ];
  129. /** @var ProductAction $action */
  130. $action = Bootstrap::getObjectManager()->get(ProductAction::class);
  131. foreach ($this->storeIds as $storeId) {
  132. $action->updateAttributes($productIds, $attrData, $storeId);
  133. $products = $this->searchByName('Apple', $storeId);
  134. $this->assertCount(0, $products);
  135. $products = $this->searchByName('Banana', $storeId);
  136. $this->assertCount(0, $products);
  137. $products = $this->searchByName('Unknown', $storeId);
  138. $this->assertCount(0, $products);
  139. $products = $this->searchByName('Common', $storeId);
  140. $this->assertCount(2, $products);
  141. $products = $this->searchByName('Simple Product', $storeId);
  142. $this->assertCount(5, $products);
  143. }
  144. }
  145. /**
  146. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  147. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix indexerhandlertest
  148. * @magentoAppArea adminhtml
  149. * @return void
  150. */
  151. public function testReindexRowAfterDelete(): void
  152. {
  153. $productBanana = $this->productRepository->get('fulltext-2');
  154. $this->productRepository->delete($productBanana);
  155. foreach ($this->storeIds as $storeId) {
  156. $products = $this->searchByName('Banana', $storeId);
  157. $this->assertEmpty($products);
  158. $products = $this->searchByName('Simple Product', $storeId);
  159. $this->assertCount(4, $products);
  160. }
  161. }
  162. /**
  163. * @magentoDbIsolation enabled
  164. * @magentoAppArea adminhtml
  165. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  166. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix indexerhandlertest
  167. * @magentoDataFixture Magento/Elasticsearch/_files/configurable_products.php
  168. * @return void
  169. */
  170. public function testReindexRowAfterUpdateStockStatus(): void
  171. {
  172. foreach ($this->storeIds as $storeId) {
  173. $products = $this->searchByName('ProductOption1', $storeId);
  174. $this->assertNotEmpty($products);
  175. }
  176. $product = $this->productRepository->get('simple_10');
  177. /** @var StockRegistryInterface $stockRegistry */
  178. $stockRegistry = Bootstrap::getObjectManager()->create(StockRegistryInterface::class);
  179. $stockItem = $stockRegistry->getStockItem($product->getId());
  180. $stockItem->setIsInStock(false);
  181. /** @var StockItemRepositoryInterface $stockRepository */
  182. $stockRepository = Bootstrap::getObjectManager()->create(StockItemRepositoryInterface::class);
  183. $stockRepository->save($stockItem);
  184. foreach ($this->storeIds as $storeId) {
  185. $products = $this->searchByName('ProductOption1', $storeId);
  186. $this->assertEmpty($products);
  187. $products = $this->searchByName('Configurable', $storeId);
  188. $this->assertNotEmpty($products);
  189. }
  190. }
  191. /**
  192. * Search docs in Elasticsearch by name.
  193. *
  194. * @param string $text
  195. * @param int $storeId
  196. * @return array
  197. */
  198. private function searchByName(string $text, int $storeId): array
  199. {
  200. $index = $this->searchIndexNameResolver->getIndexName($storeId, $this->indexer->getId());
  201. $searchQuery = [
  202. 'index' => $index,
  203. 'type' => $this->entityType,
  204. 'body' => [
  205. 'query' => [
  206. 'bool' => [
  207. 'minimum_should_match' => 1,
  208. 'should' => [
  209. [
  210. 'match' => [
  211. 'name' => $text,
  212. ],
  213. ],
  214. ],
  215. ],
  216. ],
  217. ],
  218. ];
  219. $queryResult = $this->client->query($searchQuery);
  220. $products = isset($queryResult['hits']['hits']) ? $queryResult['hits']['hits'] : [];
  221. return $products;
  222. }
  223. }