Skip to content

Commit 48f8e0a

Browse files
author
Valeriy Naida
authored
Merge pull request #3478 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents 909591d + 4439971 commit 48f8e0a

File tree

8 files changed

+229
-61
lines changed

8 files changed

+229
-61
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public function resolve(
5555
}
5656

5757
/**
58+
* Get block identifiers
59+
*
5860
* @param array $args
5961
* @return string[]
6062
* @throws GraphQlInputException
@@ -69,19 +71,21 @@ private function getBlockIdentifiers(array $args): array
6971
}
7072

7173
/**
74+
* Get blocks data
75+
*
7276
* @param array $blockIdentifiers
7377
* @return array
7478
* @throws GraphQlNoSuchEntityException
7579
*/
7680
private function getBlocksData(array $blockIdentifiers): array
7781
{
7882
$blocksData = [];
79-
try {
80-
foreach ($blockIdentifiers as $blockIdentifier) {
83+
foreach ($blockIdentifiers as $blockIdentifier) {
84+
try {
8185
$blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier);
86+
} catch (NoSuchEntityException $e) {
87+
$blocksData[$blockIdentifier] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
8288
}
83-
} catch (NoSuchEntityException $e) {
84-
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
8589
}
8690
return $blocksData;
8791
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function __construct(
4040
}
4141

4242
/**
43+
* Get block data
44+
*
4345
* @param string $blockIdentifier
4446
* @return array
4547
* @throws NoSuchEntityException
@@ -49,7 +51,9 @@ public function getData(string $blockIdentifier): array
4951
$block = $this->blockRepository->getById($blockIdentifier);
5052

5153
if (false === $block->isActive()) {
52-
throw new NoSuchEntityException();
54+
throw new NoSuchEntityException(
55+
__('The CMS block with the "%1" ID doesn\'t exist.', $blockIdentifier)
56+
);
5357
}
5458

5559
$renderedContent = $this->widgetFilter->filter($block->getContent());

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,38 @@
22
# See COPYING.txt for license details.
33

44
type Mutation {
5-
sendEmailToFriend (input: SendEmailToFriendSenderInput): SendEmailToFriendOutput @resolver(class: "\\Magento\\SendFriendGraphQl\\Model\\Resolver\\SendEmailToFriend") @doc(description:"Recommends Product by Sending Single/Multiple Email")
5+
sendEmailToFriend (input: SendEmailToFriendInput): SendEmailToFriendOutput @resolver(class: "\\Magento\\SendFriendGraphQl\\Model\\Resolver\\SendEmailToFriend") @doc(description:"Recommends Product by Sending Single/Multiple Email")
66
}
77

8-
input SendEmailToFriendSenderInput {
9-
product_id: Int!
10-
sender: Sender!
11-
recipients: [Recipient!]!
8+
input SendEmailToFriendInput {
9+
product_id: Int!
10+
sender: SendEmailToFriendSenderInput!
11+
recipients: [SendEmailToFriendRecipientInput!]!
1212
}
1313

14-
type Sender {
14+
input SendEmailToFriendSenderInput {
1515
name: String!
1616
email: String!
1717
message: String!
1818
}
1919

20-
type Recipient {
20+
input SendEmailToFriendRecipientInput {
2121
name: String!
2222
email: String!
2323
}
2424

2525
type SendEmailToFriendOutput {
26-
sender: Sender
27-
recipients: [Recipient]
26+
sender: SendEmailToFriendSender
27+
recipients: [SendEmailToFriendRecipient]
28+
}
29+
30+
type SendEmailToFriendSender {
31+
name: String!
32+
email: String!
33+
message: String!
34+
}
35+
36+
type SendEmailToFriendRecipient {
37+
name: String!
38+
email: String!
2839
}

dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class Client
2929
private $json;
3030

3131
/**
32-
* CurlClient constructor.
33-
*
3432
* @param CurlClient|null $curlClient
3533
* @param JsonSerializer|null $json
3634
*/
@@ -81,6 +79,8 @@ public function postQuery(string $query, array $variables = [], string $operatio
8179
}
8280

8381
/**
82+
* Process errors
83+
*
8484
* @param array $responseBodyArray
8585
* @throws \Exception
8686
*/
@@ -102,13 +102,18 @@ private function processErrors($responseBodyArray)
102102
}
103103
}
104104

105-
throw new \Exception('GraphQL response contains errors: ' . $errorMessage);
105+
throw new ResponseContainsErrorsException(
106+
'GraphQL response contains errors: ' . $errorMessage,
107+
$responseBodyArray
108+
);
106109
}
107110
throw new \Exception('GraphQL responded with an unknown error: ' . json_encode($responseBodyArray));
108111
}
109112
}
110113

