From cf119bec073ba75239d72a3f737097d4f6a0aa28 Mon Sep 17 00:00:00 2001 From: Alex Taranovsky Date: Sat, 4 May 2019 01:11:04 +0300 Subject: [PATCH] 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); } }