Skip to content

Commit cd8b01b

Browse files
committed
Fix lever responses and add new test for get category by id
1 parent dfcac0c commit cd8b01b

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

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)