Skip to content

Commit

Permalink
magento/graphql-ce#387: Test coverage of getting IDs of CMS page/bloc…
Browse files Browse the repository at this point in the history
…ks by GraphQL API
  • Loading branch information
atwixfirster committed Apr 16, 2019
1 parent b8d93d8 commit 263588a
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 47 deletions.
54 changes: 8 additions & 46 deletions app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,37 @@
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
*/
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;
}

Expand All @@ -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();
}
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

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;

/**
* Cms page data provider
*/
class PageDataProvider
{
/**
* @var GetPageByIdentifierInterface
*/
private $pageByIdentifier;

/**
* @var PageRepositoryInterface
*/
private $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,
StoreManagerInterface $storeManager
) {
$this->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;
}
}
2 changes: 1 addition & 1 deletion 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\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;
Expand Down

0 comments on commit 263588a

Please sign in to comment.