Skip to content

Commit 7a90599

Browse files
committed
magento/graphql-ce#387: Test coverage of getting IDs of CMS page/blocks by GraphQL API
1 parent ab188bc commit 7a90599

File tree

4 files changed

+165
-15
lines changed

4 files changed

+165
-15
lines changed

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

Lines changed: 46 additions & 5 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,24 +20,40 @@
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

3032
/**
31-
* @param PageRepositoryInterface $pageRepository
33+
* @var StoreManagerInterface
34+
*/
35+
private $storeManager;
36+
37+
/**
38+
* @var FilterEmulate
39+
*/
40+
private $widgetFilter;
41+
42+
/**
43+
* @param GetPageByIdentifierInterface $getPageByIdentifier
3244
* @param FilterEmulate $widgetFilter
45+
* @param PageRepositoryInterface $pageRepository
46+
* @param StoreManagerInterface $storeManager
3347
*/
3448
public function __construct(
49+
GetPageByIdentifierInterface $getPageByIdentifier,
50+
FilterEmulate $widgetFilter,
3551
PageRepositoryInterface $pageRepository,
36-
FilterEmulate $widgetFilter
52+
StoreManagerInterface $storeManager
3753
) {
54+
$this->pageByIdentifier = $getPageByIdentifier;
3855
$this->pageRepository = $pageRepository;
56+
$this->storeManager = $storeManager;
3957
$this->widgetFilter = $widgetFilter;
4058
}
4159

@@ -44,10 +62,32 @@ public function __construct(
4462
* @return array
4563
* @throws NoSuchEntityException
4664
*/
47-
public function getData(int $pageId): array
65+
public function getDataByPageId(int $pageId): array
4866
{
4967
$page = $this->pageRepository->getById($pageId);
5068

69+
return $this->convertPageData($page);
70+
}
71+
72+
/**
73+
* @param string $pageIdentifier
74+
* @return array
75+
*/
76+
public function getDataByPageIdentifier(string $pageIdentifier): array
77+
{
78+
$storeId = (int)$this->storeManager->getStore()->getId();
79+
$page = $this->pageByIdentifier->execute($pageIdentifier, $storeId);
80+
81+
return $this->convertPageData($page);
82+
}
83+
84+
/**
85+
* @param PageInterface $page
86+
* @return array
87+
* @throws NoSuchEntityException
88+
*/
89+
private function convertPageData(PageInterface $page)
90+
{
5191
if (false === $page->isActive()) {
5292
throw new NoSuchEntityException();
5393
}
@@ -56,6 +96,7 @@ public function getData(int $pageId): array
5696

5797
$pageData = [
5898
'url_key' => $page->getIdentifier(),
99+
PageInterface::PAGE_ID => $page->getId(),
59100
PageInterface::TITLE => $page->getTitle(),
60101
PageInterface::CONTENT => $renderedContent,
61102
PageInterface::CONTENT_HEADING => $page->getContentHeading(),

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

Lines changed: 36 additions & 9 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,61 @@ public function resolve(
4445
array $value = null,
4546
array $args = null
4647
) {
47-
$pageId = $this->getPageId($args);
48-
$pageData = $this->getPageData($pageId);
48+
if (!isset($args['id']) && !isset($args['identifier'])) {
49+
throw new GraphQlInputException(__('"Page id/identifier should be specified'));
50+
}
51+
52+
if (isset($args['id'])) {
53+
$pageData = $this->getPageDataById($this->getPageId($args));
54+
} elseif (isset($args['identifier'])) {
55+
$pageData = $this->getPageDataByIdentifier($this->getPageIdentifier($args));
56+
}
4957

5058
return $pageData;
5159
}
5260

5361
/**
5462
* @param array $args
5563
* @return int
56-
* @throws GraphQlInputException
5764
*/
5865
private function getPageId(array $args): int
5966
{
60-
if (!isset($args['id'])) {
61-
throw new GraphQlInputException(__('"Page id should be specified'));
62-
}
67+
return isset($args['id']) ? (int)$args['id'] : 0;
68+
}
6369

64-
return (int)$args['id'];
70+
/**
71+
* @param array $args
72+
* @return string
73+
*/
74+
private function getPageIdentifier(array $args): string
75+
{
76+
return isset($args['identifier']) ? (string)$args['identifier'] : '';
6577
}
6678

6779
/**
6880
* @param int $pageId
6981
* @return array
7082
* @throws GraphQlNoSuchEntityException
7183
*/
72-
private function getPageData(int $pageId): array
84+
private function getPageDataById(int $pageId): array
85+
{
86+
try {
87+
$pageData = $this->pageDataProvider->getDataByPageId($pageId);
88+
} catch (NoSuchEntityException $e) {
89+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
90+
}
91+
return $pageData;
92+
}
93+
94+
/**
95+
* @param string $pageIdentifier
96+
* @return array
97+
* @throws GraphQlNoSuchEntityException
98+
*/
99+
private function getPageDataByIdentifier(string $pageIdentifier): array
73100
{
74101
try {
75-
$pageData = $this->pageDataProvider->getData($pageId);
102+
$pageData = $this->pageDataProvider->getDataByPageIdentifier($pageIdentifier);
76103
} catch (NoSuchEntityException $e) {
77104
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
78105
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ type StoreConfig @doc(description: "The type contains information about a store
1313
type Query {
1414
cmsPage (
1515
id: Int @doc(description: "Id of the 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")
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")
2021
}
2122

2223
type CmsPage @doc(description: "CMS page defines all CMS page information") {
24+
page_id: Int @doc(description: "Entity ID of 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/CmsPageTest.php

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,32 @@ public function testGetCmsPageById()
5050
$this->assertEquals($cmsPageData['meta_keywords'], $response['cmsPage']['meta_keywords']);
5151
}
5252

53+
/**
54+
* Verify the fields of CMS Page selected by page_id
55+
*
56+
* @magentoApiDataFixture Magento/Cms/_files/pages.php
57+
*/
58+
public function testGetCmsPageByIdentifier()
59+
{
60+
$cmsPageIdentifier = 'page100';
61+
$storeId = 0;
62+
63+
$cmsPage = ObjectManager::getInstance()->get(GetPageByIdentifier::class)->execute($cmsPageIdentifier, $storeId);
64+
$pageId = $cmsPage->getPageId();
65+
66+
$query =
67+
<<<QUERY
68+
{
69+
cmsPage(identifier: "$cmsPageIdentifier") {
70+
page_id
71+
}
72+
}
73+
QUERY;
74+
75+
$response = $this->graphQlQuery($query);
76+
$this->assertEquals($pageId, $response['cmsPage']['page_id']);
77+
}
78+
5379
/**
5480
* Verify the message when page_id is not specified.
5581
*/
@@ -72,7 +98,7 @@ public function testGetCmsPageWithoutId()
7298
QUERY;
7399

74100
$this->expectException(\Exception::class);
75-
$this->expectExceptionMessage('Page id should be specified');
101+
$this->expectExceptionMessage('Page id/identifier should be specified');
76102
$this->graphQlQuery($query);
77103
}
78104

@@ -102,6 +128,32 @@ public function testGetCmsPageByNonExistentId()
102128
$this->graphQlQuery($query);
103129
}
104130

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

0 commit comments

Comments
 (0)