@@ -364,6 +364,8 @@ private function reIndexAndCleanCache() : void
364
364
$ out = '' ;
365
365
// phpcs:ignore Magento2.Security.InsecureFunction
366
366
exec ("php -f {$ appDir }/bin/magento indexer:reindex catalog_category_product " , $ out );
367
+ // phpcs:ignore Magento2.Security.InsecureFunction
368
+ exec ("php -f {$ appDir }/bin/magento indexer:reindex catalogsearch_fulltext " , $ out );
367
369
CacheCleaner::cleanAll ();
368
370
}
369
371
@@ -670,15 +672,16 @@ public function testFilterByCategoryIdAndCustomAttribute()
670
672
$ productRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
671
673
$ product1 = $ productRepository ->get ('simple ' );
672
674
$ product2 = $ productRepository ->get ('simple-4 ' );
673
- $ filteredProducts = [$ product1 , $ product2 ];
675
+ $ filteredProducts = [$ product2 , $ product1 ];
674
676
$ productItemsInResponse = array_map (null , $ response ['products ' ]['items ' ], $ filteredProducts );
675
677
//phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
676
678
for ($ itemIndex = 0 ; $ itemIndex < count ($ filteredProducts ); $ itemIndex ++) {
677
679
$ this ->assertNotEmpty ($ productItemsInResponse [$ itemIndex ]);
678
680
//validate that correct products are returned
679
681
$ this ->assertResponseFields (
680
682
$ productItemsInResponse [$ itemIndex ][0 ],
681
- [ 'name ' => $ filteredProducts [$ itemIndex ]->getName (),
683
+ [
684
+ 'name ' => $ filteredProducts [$ itemIndex ]->getName (),
682
685
'sku ' => $ filteredProducts [$ itemIndex ]->getSku ()
683
686
]
684
687
);
@@ -1121,6 +1124,7 @@ public function testFilterWithinSpecificPriceRangeSortedByNameDesc()
1121
1124
*/
1122
1125
public function testSortByPosition ()
1123
1126
{
1127
+ $ this ->reIndexAndCleanCache ();
1124
1128
// Get category ID for filtering
1125
1129
/** @var Collection $categoryCollection */
1126
1130
$ categoryCollection = Bootstrap::getObjectManager ()->get (Collection::class);
@@ -1141,7 +1145,8 @@ public function testSortByPosition()
1141
1145
$ resultAsc = $ this ->graphQlQuery ($ queryAsc );
1142
1146
$ this ->assertArrayNotHasKey ('errors ' , $ resultAsc );
1143
1147
$ productsAsc = array_column ($ resultAsc ['products ' ]['items ' ], 'sku ' );
1144
- $ expectedProductsAsc = ['simple1000 ' , 'simple1001 ' , 'simple1002 ' ];
1148
+ $ expectedProductsAsc = ['simple1002 ' , 'simple1001 ' , 'simple1000 ' ];
1149
+ // position equal and secondary sort by entity_id DESC
1145
1150
$ this ->assertEquals ($ expectedProductsAsc , $ productsAsc );
1146
1151
1147
1152
$ queryDesc = <<<QUERY
@@ -1158,23 +1163,25 @@ public function testSortByPosition()
1158
1163
$ resultDesc = $ this ->graphQlQuery ($ queryDesc );
1159
1164
$ this ->assertArrayNotHasKey ('errors ' , $ resultDesc );
1160
1165
$ productsDesc = array_column ($ resultDesc ['products ' ]['items ' ], 'sku ' );
1161
- $ expectedProductsDesc = array_reverse ( $ expectedProductsAsc );
1162
- $ this ->assertEquals ($ expectedProductsDesc , $ productsDesc );
1166
+ // position equal and secondary sort by entity_id DESC
1167
+ $ this ->assertEquals ($ expectedProductsAsc , $ productsDesc );
1163
1168
1164
1169
//revert position
1165
1170
$ productPositions = $ category ->getProductsPosition ();
1166
- $ count = 3 ;
1171
+ $ count = 1 ;
1167
1172
foreach ($ productPositions as $ productId => $ position ) {
1168
1173
$ productPositions [$ productId ] = $ count ;
1169
- $ count-- ;
1174
+ $ count++ ;
1170
1175
}
1176
+ ksort ($ productPositions );
1171
1177
1172
1178
$ category ->setPostedProducts ($ productPositions );
1173
1179
$ category ->save ();
1180
+ $ this ->reIndexAndCleanCache ();
1174
1181
1175
1182
$ queryDesc = <<<QUERY
1176
1183
{
1177
- products(filter: {category_id: {eq: " $ categoryId"}}, sort: {position: DESC }) {
1184
+ products(filter: {category_id: {eq: " $ categoryId"}}, sort: {position: ASC }) {
1178
1185
total_count
1179
1186
items {
1180
1187
sku
@@ -1186,8 +1193,8 @@ public function testSortByPosition()
1186
1193
$ resultDesc = $ this ->graphQlQuery ($ queryDesc );
1187
1194
$ this ->assertArrayNotHasKey ('errors ' , $ resultDesc );
1188
1195
$ productsDesc = array_column ($ resultDesc ['products ' ]['items ' ], 'sku ' );
1189
- $ expectedProductsDesc = $ expectedProductsAsc ;
1190
- $ this ->assertEquals ($ expectedProductsDesc , $ productsDesc );
1196
+ // position NOT equal and oldest entity first
1197
+ $ this ->assertEquals (array_reverse ( $ expectedProductsAsc ) , $ productsDesc );
1191
1198
}
1192
1199
1193
1200
/**
@@ -1532,6 +1539,7 @@ public function testFilterProductsBySingleCategoryId()
1532
1539
$ categoryRepository = ObjectManager::getInstance ()->get (CategoryRepositoryInterface::class);
1533
1540
1534
1541
$ links = $ productLinks ->getAssignedProducts ($ queryCategoryId );
1542
+ $ links = array_reverse ($ links );
1535
1543
foreach ($ response ['products ' ]['items ' ] as $ itemIndex => $ itemData ) {
1536
1544
$ this ->assertNotEmpty ($ itemData );
1537
1545
$ this ->assertEquals ($ response ['products ' ]['items ' ][$ itemIndex ]['sku ' ], $ links [$ itemIndex ]->getSku ());
0 commit comments