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

Commit feccc42

Browse files
authored
ENGCOM-5088: 387-Test coverage of getting IDs of CMS page/blocks by GraphQL API #585
2 parents 42f5c82 + 8b1a506 commit feccc42

File tree

6 files changed

+182
-33
lines changed

6 files changed

+182
-33
lines changed

app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
namespace Magento\CmsGraphQl\Model\Resolver\DataProvider;
99

1010
use Magento\Cms\Api\Data\PageInterface;
11+
use Magento\Cms\Api\GetPageByIdentifierInterface;
1112
use Magento\Cms\Api\PageRepositoryInterface;
1213
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Store\Model\StoreManagerInterface;
1315
use Magento\Widget\Model\Template\FilterEmulate;
1416

1517
/**
@@ -18,46 +20,89 @@
1820
class Page
1921
{
2022
/**
21-
* @var FilterEmulate
23+
* @var GetPageByIdentifierInterface
2224
*/
23-
private $widgetFilter;
25+
private $pageByIdentifier;
2426

2527
/**
2628
* @var PageRepositoryInterface
2729
*/
2830
private $pageRepository;
2931

32+
/**
33+
* @var StoreManagerInterface
34+
*/
35+
private $storeManager;
36+
37+
/**
38+
* @var FilterEmulate
39+
*/
40+
private $widgetFilter;
41+
3042
/**
3143
* @param PageRepositoryInterface $pageRepository
3244
* @param FilterEmulate $widgetFilter
45+
* @param GetPageByIdentifierInterface $getPageByIdentifier
46+
* @param StoreManagerInterface $storeManager
3347
*/
3448
public function __construct(
3549
PageRepositoryInterface $pageRepository,
36-
FilterEmulate $widgetFilter
50+
FilterEmulate $widgetFilter,
51+
GetPageByIdentifierInterface $getPageByIdentifier,
52+
StoreManagerInterface $storeManager
3753
) {
54+
3855
$this->pageRepository = $pageRepository;
3956
$this->widgetFilter = $widgetFilter;
57+
$this->pageByIdentifier = $getPageByIdentifier;
58+
$this->storeManager = $storeManager;
4059
}
4160

4261
/**
43-
* Get the page data
62+
* Returns page data by page_id
4463
*
4564
* @param int $pageId
4665
* @return array
4766
* @throws NoSuchEntityException
4867
*/
49-
public function getData(int $pageId): array
68+
public function getDataByPageId(int $pageId): array
5069
{
5170
$page = $this->pageRepository->getById($pageId);
5271

72+
return $this->convertPageData($page);
73+
}
74+
75+
/**
76+
* Returns page data by page identifier
77+
*
78+
* @param string $pageIdentifier
79+
* @return array
80+
* @throws NoSuchEntityException
81+
*/
82+
public function getDataByPageIdentifier(string $pageIdentifier): array
83+
{
84+
$storeId = (int)$this->storeManager->getStore()->getId();
85+
$page = $this->pageByIdentifier->execute($pageIdentifier, $storeId);
86+
87+
return $this->convertPageData($page);
88+
}
89+
90+
/**
91+
* Convert page data
92+
*
93+
* @param PageInterface $page
94+
* @return array
95+
* @throws NoSuchEntityException
96+
*/
97+
private function convertPageData(PageInterface $page)
98+
{
5399
if (false === $page->isActive()) {
54100
throw new NoSuchEntityException();
55101
}
56102

57103
$renderedContent = $this->widgetFilter->filter($page->getContent());
58104

59105
$pageData = [
60-
PageInterface::PAGE_ID => $page->getId(),
61106
'url_key' => $page->getIdentifier(),
62107
PageInterface::TITLE => $page->getTitle(),
63108
PageInterface::CONTENT => $renderedContent,
@@ -66,6 +111,8 @@ public function getData(int $pageId): array
66111
PageInterface::META_TITLE => $page->getMetaTitle(),
67112
PageInterface::META_DESCRIPTION => $page->getMetaDescription(),
68113
PageInterface::META_KEYWORDS => $page->getMetaKeywords(),
114+
PageInterface::PAGE_ID => $page->getId(),
115+
PageInterface::IDENTIFIER => $page->getIdentifier(),
69116
];
70117
return $pageData;
71118
}

app/code/Magento/CmsGraphQl/Model/Resolver/Page.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Page implements ResolverInterface
2626
private $pageDataProvider;
2727

