|
| 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\SendFriend\Model\SendFriend; |
| 11 | +use Magento\SendFriend\Model\SendFriendFactory; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | + |
| 15 | +class SendFriendTest extends GraphQlAbstract |
| 16 | +{ |
| 17 | + |
| 18 | + /** |
| 19 | + * @var SendFriendFactory |
| 20 | + */ |
| 21 | + private $sendFriendFactory; |
| 22 | + |
| 23 | + protected function setUp() |
| 24 | + { |
| 25 | + $this->sendFriendFactory = Bootstrap::getObjectManager()->get(SendFriendFactory::class); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 30 | + */ |
| 31 | + public function testSendFriend() |
| 32 | + { |
| 33 | + $query = |
| 34 | + <<<QUERY |
| 35 | +mutation { |
| 36 | + sendEmailToFriend( |
| 37 | + input: { |
| 38 | + product_id: 1 |
| 39 | + sender: { |
| 40 | + name: "Name" |
| 41 | + email: "e@mail.com" |
| 42 | + message: "Lorem Ipsum" |
| 43 | + } |
| 44 | + recipients: [ |
| 45 | + { |
| 46 | + name: "Recipient Name 1" |
| 47 | + email:"recipient1@mail.com" |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Recipient Name 2" |
| 51 | + email:"recipient2@mail.com" |
| 52 | + } |
| 53 | + ] |
| 54 | + } |
| 55 | + ) { |
| 56 | + sender { |
| 57 | + name |
| 58 | + email |
| 59 | + message |
| 60 | + } |
| 61 | + recipients { |
| 62 | + name |
| 63 | + email |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | +QUERY; |
| 68 | + |
| 69 | + $response = $this->graphQlQuery($query); |
| 70 | + self::assertEquals('Name', $response['sendEmailToFriend']['sender']['name']); |
| 71 | + self::assertEquals('e@mail.com', $response['sendEmailToFriend']['sender']['email']); |
| 72 | + self::assertEquals('Lorem Ipsum', $response['sendEmailToFriend']['sender']['message']); |
| 73 | + self::assertEquals('Recipient Name 1', $response['sendEmailToFriend']['recipients'][0]['name']); |
| 74 | + self::assertEquals('recipient1@mail.com', $response['sendEmailToFriend']['recipients'][0]['email']); |
| 75 | + self::assertEquals('Recipient Name 2', $response['sendEmailToFriend']['recipients'][1]['name']); |
| 76 | + self::assertEquals('recipient2@mail.com', $response['sendEmailToFriend']['recipients'][1]['email']); |
| 77 | + } |
| 78 | + |
| 79 | + public function testSendWithoutExistProduct() |
| 80 | + { |
| 81 | + $query = |
| 82 | + <<<QUERY |
| 83 | +mutation { |
| 84 | + sendEmailToFriend( |
| 85 | + input: { |
| 86 | + product_id: 2018 |
| 87 | + sender: { |
| 88 | + name: "Name" |
| 89 | + email: "e@mail.com" |
| 90 | + message: "Lorem Ipsum" |
| 91 | + } |
| 92 | + recipients: [ |
| 93 | + { |
| 94 | + name: "Recipient Name 1" |
| 95 | + email:"recipient1@mail.com" |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "Recipient Name 2" |
| 99 | + email:"recipient2@mail.com" |
| 100 | + } |
| 101 | + ] |
| 102 | + } |
| 103 | + ) { |
| 104 | + sender { |
| 105 | + name |
| 106 | + email |
| 107 | + message |
| 108 | + } |
| 109 | + recipients { |
| 110 | + name |
| 111 | + email |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | +QUERY; |
| 116 | + $this->expectException(\Exception::class); |
| 117 | + $this->expectExceptionMessage( |
| 118 | + 'The product that was requested doesn\'t exist. Verify the product and try again.' |
| 119 | + ); |
| 120 | + $this->graphQlQuery($query); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 125 | + */ |
| 126 | + public function testMaxSendEmailToFriend() |
| 127 | + { |
| 128 | + /** @var SendFriend $sendFriend */ |
| 129 | + $sendFriend = $this->sendFriendFactory->create(); |
| 130 | + |
| 131 | + $query = |
| 132 | + <<<QUERY |
| 133 | +mutation { |
| 134 | + sendEmailToFriend( |
| 135 | + input: { |
| 136 | + product_id: 1 |
| 137 | + sender: { |
| 138 | + name: "Name" |
| 139 | + email: "e@mail.com" |
| 140 | + message: "Lorem Ipsum" |
| 141 | + } |
| 142 | + recipients: [ |
| 143 | + { |
| 144 | + name: "Recipient Name 1" |
| 145 | + email:"recipient1@mail.com" |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "Recipient Name 1" |
| 149 | + email:"recipient1@mail.com" |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "Recipient Name 1" |
| 153 | + email:"recipient1@mail.com" |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "Recipient Name 1" |
| 157 | + email:"recipient1@mail.com" |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "Recipient Name 1" |
| 161 | + email:"recipient1@mail.com" |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "Recipient Name 1" |
| 165 | + email:"recipient1@mail.com" |
| 166 | + } |
| 167 | + ] |
| 168 | + } |
| 169 | + ) { |
| 170 | + sender { |
| 171 | + name |
| 172 | + email |
| 173 | + message |
| 174 | + } |
| 175 | + recipients { |
| 176 | + name |
| 177 | + email |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | +QUERY; |
| 182 | + $this->expectException(\Exception::class); |
| 183 | + $this->expectExceptionMessage("No more than {$sendFriend->getMaxRecipients()} emails can be sent at a time."); |
| 184 | + $this->graphQlQuery($query); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 189 | + * @dataProvider sendFriendsErrorsDataProvider |
| 190 | + * @param string $input |
| 191 | + * @param string $errorMessage |
| 192 | + */ |
| 193 | + public function testErrors(string $input, string $errorMessage) |
| 194 | + { |
| 195 | + $query = |
| 196 | + <<<QUERY |
| 197 | +mutation { |
| 198 | + sendEmailToFriend( |
| 199 | + input: { |
| 200 | + $input |
| 201 | + } |
| 202 | + ) { |
| 203 | + sender { |
| 204 | + name |
| 205 | + email |
| 206 | + message |
| 207 | + } |
| 208 | + recipients { |
| 209 | + name |
| 210 | + email |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | +QUERY; |
| 215 | + $this->expectException(\Exception::class); |
| 216 | + $this->expectExceptionMessage($errorMessage); |
| 217 | + $this->graphQlQuery($query); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 222 | + * TODO: use magentoApiConfigFixture (to be merged https://github.com/magento/graphql-ce/pull/351) |
| 223 | + * @magentoApiDataFixture Magento/SendFriend/Fixtures/sendfriend_configuration.php |
| 224 | + */ |
| 225 | + public function testLimitMessagesPerHour() |
| 226 | + { |
| 227 | + |
| 228 | + /** @var SendFriend $sendFriend */ |
| 229 | + $sendFriend = $this->sendFriendFactory->create(); |
| 230 | + |
| 231 | + $query = |
| 232 | + <<<QUERY |
| 233 | +mutation { |
| 234 | + sendEmailToFriend( |
| 235 | + input: { |
| 236 | + product_id: 1 |
| 237 | + sender: { |
| 238 | + name: "Name" |
| 239 | + email: "e@mail.com" |
| 240 | + message: "Lorem Ipsum" |
| 241 | + } |
| 242 | + recipients: [ |
| 243 | + { |
| 244 | + name: "Recipient Name 1" |
| 245 | + email:"recipient1@mail.com" |
| 246 | + }, |
| 247 | + { |
| 248 | + name: "Recipient Name 2" |
| 249 | + email:"recipient2@mail.com" |
| 250 | + } |
| 251 | +
|
| 252 | + ] |
| 253 | + } |
| 254 | + ) { |
| 255 | + sender { |
| 256 | + name |
| 257 | + email |
| 258 | + message |
| 259 | + } |
| 260 | + recipients { |
| 261 | + name |
| 262 | + email |
| 263 | + } |
| 264 | + } |
| 265 | +} |
| 266 | +QUERY; |
| 267 | + $this->expectException(\Exception::class); |
| 268 | + $this->expectExceptionMessage( |
| 269 | + "You can't send messages more than {$sendFriend->getMaxSendsToFriend()} times an hour." |
| 270 | + ); |
| 271 | + |
| 272 | + for ($i = 0; $i <= $sendFriend->getMaxSendsToFriend() + 1; $i++) { |
| 273 | + $this->graphQlQuery($query); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * @return array |
| 279 | + */ |
| 280 | + public function sendFriendsErrorsDataProvider() |
| 281 | + { |
| 282 | + return [ |
| 283 | + [ |
| 284 | + 'product_id: 1 |
| 285 | + sender: { |
| 286 | + name: "Name" |
| 287 | + email: "e@mail.com" |
| 288 | + message: "Lorem Ipsum" |
| 289 | + } |
| 290 | + recipients: [ |
| 291 | + { |
| 292 | + name: "" |
| 293 | + email:"recipient1@mail.com" |
| 294 | + }, |
| 295 | + { |
| 296 | + name: "" |
| 297 | + email:"recipient2@mail.com" |
| 298 | + } |
| 299 | + ]', 'Please provide Name for all of recipients.' |
| 300 | + ], |
| 301 | + [ |
| 302 | + 'product_id: 1 |
| 303 | + sender: { |
| 304 | + name: "Name" |
| 305 | + email: "e@mail.com" |
| 306 | + message: "Lorem Ipsum" |
| 307 | + } |
| 308 | + recipients: [ |
| 309 | + { |
| 310 | + name: "Recipient Name 1" |
| 311 | + email:"" |
| 312 | + }, |
| 313 | + { |
| 314 | + name: "Recipient Name 2" |
| 315 | + email:"" |
| 316 | + } |
| 317 | + ]', 'Please provide Email for all of recipients.' |
| 318 | + ], |
| 319 | + [ |
| 320 | + 'product_id: 1 |
| 321 | + sender: { |
| 322 | + name: "" |
| 323 | + email: "e@mail.com" |
| 324 | + message: "Lorem Ipsum" |
| 325 | + } |
| 326 | + recipients: [ |
| 327 | + { |
| 328 | + name: "Recipient Name 1" |
| 329 | + email:"recipient1@mail.com" |
| 330 | + }, |
| 331 | + { |
| 332 | + name: "Recipient Name 2" |
| 333 | + email:"recipient2@mail.com" |
| 334 | + } |
| 335 | + ]', 'Please provide Name of sender.' |
| 336 | + ], |
| 337 | + [ |
| 338 | + 'product_id: 1 |
| 339 | + sender: { |
| 340 | + name: "Name" |
| 341 | + email: "e@mail.com" |
| 342 | + message: "" |
| 343 | + } |
| 344 | + recipients: [ |
| 345 | + { |
| 346 | + name: "Recipient Name 1" |
| 347 | + email:"recipient1@mail.com" |
| 348 | + }, |
| 349 | + { |
| 350 | + name: "Recipient Name 2" |
| 351 | + email:"recipient2@mail.com" |
| 352 | + } |
| 353 | + ]', 'Please provide Message.' |
| 354 | + ] |
| 355 | + ]; |
| 356 | + } |
| 357 | +} |
0 commit comments