From 7a905997efa240d577cdba6cdea9ff3aacd2ee34 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Wed, 10 Apr 2019 14:48:14 +0300 Subject: [PATCH] magento/graphql-ce#387: Test coverage of getting IDs of CMS page/blocks by GraphQL API --- .../Model/Resolver/DataProvider/Page.php | 51 ++++++++++-- .../CmsGraphQl/Model/Resolver/Page.php | 45 ++++++++-- .../Magento/CmsGraphQl/etc/schema.graphqls | 2 + .../Magento/GraphQl/Cms/CmsPageTest.php | 82 ++++++++++++++++++- 4 files changed, 165 insertions(+), 15 deletions(-) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php index 22009824452be..0745296822c3c 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,9 +20,9 @@ class Page { /** - * @var FilterEmulate + * @var GetPageByIdentifierInterface */ - private $widgetFilter; + private $pageByIdentifier; /** * @var PageRepositoryInterface @@ -28,14 +30,30 @@ class Page private $pageRepository; /** - * @param PageRepositoryInterface $pageRepository + * @var StoreManagerInterface + */ + private $storeManager; + + /** + * @var FilterEmulate + */ + private $widgetFilter; + + /** + * @param GetPageByIdentifierInterface $getPageByIdentifier * @param FilterEmulate $widgetFilter + * @param PageRepositoryInterface $pageRepository + * @param StoreManagerInterface $storeManager */ public function __construct( + GetPageByIdentifierInterface $getPageByIdentifier, + FilterEmulate $widgetFilter, PageRepositoryInterface $pageRepository, - FilterEmulate $widgetFilter + StoreManagerInterface $storeManager ) { + $this->pageByIdentifier = $getPageByIdentifier; $this->pageRepository = $pageRepository; + $this->storeManager = $storeManager; $this->widgetFilter = $widgetFilter; } @@ -44,10 +62,32 @@ public function __construct( * @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); + } + + /** + * @param string $pageIdentifier + * @return array + */ + public function getDataByPageIdentifier(string $pageIdentifier): array + { + $storeId = (int)$this->storeManager->getStore()->getId(); + $page = $this->pageByIdentifier->execute($pageIdentifier, $storeId); + + return $this->convertPageData($page); + } + + /** + * @param PageInterface $page + * @return array + * @throws NoSuchEntityException + */ + private function convertPageData(PageInterface $page) + { if (false === $page->isActive()) { throw new NoSuchEntityException(); } @@ -56,6 +96,7 @@ public function getData(int $pageId): array $pageData = [ 'url_key' => $page->getIdentifier(), + PageInterface::PAGE_ID => $page->getId(), PageInterface::TITLE => $page->getTitle(), PageInterface::CONTENT => $renderedContent, PageInterface::CONTENT_HEADING => $page->getContentHeading(), diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php index 1077ab81551c2..41712889a1bfd 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,8 +45,15 @@ public function resolve( array $value = null, array $args = null ) { - $pageId = $this->getPageId($args); - $pageData = $this->getPageData($pageId); + if (!isset($args['id']) && !isset($args['identifier'])) { + throw new GraphQlInputException(__('"Page id/identifier should be specified')); + } + + if (isset($args['id'])) { + $pageData = $this->getPageDataById($this->getPageId($args)); + } elseif (isset($args['identifier'])) { + $pageData = $this->getPageDataByIdentifier($this->getPageIdentifier($args)); + } return $pageData; } @@ -53,15 +61,19 @@ public function resolve( /** * @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')); - } + return isset($args['id']) ? (int)$args['id'] : 0; + } - return (int)$args['id']; + /** + * @param array $args + * @return string + */ + private function getPageIdentifier(array $args): string + { + return isset($args['identifier']) ? (string)$args['identifier'] : ''; } /** @@ -69,10 +81,25 @@ private function getPageId(array $args): int * @return array * @throws GraphQlNoSuchEntityException */ - private function getPageData(int $pageId): array + private function getPageDataById(int $pageId): array + { + try { + $pageData = $this->pageDataProvider->getDataByPageId($pageId); + } catch (NoSuchEntityException $e) { + throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); + } + return $pageData; + } + + /** + * @param string $pageIdentifier + * @return array + * @throws GraphQlNoSuchEntityException + */ + private function getPageDataByIdentifier(string $pageIdentifier): array { try { - $pageData = $this->pageDataProvider->getData($pageId); + $pageData = $this->pageDataProvider->getDataByPageIdentifier($pageIdentifier); } catch (NoSuchEntityException $e) { throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); } diff --git a/app/code/Magento/CmsGraphQl/etc/schema.graphqls b/app/code/Magento/CmsGraphQl/etc/schema.graphqls index e8abd2201b886..765e58849fc96 100644 --- a/app/code/Magento/CmsGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CmsGraphQl/etc/schema.graphqls @@ -13,6 +13,7 @@ type StoreConfig @doc(description: "The type contains information about a store type Query { cmsPage ( id: Int @doc(description: "Id of the 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") 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") { + page_id: Int @doc(description: "Entity ID of 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/CmsPageTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsPageTest.php index 86145fafa62f1..8abcee8f22403 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsPageTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsPageTest.php @@ -50,6 +50,32 @@ 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'; + $storeId = 0; + + $cmsPage = ObjectManager::getInstance()->get(GetPageByIdentifier::class)->execute($cmsPageIdentifier, $storeId); + $pageId = $cmsPage->getPageId(); + + $query = + <<graphQlQuery($query); + $this->assertEquals($pageId, $response['cmsPage']['page_id']); + } + /** * Verify the message when page_id is not specified. */ @@ -72,7 +98,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 +128,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 +182,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); + } }