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 6, 2019
1 parent ff658b5 commit fed22e0
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

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

0 comments on commit fed22e0

Please sign in to comment.