From 7a905997efa240d577cdba6cdea9ff3aacd2ee34 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Wed, 10 Apr 2019 14:48:14 +0300 Subject: [PATCH 01/13] 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); + } } From 8a5b3b7bf87959c6ed9118fcd213cdc34c563ed7 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Wed, 10 Apr 2019 15:11:08 +0300 Subject: [PATCH 02/13] magento/graphql-ce#387: Test coverage of getting IDs of CMS page/blocks by GraphQL API --- .../Model/Resolver/DataProvider/Block.php | 1 + .../Magento/CmsGraphQl/etc/schema.graphqls | 1 + .../Magento/GraphQl/Cms/CmsBlockTest.php | 38 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php index 47a2439c4fad0..fa4944381b858 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php @@ -59,6 +59,7 @@ public function getData(string $blockIdentifier): array $renderedContent = $this->widgetFilter->filter($block->getContent()); $blockData = [ + BlockInterface::BLOCK_ID => $block->getId(), BlockInterface::IDENTIFIER => $block->getIdentifier(), BlockInterface::TITLE => $block->getTitle(), BlockInterface::CONTENT => $renderedContent, diff --git a/app/code/Magento/CmsGraphQl/etc/schema.graphqls b/app/code/Magento/CmsGraphQl/etc/schema.graphqls index 765e58849fc96..5504c42b339f9 100644 --- a/app/code/Magento/CmsGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CmsGraphQl/etc/schema.graphqls @@ -37,6 +37,7 @@ type CmsBlocks @doc(description: "CMS blocks information") { } type CmsBlock @doc(description: "CMS block defines all CMS block information") { + block_id: Int @doc(description: "Entity ID of CMS block") identifier: String @doc(description: "CMS block identifier") title: String @doc(description: "CMS block title") content: String @doc(description: "CMS block 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 57f526b1cb2f7..6e07f8cd7876a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php @@ -47,6 +47,7 @@ public function testGetCmsBlock() { cmsBlocks(identifiers: "enabled_block") { items { + block_id identifier title content @@ -59,6 +60,43 @@ public function testGetCmsBlock() self::assertArrayHasKey('cmsBlocks', $response); self::assertArrayHasKey('items', $response['cmsBlocks']); + self::assertEquals($cmsBlockData['block_id'], $response['cmsBlocks']['items'][0]['block_id']); + 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 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($blockId, $response['cmsBlocks']['items'][0]['block_id']); 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']); From b8d93d8533ba09e5d83cc119f3c7ab1dea5925b4 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Thu, 11 Apr 2019 09:42:39 +0300 Subject: [PATCH 03/13] magento/graphql-ce#387: Test coverage of getting IDs of CMS page/blocks by GraphQL API 1. Set hard dependency --- app/code/Magento/CmsGraphQl/composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/CmsGraphQl/composer.json b/app/code/Magento/CmsGraphQl/composer.json index 6a2e3950f93d0..bea7ee1356d80 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": { From 263588a28f1b529ce225f62cc87b4d4639614e39 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Tue, 16 Apr 2019 09:22:28 +0300 Subject: [PATCH 04/13] magento/graphql-ce#387: Test coverage of getting IDs of CMS page/blocks by GraphQL API --- .../Model/Resolver/DataProvider/Page.php | 54 ++------- .../DataProvider/PageDataProvider.php | 110 ++++++++++++++++++ .../CmsGraphQl/Model/Resolver/Page.php | 2 +- 3 files changed, 119 insertions(+), 47 deletions(-) create mode 100644 app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php index 0745296822c3c..e943ba0c2fd5e 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php @@ -8,21 +8,22 @@ 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; /** + * @deprecated + * @see Magento\CmsGraphQl\Model\Resolver\DataProvider\PageDataProvider + * * Cms page data provider */ class Page { /** - * @var GetPageByIdentifierInterface + * @var FilterEmulate */ - private $pageByIdentifier; + private $widgetFilter; /** * @var PageRepositoryInterface @@ -30,30 +31,14 @@ class Page private $pageRepository; /** - * @var StoreManagerInterface - */ - private $storeManager; - - /** - * @var FilterEmulate - */ - private $widgetFilter; - - /** - * @param GetPageByIdentifierInterface $getPageByIdentifier - * @param FilterEmulate $widgetFilter * @param PageRepositoryInterface $pageRepository - * @param StoreManagerInterface $storeManager + * @param FilterEmulate $widgetFilter */ public function __construct( - GetPageByIdentifierInterface $getPageByIdentifier, - FilterEmulate $widgetFilter, PageRepositoryInterface $pageRepository, - StoreManagerInterface $storeManager + FilterEmulate $widgetFilter ) { - $this->pageByIdentifier = $getPageByIdentifier; $this->pageRepository = $pageRepository; - $this->storeManager = $storeManager; $this->widgetFilter = $widgetFilter; } @@ -62,32 +47,10 @@ public function __construct( * @return array * @throws NoSuchEntityException */ - public function getDataByPageId(int $pageId): array + public function getData(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(); } @@ -96,7 +59,6 @@ private function convertPageData(PageInterface $page) $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/DataProvider/PageDataProvider.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php new file mode 100644 index 0000000000000..7391f736e95e6 --- /dev/null +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php @@ -0,0 +1,110 @@ +pageByIdentifier = $getPageByIdentifier; + $this->pageRepository = $pageRepository; + $this->storeManager = $storeManager; + $this->widgetFilter = $widgetFilter; + } + + /** + * @param int $pageId + * @return array + * @throws NoSuchEntityException + */ + 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(); + } + + $renderedContent = $this->widgetFilter->filter($page->getContent()); + + $pageData = [ + 'url_key' => $page->getIdentifier(), + PageInterface::PAGE_ID => $page->getId(), + PageInterface::TITLE => $page->getTitle(), + PageInterface::CONTENT => $renderedContent, + PageInterface::CONTENT_HEADING => $page->getContentHeading(), + PageInterface::PAGE_LAYOUT => $page->getPageLayout(), + PageInterface::META_TITLE => $page->getMetaTitle(), + PageInterface::META_DESCRIPTION => $page->getMetaDescription(), + PageInterface::META_KEYWORDS => $page->getMetaKeywords(), + ]; + return $pageData; + } +} diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php index 41712889a1bfd..544a09c780070 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php @@ -7,7 +7,7 @@ namespace Magento\CmsGraphQl\Model\Resolver; -use Magento\CmsGraphQl\Model\Resolver\DataProvider\Page as PageDataProvider; +use Magento\CmsGraphQl\Model\Resolver\DataProvider\PageDataProvider as PageDataProvider; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; From 81984dc80fa1f436aecc752222efd588090d5a37 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Tue, 16 Apr 2019 09:32:51 +0300 Subject: [PATCH 05/13] magento/graphql-ce#387: Test coverage of getting IDs of CMS page/blocks by GraphQL API --- .../CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php | 1 + app/code/Magento/CmsGraphQl/etc/schema.graphqls | 1 + 2 files changed, 2 insertions(+) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php index 7391f736e95e6..fdcd0c88cd60c 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php @@ -97,6 +97,7 @@ private function convertPageData(PageInterface $page) $pageData = [ 'url_key' => $page->getIdentifier(), PageInterface::PAGE_ID => $page->getId(), + PageInterface::IDENTIFIER => $page->getIdentifier(), PageInterface::TITLE => $page->getTitle(), PageInterface::CONTENT => $renderedContent, PageInterface::CONTENT_HEADING => $page->getContentHeading(), diff --git a/app/code/Magento/CmsGraphQl/etc/schema.graphqls b/app/code/Magento/CmsGraphQl/etc/schema.graphqls index 5504c42b339f9..8c0cb80f34269 100644 --- a/app/code/Magento/CmsGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CmsGraphQl/etc/schema.graphqls @@ -22,6 +22,7 @@ type Query { type CmsPage @doc(description: "CMS page defines all CMS page information") { page_id: Int @doc(description: "Entity ID of CMS page") + 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") From f4a4a340b4983ad80a5ede504993ed8d753c5f9d Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Wed, 24 Apr 2019 12:31:05 +0300 Subject: [PATCH 06/13] magento/graphql-ce#387: Test coverage of getting IDs of CMS page/blocks by GraphQL API --- .../CmsGraphQl/Model/Resolver/DataProvider/Block.php | 1 - .../Model/Resolver/DataProvider/PageDataProvider.php | 1 - app/code/Magento/CmsGraphQl/etc/schema.graphqls | 4 +--- .../testsuite/Magento/GraphQl/Cms/CmsBlockTest.php | 5 ----- .../testsuite/Magento/GraphQl/Cms/CmsPageTest.php | 8 ++------ 5 files changed, 3 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php index fa4944381b858..47a2439c4fad0 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php @@ -59,7 +59,6 @@ public function getData(string $blockIdentifier): array $renderedContent = $this->widgetFilter->filter($block->getContent()); $blockData = [ - BlockInterface::BLOCK_ID => $block->getId(), BlockInterface::IDENTIFIER => $block->getIdentifier(), BlockInterface::TITLE => $block->getTitle(), BlockInterface::CONTENT => $renderedContent, diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php index fdcd0c88cd60c..47fa4c08a9c31 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php @@ -96,7 +96,6 @@ private function convertPageData(PageInterface $page) $pageData = [ 'url_key' => $page->getIdentifier(), - PageInterface::PAGE_ID => $page->getId(), PageInterface::IDENTIFIER => $page->getIdentifier(), PageInterface::TITLE => $page->getTitle(), PageInterface::CONTENT => $renderedContent, diff --git a/app/code/Magento/CmsGraphQl/etc/schema.graphqls b/app/code/Magento/CmsGraphQl/etc/schema.graphqls index 8c0cb80f34269..85bff91dea8a2 100644 --- a/app/code/Magento/CmsGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CmsGraphQl/etc/schema.graphqls @@ -12,7 +12,7 @@ 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: "Use `identifier`") @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") cmsBlocks ( @@ -21,7 +21,6 @@ type Query { } type CmsPage @doc(description: "CMS page defines all CMS page information") { - page_id: Int @doc(description: "Entity ID of CMS page") 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") @@ -38,7 +37,6 @@ type CmsBlocks @doc(description: "CMS blocks information") { } type CmsBlock @doc(description: "CMS block defines all CMS block information") { - block_id: Int @doc(description: "Entity ID of CMS block") identifier: String @doc(description: "CMS block identifier") title: String @doc(description: "CMS block title") content: String @doc(description: "CMS block 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 6e07f8cd7876a..542b00d434db0 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php @@ -47,7 +47,6 @@ public function testGetCmsBlock() { cmsBlocks(identifiers: "enabled_block") { items { - block_id identifier title content @@ -60,7 +59,6 @@ public function testGetCmsBlock() self::assertArrayHasKey('cmsBlocks', $response); self::assertArrayHasKey('items', $response['cmsBlocks']); - self::assertEquals($cmsBlockData['block_id'], $response['cmsBlocks']['items'][0]['block_id']); 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']); @@ -83,7 +81,6 @@ public function testGetCmsBlockByBlockId() { cmsBlocks(identifiers: "$blockId") { items { - block_id identifier title content @@ -95,8 +92,6 @@ public function testGetCmsBlockByBlockId() self::assertArrayHasKey('cmsBlocks', $response); self::assertArrayHasKey('items', $response['cmsBlocks']); - - self::assertEquals($blockId, $response['cmsBlocks']['items'][0]['block_id']); 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']); 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 8abcee8f22403..53e47185c9866 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsPageTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsPageTest.php @@ -58,22 +58,18 @@ public function testGetCmsPageById() 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']); + $this->assertEquals($cmsPageIdentifier, $response['cmsPage']['identifier']); } /** From cf119bec073ba75239d72a3f737097d4f6a0aa28 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Sat, 4 May 2019 01:11:04 +0300 Subject: [PATCH 07/13] 387-Test coverage of getting IDs of CMS page/blocks by GraphQL API --- .../Model/Resolver/DataProvider/Page.php | 84 +++++++++++-- .../DataProvider/PageDataProvider.php | 110 ------------------ .../CmsGraphQl/Model/Resolver/Page.php | 30 +++-- 3 files changed, 90 insertions(+), 134 deletions(-) delete mode 100644 app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php index 2cee9e8e9e44c..a9513b1a24932 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php @@ -8,22 +8,21 @@ 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; /** - * @deprecated - * @see Magento\CmsGraphQl\Model\Resolver\DataProvider\PageDataProvider - * * Cms page data provider */ class Page { /** - * @var FilterEmulate + * @var GetPageByIdentifierInterface */ - private $widgetFilter; + private $pageByIdentifier; /** * @var PageRepositoryInterface @@ -31,18 +30,37 @@ 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; } /** + * @deprecated + * @see getDataByPageId(int $pageId) + * * Get the page data * * @param int $pageId @@ -72,4 +90,56 @@ public function getData(int $pageId): array ]; return $pageData; } + + /** + * @param int $pageId + * @return array + * @throws NoSuchEntityException + */ + public function getDataByPageId(int $pageId): array + { + $page = $this->pageRepository->getById($pageId); + + return $this->convertPageData($page); + } + + /** + * @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); + } + + /** + * @param PageInterface $page + * @return array + * @throws NoSuchEntityException + */ + private function convertPageData(PageInterface $page) + { + if (false === $page->isActive()) { + throw new NoSuchEntityException(); + } + + $renderedContent = $this->widgetFilter->filter($page->getContent()); + + $pageData = [ + 'url_key' => $page->getIdentifier(), + PageInterface::IDENTIFIER => $page->getIdentifier(), + PageInterface::TITLE => $page->getTitle(), + PageInterface::CONTENT => $renderedContent, + PageInterface::CONTENT_HEADING => $page->getContentHeading(), + PageInterface::PAGE_LAYOUT => $page->getPageLayout(), + PageInterface::META_TITLE => $page->getMetaTitle(), + PageInterface::META_DESCRIPTION => $page->getMetaDescription(), + PageInterface::META_KEYWORDS => $page->getMetaKeywords(), + ]; + return $pageData; + } } diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php deleted file mode 100644 index 47fa4c08a9c31..0000000000000 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/PageDataProvider.php +++ /dev/null @@ -1,110 +0,0 @@ -pageByIdentifier = $getPageByIdentifier; - $this->pageRepository = $pageRepository; - $this->storeManager = $storeManager; - $this->widgetFilter = $widgetFilter; - } - - /** - * @param int $pageId - * @return array - * @throws NoSuchEntityException - */ - 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(); - } - - $renderedContent = $this->widgetFilter->filter($page->getContent()); - - $pageData = [ - 'url_key' => $page->getIdentifier(), - PageInterface::IDENTIFIER => $page->getIdentifier(), - PageInterface::TITLE => $page->getTitle(), - PageInterface::CONTENT => $renderedContent, - PageInterface::CONTENT_HEADING => $page->getContentHeading(), - PageInterface::PAGE_LAYOUT => $page->getPageLayout(), - PageInterface::META_TITLE => $page->getMetaTitle(), - PageInterface::META_DESCRIPTION => $page->getMetaDescription(), - PageInterface::META_KEYWORDS => $page->getMetaKeywords(), - ]; - return $pageData; - } -} diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php index 544a09c780070..489a3e0a75c80 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php @@ -7,7 +7,7 @@ namespace Magento\CmsGraphQl\Model\Resolver; -use Magento\CmsGraphQl\Model\Resolver\DataProvider\PageDataProvider as PageDataProvider; +use Magento\CmsGraphQl\Model\Resolver\DataProvider\Page as PageDataProvider; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -49,10 +49,16 @@ public function resolve( 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)); + $pageData = []; + + try { + if (isset($args['id'])) { + $pageData = $this->getPageDataById($this->getPageId($args)); + } elseif (isset($args['identifier'])) { + $pageData = $this->getPageDataByIdentifier($this->getPageIdentifier($args)); + } + } catch (NoSuchEntityException $e) { + throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); } return $pageData; @@ -83,12 +89,7 @@ private function getPageIdentifier(array $args): string */ private function getPageDataById(int $pageId): array { - try { - $pageData = $this->pageDataProvider->getDataByPageId($pageId); - } catch (NoSuchEntityException $e) { - throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); - } - return $pageData; + return $this->pageDataProvider->getDataByPageId($pageId); } /** @@ -98,11 +99,6 @@ private function getPageDataById(int $pageId): array */ private function getPageDataByIdentifier(string $pageIdentifier): array { - try { - $pageData = $this->pageDataProvider->getDataByPageIdentifier($pageIdentifier); - } catch (NoSuchEntityException $e) { - throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); - } - return $pageData; + return $this->pageDataProvider->getDataByPageIdentifier($pageIdentifier); } } From ff658b515ba18f6579e692935e195ad5d3cba99b Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Sat, 4 May 2019 01:18:19 +0300 Subject: [PATCH 08/13] 387-Test coverage of getting IDs of CMS page/blocks by GraphQL API --- app/code/Magento/CmsGraphQl/etc/schema.graphqls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/CmsGraphQl/etc/schema.graphqls b/app/code/Magento/CmsGraphQl/etc/schema.graphqls index 26689c5d4c91d..3558d853aa4df 100644 --- a/app/code/Magento/CmsGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CmsGraphQl/etc/schema.graphqls @@ -12,7 +12,7 @@ type StoreConfig @doc(description: "The type contains information about a store type Query { cmsPage ( - id: Int @doc(description: "Id of the CMS page") @deprecated(reason: "Use `identifier`") @doc(description: "The CMS page query returns information about a 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 ( From fed22e0af98c5571e65a30e05ef5c8c44dcb6b4a Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Mon, 6 May 2019 15:51:47 +0300 Subject: [PATCH 09/13] 387-Test coverage of getting IDs of CMS page/blocks by GraphQL API --- .../Model/Resolver/DataProvider/Page.php | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php index a9513b1a24932..a67cc877e7db8 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php @@ -10,6 +10,7 @@ use Magento\Cms\Api\Data\PageInterface; use Magento\Cms\Api\GetPageByIdentifierInterface; use Magento\Cms\Api\PageRepositoryInterface; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Store\Model\StoreManagerInterface; use Magento\Widget\Model\Template\FilterEmulate; @@ -40,21 +41,22 @@ class Page private $widgetFilter; /** - * @param GetPageByIdentifierInterface $getPageByIdentifier - * @param FilterEmulate $widgetFilter * @param PageRepositoryInterface $pageRepository + * @param FilterEmulate $widgetFilter + * @param GetPageByIdentifierInterface $getPageByIdentifier * @param StoreManagerInterface $storeManager */ public function __construct( - GetPageByIdentifierInterface $getPageByIdentifier, - FilterEmulate $widgetFilter, PageRepositoryInterface $pageRepository, - StoreManagerInterface $storeManager + FilterEmulate $widgetFilter, + GetPageByIdentifierInterface $getPageByIdentifier = null, + StoreManagerInterface $storeManager = null ) { - $this->pageByIdentifier = $getPageByIdentifier; + $this->pageRepository = $pageRepository; - $this->storeManager = $storeManager; $this->widgetFilter = $widgetFilter; + $this->pageByIdentifier = $getPageByIdentifier ?: ObjectManager::getInstance()->get(GetPageByIdentifierInterface::class); + $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class); } /** @@ -75,23 +77,12 @@ public function getData(int $pageId): array throw new NoSuchEntityException(); } - $renderedContent = $this->widgetFilter->filter($page->getContent()); - - $pageData = [ - PageInterface::PAGE_ID => $page->getId(), - 'url_key' => $page->getIdentifier(), - PageInterface::TITLE => $page->getTitle(), - PageInterface::CONTENT => $renderedContent, - PageInterface::CONTENT_HEADING => $page->getContentHeading(), - PageInterface::PAGE_LAYOUT => $page->getPageLayout(), - PageInterface::META_TITLE => $page->getMetaTitle(), - PageInterface::META_DESCRIPTION => $page->getMetaDescription(), - PageInterface::META_KEYWORDS => $page->getMetaKeywords(), - ]; - return $pageData; + return $this->convertPageData($page); } /** + * Returns page data by page_id + * * @param int $pageId * @return array * @throws NoSuchEntityException @@ -100,10 +91,12 @@ public function getDataByPageId(int $pageId): array { $page = $this->pageRepository->getById($pageId); - return $this->convertPageData($page); + return $this->convertPageData($page, false, true); } /** + * Returns page data by page identifier + * * @param string $pageIdentifier * @return array * @throws NoSuchEntityException @@ -113,15 +106,17 @@ public function getDataByPageIdentifier(string $pageIdentifier): array $storeId = (int)$this->storeManager->getStore()->getId(); $page = $this->pageByIdentifier->execute($pageIdentifier, $storeId); - return $this->convertPageData($page); + return $this->convertPageData($page, false, true); } /** * @param PageInterface $page + * @param bool $includePageId + * @param bool $includePageIdentifier * @return array * @throws NoSuchEntityException */ - private function convertPageData(PageInterface $page) + private function convertPageData(PageInterface $page, $includePageId = true, $includePageIdentifier = false) { if (false === $page->isActive()) { throw new NoSuchEntityException(); @@ -131,7 +126,6 @@ private function convertPageData(PageInterface $page) $pageData = [ 'url_key' => $page->getIdentifier(), - PageInterface::IDENTIFIER => $page->getIdentifier(), PageInterface::TITLE => $page->getTitle(), PageInterface::CONTENT => $renderedContent, PageInterface::CONTENT_HEADING => $page->getContentHeading(), @@ -140,6 +134,15 @@ private function convertPageData(PageInterface $page) PageInterface::META_DESCRIPTION => $page->getMetaDescription(), PageInterface::META_KEYWORDS => $page->getMetaKeywords(), ]; + + if ($includePageId) { + $pageData[PageInterface::PAGE_ID] = $page->getId(); + } + + if ($includePageIdentifier) { + $pageData[PageInterface::IDENTIFIER] = $page->getIdentifier(); + } + return $pageData; } } From b4ea6b93496342a51430694f3f3ca7d82d50f746 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Mon, 6 May 2019 16:49:15 +0300 Subject: [PATCH 10/13] 387-Test coverage of getting IDs of CMS page/blocks by GraphQL API --- .../Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php index a67cc877e7db8..cfeecd402828e 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php @@ -72,11 +72,7 @@ public function __construct( public function getData(int $pageId): array { $page = $this->pageRepository->getById($pageId); - - if (false === $page->isActive()) { - throw new NoSuchEntityException(); - } - + return $this->convertPageData($page); } From 4ee64a57bbaaf8c4564c589b1c2f794869da8d74 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Mon, 6 May 2019 16:56:42 +0300 Subject: [PATCH 11/13] 387-Test coverage of getting IDs of CMS page/blocks by GraphQL API --- .../CmsGraphQl/Model/Resolver/DataProvider/Page.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php index cfeecd402828e..2001bc006bbb8 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php @@ -10,7 +10,6 @@ use Magento\Cms\Api\Data\PageInterface; use Magento\Cms\Api\GetPageByIdentifierInterface; use Magento\Cms\Api\PageRepositoryInterface; -use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Store\Model\StoreManagerInterface; use Magento\Widget\Model\Template\FilterEmulate; @@ -49,14 +48,14 @@ class Page public function __construct( PageRepositoryInterface $pageRepository, FilterEmulate $widgetFilter, - GetPageByIdentifierInterface $getPageByIdentifier = null, - StoreManagerInterface $storeManager = null + GetPageByIdentifierInterface $getPageByIdentifier, + StoreManagerInterface $storeManager ) { $this->pageRepository = $pageRepository; $this->widgetFilter = $widgetFilter; - $this->pageByIdentifier = $getPageByIdentifier ?: ObjectManager::getInstance()->get(GetPageByIdentifierInterface::class); - $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class); + $this->pageByIdentifier = $getPageByIdentifier; + $this->storeManager = $storeManager; } /** @@ -72,7 +71,7 @@ public function __construct( public function getData(int $pageId): array { $page = $this->pageRepository->getById($pageId); - + return $this->convertPageData($page); } From d7b7a2e19fe376b30ac45df91c75b80c416e38bb Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Wed, 8 May 2019 11:49:58 -0500 Subject: [PATCH 12/13] GraphQl-387: Test coverage of getting IDs of CMS page/blocks by GraphQL API --- .../Model/Resolver/DataProvider/Page.php | 36 +++------------ .../CmsGraphQl/Model/Resolver/Page.php | 45 ++----------------- 2 files changed, 8 insertions(+), 73 deletions(-) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php index 2001bc006bbb8..8b9766ca311c7 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php @@ -58,23 +58,6 @@ public function __construct( $this->storeManager = $storeManager; } - /** - * @deprecated - * @see getDataByPageId(int $pageId) - * - * Get the page data - * - * @param int $pageId - * @return array - * @throws NoSuchEntityException - */ - public function getData(int $pageId): array - { - $page = $this->pageRepository->getById($pageId); - - return $this->convertPageData($page); - } - /** * Returns page data by page_id * @@ -86,7 +69,7 @@ public function getDataByPageId(int $pageId): array { $page = $this->pageRepository->getById($pageId); - return $this->convertPageData($page, false, true); + return $this->convertPageData($page); } /** @@ -101,17 +84,15 @@ public function getDataByPageIdentifier(string $pageIdentifier): array $storeId = (int)$this->storeManager->getStore()->getId(); $page = $this->pageByIdentifier->execute($pageIdentifier, $storeId); - return $this->convertPageData($page, false, true); + return $this->convertPageData($page); } /** * @param PageInterface $page - * @param bool $includePageId - * @param bool $includePageIdentifier * @return array * @throws NoSuchEntityException */ - private function convertPageData(PageInterface $page, $includePageId = true, $includePageIdentifier = false) + private function convertPageData(PageInterface $page) { if (false === $page->isActive()) { throw new NoSuchEntityException(); @@ -128,16 +109,9 @@ private function convertPageData(PageInterface $page, $includePageId = true, $in PageInterface::META_TITLE => $page->getMetaTitle(), PageInterface::META_DESCRIPTION => $page->getMetaDescription(), PageInterface::META_KEYWORDS => $page->getMetaKeywords(), + PageInterface::PAGE_ID => $page->getId(), + PageInterface::IDENTIFIER => $page->getIdentifier(), ]; - - if ($includePageId) { - $pageData[PageInterface::PAGE_ID] = $page->getId(); - } - - if ($includePageIdentifier) { - $pageData[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 489a3e0a75c80..fbd8e26030952 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php @@ -45,7 +45,7 @@ public function resolve( array $value = null, array $args = null ) { - if (!isset($args['id']) && !isset($args['identifier'])) { + if (!isset($args['id'], $args['identifier'])) { throw new GraphQlInputException(__('"Page id/identifier should be specified')); } @@ -53,52 +53,13 @@ public function resolve( try { if (isset($args['id'])) { - $pageData = $this->getPageDataById($this->getPageId($args)); + $pageData = $this->pageDataProvider->getDataByPageId((int)$args['id']); } elseif (isset($args['identifier'])) { - $pageData = $this->getPageDataByIdentifier($this->getPageIdentifier($args)); + $pageData = $this->pageDataProvider->getDataByPageIdentifier((string)$args['identifier']); } } catch (NoSuchEntityException $e) { throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); } - return $pageData; } - - /** - * @param array $args - * @return int - */ - private function getPageId(array $args): int - { - return isset($args['id']) ? (int)$args['id'] : 0; - } - - /** - * @param array $args - * @return string - */ - private function getPageIdentifier(array $args): string - { - return isset($args['identifier']) ? (string)$args['identifier'] : ''; - } - - /** - * @param int $pageId - * @return array - * @throws GraphQlNoSuchEntityException - */ - private function getPageDataById(int $pageId): array - { - return $this->pageDataProvider->getDataByPageId($pageId); - } - - /** - * @param string $pageIdentifier - * @return array - * @throws GraphQlNoSuchEntityException - */ - private function getPageDataByIdentifier(string $pageIdentifier): array - { - return $this->pageDataProvider->getDataByPageIdentifier($pageIdentifier); - } } From 787d31b2f9d7eba3de123c5bcb799f8ff016b9d0 Mon Sep 17 00:00:00 2001 From: Valerii Naida Date: Wed, 8 May 2019 13:16:42 -0500 Subject: [PATCH 13/13] GraphQl-387: Test coverage of getting IDs of CMS page/blocks by GraphQL API --- .../Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php | 2 ++ app/code/Magento/CmsGraphQl/Model/Resolver/Page.php | 2 +- .../testsuite/Magento/GraphQl/Cms/CmsBlockTest.php | 3 +++ .../testsuite/Magento/GraphQl/Cms/CmsPageTest.php | 3 +++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php index 8b9766ca311c7..40825e70a994e 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php @@ -88,6 +88,8 @@ public function getDataByPageIdentifier(string $pageIdentifier): array } /** + * Convert page data + * * @param PageInterface $page * @return array * @throws NoSuchEntityException diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php index fbd8e26030952..64891cfeaa87a 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php @@ -45,7 +45,7 @@ public function resolve( array $value = null, array $args = null ) { - if (!isset($args['id'], $args['identifier'])) { + if (!isset($args['id']) && !isset($args['identifier'])) { throw new GraphQlInputException(__('"Page id/identifier should be specified')); } 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 542b00d434db0..d598a463a48a7 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 { /** 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 53e47185c9866..afbb3d40bc921 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 { /**