14
14
use Magento \Catalog \Model \Category ;
15
15
use Magento \Catalog \Model \CategoryLinkManagement ;
16
16
use Magento \Eav \Model \Config ;
17
- use Magento \Indexer \Model \Indexer ;
18
17
use Magento \TestFramework \ObjectManager ;
19
18
use Magento \TestFramework \TestCase \GraphQlAbstract ;
20
19
use Magento \Catalog \Model \Product ;
28
27
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
29
28
* @SuppressWarnings(PHPMD.ExcessiveClassLength)
30
29
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30
+ * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
31
31
*/
32
32
class ProductSearchTest extends GraphQlAbstract
33
33
{
34
34
/**
35
35
* Verify that layered navigation filters and aggregations are correct for product query
36
36
*
37
37
* Filter products by an array of skus
38
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
38
39
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
39
40
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
40
41
*/
@@ -84,17 +85,37 @@ public function testFilterLn()
84
85
$ response ['products ' ],
85
86
'Filters are missing in product query result. '
86
87
);
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
+
87
95
$ this ->assertFilters (
88
- $ response ,
89
- $ this -> getExpectedFiltersDataSet () ,
96
+ [ ' products ' => [ ' filters ' => $ actualFilters ]] ,
97
+ $ expectedFilters ,
90
98
'Returned filters data set does not match the expected value '
91
99
);
92
100
}
93
101
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
+
94
114
/**
95
115
* Layered navigation for Configurable products with out of stock options
96
116
* Two configurable products each having two variations and one of the child products of one Configurable set to OOS
97
117
*
118
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
98
119
* @magentoApiDataFixture Magento/Catalog/_files/configurable_products_with_custom_attribute_layered_navigation.php
99
120
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
100
121
*/
@@ -305,15 +326,17 @@ public function testFilterProductsByDropDownCustomAttribute()
305
326
306
327
/**
307
328
* @return void
329
+ * @throws \Magento\Framework\Exception\LocalizedException
308
330
*/
309
331
private function reIndexAndCleanCache () : void
310
332
{
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 );
315
337
CacheCleaner::cleanAll ();
316
338
}
339
+
317
340
/**
318
341
* Filter products using an array of multi select custom attributes
319
342
*
@@ -676,9 +699,12 @@ public function testFilterByCategoryIdAndCustomAttribute()
676
699
'value ' => '13 '
677
700
],
678
701
];
702
+ // presort expected and actual results as different search engines have different orders
703
+ usort ($ expectedCategoryInAggregrations , [$ this , 'compareLabels ' ]);
704
+ usort ($ actualCategoriesFromResponse , [$ this , 'compareLabels ' ]);
679
705
$ categoryInAggregations = array_map (null , $ expectedCategoryInAggregrations , $ actualCategoriesFromResponse );
680
706
681
- //Validate the categories and sub-categories data in the filter layer
707
+ //Validate the categories and sub-categories data in the filter layer
682
708
foreach ($ categoryInAggregations as $ index => $ categoryAggregationsData ) {
683
709
$ this ->assertNotEmpty ($ categoryAggregationsData );
684
710
$ this ->assertEquals (
@@ -694,6 +720,18 @@ public function testFilterByCategoryIdAndCustomAttribute()
694
720
}
695
721
}
696
722
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
+
697
735
/**
698
736
* Filter by exact match of product url key
699
737
*
@@ -982,6 +1020,7 @@ private function assertFilters($response, $expectedFilters, $message = '')
982
1020
/**
983
1021
* Verify product filtering using price range AND matching skus AND name sorted in DESC order
984
1022
*
1023
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
985
1024
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
986
1025
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
987
1026
*/
@@ -1052,13 +1091,14 @@ public function testFilterWithinSpecificPriceRangeSortedByNameDesc()
1052
1091
* expected - error is thrown
1053
1092
* Actual - empty array
1054
1093
*
1094
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1055
1095
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
1056
1096
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1057
1097
*/
1058
1098
1059
1099
public function testSearchWithFilterWithPageSizeEqualTotalCount ()
1060
1100
{
1061
-
1101
+ $ this -> reIndexAndCleanCache ();
1062
1102
$ query
1063
1103
= <<<QUERY
1064
1104
{
@@ -1114,6 +1154,7 @@ public function testSearchWithFilterWithPageSizeEqualTotalCount()
1114
1154
/**
1115
1155
* Filtering for products and sorting using multiple sort parameters
1116
1156
*
1157
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1117
1158
* @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
1118
1159
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1119
1160
*/
@@ -1486,18 +1527,24 @@ public function testSearchAndSortByRelevance()
1486
1527
$ this ->assertEquals (3 , $ response ['products ' ]['total_count ' ]);
1487
1528
$ this ->assertNotEmpty ($ response ['products ' ]['filters ' ], 'Filters should have the Category layer ' );
1488
1529
$ this ->assertEquals ('Colorful Category ' , $ response ['products ' ]['filters ' ][0 ]['filter_items ' ][0 ]['label ' ]);
1530
+ $ this ->assertCount (2 , $ response ['products ' ]['aggregations ' ]);
1489
1531
$ 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
+ }
1490
1537
$ count = count ($ response ['products ' ]['items ' ]);
1491
1538
for ($ i = 0 ; $ i < $ count ; $ i ++) {
1492
1539
$ this ->assertEquals ($ productsInResponse [$ i ], $ response ['products ' ]['items ' ][$ i ]['name ' ]);
1493
1540
}
1494
- $ this ->assertCount (2 , $ response ['products ' ]['aggregations ' ]);
1495
1541
}
1496
1542
1497
1543
/**
1498
1544
* Filtering for product with sku "equals" a specific value
1499
1545
* If pageSize and current page are not requested, default values are returned
1500
1546
*
1547
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1501
1548
* @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
1502
1549
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1503
1550
*/
@@ -1748,6 +1795,7 @@ public function testFilterWithinASpecificPriceRangeSortedByPriceDESC()
1748
1795
/**
1749
1796
* No items are returned if the conditions are not met
1750
1797
*
1798
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1751
1799
* @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
1752
1800
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1753
1801
*/
@@ -1807,6 +1855,7 @@ public function testQueryFilterNoMatchingItems()
1807
1855
/**
1808
1856
* Asserts that exception is thrown when current page > totalCount of items returned
1809
1857
*
1858
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1810
1859
* @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
1811
1860
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1812
1861
*/
@@ -1953,6 +2002,7 @@ public function testFilterProductsThatAreOutOfStockWithConfigSettings()
1953
2002
/**
1954
2003
* Verify that invalid current page return an error
1955
2004
*
2005
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1956
2006
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
1957
2007
* @expectedException \Exception
1958
2008
* @expectedExceptionMessage currentPage value must be greater than 0
@@ -1982,6 +2032,7 @@ public function testInvalidCurrentPage()
1982
2032
/**
1983
2033
* Verify that invalid page size returns an error.
1984
2034
*
2035
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1985
2036
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
1986
2037
* @expectedException \Exception
1987
2038
* @expectedExceptionMessage pageSize value must be greater than 0
0 commit comments