Skip to content

Commit 258a2d1

Browse files
author
Valeriy Nayda
committed
Merge branch '139-show-only-active-categories' of github.com:comwrap/graphql-ce into 139-show-only-active-categories
2 parents 1495087 + 6ac61ae commit 258a2d1

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/CategoryTree.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId): \Iterato
103103
$collection->addFieldToFilter('level', ['lteq' => $level + $depth - self::DEPTH_OFFSET]);
104104
$collection->addIsActiveFilter();
105105
$collection->setOrder('level');
106+
$collection->setOrder(
107+
'position',
108+
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection::SORT_ORDER_DESC
109+
);
106110
$collection->getSelect()->orWhere(
107111
$collection->getSelect()
108112
->getConnection()

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/ExtractDataFromCategoryTree.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
class ExtractDataFromCategoryTree
1717
{
18-
const START_CATEGORY_FETCH_LEVEL = 1;
19-
2018
/**
2119
* @var Hydrator
2220
*/
@@ -27,6 +25,11 @@ class ExtractDataFromCategoryTree
2725
*/
2826
private $iteratingCategory;
2927

28+
/**
29+
* @var int
30+
*/
31+
private $startCategoryFetchLevel = 1;
32+
3033
/**
3134
* @param Hydrator $categoryHydrator
3235
*/
@@ -51,9 +54,12 @@ public function execute(\Iterator $iterator): array
5154
$iterator->next();
5255

5356
$pathElements = explode("/", $category->getPath());
54-
$this->iteratingCategory = $category;
57+
if (empty($tree)){
58+
$this->startCategoryFetchLevel = count($pathElements) - 1;
59+
}
5560

56-
$currentLevelTree = $this->explodePathToArray($pathElements, self::START_CATEGORY_FETCH_LEVEL);
61+
$this->iteratingCategory = $category;
62+
$currentLevelTree = $this->explodePathToArray($pathElements, $this->startCategoryFetchLevel);
5763
if (empty($tree)) {
5864
$tree = $currentLevelTree;
5965
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,44 @@ public function testCategoriesTree()
112112
);
113113
}
114114

115+
/**
116+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
117+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
118+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
119+
*/
120+
public function testGetCategoryById()
121+
{
122+
$rootCategoryId = 13;
123+
$query = <<<QUERY
124+
{
125+
category(id: {$rootCategoryId}) {
126+
id
127+
name
128+
}
129+
}
130+
QUERY;
131+
132+
// get customer ID token
133+
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
134+
$customerTokenService = $this->objectManager->create(
135+
\Magento\Integration\Api\CustomerTokenServiceInterface::class
136+
);
137+
$customerToken = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
138+
139+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
140+
$response = $this->graphQlQuery($query, [], '', $headerMap);
141+
$responseDataObject = new DataObject($response);
142+
//Some sort of smoke testing
143+
self::assertEquals(
144+
'Category 1.2',
145+
$responseDataObject->getData('category/name')
146+
);
147+
self::assertEquals(
148+
13,
149+
$responseDataObject->getData('category/id')
150+
);
151+
}
152+
115153
/**
116154
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
117155
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)

0 commit comments

Comments
 (0)