Skip to content

Commit

Permalink
Merge pull request #3433 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - GraphQL
  • Loading branch information
Valeriy Naida authored Nov 12, 2018
2 parents 57d7010 + 2808c19 commit 5f976fa
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

namespace Magento\CatalogGraphQl\Model\Resolver\Product;

use Magento\Catalog\Model\Product;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Catalog\Model\Product;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Catalog\Helper\Output as OutputHelper;

/**
* Resolve rendered content for attributes where HTML content is allowed
*/
class ProductHtmlAttribute implements ResolverInterface
class ProductComplexTextAttribute implements ResolverInterface
{
/**
* @var OutputHelper
Expand All @@ -42,7 +42,7 @@ public function resolve(
ResolveInfo $info,
array $value = null,
array $args = null
) {
): array {
if (!isset($value['model'])) {
throw new GraphQlInputException(__('"model" value should be specified'));
}
Expand All @@ -51,6 +51,7 @@ public function resolve(
$product = $value['model'];
$fieldName = $field->getName();
$renderedValue = $this->outputHelper->productAttribute($product, $product->getData($fieldName), $fieldName);
return $renderedValue;

return ['html' => $renderedValue ?? ''];
}
}
4 changes: 2 additions & 2 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
id: Int @doc(description: "The ID number assigned to the product") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\EntityIdToId")
name: String @doc(description: "The product name. Customers use this name to identify the product.")
sku: String @doc(description: "A number or code assigned to a product to identify the product, options, price, and manufacturer")
description: String @doc(description: "Detailed information about the product. The value can include simple HTML tags.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductHtmlAttribute")
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")
description: ComplexTextValue @doc(description: "Detailed information about the product. The value can include simple HTML tags.") @resolver(class: "\\Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\ProductComplexTextAttribute")
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")
special_price: Float @doc(description: "The discounted price of the product")
special_from_date: String @doc(description: "The beginning date that a product has a special price")
special_to_date: String @doc(description: "The end date that a product has a special price")
Expand Down
4 changes: 4 additions & 0 deletions app/code/Magento/GraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ enum SortEnum @doc(description: "This enumeration indicates whether to return re
ASC
DESC
}

type ComplexTextValue {
html: String! @doc(description: "HTML format")
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ public function testCategoryProducts()
attribute_set_id
country_of_manufacture
created_at
description
description {
html
}
gift_message_available
id
categories {
Expand Down Expand Up @@ -222,7 +224,9 @@ public function testCategoryProducts()
position
sku
}
short_description
short_description {
html
}
sku
small_image { url, label }
thumbnail { url, label }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Catalog;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

class ProductTextAttributesTest extends GraphQlAbstract
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

protected function setUp()
{
$this->productRepository = Bootstrap::getObjectManager()::getInstance()->get(ProductRepositoryInterface::class);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
*/
public function testProductTextAttributes()
{
$productSku = 'simple';

$query = <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}})
{
items {
sku
description {
html
}
short_description {
html
}
}
}
}
QUERY;
$response = $this->graphQlQuery($query);

$this->assertEquals(
$productSku,
$response['products']['items'][0]['sku']
);
$this->assertEquals(
'Short description',
$response['products']['items'][0]['short_description']['html']
);
$this->assertEquals(
'Description with <b>html tag</b>',
$response['products']['items'][0]['description']['html']
);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
*/
public function testProductWithoutFilledTextAttributes()
{
$productSku = 'virtual-product';

$query = <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}})
{
items {
sku
description {
html
}
short_description {
html
}
}
}
}
QUERY;
$response = $this->graphQlQuery($query);

$this->assertEquals(
$productSku,
$response['products']['items'][0]['sku']
);
$this->assertEquals(
'',
$response['products']['items'][0]['short_description']['html']
);
$this->assertEquals(
'',
$response['products']['items'][0]['description']['html']
);
}

/**
* Test for checking that product fields with directives allowed are rendered correctly
*
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/Cms/_files/block.php
*/
public function testHtmlDirectivesRendering()
{
$productSku = 'simple';
$cmsBlockId = 'fixture_block';
$assertionCmsBlockText = 'Fixture Block Title';

$product = $this->productRepository->get($productSku, false, null, true);
$product->setDescription('Test: {{block id="' . $cmsBlockId . '"}}');
$product->setShortDescription('Test: {{block id="' . $cmsBlockId . '"}}');
$this->productRepository->save($product);

$query = <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}}) {
items {
description {
html
}
short_description {
html
}
}
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertContains($assertionCmsBlockText, $response['products']['items'][0]['description']['html']);
self::assertNotContains('{{block id', $response['products']['items'][0]['description']['html']);
self::assertContains($assertionCmsBlockText, $response['products']['items'][0]['short_description']['html']);
self::assertNotContains('{{block id', $response['products']['items'][0]['short_description']['html']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function testQueryAllFieldsSimpleProduct()
attribute_set_id
country_of_manufacture
created_at
description
gift_message_available
id
categories {
Expand Down Expand Up @@ -202,7 +201,6 @@ public function testQueryAllFieldsSimpleProduct()
position
sku
}
short_description
sku
small_image{ url, label }
thumbnail { url, label }
Expand Down Expand Up @@ -296,7 +294,6 @@ public function testQueryMediaGalleryEntryFieldsSimpleProduct()
}
country_of_manufacture
created_at
description
gift_message_available
id
image {url, label}
Expand Down Expand Up @@ -446,7 +443,6 @@ public function testQueryMediaGalleryEntryFieldsSimpleProduct()
position
sku
}
short_description
sku
small_image { url, label }
special_from_date
Expand Down Expand Up @@ -908,11 +904,9 @@ private function assertEavAttributes($product, $actualResponse)
{
$eavAttributes = [
'url_key',
'description',
'meta_description',
'meta_keyword',
'meta_title',
'short_description',
'country_of_manufacture',
'gift_message_available',
'news_from_date',
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public function testProductWithNoCategoriesAssigned()
items {
name,
sku,
description,
url_rewrites {
description {
html
}
url_rewrites {
url,
parameters {
name,
Expand Down Expand Up @@ -87,8 +89,10 @@ public function testProductWithOneCategoryAssigned()
items {
name,
sku,
description,
url_rewrites {
description {
html
}
url_rewrites {
url,
parameters {
name,
Expand Down
Loading

0 comments on commit 5f976fa

Please sign in to comment.