1414use Magento \Catalog \Model \Category ;
1515use Magento \Catalog \Model \CategoryLinkManagement ;
1616use Magento \Eav \Model \Config ;
17- use Magento \Indexer \Model \Indexer ;
1817use Magento \TestFramework \ObjectManager ;
1918use Magento \TestFramework \TestCase \GraphQlAbstract ;
2019use Magento \Catalog \Model \Product ;
2827 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
2928 * @SuppressWarnings(PHPMD.ExcessiveClassLength)
3029 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30+ * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
3131 */
3232class ProductSearchTest extends GraphQlAbstract
3333{
3434 /**
3535 * Verify that layered navigation filters and aggregations are correct for product query
3636 *
3737 * Filter products by an array of skus
38+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
3839 * @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
3940 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
4041 */
@@ -84,17 +85,37 @@ public function testFilterLn()
8485 $ response ['products ' ],
8586 'Filters are missing in product query result. '
8687 );
88+
89+ $ expectedFilters = $ this ->getExpectedFiltersDataSet ();
90+ $ actualFilters = $ response ['products ' ]['filters ' ];
91+ // presort expected and actual results as different search engines have different orders
92+ usort ($ expectedFilters , [$ this , 'compareFilterNames ' ]);
93+ usort ($ actualFilters , [$ this , 'compareFilterNames ' ]);
94+
8795 $ this ->assertFilters (
88- $ response ,
89- $ this -> getExpectedFiltersDataSet () ,
96+ [ ' products ' => [ ' filters ' => $ actualFilters ]] ,
97+ $ expectedFilters ,
9098 'Returned filters data set does not match the expected value '
9199 );
92100 }
93101
102+ /**
103+ * Compare arrays by value in 'name' field.
104+ *
105+ * @param array $a
106+ * @param array $b
107+ * @return int
108+ */
109+ private function compareFilterNames (array $ a , array $ b )
110+ {
111+ return strcmp ($ a ['name ' ], $ b ['name ' ]);
112+ }
113+
94114 /**
95115 * Layered navigation for Configurable products with out of stock options
96116 * Two configurable products each having two variations and one of the child products of one Configurable set to OOS
97117 *
118+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
98119 * @magentoApiDataFixture Magento/Catalog/_files/configurable_products_with_custom_attribute_layered_navigation.php
99120 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
100121 */
@@ -305,15 +326,17 @@ public function testFilterProductsByDropDownCustomAttribute()
305326
306327 /**
307328 * @return void
329+ * @throws \Magento\Framework\Exception\LocalizedException
308330 */
309331 private function reIndexAndCleanCache () : void
310332 {
311- $ objectManager = Bootstrap::getObjectManager ( );
312- $ indexer = $ objectManager -> create (Indexer::class) ;
313- $ indexer -> load ( ' catalogsearch_fulltext ' );
314- $ indexer-> reindexAll ( );
333+ $ appDir = dirname ( Bootstrap::getInstance ()-> getAppTempDir () );
334+ $ out = '' ;
335+ // phpcs:ignore Magento2.Security.InsecureFunction
336+ exec ( " php -f { $ appDir } /bin/magento indexer:reindex " , $ out );
315337 CacheCleaner::cleanAll ();
316338 }
339+
317340 /**
318341 * Filter products using an array of multi select custom attributes
319342 *
@@ -676,9 +699,12 @@ public function testFilterByCategoryIdAndCustomAttribute()
676699 'value ' => '13 '
677700 ],
678701 ];
702+ // presort expected and actual results as different search engines have different orders
703+ usort ($ expectedCategoryInAggregrations , [$ this , 'compareLabels ' ]);
704+ usort ($ actualCategoriesFromResponse , [$ this , 'compareLabels ' ]);
679705 $ categoryInAggregations = array_map (null , $ expectedCategoryInAggregrations , $ actualCategoriesFromResponse );
680706
681- //Validate the categories and sub-categories data in the filter layer
707+ //Validate the categories and sub-categories data in the filter layer
682708 foreach ($ categoryInAggregations as $ index => $ categoryAggregationsData ) {
683709 $ this ->assertNotEmpty ($ categoryAggregationsData );
684710 $ this ->assertEquals (
@@ -694,6 +720,18 @@ public function testFilterByCategoryIdAndCustomAttribute()
694720 }
695721 }
696722
723+ /**
724+ * Compare arrays by value in 'label' field.
725+ *
726+ * @param array $a
727+ * @param array $b
728+ * @return int
729+ */
730+ private function compareLabels (array $ a , array $ b )
731+ {
732+ return strcmp ($ a ['label ' ], $ b ['label ' ]);
733+ }
734+
697735 /**
698736 * Filter by exact match of product url key
699737 *
@@ -982,6 +1020,7 @@ private function assertFilters($response, $expectedFilters, $message = '')
9821020 /**
9831021 * Verify product filtering using price range AND matching skus AND name sorted in DESC order
9841022 *
1023+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
9851024 * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
9861025 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
9871026 */
@@ -1052,13 +1091,14 @@ public function testFilterWithinSpecificPriceRangeSortedByNameDesc()
10521091 * expected - error is thrown
10531092 * Actual - empty array
10541093 *
1094+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
10551095 * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
10561096 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
10571097 */
10581098
10591099 public function testSearchWithFilterWithPageSizeEqualTotalCount ()
10601100 {
1061-
1101+ $ this -> reIndexAndCleanCache ();
10621102 $ query
10631103 = <<<QUERY
10641104{
@@ -1114,6 +1154,7 @@ public function testSearchWithFilterWithPageSizeEqualTotalCount()
11141154 /**
11151155 * Filtering for products and sorting using multiple sort parameters
11161156 *
1157+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
11171158 * @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
11181159 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
11191160 */
@@ -1486,18 +1527,24 @@ public function testSearchAndSortByRelevance()
14861527 $ this ->assertEquals (3 , $ response ['products ' ]['total_count ' ]);
14871528 $ this ->assertNotEmpty ($ response ['products ' ]['filters ' ], 'Filters should have the Category layer ' );
14881529 $ this ->assertEquals ('Colorful Category ' , $ response ['products ' ]['filters ' ][0 ]['filter_items ' ][0 ]['label ' ]);
1530+ $ this ->assertCount (2 , $ response ['products ' ]['aggregations ' ]);
14891531 $ productsInResponse = ['Blue briefs ' ,'Navy Blue Striped Shoes ' ,'Grey shorts ' ];
1532+ /** @var \Magento\Config\Model\Config $config */
1533+ $ config = Bootstrap::getObjectManager ()->get (\Magento \Config \Model \Config::class);
1534+ if (strpos ($ config ->getConfigDataValue ('catalog/search/engine ' ), 'elasticsearch ' ) !== false ) {
1535+ $ this ->markTestIncomplete ('MC-20716 ' );
1536+ }
14901537 $ count = count ($ response ['products ' ]['items ' ]);
14911538 for ($ i = 0 ; $ i < $ count ; $ i ++) {
14921539 $ this ->assertEquals ($ productsInResponse [$ i ], $ response ['products ' ]['items ' ][$ i ]['name ' ]);
14931540 }
1494- $ this ->assertCount (2 , $ response ['products ' ]['aggregations ' ]);
14951541 }
14961542
14971543 /**
14981544 * Filtering for product with sku "equals" a specific value
14991545 * If pageSize and current page are not requested, default values are returned
15001546 *
1547+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
15011548 * @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
15021549 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
15031550 */
@@ -1748,6 +1795,7 @@ public function testFilterWithinASpecificPriceRangeSortedByPriceDESC()
17481795 /**
17491796 * No items are returned if the conditions are not met
17501797 *
1798+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
17511799 * @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
17521800 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
17531801 */
@@ -1807,6 +1855,7 @@ public function testQueryFilterNoMatchingItems()
18071855 /**
18081856 * Asserts that exception is thrown when current page > totalCount of items returned
18091857 *
1858+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
18101859 * @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
18111860 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
18121861 */
@@ -1953,6 +2002,7 @@ public function testFilterProductsThatAreOutOfStockWithConfigSettings()
19532002 /**
19542003 * Verify that invalid current page return an error
19552004 *
2005+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
19562006 * @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
19572007 * @expectedException \Exception
19582008 * @expectedExceptionMessage currentPage value must be greater than 0
@@ -1982,6 +2032,7 @@ public function testInvalidCurrentPage()
19822032 /**
19832033 * Verify that invalid page size returns an error.
19842034 *
2035+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
19852036 * @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
19862037 * @expectedException \Exception
19872038 * @expectedExceptionMessage pageSize value must be greater than 0
0 commit comments