diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php index 8e1e770b01e9..40825e70a994 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php @@ -8,8 +8,10 @@ namespace Magento\CmsGraphQl\Model\Resolver\DataProvider; use Magento\Cms\Api\Data\PageInterface; +use Magento\Cms\Api\GetPageByIdentifierInterface; use Magento\Cms\Api\PageRepositoryInterface; use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Store\Model\StoreManagerInterface; use Magento\Widget\Model\Template\FilterEmulate; /** @@ -18,38 +20,82 @@ class Page { /** - * @var FilterEmulate + * @var GetPageByIdentifierInterface */ - private $widgetFilter; + private $pageByIdentifier; /** * @var PageRepositoryInterface */ private $pageRepository; + /** + * @var StoreManagerInterface + */ + private $storeManager; + + /** + * @var FilterEmulate + */ + private $widgetFilter; + /** * @param PageRepositoryInterface $pageRepository * @param FilterEmulate $widgetFilter + * @param GetPageByIdentifierInterface $getPageByIdentifier + * @param StoreManagerInterface $storeManager */ public function __construct( PageRepositoryInterface $pageRepository, - FilterEmulate $widgetFilter + FilterEmulate $widgetFilter, + GetPageByIdentifierInterface $getPageByIdentifier, + StoreManagerInterface $storeManager ) { + $this->pageRepository = $pageRepository; $this->widgetFilter = $widgetFilter; + $this->pageByIdentifier = $getPageByIdentifier; + $this->storeManager = $storeManager; } /** - * Get the page data + * Returns page data by page_id * * @param int $pageId * @return array * @throws NoSuchEntityException */ - public function getData(int $pageId): array + public function getDataByPageId(int $pageId): array { $page = $this->pageRepository->getById($pageId); + return $this->convertPageData($page); + } + + /** + * Returns page data by page identifier + * + * @param string $pageIdentifier + * @return array + * @throws NoSuchEntityException + */ + public function getDataByPageIdentifier(string $pageIdentifier): array + { + $storeId = (int)$this->storeManager->getStore()->getId(); + $page = $this->pageByIdentifier->execute($pageIdentifier, $storeId); + + return $this->convertPageData($page); + } + + /** + * Convert page data + * + * @param PageInterface $page + * @return array + * @throws NoSuchEntityException + */ + private function convertPageData(PageInterface $page) + { if (false === $page->isActive()) { throw new NoSuchEntityException(); } @@ -57,7 +103,6 @@ public function getData(int $pageId): array $renderedContent = $this->widgetFilter->filter($page->getContent()); $pageData = [ - PageInterface::PAGE_ID => $page->getId(), 'url_key' => $page->getIdentifier(), PageInterface::TITLE => $page->getTitle(), PageInterface::CONTENT => $renderedContent, @@ -66,6 +111,8 @@ public function getData(int $pageId): array PageInterface::META_TITLE => $page->getMetaTitle(), PageInterface::META_DESCRIPTION => $page->getMetaDescription(), PageInterface::META_KEYWORDS => $page->getMetaKeywords(), + PageInterface::PAGE_ID => $page->getId(), + PageInterface::IDENTIFIER => $page->getIdentifier(), ]; return $pageData; } diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php index 1077ab81551c..64891cfeaa87 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php @@ -26,6 +26,7 @@ class Page implements ResolverInterface private $pageDataProvider; /** + * * @param PageDataProvider $pageDataProvider */ public function __construct( @@ -44,35 +45,18 @@ public function resolve( array $value = null, array $args = null ) { - $pageId = $this->getPageId($args); - $pageData = $this->getPageData($pageId); - - return $pageData; - } - - /** - * @param array $args - * @return int - * @throws GraphQlInputException - */ - private function getPageId(array $args): int - { - if (!isset($args['id'])) { - throw new GraphQlInputException(__('"Page id should be specified')); + if (!isset($args['id']) && !isset($args['identifier'])) { + throw new GraphQlInputException(__('"Page id/identifier should be specified')); } - return (int)$args['id']; - } + $pageData = []; - /** - * @param int $pageId - * @return array - * @throws GraphQlNoSuchEntityException - */ - private function getPageData(int $pageId): array - { try { - $pageData = $this->pageDataProvider->getData($pageId); + if (isset($args['id'])) { + $pageData = $this->pageDataProvider->getDataByPageId((int)$args['id']); + } elseif (isset($args['identifier'])) { + $pageData = $this->pageDataProvider->getDataByPageIdentifier((string)$args['identifier']); + } } catch (NoSuchEntityException $e) { throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); } diff --git a/app/code/Magento/CmsGraphQl/composer.json b/app/code/Magento/CmsGraphQl/composer.json index 18a6f1aa95e3..e0e8481d59b7 100644 --- a/app/code/Magento/CmsGraphQl/composer.json +++ b/app/code/Magento/CmsGraphQl/composer.json @@ -6,6 +6,7 @@ "php": "~7.1.3||~7.2.0", "magento/framework": "*", "magento/module-cms": "*", + "magento/module-store": "*", "magento/module-widget": "*" }, "suggest": { diff --git a/app/code/Magento/CmsGraphQl/etc/schema.graphqls b/app/code/Magento/CmsGraphQl/etc/schema.graphqls index 04d2efa4c7cd..3558d853aa4d 100644 --- a/app/code/Magento/CmsGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CmsGraphQl/etc/schema.graphqls @@ -12,7 +12,8 @@ type StoreConfig @doc(description: "The type contains information about a store type Query { cmsPage ( - id: Int @doc(description: "Id of the CMS page") + id: Int @doc(description: "Id of the CMS page") @deprecated(reason: "The `id` is deprecated. Use `identifier` instead.") @doc(description: "The CMS page query returns information about a CMS page") + identifier: String @doc(description: "Identifier of the CMS page") ): CmsPage @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\Page") @doc(description: "The CMS page query returns information about a CMS page") @cache(cacheTag: "cms_p", cacheIdentity: "Magento\\CmsGraphQl\\Model\\Resolver\\Page\\Identity") cmsBlocks ( identifiers: [String] @doc(description: "Identifiers of the CMS blocks") @@ -20,6 +21,7 @@ type Query { } type CmsPage @doc(description: "CMS page defines all CMS page information") { + identifier: String @doc(description: "Identifier of the CMS page") url_key: String @doc(description: "URL key of CMS page") title: String @doc(description: "CMS page title") content: String @doc(description: "CMS page content") diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php index 57f526b1cb2f..d598a463a48a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php @@ -13,6 +13,9 @@ use Magento\TestFramework\TestCase\GraphQlAbstract; use Magento\Widget\Model\Template\FilterEmulate; +/** + * Get CMS Block test + */ class CmsBlockTest extends GraphQlAbstract { /** @@ -64,6 +67,39 @@ public function testGetCmsBlock() self::assertEquals($renderedContent, $response['cmsBlocks']['items'][0]['content']); } + /** + * Verify the fields of CMS Block selected by block_id + * + * @magentoApiDataFixture Magento/Cms/_files/blocks.php + */ + public function testGetCmsBlockByBlockId() + { + $cmsBlock = $this->blockRepository->getById('enabled_block'); + $cmsBlockData = $cmsBlock->getData(); + $blockId = $cmsBlockData['block_id']; + $renderedContent = $this->filterEmulate->setUseSessionInUrl(false)->filter($cmsBlock->getContent()); + + $query = + <<graphQlQuery($query); + + self::assertArrayHasKey('cmsBlocks', $response); + self::assertArrayHasKey('items', $response['cmsBlocks']); + self::assertEquals($cmsBlockData['identifier'], $response['cmsBlocks']['items'][0]['identifier']); + self::assertEquals($cmsBlockData['title'], $response['cmsBlocks']['items'][0]['title']); + self::assertEquals($renderedContent, $response['cmsBlocks']['items'][0]['content']); + } + /** * Verify the message when CMS Block is disabled * diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsPageTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsPageTest.php index 86145fafa62f..afbb3d40bc92 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsPageTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsPageTest.php @@ -11,6 +11,9 @@ use Magento\TestFramework\ObjectManager; use Magento\TestFramework\TestCase\GraphQlAbstract; +/** + * Get CMS Page test + */ class CmsPageTest extends GraphQlAbstract { /** @@ -50,6 +53,28 @@ public function testGetCmsPageById() $this->assertEquals($cmsPageData['meta_keywords'], $response['cmsPage']['meta_keywords']); } + /** + * Verify the fields of CMS Page selected by page_id + * + * @magentoApiDataFixture Magento/Cms/_files/pages.php + */ + public function testGetCmsPageByIdentifier() + { + $cmsPageIdentifier = 'page100'; + + $query = + <<graphQlQuery($query); + $this->assertEquals($cmsPageIdentifier, $response['cmsPage']['identifier']); + } + /** * Verify the message when page_id is not specified. */ @@ -72,7 +97,7 @@ public function testGetCmsPageWithoutId() QUERY; $this->expectException(\Exception::class); - $this->expectExceptionMessage('Page id should be specified'); + $this->expectExceptionMessage('Page id/identifier should be specified'); $this->graphQlQuery($query); } @@ -102,6 +127,32 @@ public function testGetCmsPageByNonExistentId() $this->graphQlQuery($query); } + /** + * Verify the message when identifier does not exist. + * + * @expectedException \Exception + * @expectedExceptionMessage The CMS page with the "" ID doesn't exist. + */ + public function testGetCmsPageByNonExistentIdentifier() + { + $query = + <<graphQlQuery($query); + } + /** * Verify the message when CMS Page selected by page_id is disabled * @@ -130,4 +181,32 @@ public function testGetDisabledCmsPageById() $this->expectExceptionMessage('No such entity.'); $this->graphQlQuery($query); } + + /** + * Verify the message when CMS Page selected by identifier is disabled + * + * @magentoApiDataFixture Magento/Cms/_files/noroute.php + * @expectedException \Exception + * @expectedExceptionMessage The CMS page with the "no-route" ID doesn't exist. + */ + public function testGetDisabledCmsPageByIdentifier() + { + $cmsPageIdentifier = 'no-route'; + $query = + <<graphQlQuery($query); + } }