111114
/**
115+
* Get endpoint url
116+
*
112117
* @return string resource URL
113118
* @throws \Exception
114119
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\TestCase\GraphQl;
9+
10+
/**
11+
* Response contains errors exception
12+
*/
13+
class ResponseContainsErrorsException extends \Exception
14+
{
15+
/**
16+
* @var array
17+
*/
18+
private $responseData;
19+
20+
/**
21+
* @param string $message
22+
* @param array $responseData
23+
* @param \Exception|null $cause
24+
* @param int $code
25+
*/
26+
public function __construct(string $message, array $responseData, \Exception $cause = null, int $code = 0)
27+
{
28+
parent::__construct($message, $code, $cause);
29+
$this->responseData = $responseData;
30+
}
31+
32+
/**
33+
* Get response data
34+
*
35+
* @return array
36+
*/
37+
public function getResponseData(): array
38+
{
39+
return $this->responseData;
40+
}
41+
}

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

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,45 @@
77

88
namespace Magento\GraphQl\Cms;
99

10-
use Magento\Cms\Model\Block;
11-
use Magento\Cms\Model\GetBlockByIdentifier;
12-
use Magento\Store\Model\StoreManagerInterface;
10+
use Magento\Cms\Api\BlockRepositoryInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
1313
use Magento\TestFramework\TestCase\GraphQlAbstract;
1414
use Magento\Widget\Model\Template\FilterEmulate;
1515

