|
12 | 12 | use Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
|
13 | 13 | use Magento\Catalog\Api\ProductRepositoryInterface;
|
14 | 14 | use Magento\Catalog\Model\Product;
|
| 15 | +use Magento\Catalog\Model\ResourceModel\Category\Collection; |
15 | 16 | use Magento\ConfigurableProduct\Api\LinkManagementInterface;
|
16 | 17 | use Magento\ConfigurableProduct\Model\LinkManagement;
|
17 | 18 | use Magento\Customer\Model\Group;
|
@@ -1222,4 +1223,61 @@ private function saveProductTierPrices(ProductInterface $product, array $tierPri
|
1222 | 1223 | $product->save();
|
1223 | 1224 | }
|
1224 | 1225 | }
|
| 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 | + } |
1225 | 1283 | }
|
0 commit comments