@@ -364,6 +364,8 @@ private function reIndexAndCleanCache() : void
364364 $ out = '' ;
365365 // phpcs:ignore Magento2.Security.InsecureFunction
366366 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 );
367369 CacheCleaner::cleanAll ();
368370 }
369371
@@ -670,15 +672,16 @@ public function testFilterByCategoryIdAndCustomAttribute()
670672 $ productRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
671673 $ product1 = $ productRepository ->get ('simple ' );
672674 $ product2 = $ productRepository ->get ('simple-4 ' );
673- $ filteredProducts = [$ product1 , $ product2 ];
675+ $ filteredProducts = [$ product2 , $ product1 ];
674676 $ productItemsInResponse = array_map (null , $ response ['products ' ]['items ' ], $ filteredProducts );
675677 //phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
676678 for ($ itemIndex = 0 ; $ itemIndex < count ($ filteredProducts ); $ itemIndex ++) {
677679 $ this ->assertNotEmpty ($ productItemsInResponse [$ itemIndex ]);
678680 //validate that correct products are returned
679681 $ this ->assertResponseFields (
680682 $ productItemsInResponse [$ itemIndex ][0 ],
681- [ 'name ' => $ filteredProducts [$ itemIndex ]->getName (),
683+ [
684+ 'name ' => $ filteredProducts [$ itemIndex ]->getName (),
682685 'sku ' => $ filteredProducts [$ itemIndex ]->getSku ()
683686 ]
684687 );
@@ -1121,6 +1124,7 @@ public function testFilterWithinSpecificPriceRangeSortedByNameDesc()
11211124 */
11221125 public function testSortByPosition ()
11231126 {
1127+ $ this ->reIndexAndCleanCache ();
11241128 // Get category ID for filtering
11251129 /** @var Collection $categoryCollection */
11261130 $ categoryCollection = Bootstrap::getObjectManager ()->get (Collection::class);
@@ -1141,7 +1145,8 @@ public function testSortByPosition()
11411145 $ resultAsc = $ this ->graphQlQuery ($ queryAsc );
11421146 $ this ->assertArrayNotHasKey ('errors ' , $ resultAsc );
11431147 $ 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
11451150 $ this ->assertEquals ($ expectedProductsAsc , $ productsAsc );
11461151
11471152 $ queryDesc = <<<QUERY
@@ -1158,23 +1163,25 @@ public function testSortByPosition()
11581163 $ resultDesc = $ this ->graphQlQuery ($ queryDesc );
11591164 $ this ->assertArrayNotHasKey ('errors ' , $ resultDesc );
11601165 $ 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 );
11631168
11641169 //revert position
11651170 $ productPositions = $ category ->getProductsPosition ();
1166- $ count = 3 ;
1171+ $ count = 1 ;
11671172 foreach ($ productPositions as $ productId => $ position ) {
11681173 $ productPositions [$ productId ] = $ count ;
1169- $ count-- ;
1174+ $ count++ ;
11701175 }
1176+ ksort ($ productPositions );
11711177
11721178 $ category ->setPostedProducts ($ productPositions );
11731179 $ category ->save ();
1180+ $ this ->reIndexAndCleanCache ();
11741181
11751182 $ queryDesc = <<<QUERY
11761183{
1177- products(filter: {category_id: {eq: " $ categoryId"}}, sort: {position: DESC }) {
1184+ products(filter: {category_id: {eq: " $ categoryId"}}, sort: {position: ASC }) {
11781185 total_count
11791186 items {
11801187 sku
@@ -1186,8 +1193,8 @@ public function testSortByPosition()
11861193 $ resultDesc = $ this ->graphQlQuery ($ queryDesc );
11871194 $ this ->assertArrayNotHasKey ('errors ' , $ resultDesc );
11881195 $ 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 );
11911198 }
11921199
11931200 /**
@@ -1532,6 +1539,7 @@ public function testFilterProductsBySingleCategoryId()
15321539 $ categoryRepository = ObjectManager::getInstance ()->get (CategoryRepositoryInterface::class);
15331540
15341541 $ links = $ productLinks ->getAssignedProducts ($ queryCategoryId );
1542+ $ links = array_reverse ($ links );
15351543 foreach ($ response ['products ' ]['items ' ] as $ itemIndex => $ itemData ) {
15361544 $ this ->assertNotEmpty ($ itemData );
15371545 $ this ->assertEquals ($ response ['products ' ]['items ' ][$ itemIndex ]['sku ' ], $ links [$ itemIndex ]->getSku ());
0 commit comments