Skip to content

Commit

Permalink
387-Test coverage of getting IDs of CMS page/blocks by GraphQL API
Browse files Browse the repository at this point in the history
  • Loading branch information
atwixfirster committed May 3, 2019
1 parent aae7428 commit cf119be
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 134 deletions.
84 changes: 77 additions & 7 deletions app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,59 @@
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
*/
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
Expand Down Expand Up @@ -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;
}
}

This file was deleted.

30 changes: 13 additions & 17 deletions app/code/Magento/CmsGraphQl/Model/Resolver/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}
}

0 comments on commit cf119be

Please sign in to comment.