Skip to content

Commit a21f81d

Browse files
committed
Merge branch 'ACP2E-51' of https://github.com/magento-l3/magento2ce into L3-PR-20211001
2 parents 66206e6 + cdaaf08 commit a21f81d

File tree

3 files changed

+227
-124
lines changed

3 files changed

+227
-124
lines changed

app/code/Magento/CatalogGraphQl/DataProvider/Product/SearchCriteriaBuilder.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
104104
$this->addDefaultSortOrder($searchCriteria, $args, $isSearch);
105105
}
106106

107-
$this->addEntityIdSort($searchCriteria, $isSearch);
107+
$this->addEntityIdSort($searchCriteria, $args);
108108
$this->addVisibilityFilter($searchCriteria, $isSearch, !empty($args['filter']));
109109

110110
$searchCriteria->setCurrentPage($args['currentPage']);
@@ -137,17 +137,15 @@ private function addVisibilityFilter(SearchCriteriaInterface $searchCriteria, bo
137137
* Add sort by Entity ID
138138
*
139139
* @param SearchCriteriaInterface $searchCriteria
140-
* @param bool $isSearch
140+
* @param array $args
141141
*/
142-
private function addEntityIdSort(SearchCriteriaInterface $searchCriteria, bool $isSearch): void
142+
private function addEntityIdSort(SearchCriteriaInterface $searchCriteria, array $args): void
143143
{
144-
if ($isSearch) {
145-
return;
146-
}
144+
$sortOrder = !empty($args['sort']) ? reset($args['sort']) : SortOrder::SORT_DESC;
147145
$sortOrderArray = $searchCriteria->getSortOrders();
148146
$sortOrderArray[] = $this->sortOrderBuilder
149147
->setField('_id')
150-
->setDirection(SortOrder::SORT_DESC)
148+
->setDirection($sortOrder)
151149
->create();
152150
$searchCriteria->setSortOrders($sortOrderArray);
153151
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductPriceTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
1313
use Magento\Catalog\Api\ProductRepositoryInterface;
1414
use Magento\Catalog\Model\Product;
15+
use Magento\Catalog\Model\ResourceModel\Category\Collection;
1516
use Magento\ConfigurableProduct\Api\LinkManagementInterface;
1617
use Magento\ConfigurableProduct\Model\LinkManagement;
1718
use Magento\Customer\Model\Group;
@@ -1222,4 +1223,61 @@ private function saveProductTierPrices(ProductInterface $product, array $tierPri
12221223
$product->save();
12231224
}
12241225
}
1226+
1227+
/**
1228+
* Test products with the same price reverse position with ASC and DESC sorting
1229+
*
1230+
* @magentoApiDataFixture Magento/Catalog/_files/category_with_three_products.php
1231+
*/
1232+
public function testSortByEqualPriceAndAscDescReversePosition()
1233+
{
1234+
/** @var Product $product */
1235+
$product = $this->productRepository->get('simple1001');
1236+
//setting the same price for the product as all the rest have
1237+
$product->setPrice('10');
1238+
$this->productRepository->save($product);
1239+
1240+
/** @var Collection $categoryCollection */
1241+
$categoryCollection = Bootstrap::getObjectManager()->get(Collection::class);
1242+
$category = $categoryCollection->addFieldToFilter('name', 'Category 999')->getFirstItem();
1243+
$categoryId = (int) $category->getId();
1244+
1245+
$expectedProductsAsc = ['simple1000', 'simple1001', 'simple1002'];
1246+
$queryAsc = $this->getCategoryFilterPriceQuery($categoryId, 'ASC');
1247+
$resultAsc = $this->graphQlQuery($queryAsc);
1248+
$this->assertArrayNotHasKey('errors', $resultAsc);
1249+
$productsAsc = array_column($resultAsc['products']['items'], 'sku');
1250+
$this->assertEquals($expectedProductsAsc, $productsAsc);
1251+
1252+
$expectedProductsDesc = array_reverse($expectedProductsAsc);
1253+
$queryDesc = $this->getCategoryFilterPriceQuery($categoryId, 'DESC');
1254+
$resultDesc = $this->graphQlQuery($queryDesc);
1255+
$this->assertArrayNotHasKey('errors', $resultDesc);
1256+
$productsDesc = array_column($resultDesc['products']['items'], 'sku');
1257+
$this->assertEquals($expectedProductsDesc, $productsDesc);
1258+
}
1259+
1260+
/**
1261+
* Query for category filter price
1262+
*
1263+
* @param int $categoryId
1264+
* @param string $direction
1265+
* @return string
1266+
*/
1267+
protected function getCategoryFilterPriceQuery(int $categoryId, string $direction): string
1268+
{
1269+
$query = <<<QUERY
1270+
{
1271+
products(filter: {category_id: {eq: "$categoryId"}}, sort: {price: $direction}) {
1272+
total_count
1273+
items {
1274+
sku
1275+
name
1276+
}
1277+
}
1278+
}
1279+
QUERY;
1280+
1281+
return $query;
1282+
}
12251283
}

0 commit comments

Comments
 (0)