-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-3070: Render category description directives #148
- Loading branch information
Showing
6 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/code/Magento/CatalogGraphQl/Model/Resolver/Category/CategoryHtmlAttribute.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Category; | ||
|
||
use Magento\Catalog\Model\Category; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Catalog\Helper\Output as OutputHelper; | ||
|
||
/** | ||
* Resolve rendered content for category attributes where HTML content is allowed | ||
*/ | ||
class CategoryHtmlAttribute implements ResolverInterface | ||
{ | ||
/** | ||
* @var OutputHelper | ||
*/ | ||
private $outputHelper; | ||
|
||
/** | ||
* @param OutputHelper $outputHelper | ||
*/ | ||
public function __construct( | ||
OutputHelper $outputHelper | ||
) { | ||
$this->outputHelper = $outputHelper; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
if (!isset($value['model'])) { | ||
throw new LocalizedException(__('"model" value should be specified')); | ||
} | ||
|
||
/* @var $category Category */ | ||
$category = $value['model']; | ||
$fieldName = $field->getName(); | ||
$renderedValue = $this->outputHelper->categoryAttribute($category, $category->getData($fieldName), $fieldName); | ||
|
||
return $renderedValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...pi-functional/testsuite/Magento/GraphQl/Catalog/CategoryWithDescriptionDirectivesTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?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\CategoryRepositoryInterface; | ||
use Magento\Catalog\Api\Data\CategoryInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use Magento\TestFramework\ObjectManager; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
/** | ||
* Test for checking that category description directives are rendered correctly | ||
*/ | ||
class CategoryWithDescriptionDirectivesTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @var \Magento\TestFramework\ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Catalog/_files/category.php | ||
*/ | ||
public function testHtmlDirectivesRendered() | ||
{ | ||
$categoryId = 333; | ||
$mediaFilePath = '/path/to/mediafile'; | ||
/** @var StoreManagerInterface $storeManager */ | ||
$storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class); | ||
$storeBaseUrl = $storeManager->getStore()->getBaseUrl(); | ||
|
||
/* Remove index.php from base URL */ | ||
$storeBaseUrlParts = explode('/index.php', $storeBaseUrl); | ||
$storeBaseUrl = $storeBaseUrlParts[0]; | ||
|
||
/** @var CategoryRepositoryInterface $categoryRepository */ | ||
$categoryRepository = ObjectManager::getInstance()->get(CategoryRepositoryInterface::class); | ||
/** @var CategoryInterface $category */ | ||
$category = $categoryRepository->get($categoryId); | ||
$category->setDescription('Test: {{media url="' . $mediaFilePath . '"}}'); | ||
$categoryRepository->save($category); | ||
|
||
$query = <<<QUERY | ||
{ | ||
category(id: {$categoryId}) { | ||
description | ||
} | ||
} | ||
QUERY; | ||
$response = $this->graphQlQuery($query); | ||
|
||
self::assertNotContains('media url', $response['category']['description']); | ||
self::assertContains($storeBaseUrl, $response['category']['description']); | ||
} | ||
} |