2828
/**
29+
*
2930
* @param PageDataProvider $pageDataProvider
3031
*/
3132
public function __construct(
@@ -44,35 +45,18 @@ public function resolve(
4445
array $value = null,
4546
array $args = null
4647
) {
47-
$pageId = $this->getPageId($args);
48-
$pageData = $this->getPageData($pageId);
49-
50-
return $pageData;
51-
}
52-
53-
/**
54-
* @param array $args
55-
* @return int
56-
* @throws GraphQlInputException
57-
*/
58-
private function getPageId(array $args): int
59-
{
60-
if (!isset($args['id'])) {
61-
throw new GraphQlInputException(__('"Page id should be specified'));
48+
if (!isset($args['id']) && !isset($args['identifier'])) {
49+
throw new GraphQlInputException(__('"Page id/identifier should be specified'));
6250
}
6351

64-
return (int)$args['id'];
65-
}
52+
$pageData = [];
6653

67-
/**
68-
* @param int $pageId
69-
* @return array
70-
* @throws GraphQlNoSuchEntityException
71-
*/
72-
private function getPageData(int $pageId): array
73-
{
7454
try {
75-
$pageData = $this->pageDataProvider->getData($pageId);
55+
if (isset($args['id'])) {
56+
$pageData = $this->pageDataProvider->getDataByPageId((int)$args['id']);
57+
} elseif (isset($args['identifier'])) {
58+
$pageData = $this->pageDataProvider->getDataByPageIdentifier((string)$args['identifier']);
59+
}
7660
} catch (NoSuchEntityException $e) {
7761
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
7862
}

app/code/Magento/CmsGraphQl/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"php": "~7.1.3||~7.2.0",
77
"magento/framework": "*",
88
"magento/module-cms": "*",
9+
"magento/module-store": "*",
910
"magento/module-widget": "*"
1011
},
1112
"suggest": {

app/code/Magento/CmsGraphQl/etc/schema.graphqls

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ type StoreConfig @doc(description: "The type contains information about a store
1212

1313
type Query {
1414
cmsPage (
15-
id: Int @doc(description: "Id of the CMS page")
15+
id: Int @doc(description: "Id of the CMS page") @deprecated(reason: "The `id` is deprecated. Use `identifier` instead.") @doc(description: "The CMS page query returns information about a CMS page")
16+
identifier: String @doc(description: "Identifier of the CMS page")
1617
): CmsPage @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\Page") @doc(description: "The CMS page query returns information about a CMS page") @cache(cacheTag: "cms_p", cacheIdentity: "Magento\\CmsGraphQl\\Model\\Resolver\\Page\\Identity")
1718
cmsBlocks (
1819
identifiers: [String] @doc(description: "Identifiers of the CMS blocks")
1920
): CmsBlocks @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\Blocks") @doc(description: "The CMS block query returns information about CMS blocks") @cache(cacheTag: "cms_b", cacheIdentity: "Magento\\CmsGraphQl\\Model\\Resolver\\Block\\Identity")
2021
}
2122

2223
type CmsPage @doc(description: "CMS page defines all CMS page information") {
24+
identifier: String @doc(description: "Identifier of the CMS page")
2325
url_key: String @doc(description: "URL key of CMS page")
2426
title: String @doc(description: "CMS page title")
2527
content: String @doc(description: "CMS page content")

dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsBlockTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Magento\TestFramework\TestCase\GraphQlAbstract;
1414
use Magento\Widget\Model\Template\FilterEmulate;
1515

16+
/**
17+
* Get CMS Block test
18+
*/
1619
class CmsBlockTest extends GraphQlAbstract
1720
{
1821
/**
@@ -64,6 +67,39 @@ public function testGetCmsBlock()
6467
self::assertEquals($renderedContent, $response['cmsBlocks']['items'][0]['content']);
6568
}
6669

70+
/**
71+
* Verify the fields of CMS Block selected by block_id
72+
*
73+
* @magentoApiDataFixture Magento/Cms/_files/blocks.php
74+
*/
75+
public function testGetCmsBlockByBlockId()
76+
{
77+
$cmsBlock = $this->blockRepository->getById('enabled_block');
78+
$cmsBlockData = $cmsBlock->getData();
79+
$blockId = $cmsBlockData['block_id'];
80+
$renderedContent = $this->filterEmulate->setUseSessionInUrl(false)->filter($cmsBlock->getContent());
81+
82+
$query =
83+
<<<QUERY
84+
{
85+
cmsBlocks(identifiers: "$blockId") {
86+
items {
87+
identifier
88+
title
89+
content
90+
}
91+
}
92+
}
93+
QUERY;
94+
$response = $this->graphQlQuery($query);
95+
96+
self::assertArrayHasKey('cmsBlocks', $response);
97+
self::assertArrayHasKey('items', $response['cmsBlocks']);
98+
self::assertEquals($cmsBlockData['identifier'], $response['cmsBlocks']['items'][0]['identifier']);
99+
self::assertEquals($cmsBlockData['title'], $response['cmsBlocks']['items'][0]['title']);
100+
self::assertEquals($renderedContent, $response['cmsBlocks']['items'][0]['content']);
101+
}
102+
67103
/**
68104
* Verify the message when CMS Block is disabled
69105
*

dev/tests/api-functional/testsuite/Magento/GraphQl/Cms/CmsPageTest.php

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Magento\TestFramework\ObjectManager;
1212
use Magento\TestFramework\TestCase\GraphQlAbstract;
1313

14+
/**
15+
* Get CMS Page test
16+
*/
1417
class CmsPageTest extends GraphQlAbstract
1518
{
1619
/**
@@ -50,6 +53,28 @@ public function testGetCmsPageById()
5053
$this->assertEquals($cmsPageData['meta_keywords'], $response['cmsPage']['meta_keywords']);
5154
}
5255

56+
/**
57+
* Verify the fields of CMS Page selected by page_id
58+
*
59+
* @magentoApiDataFixture Magento/Cms/_files/pages.php
60+
*/
61+
public function testGetCmsPageByIdentifier()
62+
{
63+
$cmsPageIdentifier = 'page100';
64+
65+
$query =
66+
<<<QUERY
67+
{
68+
cmsPage(identifier: "$cmsPageIdentifier") {
69+
identifier
70+
}
71+
}
72+
QUERY;
73+
74+
$response = $this->graphQlQuery($query);
75+
$this->assertEquals($cmsPageIdentifier, $response['cmsPage']['identifier']);
76+
}
77+
5378
/**
5479
* Verify the message when page_id is not specified.
5580
*/
@@ -72,7 +97,7 @@ public function testGetCmsPageWithoutId()
7297
QUERY;
7398

7499
$this->expectException(\Exception::class);
75-
$this->expectExceptionMessage('Page id should be specified');
100+
$this->expectExceptionMessage('Page id/identifier should be specified');
76101
$this->graphQlQuery($query);
77102
}
78103

@@ -102,6 +127,32 @@ public function testGetCmsPageByNonExistentId()
102127
$this->graphQlQuery($query);
103128
}
104129

130+
/**
131+
* Verify the message when identifier does not exist.
132+
*
133+
* @expectedException \Exception
134+
* @expectedExceptionMessage The CMS page with the "" ID doesn't exist.
135+
*/
136+
public function testGetCmsPageByNonExistentIdentifier()
137+
{
138+
$query =
139+
<<<QUERY
140+
{
141+
cmsPage(identifier: "") {
142+
url_key
143+
title
144+
content
145+
content_heading
146+
page_layout
147+
meta_title
148+
meta_description
149+
meta_keywords
150+
}
151+
}
152+
QUERY;
153+
$this->graphQlQuery($query);
154+
}
155+
105156
/**
106157
* Verify the message when CMS Page selected by page_id is disabled
107158
*
@@ -130,4 +181,32 @@ public function testGetDisabledCmsPageById()
130181
$this->expectExceptionMessage('No such entity.');
131182
$this->graphQlQuery($query);
132183
}
184+
185+
/**
186+
* Verify the message when CMS Page selected by identifier is disabled
187+
*
188+
* @magentoApiDataFixture Magento/Cms/_files/noroute.php
189+
* @expectedException \Exception
190+
* @expectedExceptionMessage The CMS page with the "no-route" ID doesn't exist.
191+
*/
192+
public function testGetDisabledCmsPageByIdentifier()
193+
{
194+
$cmsPageIdentifier = 'no-route';
195+
$query =
196+
<<<QUERY
197+
{
198+
cmsPage(identifier: "$cmsPageIdentifier") {
199+
url_key
200+
title
201+
content
202+
content_heading
203+
page_layout
204+
meta_title
205+
meta_description
206+
meta_keywords
207+
}
208+
}
209+
QUERY;
210+
$this->graphQlQuery($query);
211+
}
133212
}

0 commit comments

Comments
 (0)