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
+ * //@todo remove included categories in fixtures and replace them with category fixture
31
32
*/
32
33
class ProductSearchTest extends GraphQlAbstract
33
34
{
34
35
/**
35
36
* Verify that layered navigation filters and aggregations are correct for product query
36
37
*
37
38
* Filter products by an array of skus
39
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
38
40
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
39
41
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
40
42
*/
@@ -84,17 +86,37 @@ public function testFilterLn()
84
86
$ response ['products ' ],
85
87
'Filters are missing in product query result. '
86
88
);
89
+
90
+ $ expectedFilters = $ this ->getExpectedFiltersDataSet ();
91
+ $ actualFilters = $ response ['products ' ]['filters ' ];
92
+ // presort expected and actual results as different search engines have different orders
93
+ usort ($ expectedFilters , [$ this , 'compareFilterNames ' ]);
94
+ usort ($ actualFilters , [$ this , 'compareFilterNames ' ]);
95
+
87
96
$ this ->assertFilters (
88
- $ response ,
89
- $ this -> getExpectedFiltersDataSet () ,
97
+ [ ' products ' => [ ' filters ' => $ actualFilters ]] ,
98
+ $ expectedFilters ,
90
99
'Returned filters data set does not match the expected value '
91
100
);
92
101
}
93
102
103
+ /**
104
+ * Compare arrays by value in 'name' field.
105
+ *
106
+ * @param array $a
107
+ * @param array $b
108
+ * @return int
109
+ */
110
+ private function compareFilterNames (array $ a , array $ b )
111
+ {
112
+ return strcmp ($ a ['name ' ], $ b ['name ' ]);
113
+ }
114
+
94
115
/**
95
116
* Layered navigation for Configurable products with out of stock options
96
117
* Two configurable products each having two variations and one of the child products of one Configurable set to OOS
97
118
*
119
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
98
120
* @magentoApiDataFixture Magento/Catalog/_files/configurable_products_with_custom_attribute_layered_navigation.php
99
121
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
100
122
*/
@@ -199,6 +221,7 @@ private function getQueryProductsWithArrayOfCustomAttributes($attributeCode, $fi
199
221
/**
200
222
* Filter products by custom attribute of dropdown type and filterTypeInput eq
201
223
*
224
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
202
225
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_custom_attribute.php
203
226
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
204
227
*/
@@ -305,15 +328,17 @@ public function testFilterProductsByDropDownCustomAttribute()
305
328
306
329
/**
307
330
* @return void
331
+ * @throws \Magento\Framework\Exception\LocalizedException
308
332
*/
309
333
private function reIndexAndCleanCache () : void
310
334
{
311
- $ objectManager = Bootstrap::getObjectManager ( );
312
- $ indexer = $ objectManager -> create (Indexer::class) ;
313
- $ indexer -> load ( ' catalogsearch_fulltext ' );
314
- $ indexer-> reindexAll ( );
335
+ $ appDir = dirname ( Bootstrap::getInstance ()-> getAppTempDir () );
336
+ $ out = '' ;
337
+ // phpcs:ignore Magento2.Security.InsecureFunction
338
+ exec ( " php -f { $ appDir } /bin/magento indexer:reindex " , $ out );
315
339
CacheCleaner::cleanAll ();
316
340
}
341
+
317
342
/**
318
343
* Filter products using an array of multi select custom attributes
319
344
*
@@ -410,6 +435,7 @@ private function getDefaultAttributeOptionValue(string $attributeCode) : string
410
435
/**
411
436
* Full text search for Products and then filter the results by custom attribute ( sort is by defaulty by relevance)
412
437
*
438
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
413
439
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_custom_attribute.php
414
440
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
415
441
*/
@@ -557,6 +583,7 @@ public function testSearchAndFilterByCustomAttribute()
557
583
/**
558
584
* Filter by category and custom attribute
559
585
*
586
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
560
587
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_custom_attribute.php
561
588
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
562
589
*/
@@ -676,9 +703,12 @@ public function testFilterByCategoryIdAndCustomAttribute()
676
703
'value ' => '13 '
677
704
],
678
705
];
706
+ // presort expected and actual results as different search engines have different orders
707
+ usort ($ expectedCategoryInAggregrations , [$ this , 'compareLabels ' ]);
708
+ usort ($ actualCategoriesFromResponse , [$ this , 'compareLabels ' ]);
679
709
$ categoryInAggregations = array_map (null , $ expectedCategoryInAggregrations , $ actualCategoriesFromResponse );
680
710
681
- //Validate the categories and sub-categories data in the filter layer
711
+ //Validate the categories and sub-categories data in the filter layer
682
712
foreach ($ categoryInAggregations as $ index => $ categoryAggregationsData ) {
683
713
$ this ->assertNotEmpty ($ categoryAggregationsData );
684
714
$ this ->assertEquals (
@@ -694,6 +724,18 @@ public function testFilterByCategoryIdAndCustomAttribute()
694
724
}
695
725
}
696
726
727
+ /**
728
+ * Compare arrays by value in 'label' field.
729
+ *
730
+ * @param array $a
731
+ * @param array $b
732
+ * @return int
733
+ */
734
+ private function compareLabels (array $ a , array $ b )
735
+ {
736
+ return strcmp ($ a ['label ' ], $ b ['label ' ]);
737
+ }
738
+
697
739
/**
698
740
* Filter by exact match of product url key
699
741
*
@@ -982,6 +1024,7 @@ private function assertFilters($response, $expectedFilters, $message = '')
982
1024
/**
983
1025
* Verify product filtering using price range AND matching skus AND name sorted in DESC order
984
1026
*
1027
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
985
1028
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
986
1029
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
987
1030
*/
@@ -1052,13 +1095,14 @@ public function testFilterWithinSpecificPriceRangeSortedByNameDesc()
1052
1095
* expected - error is thrown
1053
1096
* Actual - empty array
1054
1097
*
1098
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1055
1099
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
1056
1100
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1057
1101
*/
1058
1102
1059
1103
public function testSearchWithFilterWithPageSizeEqualTotalCount ()
1060
1104
{
1061
-
1105
+ $ this -> reIndexAndCleanCache ();
1062
1106
$ query
1063
1107
= <<<QUERY
1064
1108
{
@@ -1114,6 +1158,7 @@ public function testSearchWithFilterWithPageSizeEqualTotalCount()
1114
1158
/**
1115
1159
* Filtering for products and sorting using multiple sort parameters
1116
1160
*
1161
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1117
1162
* @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
1118
1163
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1119
1164
*/
@@ -1486,18 +1531,24 @@ public function testSearchAndSortByRelevance()
1486
1531
$ this ->assertEquals (3 , $ response ['products ' ]['total_count ' ]);
1487
1532
$ this ->assertNotEmpty ($ response ['products ' ]['filters ' ], 'Filters should have the Category layer ' );
1488
1533
$ this ->assertEquals ('Colorful Category ' , $ response ['products ' ]['filters ' ][0 ]['filter_items ' ][0 ]['label ' ]);
1534
+ $ this ->assertCount (2 , $ response ['products ' ]['aggregations ' ]);
1489
1535
$ productsInResponse = ['Blue briefs ' ,'Navy Blue Striped Shoes ' ,'Grey shorts ' ];
1536
+ /** @var \Magento\Config\Model\Config $config */
1537
+ $ config = Bootstrap::getObjectManager ()->get (\Magento \Config \Model \Config::class);
1538
+ if (strpos ($ config ->getConfigDataValue ('catalog/search/engine ' ), 'elasticsearch ' ) !== false ) {
1539
+ $ this ->markTestIncomplete ('MC-20716 ' );
1540
+ }
1490
1541
$ count = count ($ response ['products ' ]['items ' ]);
1491
1542
for ($ i = 0 ; $ i < $ count ; $ i ++) {
1492
1543
$ this ->assertEquals ($ productsInResponse [$ i ], $ response ['products ' ]['items ' ][$ i ]['name ' ]);
1493
1544
}
1494
- $ this ->assertCount (2 , $ response ['products ' ]['aggregations ' ]);
1495
1545
}
1496
1546
1497
1547
/**
1498
1548
* Filtering for product with sku "equals" a specific value
1499
1549
* If pageSize and current page are not requested, default values are returned
1500
1550
*
1551
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1501
1552
* @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
1502
1553
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1503
1554
*/
@@ -1748,6 +1799,7 @@ public function testFilterWithinASpecificPriceRangeSortedByPriceDESC()
1748
1799
/**
1749
1800
* No items are returned if the conditions are not met
1750
1801
*
1802
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1751
1803
* @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
1752
1804
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1753
1805
*/
@@ -1807,6 +1859,7 @@ public function testQueryFilterNoMatchingItems()
1807
1859
/**
1808
1860
* Asserts that exception is thrown when current page > totalCount of items returned
1809
1861
*
1862
+ * @magentoApiDataFixture Magento/Catalog/_files/category.php
1810
1863
* @magentoApiDataFixture Magento/Catalog/_files/multiple_mixed_products_2.php
1811
1864
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1812
1865
*/
0 commit comments