Skip to content

Commit b74a600

Browse files
author
Stanislav Idolov
authored
ENGCOM-2321: Additional checks for fragments added in category tree #101
2 parents 7b592b9 + f01c5f3 commit b74a600

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

app/code/Magento/CatalogGraphQl/Model/AttributesJoiner.php

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function join(FieldNode $fieldNode, AbstractCollection $collection) : voi
2828

2929
/** @var FieldNode $field */
3030
foreach ($query as $field) {
31+
if ($field->kind === 'InlineFragment') {
32+
continue;
33+
}
34+
3135
if (!$collection->isAttributeAdded($field->name->value)) {
3236
$collection->addAttributeToSelect($field->name->value);
3337
}

app/code/Magento/CatalogGraphQl/Model/Category/DepthCalculator.php

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public function calculate(FieldNode $fieldNode) : int
2626
$depth = count($selections) ? 1 : 0;
2727
$childrenDepth = [0];
2828
foreach ($selections as $node) {
29+
if ($node->kind === 'InlineFragment') {
30+
continue;
31+
}
32+
2933
$childrenDepth[] = $this->calculate($node);
3034
}
3135

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

+4
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ private function joinAttributesRecursively(Collection $collection, FieldNode $fi
143143

144144
/** @var FieldNode $node */
145145
foreach ($subSelection as $node) {
146+
if ($node->kind === 'InlineFragment') {
147+
continue;
148+
}
149+
146150
$this->joinAttributesRecursively($collection, $node);
147151
}
148152
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Catalog;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\TestFramework\ObjectManager;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
15+
/**
16+
* Test of getting child products info of configurable product on category request
17+
*/
18+
class CategoryProductsVariantsTest extends GraphQlAbstract
19+
{
20+
/**
21+
*
22+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
23+
* @throws \Magento\Framework\Exception\NoSuchEntityException
24+
*/
25+
public function testGetSimpleProductsFromCategory()
26+
{
27+
28+
$query
29+
= <<<QUERY
30+
{
31+
category(id: 2) {
32+
id
33+
name
34+
products {
35+
items {
36+
sku
37+
... on ConfigurableProduct {
38+
variants {
39+
product {
40+
sku
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
QUERY;
49+
50+
$response = $this->graphQlQuery($query);
51+
52+
/** @var ProductRepositoryInterface $productRepository */
53+
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
54+
$product = $productRepository->get('simple_10', false, null, true);
55+
56+
$this->assertArrayHasKey('variants', $response['category']['products']['items'][0]);
57+
$this->assertCount(2, $response['category']['products']['items'][0]['variants']);
58+
$this->assertSimpleProductFields($product, $response['category']['products']['items'][0]['variants'][0]);
59+
}
60+
61+
/**
62+
* @param ProductInterface $product
63+
* @param array $actualResponse
64+
*/
65+
private function assertSimpleProductFields($product, $actualResponse)
66+
{
67+
$assertionMap = [
68+
[
69+
'response_field' => 'product', 'expected_value' => [
70+
"sku" => $product->getSku()
71+
]
72+
],
73+
];
74+
75+
$this->assertResponseFields($actualResponse, $assertionMap);
76+
}
77+
78+
/**
79+
* @param array $actualResponse
80+
* @param array $assertionMap
81+
*/
82+
private function assertResponseFields($actualResponse, $assertionMap)
83+
{
84+
foreach ($assertionMap as $key => $assertionData) {
85+
$expectedValue = isset($assertionData['expected_value'])
86+
? $assertionData['expected_value']
87+
: $assertionData;
88+
$responseField = isset($assertionData['response_field']) ? $assertionData['response_field'] : $key;
89+
self::assertNotNull(
90+
$expectedValue,
91+
"Value of '{$responseField}' field must not be NULL"
92+
);
93+
self::assertEquals(
94+
$expectedValue,
95+
$actualResponse[$responseField],
96+
"Value of '{$responseField}' field in response does not match expected value: "
97+
. var_export($expectedValue, true)
98+
);
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)