8
8
namespace Magento \GraphQl \SendFriend ;
9
9
10
10
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
+ use Magento \Framework \Exception \AuthenticationException ;
12
+ use Magento \Integration \Api \CustomerTokenServiceInterface ;
11
13
use Magento \SendFriend \Model \SendFriend ;
12
14
use Magento \SendFriend \Model \SendFriendFactory ;
13
15
use Magento \TestFramework \Helper \Bootstrap ;
@@ -23,21 +25,29 @@ class SendFriendTest extends GraphQlAbstract
23
25
* @var SendFriendFactory
24
26
*/
25
27
private $ sendFriendFactory ;
28
+
26
29
/**
27
30
* @var ProductRepositoryInterface
28
31
*/
29
32
private $ productRepository ;
30
33
34
+ /**
35
+ * @var CustomerTokenServiceInterface
36
+ */
37
+ private $ customerTokenService ;
38
+
31
39
protected function setUp ()
32
40
{
33
41
$ this ->sendFriendFactory = Bootstrap::getObjectManager ()->get (SendFriendFactory::class);
34
42
$ this ->productRepository = Bootstrap::getObjectManager ()->get (ProductRepositoryInterface::class);
43
+ $ this ->customerTokenService = Bootstrap::getObjectManager ()->get (CustomerTokenServiceInterface::class);
35
44
}
36
45
37
46
/**
38
47
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
48
+ * @magentoApiDataFixture Magento/GraphQl/SendFriend/_files/enable_send_friend_guest.php
39
49
*/
40
- public function testSendFriend ()
50
+ public function testSendFriendGuestEnable ()
41
51
{
42
52
$ productId = (int )$ this ->productRepository ->get ('simple_product ' )->getId ();
43
53
$ recipients = '{
@@ -51,15 +61,57 @@ public function testSendFriend()
51
61
$ query = $ this ->getQuery ($ productId , $ recipients );
52
62
53
63
$ response = $ this ->graphQlMutation ($ query );
54
- self ::assertEquals ('Name ' , $ response ['sendEmailToFriend ' ]['sender ' ]['name ' ]);
55
- self ::
assertEquals (
'[email protected] ' ,
$ response[
'sendEmailToFriend ' ][
'sender ' ][
'email ' ]);
56
- self ::assertEquals ('Lorem Ipsum ' , $ response ['sendEmailToFriend ' ]['sender ' ]['message ' ]);
57
- self ::assertEquals ('Recipient Name 1 ' , $ response ['sendEmailToFriend ' ]['recipients ' ][0 ]['name ' ]);
58
- self ::
assertEquals (
'[email protected] ' ,
$ response[
'sendEmailToFriend ' ][
'recipients ' ][
0 ][
'email ' ]);
59
- self ::assertEquals ('Recipient Name 2 ' , $ response ['sendEmailToFriend ' ]['recipients ' ][1 ]['name ' ]);
60
- self ::
assertEquals (
'[email protected] ' ,
$ response[
'sendEmailToFriend ' ][
'recipients ' ][
1 ][
'email ' ]);
64
+ $ this ->assertResponse ($ response );
65
+ }
66
+
67
+ /**
68
+ * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
69
+ * @magentoApiDataFixture Magento/GraphQl/SendFriend/_files/disable_send_friend_guest.php
70
+ * @expectedException \Exception
71
+ * @expectedExceptionMessage The current customer isn't authorized.
72
+ */
73
+ public function testSendFriendGuestDisableAsGuest ()
74
+ {
75
+ $ productId = (int )$ this ->productRepository ->get ('simple_product ' )->getId ();
76
+ $ recipients = '{
77
+ name: "Recipient Name 1"
78
+
79
+ },
80
+ {
81
+ name: "Recipient Name 2"
82
+
83
+ } ' ;
84
+ $ query = $ this ->getQuery ($ productId , $ recipients );
85
+
86
+ $ response = $ this ->graphQlMutation ($ query );
87
+ $ this ->assertResponse ($ response );
88
+ }
89
+
90
+ /**
91
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
92
+ * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
93
+ * @magentoApiDataFixture Magento/GraphQl/SendFriend/_files/disable_send_friend_guest.php
94
+ */
95
+ public function testSendFriendGuestDisableAsCustomer ()
96
+ {
97
+ $ productId = (int )$ this ->productRepository ->get ('simple_product ' )->getId ();
98
+ $ recipients = '{
99
+ name: "Recipient Name 1"
100
+
101
+ },
102
+ {
103
+ name: "Recipient Name 2"
104
+
105
+ } ' ;
106
+ $ query = $ this ->getQuery ($ productId , $ recipients );
107
+
108
+ $ response = $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeaderMap ());
109
+ $ this ->assertResponse ($ response );
61
110
}
62
111
112
+ /**
113
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
114
+ */
63
115
public function testSendWithoutExistProduct ()
64
116
{
65
117
$ productId = 2018 ;
@@ -77,10 +129,11 @@ public function testSendWithoutExistProduct()
77
129
$ this ->expectExceptionMessage (
78
130
'The product that was requested doesn \'t exist. Verify the product and try again. '
79
131
);
80
- $ this ->graphQlMutation ($ query );
132
+ $ this ->graphQlMutation ($ query, [], '' , $ this -> getHeaderMap () );
81
133
}
82
134
83
135
/**
136
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
84
137
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
85
138
*/
86
139
public function testMaxSendEmailToFriend ()
@@ -118,10 +171,11 @@ public function testMaxSendEmailToFriend()
118
171
119
172
$ this ->expectException (\Exception::class);
120
173
$ this ->expectExceptionMessage ("No more than {$ sendFriend ->getMaxRecipients ()} emails can be sent at a time. " );
121
- $ this ->graphQlMutation ($ query );
174
+ $ this ->graphQlMutation ($ query, [], '' , $ this -> getHeaderMap () );
122
175
}
123
176
124
177
/**
178
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
125
179
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
126
180
* @dataProvider sendFriendsErrorsDataProvider
127
181
* @param string $input
@@ -151,10 +205,11 @@ public function testErrors(string $input, string $errorMessage)
151
205
QUERY ;
152
206
$ this ->expectException (\Exception::class);
153
207
$ this ->expectExceptionMessage ($ errorMessage );
154
- $ this ->graphQlMutation ($ query );
208
+ $ this ->graphQlMutation ($ query, [], '' , $ this -> getHeaderMap () );
155
209
}
156
210
157
211
/**
212
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
158
213
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
159
214
* TODO: use magentoApiConfigFixture (to be merged https://github.com/magento/graphql-ce/pull/351)
160
215
* @magentoApiDataFixture Magento/SendFriend/Fixtures/sendfriend_configuration.php
@@ -183,11 +238,12 @@ public function testLimitMessagesPerHour()
183
238
184
239
$ maxSendToFriends = $ sendFriend ->getMaxSendsToFriend ();
185
240
for ($ i = 0 ; $ i <= $ maxSendToFriends + 1 ; $ i ++) {
186
- $ this ->graphQlMutation ($ query );
241
+ $ this ->graphQlMutation ($ query, [], '' , $ this -> getHeaderMap () );
187
242
}
188
243
}
189
244
190
245
/**
246
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
191
247
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
192
248
*/
193
249
public function testSendProductWithoutSenderEmail ()
@@ -201,10 +257,11 @@ public function testSendProductWithoutSenderEmail()
201
257
202
258
$ this ->expectException (\Exception::class);
203
259
$ this ->expectExceptionMessage ('GraphQL response contains errors: Please provide Email for all of recipients. ' );
204
- $ this ->graphQlMutation ($ query );
260
+ $ this ->graphQlMutation ($ query, [], '' , $ this -> getHeaderMap () );
205
261
}
206
262
207
263
/**
264
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
208
265
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product_without_visibility.php
209
266
*/
210
267
public function testSendProductWithoutVisibility ()
@@ -220,14 +277,8 @@ public function testSendProductWithoutVisibility()
220
277
} ' ;
221
278
$ query = $ this ->getQuery ($ productId , $ recipients );
222
279
223
- $ response = $ this ->graphQlMutation ($ query );
224
- self ::assertEquals ('Name ' , $ response ['sendEmailToFriend ' ]['sender ' ]['name ' ]);
225
- self ::
assertEquals (
'[email protected] ' ,
$ response[
'sendEmailToFriend ' ][
'sender ' ][
'email ' ]);
226
- self ::assertEquals ('Lorem Ipsum ' , $ response ['sendEmailToFriend ' ]['sender ' ]['message ' ]);
227
- self ::assertEquals ('Recipient Name 1 ' , $ response ['sendEmailToFriend ' ]['recipients ' ][0 ]['name ' ]);
228
- self ::
assertEquals (
'[email protected] ' ,
$ response[
'sendEmailToFriend ' ][
'recipients ' ][
0 ][
'email ' ]);
229
- self ::assertEquals ('Recipient Name 2 ' , $ response ['sendEmailToFriend ' ]['recipients ' ][1 ]['name ' ]);
230
- self ::
assertEquals (
'[email protected] ' ,
$ response[
'sendEmailToFriend ' ][
'recipients ' ][
1 ][
'email ' ]);
280
+ $ response = $ this ->graphQlMutation ($ query , [], '' , $ this ->getHeaderMap ());
281
+ $ this ->assertResponse ($ response );
231
282
}
232
283
233
284
/**
@@ -311,6 +362,37 @@ public function sendFriendsErrorsDataProvider()
311
362
];
312
363
}
313
364
365
+ /**
366
+ * Generic assertions for send a friend response
367
+ *
368
+ * @param array $response
369
+ */
370
+ private function assertResponse (array $ response ): void
371
+ {
372
+ self ::assertEquals ('Name ' , $ response ['sendEmailToFriend ' ]['sender ' ]['name ' ]);
373
+ self ::
assertEquals (
'[email protected] ' ,
$ response[
'sendEmailToFriend ' ][
'sender ' ][
'email ' ]);
374
+ self ::assertEquals ('Lorem Ipsum ' , $ response ['sendEmailToFriend ' ]['sender ' ]['message ' ]);
375
+ self ::assertEquals ('Recipient Name 1 ' , $ response ['sendEmailToFriend ' ]['recipients ' ][0 ]['name ' ]);
376
+ self ::
assertEquals (
'[email protected] ' ,
$ response[
'sendEmailToFriend ' ][
'recipients ' ][
0 ][
'email ' ]);
377
+ self ::assertEquals ('Recipient Name 2 ' , $ response ['sendEmailToFriend ' ]['recipients ' ][1 ]['name ' ]);
378
+ self ::
assertEquals (
'[email protected] ' ,
$ response[
'sendEmailToFriend ' ][
'recipients ' ][
1 ][
'email ' ]);
379
+ }
380
+
381
+ /**
382
+ * Retrieve customer authorization headers
383
+ *
384
+ * @param string $username
385
+ * @param string $password
386
+ * @return array
387
+ * @throws AuthenticationException
388
+ */
389
+ private function getHeaderMap (
string $ username =
'[email protected] ' ,
string $ password =
'password ' ):
array
390
+ {
391
+ $ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ($ username , $ password );
392
+ $ headerMap = ['Authorization ' => 'Bearer ' . $ customerToken ];
393
+ return $ headerMap ;
394
+ }
395
+
314
396
/**
315
397
* @param int $productId
316
398
* @param string $recipients
0 commit comments