Skip to content

Commit ef13e6b

Browse files
committed
MC-32278: Position sort does not work in GraphQl.
- adding test
1 parent 6d814c3 commit ef13e6b

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

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

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Catalog\Api\ProductRepositoryInterface;
1414
use Magento\Catalog\Model\Category;
1515
use Magento\Catalog\Model\CategoryLinkManagement;
16+
use Magento\Catalog\Model\ResourceModel\Category\Collection;
1617
use Magento\Eav\Model\Config;
1718
use Magento\TestFramework\ObjectManager;
1819
use Magento\TestFramework\TestCase\GraphQlAbstract;
@@ -463,7 +464,7 @@ private function getDefaultAttributeOptionValue(string $attributeCode) : string
463464
}
464465

465466
/**
466-
* Full text search for Products and then filter the results by custom attribute ( sort is by defaulty by relevance)
467+
* Full text search for Products and then filter the results by custom attribute (default sort is relevance)
467468
*
468469
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_custom_attribute.php
469470
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -555,7 +556,7 @@ public function testSearchAndFilterByCustomAttribute()
555556
);
556557
}
557558

558-
// Validate the price layer of aggregations from the response
559+
// Validate the price layer of aggregations from the response
559560
$this->assertResponseFields(
560561
$response['products']['aggregations'][0],
561562
[
@@ -695,7 +696,7 @@ public function testFilterByCategoryIdAndCustomAttribute()
695696
//Validate the number of categories/sub-categories that contain the products with the custom attribute
696697
$this->assertCount(6, $actualCategoriesFromResponse);
697698

698-
$expectedCategoryInAggregrations =
699+
$expectedCategoryInAggregations =
699700
[
700701
[
701702
'count' => 2,
@@ -732,9 +733,9 @@ public function testFilterByCategoryIdAndCustomAttribute()
732733
],
733734
];
734735
// presort expected and actual results as different search engines have different orders
735-
usort($expectedCategoryInAggregrations, [$this, 'compareLabels']);
736+
usort($expectedCategoryInAggregations, [$this, 'compareLabels']);
736737
usort($actualCategoriesFromResponse, [$this, 'compareLabels']);
737-
$categoryInAggregations = array_map(null, $expectedCategoryInAggregrations, $actualCategoriesFromResponse);
738+
$categoryInAggregations = array_map(null, $expectedCategoryInAggregations, $actualCategoriesFromResponse);
738739

739740
//Validate the categories and sub-categories data in the filter layer
740741
foreach ($categoryInAggregations as $index => $categoryAggregationsData) {
@@ -1118,6 +1119,52 @@ public function testFilterWithinSpecificPriceRangeSortedByNameDesc()
11181119
$this->assertEquals(4, $response['products']['page_info']['page_size']);
11191120
}
11201121

1122+
/**
1123+
* @magentoApiDataFixture Magento/Catalog/_files/category_with_three_products.php
1124+
*/
1125+
public function testSortByPosition()
1126+
{
1127+
// Get category ID for filtering
1128+
/** @var Collection $categoryCollection */
1129+
$categoryCollection = Bootstrap::getObjectManager()->get(Collection::class);
1130+
$category = $categoryCollection->addFieldToFilter('name', 'Category 999')->getFirstItem();
1131+
$categoryId = $category->getId();
1132+
1133+
$queryAsc = <<<QUERY
1134+
{
1135+
products(filter: {category_id: {eq: "$categoryId"}}, sort: {position: ASC}) {
1136+
total_count
1137+
items {
1138+
sku
1139+
name
1140+
}
1141+
}
1142+
}
1143+
QUERY;
1144+
$resultAsc = $this->graphQlQuery($queryAsc);
1145+
$this->assertArrayNotHasKey('errors', $resultAsc);
1146+
$productsAsc = array_column($resultAsc['products']['items'], 'sku');
1147+
$expectedProductsAsc = ['simple1000', 'simple1001', 'simple1002'];
1148+
$this->assertEquals($expectedProductsAsc, $productsAsc);
1149+
1150+
$queryDesc = <<<QUERY
1151+
{
1152+
products(filter: {category_id: {eq: "$categoryId"}}, sort: {position: DESC}) {
1153+
total_count
1154+
items {
1155+
sku
1156+
name
1157+
}
1158+
}
1159+
}
1160+
QUERY;
1161+
$resultDesc = $this->graphQlQuery($queryDesc);
1162+
$this->assertArrayNotHasKey('errors', $resultDesc);
1163+
$productsDesc = array_column($resultAsc['products']['items'], 'sku');
1164+
$expectedProductsDesc = array_reverse($expectedProductsAsc);
1165+
$this->assertEquals($expectedProductsDesc, $productsDesc);
1166+
}
1167+
11211168
/**
11221169
* pageSize = total_count and current page = 2
11231170
* expected - error is thrown

0 commit comments

Comments
 (0)