Skip to content

Commit be1cec7

Browse files
committed
Merge remote-tracking branch 'origin/MC-29423-sendfriend' into honeycombined24
2 parents c061d5c + 15dd3ec commit be1cec7

File tree

5 files changed

+193
-17
lines changed

5 files changed

+193
-17
lines changed

app/code/Magento/SendFriendGraphQl/Model/Resolver/SendEmailToFriend.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ public function __construct(
4848
*/
4949
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
5050
{
51+
$storeId = $context->getExtensionAttributes()->getStore()->getId();
52+
53+
if (!$this->sendFriendHelper->isEnabled($storeId)) {
54+
throw new GraphQlInputException(__('"Email to a Friend" is not enabled.'));
55+
}
56+
5157
/** @var ContextInterface $context */
52-
if (!$this->sendFriendHelper->isAllowForGuest()
58+
if (!$this->sendFriendHelper->isAllowForGuest($storeId)
5359
&& false === $context->getExtensionAttributes()->getIsCustomer()
5460
) {
5561
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\SendFriendGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Query\ResolverInterface;
12+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
use Magento\SendFriend\Helper\Data as SendFriendHelper;
14+
15+
/**
16+
* Resolve Store Config information for SendFriend
17+
*/
18+
class SendFriendConfiguration implements ResolverInterface
19+
{
20+
/**
21+
* @var SendFriendHelper
22+
*/
23+
private $sendFriendHelper;
24+
25+
/**
26+
* @param SendFriendHelper $sendFriendHelper
27+
*/
28+
public function __construct(SendFriendHelper $sendFriendHelper)
29+
{
30+
$this->sendFriendHelper = $sendFriendHelper;
31+
}
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
37+
{
38+
$store = $context->getExtensionAttributes()->getStore();
39+
$storeId = $store->getId();
40+
41+
return [
42+
'enabled_for_customers' => $this->sendFriendHelper->isEnabled($storeId),
43+
'enabled_for_guests' => $this->sendFriendHelper->isAllowForGuest($storeId)
44+
];
45+
}
46+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,12 @@ type SendEmailToFriendRecipient {
3737
name: String!
3838
email: String!
3939
}
40+
41+
type StoreConfig {
42+
send_friend: SendFriendConfiguration @resolver(class: "\\Magento\\SendFriendGraphQl\\Model\\Resolver\\SendFriendConfiguration") @doc(description: "Email to a Friend configuration.")
43+
}
44+
45+
type SendFriendConfiguration {
46+
enabled_for_customers: Boolean! @doc(description: "Indicates whether the Email to a Friend feature is enabled.")
47+
enabled_for_guests: Boolean! @doc(description: "Indicates whether the Email to a Friend feature is enabled for guests.")
48+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected function setUp()
4545

4646
/**
4747
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
48+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
4849
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
4950
*/
5051
public function testSendFriendGuestEnable()
@@ -66,6 +67,7 @@ public function testSendFriendGuestEnable()
6667

6768
/**
6869
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
70+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
6971
* @magentoConfigFixture default_store sendfriend/email/allow_guest 0
7072
* @expectedException \Exception
7173
* @expectedExceptionMessage The current customer isn't authorized.
@@ -90,9 +92,11 @@ public function testSendFriendGuestDisableAsGuest()
9092
/**
9193
* @magentoApiDataFixture Magento/Customer/_files/customer.php
9294
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
93-
* @magentoConfigFixture default_store sendfriend/email/allow_guest 0
95+
* @magentoConfigFixture default_store sendfriend/email/enabled 0
96+
* @expectedException \Exception
97+
* @expectedExceptionMessage "Email to a Friend" is not enabled.
9498
*/
95-
public function testSendFriendGuestDisableAsCustomer()
99+
public function testSendFriendDisableAsCustomer()
96100
{
97101
$productId = (int)$this->productRepository->get('simple_product')->getId();
98102
$recipients = '{
@@ -111,6 +115,9 @@ public function testSendFriendGuestDisableAsCustomer()
111115

112116
/**
113117
* @magentoApiDataFixture Magento/Customer/_files/customer.php
118+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
119+
* @expectedException \Exception
120+
* @expectedExceptionMessage The product that was requested doesn't exist. Verify the product and try again.
114121
*/
115122
public function testSendWithoutExistProduct()
116123
{
@@ -125,15 +132,13 @@ public function testSendWithoutExistProduct()
125132
}';
126133
$query = $this->getQuery($productId, $recipients);
127134

128-
$this->expectExceptionMessage(
129-
'The product that was requested doesn\'t exist. Verify the product and try again.'
130-
);
131135
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
132136
}
133137

134138
/**
135139
* @magentoApiDataFixture Magento/Customer/_files/customer.php
136140
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
141+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
137142
*/
138143
public function testMaxSendEmailToFriend()
139144
{
@@ -176,6 +181,7 @@ public function testMaxSendEmailToFriend()
176181
/**
177182
* @magentoApiDataFixture Magento/Customer/_files/customer.php
178183
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
184+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
179185
* @dataProvider sendFriendsErrorsDataProvider
180186
* @param string $input
181187
* @param string $errorMessage
@@ -188,7 +194,7 @@ public function testErrors(string $input, string $errorMessage)
188194
sendEmailToFriend(
189195
input: {
190196
$input
191-
}
197+
}
192198
) {
193199
sender {
194200
name
@@ -210,6 +216,7 @@ public function testErrors(string $input, string $errorMessage)
210216
/**
211217
* @magentoApiDataFixture Magento/Customer/_files/customer.php
212218
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
219+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
213220
* @magentoConfigFixture default_store sendfriend/email/max_per_hour 1
214221
* @magentoApiDataFixture Magento/SendFriend/Fixtures/sendfriend_configuration.php
215222
*/
@@ -238,6 +245,7 @@ public function testLimitMessagesPerHour()
238245
/**
239246
* @magentoApiDataFixture Magento/Customer/_files/customer.php
240247
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
248+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
241249
*/
242250
public function testSendProductWithoutSenderEmail()
243251
{
@@ -256,6 +264,7 @@ public function testSendProductWithoutSenderEmail()
256264
/**
257265
* @magentoApiDataFixture Magento/Customer/_files/customer.php
258266
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product_without_visibility.php
267+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
259268
*/
260269
public function testSendProductWithoutVisibility()
261270
{
@@ -282,12 +291,12 @@ public function sendFriendsErrorsDataProvider()
282291
{
283292
return [
284293
[
285-
'product_id: 1
294+
'product_id: 1
286295
sender: {
287296
name: "Name"
288297
email: "e@mail.com"
289298
message: "Lorem Ipsum"
290-
}
299+
}
291300
recipients: [
292301
{
293302
name: ""
@@ -300,12 +309,12 @@ public function sendFriendsErrorsDataProvider()
300309
]', 'Please provide Name for all of recipients.'
301310
],
302311
[
303-
'product_id: 1
312+
'product_id: 1
304313
sender: {
305314
name: "Name"
306315
email: "e@mail.com"
307316
message: "Lorem Ipsum"
308-
}
317+
}
309318
recipients: [
310319
{
311320
name: "Recipient Name 1"
@@ -318,12 +327,12 @@ public function sendFriendsErrorsDataProvider()
318327
]', 'Please provide Email for all of recipients.'
319328
],
320329
[
321-
'product_id: 1
330+
'product_id: 1
322331
sender: {
323332
name: ""
324333
email: "e@mail.com"
325334
message: "Lorem Ipsum"
326-
}
335+
}
327336
recipients: [
328337
{
329338
name: "Recipient Name 1"
@@ -336,12 +345,12 @@ public function sendFriendsErrorsDataProvider()
336345
]', 'Please provide Name of sender.'
337346
],
338347
[
339-
'product_id: 1
348+
'product_id: 1
340349
sender: {
341350
name: "Name"
342351
email: "e@mail.com"
343352
message: ""
344-
}
353+
}
345354
recipients: [
346355
{
347356
name: "Recipient Name 1"
@@ -403,9 +412,9 @@ private function getQuery(int $productId, string $recipients): string
403412
name: "Name"
404413
email: "e@mail.com"
405414
message: "Lorem Ipsum"
406-
}
415+
}
407416
recipients: [{$recipients}]
408-
}
417+
}
409418
) {
410419
sender {
411420
name
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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\GraphQl\SendFriend;
9+
10+
use Magento\TestFramework\TestCase\GraphQlAbstract;
11+
12+
/**
13+
* Test SendFriend configuration resolves correctly in StoreConfig
14+
*/
15+
class StoreConfigTest extends GraphQlAbstract
16+
{
17+
public function testSendFriendFieldsAreReturnedWithoutError()
18+
{
19+
$query = $this->getStoreConfigQuery();
20+
21+
$response = $this->graphQlQuery($query);
22+
$this->assertArrayNotHasKey('errors', $response);
23+
$this->assertArrayHasKey('send_friend', $response['storeConfig']);
24+
$this->assertArrayHasKey('enabled_for_customers', $response['storeConfig']['send_friend']);
25+
$this->assertArrayHasKey('enabled_for_guests', $response['storeConfig']['send_friend']);
26+
$this->assertNotNull($response['storeConfig']['send_friend']['enabled_for_customers']);
27+
$this->assertNotNull($response['storeConfig']['send_friend']['enabled_for_guests']);
28+
}
29+
30+
/**
31+
* @magentoConfigFixture default_store sendfriend/email/enabled 0
32+
*/
33+
public function testSendFriendDisabled()
34+
{
35+
$response = $this->graphQlQuery($this->getStoreConfigQuery());
36+
37+
$this->assertResponse(
38+
['enabled_for_customers' => false, 'enabled_for_guests' => false],
39+
$response
40+
);
41+
}
42+
43+
/**
44+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
45+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 0
46+
*/
47+
public function testSendFriendEnabledGuestDisabled()
48+
{
49+
$response = $this->graphQlQuery($this->getStoreConfigQuery());
50+
51+
$this->assertResponse(
52+
['enabled_for_customers' => true, 'enabled_for_guests' => false],
53+
$response
54+
);
55+
}
56+
57+
/**
58+
* @magentoConfigFixture default_store sendfriend/email/enabled 1
59+
* @magentoConfigFixture default_store sendfriend/email/allow_guest 1
60+
*/
61+
public function testSendFriendEnabledGuestEnabled()
62+
{
63+
$response = $this->graphQlQuery($this->getStoreConfigQuery());
64+
65+
$this->assertResponse(
66+
['enabled_for_customers' => true, 'enabled_for_guests' => true],
67+
$response
68+
);
69+
}
70+
71+
/**
72+
* Assert response matches expected output
73+
*
74+
* @param array $expectedValues
75+
* @param array $response
76+
*/
77+
private function assertResponse(array $expectedValues, array $response)
78+
{
79+
$this->assertArrayNotHasKey('errors', $response);
80+
$this->assertArrayHasKey('send_friend', $response['storeConfig']);
81+
$this->assertArrayHasKey('enabled_for_customers', $response['storeConfig']['send_friend']);
82+
$this->assertArrayHasKey('enabled_for_guests', $response['storeConfig']['send_friend']);
83+
$this->assertEquals($expectedValues['enabled_for_customers'], $response['storeConfig']['send_friend']['enabled_for_customers']);
84+
$this->assertEquals($expectedValues['enabled_for_guests'], $response['storeConfig']['send_friend']['enabled_for_guests']);
85+
}
86+
87+
/**
88+
* Return simple storeConfig query to get sendFriend configuration
89+
*
90+
* @return string
91+
*/
92+
private function getStoreConfigQuery()
93+
{
94+
return <<<QUERY
95+
{
96+
storeConfig{
97+
id
98+
send_friend {
99+
enabled_for_customers
100+
enabled_for_guests
101+
}
102+
}
103+
}
104+
QUERY;
105+
}
106+
}

0 commit comments

Comments
 (0)