Skip to content

Commit 25fd467

Browse files
author
Valeriy Nayda
committed
GraphQl-45: Product textarea field format
1 parent 43804b9 commit 25fd467

File tree

7 files changed

+150
-141
lines changed

7 files changed

+150
-141
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductComplexTextAttribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ public function resolve(
5252
$fieldName = $field->getName();
5353
$renderedValue = $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
5454

55-
return ['html' => $renderedValue];
55+
return ['html' => $renderedValue ?? ''];
5656
}
5757
}

app/code/Magento/GraphQl/Model/Resolver/ComplexTextValue/HtmlFormat.php

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ enum SortEnum @doc(description: "This enumeration indicates whether to return re
3838
}
3939

4040
type ComplexTextValue {
41-
html: String! @doc(description: "HTML format") @resolver(class: "\\Magento\\GraphQl\\Model\\Resolver\\ComplexTextValue\\HtmlFormat")
41+
html: String! @doc(description: "HTML format")
4242
}

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 {
@@ -223,7 +225,9 @@ public function testCategoryProducts()
223225
position
224226
sku
225227
}
226-
short_description
228+
short_description {
229+
html
230+
}
227231
sku
228232
small_image {
229233
path
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 & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ public function testQueryAllFieldsSimpleProduct()
4444
attribute_set_id
4545
country_of_manufacture
4646
created_at
47-
description {
48-
html
49-
}
5047
gift_message_available
5148
id
5249
categories {
@@ -205,9 +202,6 @@ public function testQueryAllFieldsSimpleProduct()
205202
position
206203
sku
207204
}
208-
short_description {
209-
html
210-
}
211205
sku
212206
small_image {
213207
path
@@ -266,7 +260,6 @@ public function testQueryAllFieldsSimpleProduct()
266260
$this->assertArrayHasKey(0, $response['products']['items']);
267261
$this->assertBaseFields($product, $response['products']['items'][0]);
268262
$this->assertEavAttributes($product, $response['products']['items'][0]);
269-
$this->assertComplexTextAttributes($product, $response['products']['items'][0]);
270263
$this->assertOptions($product, $response['products']['items'][0]);
271264
$this->assertTierPrices($product, $response['products']['items'][0]);
272265
$this->assertArrayHasKey('websites', $response['products']['items'][0]);
@@ -306,7 +299,6 @@ public function testQueryMediaGalleryEntryFieldsSimpleProduct()
306299
}
307300
country_of_manufacture
308301
created_at
309-
description
310302
gift_message_available
311303
id
312304
image
@@ -457,7 +449,6 @@ public function testQueryMediaGalleryEntryFieldsSimpleProduct()
457449
position
458450
sku
459451
}
460-
short_description
461452
sku
462453
small_image {
463454
path
@@ -947,31 +938,6 @@ private function assertEavAttributes($product, $actualResponse)
947938
$this->assertResponseFields($actualResponse, $assertionMap);
948939
}
949940

950-
/**
951-
* @param ProductInterface $product
952-
* @param array $actualResponse
953-
*/
954-
private function assertComplexTextAttributes($product, $actualResponse)
955-
{
956-
$eavAttributes = [
957-
'description',
958-
'short_description',
959-
];
960-
$assertionMap = [];
961-
foreach ($eavAttributes as $attributeCode) {
962-
$expectedAttribute = $product->getCustomAttribute($attributeCode);
963-
964-
$assertionMap[] = [
965-
'response_field' => $this->eavAttributesToGraphQlSchemaFieldTranslator($attributeCode),
966-
'expected_value' => $expectedAttribute ? [
967-
'html' => $expectedAttribute->getValue()
968-
] : null
969-
];
970-
}
971-
972-
$this->assertResponseFields($actualResponse, $assertionMap);
973-
}
974-
975941
/**
976942
* @param string $eavAttributeCode
977943
* @return string

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

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

0 commit comments

Comments
 (0)