Skip to content

Commit 654cb39

Browse files
committed
Merge branch '2.3-develop' into 2.3-develop-pr14
2 parents fb143ac + 0122d0b commit 654cb39

File tree

8 files changed

+132
-23
lines changed

8 files changed

+132
-23
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,21 @@ public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId): \Iterato
101101

102102
$collection->addFieldToFilter('level', ['gt' => $level]);
103103
$collection->addFieldToFilter('level', ['lteq' => $level + $depth - self::DEPTH_OFFSET]);
104+
$collection->addIsActiveFilter();
104105
$collection->setOrder('level');
106+
$collection->setOrder(
107+
'position',
108+
$collection::SORT_ORDER_DESC
109+
);
105110
$collection->getSelect()->orWhere(
106-
$this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField() . ' = ?',
111+
$collection->getSelect()
112+
->getConnection()
113+
->quoteIdentifier(
114+
'e.' . $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
115+
) . ' = ?',
107116
$rootCategoryId
108117
);
118+
109119
return $collection->getIterator();
110120
}
111121

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

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ class ExtractDataFromCategoryTree
2020
*/
2121
private $categoryHydrator;
2222

23+
/**
24+
* @var CategoryInterface;
25+
*/
26+
private $iteratingCategory;
27+
28+
/**
29+
* @var int
30+
*/
31+
private $startCategoryFetchLevel = 1;
32+
2333
/**
2434
* @param Hydrator $categoryHydrator
2535
*/
@@ -42,14 +52,63 @@ public function execute(\Iterator $iterator): array
4252
/** @var CategoryInterface $category */
4353
$category = $iterator->current();
4454
$iterator->next();
45-
$nextCategory = $iterator->current();
46-
$tree[$category->getId()] = $this->categoryHydrator->hydrateCategory($category);
47-
$tree[$category->getId()]['model'] = $category;
48-
if ($nextCategory && (int) $nextCategory->getLevel() !== (int) $category->getLevel()) {
49-
$tree[$category->getId()]['children'] = $this->execute($iterator);
55+
56+
$pathElements = explode("/", $category->getPath());
57+
if (empty($tree)) {
58+
$this->startCategoryFetchLevel = count($pathElements) - 1;
5059
}
60+
61+
$this->iteratingCategory = $category;
62+
$currentLevelTree = $this->explodePathToArray($pathElements, $this->startCategoryFetchLevel);
63+
if (empty($tree)) {
64+
$tree = $currentLevelTree;
65+
}
66+
$tree = $this->mergeCategoriesTrees($currentLevelTree, $tree);
5167
}
5268

5369
return $tree;
5470
}
71+
72+
/**
73+
* Merge together complex categories trees
74+
*
75+
* @param array $tree1
76+
* @param array $tree2
77+
* @return array
78+
*/
79+
private function mergeCategoriesTrees(array &$tree1, array &$tree2): array
80+
{
81+
$mergedTree = $tree1;
82+
foreach ($tree2 as $currentKey => &$value) {
83+
if (is_array($value) && isset($mergedTree[$currentKey]) && is_array($mergedTree[$currentKey])) {
84+
$mergedTree[$currentKey] = $this->mergeCategoriesTrees($mergedTree[$currentKey], $value);
85+
} else {
86+
$mergedTree[$currentKey] = $value;
87+
}
88+
}
89+
return $mergedTree;
90+
}
91+
92+
/**
93+
* Recursive method to generate tree for one category path
94+
*
95+
* @param array $pathElements
96+
* @param int $index
97+
* @return array
98+
*/
99+
private function explodePathToArray(array $pathElements, int $index): array
100+
{
101+
$tree = [];
102+
$tree[$pathElements[$index]]['id'] = $pathElements[$index];
103+
if ($index === count($pathElements) - 1) {
104+
$tree[$pathElements[$index]] = $this->categoryHydrator->hydrateCategory($this->iteratingCategory);
105+
$tree[$pathElements[$index]]['model'] = $this->iteratingCategory;
106+
}
107+
$currentIndex = $index;
108+
$index++;
109+
if (isset($pathElements[$index])) {
110+
$tree[$pathElements[$currentIndex]]['children'] = $this->explodePathToArray($pathElements, $index);
111+
}
112+
return $tree;
113+
}
55114
}