1616
class CmsBlockTest extends GraphQlAbstract
1717
{
1818
/**
19-
* @var \Magento\TestFramework\ObjectManager
19+
* @var BlockRepositoryInterface
2020
*/
21-
private $objectManager;
21+
private $blockRepository;
22+
23+
/**
24+
* @var FilterEmulate
25+
*/
26+
private $filterEmulate;
2227

2328
protected function setUp()
2429
{
25-
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
30+
$this->blockRepository = Bootstrap::getObjectManager()->get(BlockRepositoryInterface::class);
31+
$this->filterEmulate = Bootstrap::getObjectManager()->get(FilterEmulate::class);
2632
}
2733

2834
/**
2935
* Verify the fields of CMS Block selected by identifiers
3036
*
31-
* @magentoApiDataFixture Magento/Cms/_files/block.php
37+
* @magentoApiDataFixture Magento/Cms/_files/blocks.php
3238
*/
33-
public function testGetCmsBlocksByIdentifiers()
39+
public function testGetCmsBlock()
3440
{
35-
/** @var StoreManagerInterface $storeManager */
36-
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
37-
$storeId = (int)$storeManager->getStore()->getId();
38-
$cmsBlock = $this->objectManager->get(GetBlockByIdentifier::class)->execute("fixture_block", $storeId);
41+
$cmsBlock = $this->blockRepository->getById('enabled_block');
3942
$cmsBlockData = $cmsBlock->getData();
40-
/** @var FilterEmulate $widgetFilter */
41-
$widgetFilter = $this->objectManager->get(FilterEmulate::class);
42-
$renderedContent = $widgetFilter->setUseSessionInUrl(false)->filter($cmsBlock->getContent());
43+
$renderedContent = $this->filterEmulate->setUseSessionInUrl(false)->filter($cmsBlock->getContent());
44+
4345
$query =
4446
<<<QUERY
4547
{
46-
cmsBlocks(identifiers: "fixture_block") {
48+
cmsBlocks(identifiers: "enabled_block") {
4749
items {
4850
identifier
4951
title
@@ -52,34 +54,30 @@ public function testGetCmsBlocksByIdentifiers()
5254
}
5355
}
5456
QUERY;
55-
5657
$response = $this->graphQlQuery($query);
57-
$this->assertArrayHasKey('cmsBlocks', $response);
58-
$this->assertArrayHasKey('items', $response['cmsBlocks']);
59-
$this->assertArrayHasKey('content', $response['cmsBlocks']['items'][0]);
60-
$this->assertEquals($cmsBlockData['identifier'], $response['cmsBlocks']['items'][0]['identifier']);
61-
$this->assertEquals($cmsBlockData['title'], $response['cmsBlocks']['items'][0]['title']);
62-
$this->assertEquals($renderedContent, $response['cmsBlocks']['items'][0]['content']);
58+
59+
self::assertArrayHasKey('cmsBlocks', $response);
60+
self::assertArrayHasKey('items', $response['cmsBlocks']);
61+
62+
self::assertEquals($cmsBlockData['identifier'], $response['cmsBlocks']['items'][0]['identifier']);
63+
self::assertEquals($cmsBlockData['title'], $response['cmsBlocks']['items'][0]['title']);
64+
self::assertEquals($renderedContent, $response['cmsBlocks']['items'][0]['content']);
6365
}
6466

6567
/**
6668
* Verify the message when CMS Block is disabled
6769
*
68-
* @magentoApiDataFixture Magento/Cms/_files/block.php
70+
* @expectedException \Exception
71+
* @expectedExceptionMessage The CMS block with the "disabled_block" ID doesn't exist
72+
*
73+
* @magentoApiDataFixture Magento/Cms/_files/blocks.php
6974
*/
70-
public function testGetDisabledCmsBlockByIdentifiers()
75+
public function testGetDisabledCmsBlock()
7176
{
72-
/** @var StoreManagerInterface $storeManager */
73-
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
74-
$storeId = (int)$storeManager->getStore()->getId();
75-
$cmsBlockId = $this->objectManager->get(GetBlockByIdentifier::class)
76-
->execute("fixture_block", $storeId)
77-
->getId();
78-
$this->objectManager->get(Block::class)->load($cmsBlockId)->setIsActive(0)->save();
7977
$query =
8078
<<<QUERY
8179
{
82-
cmsBlocks(identifiers: "fixture_block") {
80+
cmsBlocks(identifiers: "disabled_block") {
8381
items {
8482
identifier
8583
title
@@ -88,16 +86,16 @@ public function testGetDisabledCmsBlockByIdentifiers()
8886
}
8987
}
9088
QUERY;
91-
92-
$this->expectException(\Exception::class);
93-
$this->expectExceptionMessage('No such entity.');
9489
$this->graphQlQuery($query);
9590
}
9691

9792
/**
9893
* Verify the message when identifiers were not specified
94+
*
95+
* @expectedException \Exception
96+
* @expectedExceptionMessage "identifiers" of CMS blocks should be specified
9997
*/
100-
public function testGetCmsBlockBypassingIdentifiers()
98+
public function testGetCmsBlocksWithoutIdentifiers()
10199
{
102100
$query =
103101
<<<QUERY
@@ -111,21 +109,21 @@ public function testGetCmsBlockBypassingIdentifiers()
111109
}
112110
}
113111
QUERY;
114-
115-
$this->expectException(\Exception::class);
116-
$this->expectExceptionMessage('"identifiers" of CMS blocks should be specified');
117112
$this->graphQlQuery($query);
118113
}
119114

120115
/**
121116
* Verify the message when CMS Block with such identifiers does not exist
117+
*
118+
* @expectedException \Exception
119+
* @expectedExceptionMessage The CMS block with the "nonexistent_id" ID doesn't exist.
122120
*/
123121
public function testGetCmsBlockByNonExistentIdentifier()
124122
{
125123
$query =
126124
<<<QUERY
127125
{
128-
cmsBlocks(identifiers: "0") {
126+
cmsBlocks(identifiers: "nonexistent_id") {
129127
items {
130128
identifier
131129
title
@@ -134,9 +132,39 @@ public function testGetCmsBlockByNonExistentIdentifier()
134132
}
135133
}
136134
QUERY;
137-
138-
$this->expectException(\Exception::class);
139-
$this->expectExceptionMessage('The CMS block with the "0" ID doesn\'t exist.');
140135
$this->graphQlQuery($query);
141136
}
137+
138+
/**
139+
* Verify the fields of CMS Block selected by identifiers
140+
*
141+
* @magentoApiDataFixture Magento/Cms/_files/blocks.php
142+
*/
143+
public function testGetEnabledAndDisabledCmsBlockInOneRequest()
144+
{
145+
$query =
146+
<<<QUERY
147+
{
148+
cmsBlocks(identifiers: ["enabled_block", "disabled_block"]) {
149+
items {
150+
identifier
151+
}
152+
}
153+
}
154+
QUERY;
155+
156+
try {
157+
$this->graphQlQuery($query);
158+
self::fail('Response should contains errors.');
159+
} catch (ResponseContainsErrorsException $e) {
160+
$responseData = $e->getResponseData();
161+
}
162+
163+
self::assertNotEmpty($responseData);
164+
self::assertEquals('enabled_block', $responseData['data']['cmsBlocks']['items'][0]['identifier']);
165+
self::assertEquals(
166+
'The CMS block with the "disabled_block" ID doesn\'t exist.',
167+
$responseData['errors'][0]['message']
168+
);
169+
}
142170
}

0 commit comments

Comments
 (0)