AdapterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\SearchAdapter;
  7. /**
  8. * Class AdapterTest
  9. *
  10. * @magentoDbIsolation disabled
  11. * @magentoDataFixture Magento/Framework/Search/_files/products.php
  12. *
  13. * Important: Please make sure that each integration test file works with unique elastic search index. In order to
  14. * achieve this, use @ magentoConfigFixture to pass unique value for 'elasticsearch_index_prefix' for every test
  15. * method. E.g. '@ magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest'
  16. *
  17. * In ElasticSearch, a reindex is required if the test includes a new data fixture with new items to search, see
  18. * testAdvancedSearchDateField().
  19. *
  20. */
  21. class AdapterTest extends \Magento\Framework\Search\Adapter\Mysql\AdapterTest
  22. {
  23. /**
  24. * @var string
  25. */
  26. protected $searchEngine = 'elasticsearch6';
  27. /**
  28. * Get request config path
  29. *
  30. * @return string
  31. */
  32. protected function getRequestConfigPath()
  33. {
  34. return __DIR__ . '/../_files/requests.xml';
  35. }
  36. /**
  37. * @return \Magento\Framework\Search\AdapterInterface
  38. */
  39. protected function createAdapter()
  40. {
  41. return $this->objectManager->create(\Magento\Elasticsearch\Elasticsearch5\SearchAdapter\Adapter::class);
  42. }
  43. /**
  44. * @magentoAppIsolation enabled
  45. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  46. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  47. */
  48. public function testMatchQuery()
  49. {
  50. parent::testMatchQuery();
  51. }
  52. /**
  53. * @magentoAppIsolation enabled
  54. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  55. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  56. */
  57. public function testMatchOrderedQuery()
  58. {
  59. $this->markTestSkipped(
  60. 'Elasticsearch not expected to order results by default. Test is skipped intentionally.'
  61. );
  62. }
  63. /**
  64. * @magentoAppIsolation enabled
  65. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  66. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  67. */
  68. public function testAggregationsQuery()
  69. {
  70. $this->markTestSkipped('Range query is not supported. Test is skipped intentionally.');
  71. }
  72. /**
  73. * @magentoAppIsolation enabled
  74. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  75. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  76. */
  77. public function testMatchQueryFilters()
  78. {
  79. parent::testMatchQueryFilters();
  80. }
  81. /**
  82. * Range filter test with all fields filled
  83. *
  84. * @magentoAppIsolation enabled
  85. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  86. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  87. */
  88. public function testRangeFilterWithAllFields()
  89. {
  90. parent::testRangeFilterWithAllFields();
  91. }
  92. /**
  93. * Range filter test with all fields filled
  94. *
  95. * @magentoAppIsolation enabled
  96. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  97. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  98. */
  99. public function testRangeFilterWithoutFromField()
  100. {
  101. parent::testRangeFilterWithoutFromField();
  102. }
  103. /**
  104. * Range filter test with all fields filled
  105. *
  106. * @magentoAppIsolation enabled
  107. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  108. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  109. */
  110. public function testRangeFilterWithoutToField()
  111. {
  112. parent::testRangeFilterWithoutToField();
  113. }
  114. /**
  115. * Term filter test
  116. *
  117. * @magentoAppIsolation enabled
  118. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  119. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  120. */
  121. public function testTermFilter()
  122. {
  123. parent::testTermFilter();
  124. }
  125. /**
  126. * Term filter test
  127. *
  128. * @magentoAppIsolation enabled
  129. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  130. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  131. */
  132. public function testTermFilterArray()
  133. {
  134. parent::testTermFilterArray();
  135. }
  136. /**
  137. * Term filter test
  138. *
  139. * @magentoAppIsolation enabled
  140. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  141. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  142. */
  143. public function testWildcardFilter()
  144. {
  145. parent::testWildcardFilter();
  146. }
  147. /**
  148. * Request limits test
  149. *
  150. * @magentoAppIsolation enabled
  151. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  152. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  153. */
  154. public function testSearchLimit()
  155. {
  156. parent::testSearchLimit();
  157. }
  158. /**
  159. * Bool filter test
  160. *
  161. * @magentoAppIsolation enabled
  162. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  163. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  164. */
  165. public function testBoolFilter()
  166. {
  167. parent::testBoolFilter();
  168. }
  169. /**
  170. * Test bool filter with nested negative bool filter
  171. *
  172. * @magentoAppIsolation enabled
  173. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  174. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  175. */
  176. public function testBoolFilterWithNestedNegativeBoolFilter()
  177. {
  178. parent::testBoolFilterWithNestedNegativeBoolFilter();
  179. }
  180. /**
  181. * Test range inside nested negative bool filter
  182. *
  183. * @magentoAppIsolation enabled
  184. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  185. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  186. */
  187. public function testBoolFilterWithNestedRangeInNegativeBoolFilter()
  188. {
  189. parent::testBoolFilterWithNestedRangeInNegativeBoolFilter();
  190. }
  191. /**
  192. * Sample Advanced search request test
  193. *
  194. * @dataProvider elasticSearchAdvancedSearchDataProvider
  195. * @magentoAppIsolation enabled
  196. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  197. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  198. * @param string $nameQuery
  199. * @param string $descriptionQuery
  200. * @param array $rangeFilter
  201. * @param int $expectedRecordsCount
  202. */
  203. public function testSimpleAdvancedSearch(
  204. $nameQuery,
  205. $descriptionQuery,
  206. $rangeFilter,
  207. $expectedRecordsCount
  208. ) {
  209. parent::testSimpleAdvancedSearch(
  210. $nameQuery,
  211. $descriptionQuery,
  212. $rangeFilter,
  213. $expectedRecordsCount
  214. );
  215. }
  216. /**
  217. * Elastic Search specific data provider for advanced search test.
  218. *
  219. * The expected array is for Elastic Search is different that the one for MySQL
  220. * because sometimes more matches are returned. For instance, 3rd index below
  221. * will return 3 matches instead of 1 (which is what MySQL returns).
  222. *
  223. * @return array
  224. */
  225. public function elasticSearchAdvancedSearchDataProvider()
  226. {
  227. return [
  228. ['white', 'shorts', ['from' => '16', 'to' => '18'], 0],
  229. ['white', 'shorts',['from' => '12', 'to' => '18'], 1],
  230. ['black', 'tshirts', ['from' => '12', 'to' => '20'], 0],
  231. ['shorts', 'green', ['from' => '12', 'to' => '22'], 3],
  232. //Search with empty fields/values
  233. ['white', ' ', ['from' => '12', 'to' => '22'], 1],
  234. [' ', 'green', ['from' => '12', 'to' => '22'], 2]
  235. ];
  236. }
  237. /**
  238. * @magentoAppIsolation enabled
  239. * @magentoDataFixture Magento/Framework/Search/_files/filterable_attribute.php
  240. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  241. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  242. */
  243. public function testCustomFilterableAttribute()
  244. {
  245. // Reindex Elastic Search since filterable_attribute data fixture added new fields to be indexed
  246. $this->reindexAll();
  247. parent::testCustomFilterableAttribute();
  248. }
  249. /**
  250. * Test filtering by two attributes.
  251. *
  252. * @magentoAppIsolation enabled
  253. * @magentoDataFixture Magento/Framework/Search/_files/filterable_attributes.php
  254. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  255. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  256. * @dataProvider filterByAttributeValuesDataProvider
  257. * @param string $requestName
  258. * @param array $additionalData
  259. * @return void
  260. */
  261. public function testFilterByAttributeValues($requestName, $additionalData)
  262. {
  263. // Reindex Elastic Search since filterable_attribute data fixture added new fields to be indexed
  264. $this->reindexAll();
  265. parent::testFilterByAttributeValues($requestName, $additionalData);
  266. }
  267. /**
  268. * Advanced search request using date product attribute
  269. *
  270. * @param $rangeFilter
  271. * @param $expectedRecordsCount
  272. * @magentoDataFixture Magento/Framework/Search/_files/date_attribute.php
  273. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  274. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  275. * @magentoAppIsolation enabled
  276. * @dataProvider dateDataProvider
  277. */
  278. public function testAdvancedSearchDateField($rangeFilter, $expectedRecordsCount)
  279. {
  280. // Reindex Elastic Search since date_attribute data fixture added new fields to be indexed
  281. $this->reindexAll();
  282. parent::testAdvancedSearchDateField($rangeFilter, $expectedRecordsCount);
  283. }
  284. /**
  285. * @magentoDataFixture Magento/Framework/Search/_files/product_configurable.php
  286. * @magentoAppIsolation enabled
  287. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  288. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  289. */
  290. public function testAdvancedSearchCompositeProductWithOutOfStockOption()
  291. {
  292. parent::testAdvancedSearchCompositeProductWithOutOfStockOption();
  293. }
  294. /**
  295. * @magentoDataFixture Magento/Framework/Search/_files/product_configurable_with_disabled_child.php
  296. * @magentoAppIsolation enabled
  297. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  298. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  299. */
  300. public function testAdvancedSearchCompositeProductWithDisabledChild()
  301. {
  302. // Reindex Elastic Search since date_attribute data fixture added new fields to be indexed
  303. $this->reindexAll();
  304. parent::testAdvancedSearchCompositeProductWithDisabledChild();
  305. }
  306. /**
  307. * @magentoDataFixture Magento/Framework/Search/_files/search_weight_products.php
  308. * @magentoAppIsolation enabled
  309. * @magentoConfigFixture default/catalog/search/engine elasticsearch6
  310. * @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix adaptertest
  311. */
  312. public function testSearchQueryBoost()
  313. {
  314. // Reindex Elastic Search since date_attribute data fixture added new fields to be indexed
  315. $this->reindexAll();
  316. parent::testSearchQueryBoost();
  317. }
  318. /**
  319. * Perform full reindex
  320. *
  321. * @return void
  322. */
  323. private function reindexAll()
  324. {
  325. $indexer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  326. \Magento\Indexer\Model\Indexer::class
  327. );
  328. $indexer->load('catalogsearch_fulltext');
  329. $indexer->reindexAll();
  330. }
  331. /**
  332. * Date data provider
  333. *
  334. * @return array
  335. */
  336. public function dateDataProvider()
  337. {
  338. return [
  339. [['from' => '1999-12-31T00:00:00Z', 'to' => '2000-01-01T00:00:00Z'], 1],
  340. [['from' => '2000-02-01T00:00:00Z', 'to' => ''], 0],
  341. ];
  342. }
  343. }