app/code/Magento/CustomerGraphQl/Model/Resolver/UpdateCustomerAddress.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ private function updateCustomerAddress(int $customerId, int $addressId, array $a
109109
{
110110
$address = $this->getCustomerAddressForUser->execute($addressId, $customerId);
111111
$this->dataObjectHelper->populateWithArray($address, $addressData, AddressInterface::class);
112+
if (isset($addressData['region']['region_id'])) {
113+
$address->setRegionId($address->getRegion()->getRegionId());
114+
}
115+
112116
return $this->addressRepository->save($address);
113117
}
114118
}

app/code/Magento/GraphQl/etc/schema.graphqls

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ type Query {
55
}
66

77
type Mutation {
8-
placeholderMutation: String @doc(description: "Mutation type cannot be declared without fields. The placeholder will be removed when at least one mutation field is declared.")
98
}
109

1110
input FilterTypeInput @doc(description: "FilterTypeInput specifies which action will be performed in a query ") {

app/code/Magento/StoreGraphQl/etc/schema.graphqls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ type Query {
66

77
type Website @doc(description: "The type contains information about a website") {
88
id : Int @doc(description: "The ID number assigned to the website")
9-
name : String @doc(description: "The website name. Websites use this name to identify it easyer.")
9+
name : String @doc(description: "The website name. Websites use this name to identify it easier.")
1010
code : String @doc(description: "A code assigned to the website to identify it")
1111
sort_order : Int @doc(description: "The attribute to use for sorting websites")
12-
default_group_id : String @doc(description: "The default group id that the website has")
12+
default_group_id : String @doc(description: "The default group ID that the website has")
1313
is_default : Boolean @doc(description: "Specifies if this is the default website")
1414
}
1515

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

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public function testCategoriesTree()
8383
$responseDataObject = new DataObject($response);
8484
//Some sort of smoke testing
8585
self::assertEquals(
86-
'Ololo',
87-
$responseDataObject->getData('category/children/7/children/1/description')
86+
'Its a description of Test Category 1.2',
87+
$responseDataObject->getData('category/children/0/children/1/description')
8888
);
8989
self::assertEquals(
9090
'default-category',
@@ -99,16 +99,54 @@ public function testCategoriesTree()
9999
$responseDataObject->getData('category/children/0/default_sort_by')
100100
);
101101
self::assertCount(
102-
8,
102+
7,
103103
$responseDataObject->getData('category/children')
104104
);
105105
self::assertCount(
106106
2,
107-
$responseDataObject->getData('category/children/7/children')
107+
$responseDataObject->getData('category/children/0/children')
108108
);
109109
self::assertEquals(
110-
5,
111-
$responseDataObject->getData('category/children/7/children/1/children/0/id')
110+
13,
111+
$responseDataObject->getData('category/children/0/children/1/id')
112+
);
113+
}
114+
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')
112150
);
113151
}
114152

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,12 @@ private function assertCustomerAddressesFields(AddressInterface $address, $actua
218218
];
219219
$this->assertResponseFields($actualResponse, $assertionMap);
220220
$this->assertTrue(is_array([$actualResponse['region']]), "region field must be of an array type.");
221-
// https://github.com/magento/graphql-ce/issues/270
222-
// $assertionRegionMap = [
223-
// ['response_field' => 'region', 'expected_value' => $address->getRegion()->getRegion()],
224-
// ['response_field' => 'region_code', 'expected_value' => $address->getRegion()->getRegionCode()],
225-
// ['response_field' => 'region_id', 'expected_value' => $address->getRegion()->getRegionId()]
226-
// ];
227-
// $this->assertResponseFields($actualResponse['region'], $assertionRegionMap);
221+
$assertionRegionMap = [
222+
['response_field' => 'region', 'expected_value' => $address->getRegion()->getRegion()],
223+
['response_field' => 'region_code', 'expected_value' => $address->getRegion()->getRegionCode()],
224+
['response_field' => 'region_id', 'expected_value' => $address->getRegion()->getRegionId()]
225+
];
226+
$this->assertResponseFields($actualResponse['region'], $assertionRegionMap);
228227
}
229228

230229
/**

dev/tests/integration/testsuite/Magento/Catalog/_files/categories.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
->setParentId(3)
179179
->setPath('1/2/3/13')
180180
->setLevel(3)
181-
->setDescription('Ololo')
181+
->setDescription('Its a description of Test Category 1.2')
182182
->setAvailableSortBy('name')
183183
->setDefaultSortBy('name')
184184
->setIsActive(true)

0 commit comments

Comments
 (0)