Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

387-Test coverage of getting IDs of CMS page/blocks by GraphQL API #585

Merged
merged 15 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
7a90599
magento/graphql-ce#387: Test coverage of getting IDs of CMS page/bloc…
atwixfirster Apr 10, 2019
8a5b3b7
magento/graphql-ce#387: Test coverage of getting IDs of CMS page/bloc…
atwixfirster Apr 10, 2019
b8d93d8
magento/graphql-ce#387: Test coverage of getting IDs of CMS page/bloc…
atwixfirster Apr 11, 2019
263588a
magento/graphql-ce#387: Test coverage of getting IDs of CMS page/bloc…
atwixfirster Apr 16, 2019
81984dc
magento/graphql-ce#387: Test coverage of getting IDs of CMS page/bloc…
atwixfirster Apr 16, 2019
f4a4a34
magento/graphql-ce#387: Test coverage of getting IDs of CMS page/bloc…
atwixfirster Apr 24, 2019
aae7428
387-Test coverage of getting IDs of CMS page/blocks by GraphQL API
atwixfirster Apr 30, 2019
cf119be
387-Test coverage of getting IDs of CMS page/blocks by GraphQL API
atwixfirster May 3, 2019
ff658b5
387-Test coverage of getting IDs of CMS page/blocks by GraphQL API
atwixfirster May 3, 2019
fed22e0
387-Test coverage of getting IDs of CMS page/blocks by GraphQL API
atwixfirster May 6, 2019
b4ea6b9
387-Test coverage of getting IDs of CMS page/blocks by GraphQL API
atwixfirster May 6, 2019
4ee64a5
387-Test coverage of getting IDs of CMS page/blocks by GraphQL API
atwixfirster May 6, 2019
d7b7a2e
GraphQl-387: Test coverage of getting IDs of CMS page/blocks by Graph…
naydav May 8, 2019
478f438
Merge remote-tracking branch 'origin/2.3-develop' into 387-test-cover…
naydav May 8, 2019
787d31b
GraphQl-387: Test coverage of getting IDs of CMS page/blocks by Graph…
naydav May 8, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
51 changes: 46 additions & 5 deletions app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -18,24 +20,40 @@
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(
vpodorozh marked this conversation as resolved.
Show resolved Hide resolved
GetPageByIdentifierInterface $getPageByIdentifier,
vpodorozh marked this conversation as resolved.
Show resolved Hide resolved
FilterEmulate $widgetFilter,
PageRepositoryInterface $pageRepository,
FilterEmulate $widgetFilter
StoreManagerInterface $storeManager
) {
$this->pageByIdentifier = $getPageByIdentifier;
$this->pageRepository = $pageRepository;
$this->storeManager = $storeManager;
$this->widgetFilter = $widgetFilter;
}

Expand All @@ -44,10 +62,32 @@ public function __construct(
* @return array
* @throws NoSuchEntityException
*/
public function getData(int $pageId): array
vpodorozh marked this conversation as resolved.
Show resolved Hide resolved
public function getDataByPageId(int $pageId): array
vpodorozh marked this conversation as resolved.
Show resolved Hide resolved
{
$page = $this->pageRepository->getById($pageId);

return $this->convertPageData($page);
}

/**
* @param string $pageIdentifier
* @return array
*/
public function getDataByPageIdentifier(string $pageIdentifier): array
vpodorozh marked this conversation as resolved.
Show resolved Hide resolved
{
$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)
vpodorozh marked this conversation as resolved.
Show resolved Hide resolved
{
if (false === $page->isActive()) {
throw new NoSuchEntityException();
}
Expand All @@ -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(),
Expand Down
45 changes: 36 additions & 9 deletions app/code/Magento/CmsGraphQl/Model/Resolver/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Page implements ResolverInterface
private $pageDataProvider;

/**
*
* @param PageDataProvider $pageDataProvider
*/
public function __construct(
Expand All @@ -44,35 +45,61 @@ 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;
}

/**
* @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'] : '';
}

/**
* @param int $pageId
* @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) {
vpodorozh marked this conversation as resolved.
Show resolved Hide resolved
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);
vpodorozh marked this conversation as resolved.
Show resolved Hide resolved
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
}
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/CmsGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"php": "~7.1.3||~7.2.0",
"magento/framework": "*",
"magento/module-cms": "*",
"magento/module-store": "*",
"magento/module-widget": "*"
},
"suggest": {
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/CmsGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ 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")
vpodorozh marked this conversation as resolved.
Show resolved Hide resolved
): 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")
): CmsBlocks @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\Blocks") @doc(description: "The CMS block query returns information about CMS blocks")
}

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")
Expand All @@ -35,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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function testGetCmsBlock()
{
cmsBlocks(identifiers: "enabled_block") {
items {
block_id
identifier
title
content
Expand All @@ -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 =
<<<QUERY
{
cmsBlocks(identifiers: "$blockId") {
items {
block_id
identifier
title
content
}
}
}
QUERY;
$response = $this->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']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
<<<QUERY
{
cmsPage(identifier: "$cmsPageIdentifier") {
page_id
}
}
QUERY;

$response = $this->graphQlQuery($query);
$this->assertEquals($pageId, $response['cmsPage']['page_id']);
}

/**
* Verify the message when page_id is not specified.
*/
Expand All @@ -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);
}

Expand Down Expand Up @@ -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 =
<<<QUERY
{
cmsPage(identifier: "") {
url_key
title
content
content_heading
page_layout
meta_title
meta_description
meta_keywords
}
}
QUERY;
$this->graphQlQuery($query);
}

/**
* Verify the message when CMS Page selected by page_id is disabled
*
Expand Down Expand Up @@ -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 =
<<<QUERY
{
cmsPage(identifier: "$cmsPageIdentifier") {
url_key
title
content
content_heading
page_layout
meta_title
meta_description
meta_keywords
}
}
QUERY;
$this->graphQlQuery($query);
}
}