Skip to content

Commit 5f976fa

Browse files
author
Valeriy Naida
authored
Merge pull request #3433 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents 57d7010 + 2808c19 commit 5f976fa

File tree

9 files changed

+178
-94
lines changed

9 files changed

+178
-94
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductHtmlAttribute.php renamed to app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductComplexTextAttribute.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
99

10-
use Magento\Catalog\Model\Product;
1110
use Magento\Framework\GraphQl\Config\Element\Field;
12-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1311
use Magento\Framework\GraphQl\Query\ResolverInterface;
1412
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
use Magento\Catalog\Model\Product;
14+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1515
use Magento\Catalog\Helper\Output as OutputHelper;
1616

1717
/**
1818
* Resolve rendered content for attributes where HTML content is allowed
1919
*/
20-
class ProductHtmlAttribute implements ResolverInterface
20+
class ProductComplexTextAttribute implements ResolverInterface
2121
{
2222
/**
2323
* @var OutputHelper
@@ -42,7 +42,7 @@ public function resolve(
4242
ResolveInfo $info,
4343
array $value = null,
4444
array $args = null
45-
) {
45+
): array {
4646
if (!isset($value['model'])) {
4747
throw new GraphQlInputException(__('"model" value should be specified'));
4848
}
@@ -51,6 +51,7 @@ public function resolve(
5151
$product = $value['model'];
5252
$fieldName = $field->getName();
5353
$renderedValue = $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
54-
return $renderedValue;
54+
55+
return ['html' => $renderedValue ?? ''];
5556
}
5657
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
248248
id: Int @doc(description: "The ID number assigned to the product") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\EntityIdToId")
249249
name: String @doc(description: "The product name. Customers use this name to identify the product.")
250250
sku: String @doc(description: "A number or code assigned to a product to identify the product, options, price, and manufacturer")
251-
description: String @doc(description: "Detailed information about the product. The value can include simple HTML tags.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductHtmlAttribute")
252-
short_description: String @doc(description: "A short description of the product. Its use depends on the theme.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductHtmlAttribute")
251+
description: ComplexTextValue @doc(description: "Detailed information about the product. The value can include simple HTML tags.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductComplexTextAttribute")
252+
short_description: ComplexTextValue @doc(description: "A short description of the product. Its use depends on the theme.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductComplexTextAttribute")
253253
special_price: Float @doc(description: "The discounted price of the product")
254254
special_from_date: String @doc(description: "The beginning date that a product has a special price")
255255
special_to_date: String @doc(description: "The end date that a product has a special price")

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ enum SortEnum @doc(description: "This enumeration indicates whether to return re
3636
ASC
3737
DESC
3838
}
39+
40+
type ComplexTextValue {
41+
html: String! @doc(description: "HTML format")
42+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ public function testCategoryProducts()
132132
attribute_set_id
133133
country_of_manufacture
134134
created_at
135-
description
135+
description {
136+
html
137+
}
136138
gift_message_available
137139
id
138140
categories {
@@ -222,7 +224,9 @@ public function testCategoryProducts()
222224
position
223225
sku
224226
}
225-
short_description
227+
short_description {
228+
html
229+
}
226230
sku
227231
small_image { url, label }
228232
thumbnail { url, label }
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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\ProductRepositoryInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
14+
class ProductTextAttributesTest extends GraphQlAbstract
15+
{
16+
/**
17+
* @var ProductRepositoryInterface
18+
*/
19+
private $productRepository;
20+
21+
protected function setUp()
22+
{
23+
$this->productRepository = Bootstrap::getObjectManager()::getInstance()->get(ProductRepositoryInterface::class);
24+
}
25+
26+
/**
27+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
28+
*/
29+
public function testProductTextAttributes()
30+
{
31+
$productSku = 'simple';
32+
33+
$query = <<<QUERY
34+
{
35+
products(filter: {sku: {eq: "{$productSku}"}})
36+
{
37+
items {
38+
sku
39+
description {
40+
html
41+
}
42+
short_description {
43+
html
44+
}
45+
}
46+
}
47+
}
48+
QUERY;
49+
$response = $this->graphQlQuery($query);
50+
51+
$this->assertEquals(
52+
$productSku,
53+
$response['products']['items'][0]['sku']
54+
);
55+
$this->assertEquals(
56+
'Short description',
57+
$response['products']['items'][0]['short_description']['html']
58+
);
59+
$this->assertEquals(
60+
'Description with <b>html tag</b>',
61+
$response['products']['items'][0]['description']['html']
62+
);
63+
}
64+
65+
/**
66+
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
67+
*/
68+
public function testProductWithoutFilledTextAttributes()
69+
{
70+
$productSku = 'virtual-product';
71+
72+
$query = <<<QUERY
73+
{
74+
products(filter: {sku: {eq: "{$productSku}"}})
75+
{
76+
items {
77+
sku
78+
description {
79+
html
80+
}
81+
short_description {
82+
html
83+
}
84+
}
85+
}
86+
}
87+
QUERY;
88+
$response = $this->graphQlQuery($query);
89+
90+
$this->assertEquals(
91+
$productSku,
92+
$response['products']['items'][0]['sku']
93+
);
94+
$this->assertEquals(
95+
'',
96+
$response['products']['items'][0]['short_description']['html']
97+
);
98+
$this->assertEquals(
99+
'',
100+
$response['products']['items'][0]['description']['html']
101+
);
102+
}
103+
104+
/**
105+
* Test for checking that product fields with directives allowed are rendered correctly
106+
*
107+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
108+
* @magentoApiDataFixture Magento/Cms/_files/block.php
109+
*/
110+
public function testHtmlDirectivesRendering()
111+
{
112+
$productSku = 'simple';
113+
$cmsBlockId = 'fixture_block';
114+
$assertionCmsBlockText = 'Fixture Block Title';
115+
116+
$product = $this->productRepository->get($productSku, false, null, true);
117+
$product->setDescription('Test: {{block id="' . $cmsBlockId . '"}}');
118+
$product->setShortDescription('Test: {{block id="' . $cmsBlockId . '"}}');
119+
$this->productRepository->save($product);
120+
121+
$query = <<<QUERY
122+
{
123+
products(filter: {sku: {eq: "{$productSku}"}}) {
124+
items {
125+
description {
126+
html
127+
}
128+
short_description {
129+
html
130+
}
131+
}
132+
}
133+
}
134+
QUERY;
135+
$response = $this->graphQlQuery($query);
136+
137+
self::assertContains($assertionCmsBlockText, $response['products']['items'][0]['description']['html']);
138+
self::assertNotContains('{{block id', $response['products']['items'][0]['description']['html']);
139+
self::assertContains($assertionCmsBlockText, $response['products']['items'][0]['short_description']['html']);
140+
self::assertNotContains('{{block id', $response['products']['items'][0]['short_description']['html']);
141+
}
142+
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public function testQueryAllFieldsSimpleProduct()
4444
attribute_set_id
4545
country_of_manufacture
4646
created_at
47-
description
4847
gift_message_available
4948
id
5049
categories {
@@ -202,7 +201,6 @@ public function testQueryAllFieldsSimpleProduct()
202201
position
203202
sku
204203
}
205-
short_description
206204
sku
207205
small_image{ url, label }
208206
thumbnail { url, label }
@@ -296,7 +294,6 @@ public function testQueryMediaGalleryEntryFieldsSimpleProduct()
296294
}
297295
country_of_manufacture
298296
created_at
299-
description
300297
gift_message_available
301298
id
302299
image {url, label}
@@ -446,7 +443,6 @@ public function testQueryMediaGalleryEntryFieldsSimpleProduct()
446443
position
447444
sku
448445
}
449-
short_description
450446
sku
451447
small_image { url, label }
452448
special_from_date
@@ -908,11 +904,9 @@ private function assertEavAttributes($product, $actualResponse)
908904
{
909905
$eavAttributes = [
910906
'url_key',
911-
'description',
912907
'meta_description',
913908
'meta_keyword',
914909
'meta_title',
915-
'short_description',
916910
'country_of_manufacture',
917911
'gift_message_available',
918912
'news_from_date',

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

Lines changed: 0 additions & 65 deletions
This file was deleted.

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ public function testProductWithNoCategoriesAssigned()
3333
items {
3434
name,
3535
sku,
36-
description,
37-
url_rewrites {
36+
description {
37+
html
38+
}
39+
url_rewrites {
3840
url,
3941
parameters {
4042
name,
@@ -87,8 +89,10 @@ public function testProductWithOneCategoryAssigned()
8789
items {
8890
name,
8991
sku,
90-
description,
91-
url_rewrites {
92+
description {
93+
html
94+
}
95+
url_rewrites {
9296
url,
9397
parameters {
9498
name,

0 commit comments

Comments
 (0)