ElasticsearchTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Model\Client;
  7. use Magento\Catalog\Api\ProductRepositoryInterface;
  8. use Magento\Indexer\Model\Indexer;
  9. use Magento\TestFramework\Helper\Bootstrap;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
  12. use Magento\Elasticsearch6\Model\Client\Elasticsearch as ElasticsearchClient;
  13. use Magento\Elasticsearch\Model\Config;
  14. use Magento\Elasticsearch\SearchAdapter\SearchIndexNameResolver;
  15. /**
  16. * @magentoDbIsolation enabled
  17. * @magentoAppIsolation enabled
  18. * @magentoDataFixture Magento/Elasticsearch/_files/configurable_products.php
  19. */
  20. class ElasticsearchTest extends \PHPUnit\Framework\TestCase
  21. {
  22. /**
  23. * @var ConnectionManager
  24. */
  25. private $connectionManager;
  26. /**
  27. * @var ElasticsearchClient
  28. */
  29. private $client;
  30. /**
  31. * @var StoreManagerInterface
  32. */
  33. private $storeManager;
  34. /**
  35. * @var Config
  36. */
  37. private $clientConfig;
  38. /**
  39. * @var SearchIndexNameResolver
  40. */
  41. private $searchIndexNameResolver;
  42. /**
  43. * @var ProductRepositoryInterface
  44. */
  45. private $productRepository;
  46. protected function setUp()
  47. {
  48. $objectManager = Bootstrap::getObjectManager();
  49. $this->connectionManager = $objectManager->create(ConnectionManager::class);
  50. $this->client = $this->connectionManager->getConnection();
  51. $this->storeManager = $objectManager->create(StoreManagerInterface::class);
  52. $this->clientConfig = $objectManager->create(Config::class);
  53. $this->searchIndexNameResolver = $objectManager->create(SearchIndexNameResolver::class);
  54. $this->productRepository = $objectManager->create(ProductRepositoryInterface::class);
  55. $indexer = $objectManager->create(Indexer::class);
  56. $indexer->load('catalogsearch_fulltext');
  57. $indexer->reindexAll();
  58. }
  59. /**
  60. * @param string $text
  61. * @return array
  62. */
  63. private function search($text)
  64. {
  65. $storeId = $this->storeManager->getDefaultStoreView()->getId();
  66. $searchQuery = [
  67. 'index' => $this->searchIndexNameResolver->getIndexName($storeId, 'catalogsearch_fulltext'),
  68. 'type' => $this->clientConfig->getEntityType(),
  69. 'body' => [
  70. 'query' => [
  71. 'bool' => [
  72. 'minimum_should_match' => 1,
  73. 'should' => [
  74. [
  75. 'match' => [
  76. '_all' => $text,
  77. ],
  78. ],
  79. ],
  80. ],
  81. ],
  82. ],
  83. ];
  84. $queryResult = $this->client->query($searchQuery);
  85. return isset($queryResult['hits']['hits']) ? $queryResult['hits']['hits'] : [];
  86. }
  87. /**
  88. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  89. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix composite_product_search
  90. */
  91. public function testSearchConfigurableProductBySimpleProductName()
  92. {
  93. $this->assertProductWithSkuFound('configurable', $this->search('Configurable Option'));
  94. }
  95. /**
  96. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  97. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix composite_product_search
  98. */
  99. public function testSearchConfigurableProductBySimpleProductAttributeMultiselect()
  100. {
  101. $this->assertProductWithSkuFound('configurable', $this->search('dog'));
  102. }
  103. /**
  104. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  105. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix composite_product_search
  106. */
  107. public function testSearchConfigurableProductBySimpleProductAttributeSelect()
  108. {
  109. $this->assertProductWithSkuFound('configurable', $this->search('chair'));
  110. }
  111. /**
  112. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  113. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix composite_product_search
  114. */
  115. public function testSearchConfigurableProductBySimpleProductAttributeShortDescription()
  116. {
  117. $this->assertProductWithSkuFound('configurable', $this->search('simpledescription'));
  118. }
  119. /**
  120. * Assert that product with SKU is present in response
  121. *
  122. * @param string $sku
  123. * @param array $result
  124. * @return bool
  125. */
  126. private function assertProductWithSkuFound($sku, array $result)
  127. {
  128. foreach ($result as $item) {
  129. if ($item['_source']['sku'] == $sku) {
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. }