1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2016 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\EmailBundle\Tests\Helper; |
||
13 | |||
14 | use Mautic\CoreBundle\Factory\MauticFactory; |
||
15 | use Mautic\EmailBundle\Entity\Email; |
||
16 | use Mautic\EmailBundle\Helper\MailHelper; |
||
17 | use Mautic\EmailBundle\MonitoredEmail\Mailbox; |
||
18 | use Mautic\EmailBundle\Swiftmailer\Exception\BatchQueueMaxException; |
||
19 | use Mautic\EmailBundle\Swiftmailer\Spool\DelegatingSpool; |
||
20 | use Mautic\EmailBundle\Swiftmailer\Transport\SpoolTransport; |
||
21 | use Mautic\EmailBundle\Tests\Helper\Transport\BatchTransport; |
||
22 | use Mautic\EmailBundle\Tests\Helper\Transport\BcInterfaceTokenTransport; |
||
23 | use Mautic\EmailBundle\Tests\Helper\Transport\SmtpTransport; |
||
24 | use Mautic\LeadBundle\Entity\LeadRepository; |
||
25 | use Mautic\LeadBundle\Model\LeadModel; |
||
26 | use Monolog\Logger; |
||
27 | |||
28 | class MailHelperTest extends \PHPUnit\Framework\TestCase |
||
29 | { |
||
30 | /** |
||
31 | * @var MauticFactory|\PHPUnit\Framework\MockObject\MockObject |
||
32 | */ |
||
33 | private $mockFactory; |
||
34 | |||
35 | /** |
||
36 | * @var SpoolTransport |
||
37 | */ |
||
38 | private $spoolTransport; |
||
39 | |||
40 | /** |
||
41 | * @var \Swift_Events_EventDispatcher |
||
42 | */ |
||
43 | private $swiftEventsDispatcher; |
||
44 | |||
45 | /** |
||
46 | * @var DelegatingSpool |
||
47 | */ |
||
48 | private $delegatingSpool; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $contacts = [ |
||
54 | [ |
||
55 | 'id' => 1, |
||
56 | 'email' => '[email protected]', |
||
57 | 'firstname' => 'Contact', |
||
58 | 'lastname' => '1', |
||
59 | 'owner_id' => 1, |
||
60 | ], |
||
61 | [ |
||
62 | 'id' => 2, |
||
63 | 'email' => '[email protected]', |
||
64 | 'firstname' => 'Contact', |
||
65 | 'lastname' => '2', |
||
66 | 'owner_id' => 0, |
||
67 | ], |
||
68 | [ |
||
69 | 'id' => 3, |
||
70 | 'email' => '[email protected]', |
||
71 | 'firstname' => 'Contact', |
||
72 | 'lastname' => '3', |
||
73 | 'owner_id' => 2, |
||
74 | ], |
||
75 | [ |
||
76 | 'id' => 4, |
||
77 | 'email' => '[email protected]', |
||
78 | 'firstname' => 'Contact', |
||
79 | 'lastname' => '4', |
||
80 | 'owner_id' => 1, |
||
81 | ], |
||
82 | ]; |
||
83 | |||
84 | protected function setUp(): void |
||
85 | { |
||
86 | defined('MAUTIC_ENV') or define('MAUTIC_ENV', 'test'); |
||
87 | |||
88 | $this->mockFactory = $this->createMock(MauticFactory::class); |
||
89 | $this->swiftEventsDispatcher = $this->createMock(\Swift_Events_EventDispatcher::class); |
||
90 | $this->delegatingSpool = $this->createMock(DelegatingSpool::class); |
||
91 | |||
92 | $this->spoolTransport = new SpoolTransport($this->swiftEventsDispatcher, $this->delegatingSpool); |
||
93 | } |
||
94 | |||
95 | public function testQueueModeThrowsExceptionWhenBatchLimitHit() |
||
96 | { |
||
97 | $this->expectException(BatchQueueMaxException::class); |
||
98 | |||
99 | $mockFactory = $this->getMockBuilder(MauticFactory::class) |
||
100 | ->disableOriginalConstructor() |
||
101 | ->getMock(); |
||
102 | $mockFactory->method('getParameter') |
||
103 | ->will( |
||
104 | $this->returnValueMap( |
||
105 | [ |
||
106 | ['mailer_return_path', false, null], |
||
107 | ['mailer_spool_type', false, 'memory'], |
||
108 | ] |
||
109 | ) |
||
110 | ); |
||
111 | |||
112 | $swiftMailer = new \Swift_Mailer(new BatchTransport()); |
||
113 | |||
114 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
115 | |||
116 | // Enable queue mode |
||
117 | $mailer->enableQueue(); |
||
118 | $mailer->addTo('[email protected]'); |
||
119 | $mailer->addTo('[email protected]'); |
||
120 | $mailer->addTo('[email protected]'); |
||
121 | $mailer->addTo('[email protected]'); |
||
122 | $mailer->addTo('[email protected]'); |
||
123 | } |
||
124 | |||
125 | public function testQueueModeDisabledDoesNotThrowsExceptionWhenBatchLimitHit() |
||
126 | { |
||
127 | $mockFactory = $this->getMockBuilder(MauticFactory::class) |
||
128 | ->disableOriginalConstructor() |
||
129 | ->getMock(); |
||
130 | $mockFactory->method('getParameter') |
||
131 | ->will( |
||
132 | $this->returnValueMap( |
||
133 | [ |
||
134 | ['mailer_return_path', false, null], |
||
135 | ['mailer_spool_type', false, 'memory'], |
||
136 | ] |
||
137 | ) |
||
138 | ); |
||
139 | |||
140 | $swiftMailer = new \Swift_Mailer(new BatchTransport()); |
||
141 | |||
142 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
143 | |||
144 | // Enable queue mode |
||
145 | try { |
||
146 | $mailer->addTo('[email protected]'); |
||
147 | $mailer->addTo('[email protected]'); |
||
148 | } catch (BatchQueueMaxException $exception) { |
||
149 | $this->fail('BatchQueueMaxException thrown'); |
||
150 | } |
||
151 | |||
152 | // Otherwise success |
||
153 | $this->assertTrue(true); |
||
154 | } |
||
155 | |||
156 | public function testQueuedEmailFromOverride() |
||
157 | { |
||
158 | $mockFactory = $this->getMockFactory(false); |
||
159 | |||
160 | $transport = new BatchTransport(); |
||
161 | $swiftMailer = new \Swift_Mailer($transport); |
||
162 | |||
163 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
164 | $mailer->enableQueue(); |
||
165 | |||
166 | $email = new Email(); |
||
167 | $email->setFromAddress('[email protected]'); |
||
168 | $email->setFromName('Test'); |
||
169 | |||
170 | $mailer->setEmail($email); |
||
171 | |||
172 | foreach ($this->contacts as $contact) { |
||
173 | $mailer->addTo($contact['email']); |
||
174 | $mailer->setLead($contact); |
||
175 | $mailer->queue(); |
||
176 | } |
||
177 | |||
178 | $mailer->flushQueue(); |
||
179 | $from = $mailer->message->getFrom(); |
||
180 | |||
181 | $this->assertTrue(array_key_exists('[email protected]', $from)); |
||
182 | $this->assertTrue(1 === count($from)); |
||
183 | |||
184 | $mailer->reset(); |
||
185 | foreach ($this->contacts as $contact) { |
||
186 | $mailer->addTo($contact['email']); |
||
187 | $mailer->setLead($contact); |
||
188 | $mailer->queue(); |
||
189 | } |
||
190 | $mailer->flushQueue(); |
||
191 | $from = $mailer->message->getFrom(); |
||
192 | |||
193 | $this->assertTrue(array_key_exists('[email protected]', $from)); |
||
194 | $this->assertTrue(1 === count($from)); |
||
195 | } |
||
196 | |||
197 | public function testBatchMode() |
||
198 | { |
||
199 | $mockFactory = $this->getMockFactory(false); |
||
200 | |||
201 | $transport = new BatchTransport(true); |
||
202 | $swiftMailer = new \Swift_Mailer($transport); |
||
203 | |||
204 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
205 | $mailer->enableQueue(); |
||
206 | |||
207 | $email = new Email(); |
||
208 | $email->setSubject('Hello'); |
||
209 | $mailer->setEmail($email); |
||
210 | |||
211 | $mailer->addTo($this->contacts[0]['email']); |
||
212 | $mailer->setLead($this->contacts[0]); |
||
213 | $mailer->queue(); |
||
214 | $mailer->flushQueue(); |
||
215 | $errors = $mailer->getErrors(); |
||
216 | $this->assertEmpty($errors['failures'], var_export($errors, true)); |
||
217 | |||
218 | $mailer->reset(false); |
||
219 | $mailer->setEmail($email); |
||
220 | $mailer->addTo($this->contacts[1]['email']); |
||
221 | $mailer->setLead($this->contacts[1]); |
||
222 | $mailer->queue(); |
||
223 | $mailer->flushQueue(); |
||
224 | $errors = $mailer->getErrors(); |
||
225 | $this->assertEmpty($errors['failures'], var_export($errors, true)); |
||
226 | } |
||
227 | |||
228 | public function testQueuedOwnerAsMailer() |
||
229 | { |
||
230 | $mockFactory = $this->getMockFactory(); |
||
231 | |||
232 | $transport = new BatchTransport(); |
||
233 | $swiftMailer = new \Swift_Mailer($transport); |
||
234 | |||
235 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
236 | $mailer->enableQueue(); |
||
237 | |||
238 | $mailer->setSubject('Hello'); |
||
239 | |||
240 | foreach ($this->contacts as $contact) { |
||
241 | $mailer->addTo($contact['email']); |
||
242 | $mailer->setLead($contact); |
||
243 | $mailer->queue(); |
||
244 | } |
||
245 | |||
246 | $mailer->flushQueue([]); |
||
247 | |||
248 | $this->assertEmpty($mailer->getErrors()['failures']); |
||
249 | |||
250 | $fromAddresses = $transport->getFromAddresses(); |
||
251 | $metadatas = $transport->getMetadatas(); |
||
252 | |||
253 | $this->assertEquals(3, count($fromAddresses)); |
||
254 | $this->assertEquals(3, count($metadatas)); |
||
255 | $this->assertEquals(['[email protected]', '[email protected]', '[email protected]'], $fromAddresses); |
||
256 | |||
257 | foreach ($metadatas as $key => $metadata) { |
||
258 | $this->assertTrue(isset($metadata[$this->contacts[$key]['email']])); |
||
259 | |||
260 | if (0 === $key) { |
||
261 | // Should have two contacts |
||
262 | $this->assertEquals(2, count($metadata)); |
||
263 | $this->assertTrue(isset($metadata['[email protected]'])); |
||
264 | } else { |
||
265 | $this->assertEquals(1, count($metadata)); |
||
266 | } |
||
267 | |||
268 | // Check that signatures are valid |
||
269 | if (1 === $key) { |
||
270 | // There should not be a signature token because owner was not set and token events have not been dispatched |
||
271 | $this->assertFalse(isset($metadata['tokens']['{signature}'])); |
||
272 | } else { |
||
273 | $this->assertEquals($metadata[$this->contacts[$key]['email']]['tokens']['{signature}'], 'owner '.$this->contacts[$key]['owner_id']); |
||
274 | |||
275 | if (0 === $key) { |
||
276 | // Ensure the last contact has the correct signature |
||
277 | $this->assertEquals($metadata['[email protected]']['tokens']['{signature}'], 'owner '.$this->contacts[$key]['owner_id']); |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 | |||
282 | // Validate that the message object only has the contacts for the last "from" group to ensure we aren't sending duplicates |
||
283 | $this->assertEquals(['[email protected]' => null], $mailer->message->getTo()); |
||
284 | } |
||
285 | |||
286 | public function testMailAsOwnerWithEncodedCharactersInName() |
||
287 | { |
||
288 | $mockFactory = $this->getMockFactory(); |
||
289 | |||
290 | $transport = new BatchTransport(); |
||
291 | $swiftMailer = new \Swift_Mailer($transport); |
||
292 | |||
293 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body's Business']); |
||
294 | $mailer->enableQueue(); |
||
295 | |||
296 | $mailer->setSubject('Hello'); |
||
297 | |||
298 | $contacts = $this->contacts; |
||
299 | $contacts[3]['owner_id'] = 3; |
||
300 | |||
301 | foreach ($contacts as $contact) { |
||
302 | $mailer->addTo($contact['email']); |
||
303 | $mailer->setLead($contact); |
||
304 | $mailer->queue(); |
||
305 | } |
||
306 | |||
307 | $mailer->flushQueue([]); |
||
308 | |||
309 | $fromAddresses = $transport->getFromAddresses(); |
||
310 | $fromNames = $transport->getFromNames(); |
||
311 | |||
312 | $this->assertEquals(4, count($fromAddresses)); |
||
313 | $this->assertEquals(4, count($fromNames)); |
||
314 | $this->assertEquals(['[email protected]', '[email protected]', '[email protected]', '[email protected]'], $fromAddresses); |
||
315 | $this->assertEquals([null, "No Body's Business", null, "John S'mith"], $fromNames); |
||
316 | } |
||
317 | |||
318 | public function testBatchIsEnabledWithBcTokenInterface() |
||
319 | { |
||
320 | $mockFactory = $this->getMockFactory(); |
||
321 | |||
322 | $transport = new BcInterfaceTokenTransport(); |
||
323 | $swiftMailer = new \Swift_Mailer($transport); |
||
324 | |||
325 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
326 | $mailer->enableQueue(); |
||
327 | |||
328 | $mailer->setSubject('Hello'); |
||
329 | |||
330 | foreach ($this->contacts as $contact) { |
||
331 | $mailer->addTo($contact['email']); |
||
332 | $mailer->setLead($contact); |
||
333 | $mailer->queue(); |
||
334 | } |
||
335 | |||
336 | $mailer->flushQueue([]); |
||
337 | |||
338 | $this->assertEmpty($mailer->getErrors()['failures']); |
||
339 | |||
340 | $fromAddresses = $transport->getFromAddresses(); |
||
341 | $metadatas = $transport->getMetadatas(); |
||
342 | |||
343 | $this->assertEquals(3, count($fromAddresses)); |
||
344 | $this->assertEquals(3, count($metadatas)); |
||
345 | } |
||
346 | |||
347 | public function testGlobalFromThatAllFromAddressesAreTheSame() |
||
348 | { |
||
349 | $mockFactory = $this->getMockFactory(); |
||
350 | |||
351 | $transport = new BatchTransport(); |
||
352 | $swiftMailer = new \Swift_Mailer($transport); |
||
353 | |||
354 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
355 | $mailer->enableQueue(); |
||
356 | |||
357 | $mailer->setSubject('Hello'); |
||
358 | $mailer->setFrom('[email protected]'); |
||
359 | |||
360 | foreach ($this->contacts as $contact) { |
||
361 | $mailer->addTo($contact['email']); |
||
362 | $mailer->setLead($contact); |
||
363 | $mailer->queue(); |
||
364 | } |
||
365 | |||
366 | $mailer->flushQueue(); |
||
367 | |||
368 | $this->assertEmpty($mailer->getErrors()['failures']); |
||
369 | |||
370 | $fromAddresses = $transport->getFromAddresses(); |
||
371 | |||
372 | $this->assertEquals(['[email protected]'], array_unique($fromAddresses)); |
||
373 | } |
||
374 | |||
375 | public function testStandardOwnerAsMailer() |
||
376 | { |
||
377 | $mockFactory = $this->getMockFactory(); |
||
378 | |||
379 | $transport = new SmtpTransport(); |
||
380 | $swiftMailer = new \Swift_Mailer($transport); |
||
381 | |||
382 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
383 | $mailer->setBody('{signature}'); |
||
384 | |||
385 | foreach ($this->contacts as $key => $contact) { |
||
386 | $mailer->addTo($contact['email']); |
||
387 | $mailer->setLead($contact); |
||
388 | $mailer->send(); |
||
389 | |||
390 | $body = $mailer->message->getBody(); |
||
391 | $from = key($mailer->message->getFrom()); |
||
392 | |||
393 | if ($contact['owner_id']) { |
||
394 | $this->assertEquals('owner'.$contact['owner_id'].'@owner.com', $from); |
||
395 | $this->assertEquals('owner '.$contact['owner_id'], $body); |
||
396 | } else { |
||
397 | $this->assertEquals('[email protected]', $from); |
||
398 | $this->assertEquals('{signature}', $body); |
||
399 | } |
||
400 | } |
||
401 | } |
||
402 | |||
403 | public function testValidateValidEmails() |
||
404 | { |
||
405 | $helper = $this->mockEmptyMailHelper(); |
||
406 | $addresses = [ |
||
407 | '[email protected]', |
||
408 | '[email protected]', |
||
409 | '[email protected]', |
||
410 | '[email protected]', |
||
411 | '[email protected]', |
||
412 | ]; |
||
413 | |||
414 | foreach ($addresses as $address) { |
||
415 | // will throw Swift_RfcComplianceException if it will find the address invalid |
||
416 | $this->assertNull($helper::validateEmail($address)); |
||
417 | } |
||
418 | } |
||
419 | |||
420 | public function testValidateEmailWithoutTld() |
||
421 | { |
||
422 | $helper = $this->mockEmptyMailHelper(); |
||
423 | $this->expectException(\Swift_RfcComplianceException::class); |
||
424 | $helper::validateEmail('john@doe'); |
||
425 | } |
||
426 | |||
427 | public function testValidateEmailWithSpaceInIt() |
||
428 | { |
||
429 | $helper = $this->mockEmptyMailHelper(); |
||
430 | $this->expectException(\Swift_RfcComplianceException::class); |
||
431 | $helper::validateEmail('jo [email protected]'); |
||
432 | } |
||
433 | |||
434 | public function testValidateEmailWithCaretInIt() |
||
435 | { |
||
436 | $helper = $this->mockEmptyMailHelper(); |
||
437 | $this->expectException(\Swift_RfcComplianceException::class); |
||
438 | $helper::validateEmail('jo^[email protected]'); |
||
439 | } |
||
440 | |||
441 | public function testValidateEmailWithApostropheInIt() |
||
442 | { |
||
443 | $helper = $this->mockEmptyMailHelper(); |
||
444 | $this->expectException(\Swift_RfcComplianceException::class); |
||
445 | $helper::validateEmail('jo\'[email protected]'); |
||
446 | } |
||
447 | |||
448 | public function testValidateEmailWithSemicolonInIt() |
||
449 | { |
||
450 | $helper = $this->mockEmptyMailHelper(); |
||
451 | $this->expectException(\Swift_RfcComplianceException::class); |
||
452 | $helper::validateEmail('jo;[email protected]'); |
||
453 | } |
||
454 | |||
455 | public function testValidateEmailWithAmpersandInIt() |
||
456 | { |
||
457 | $helper = $this->mockEmptyMailHelper(); |
||
458 | $this->expectException(\Swift_RfcComplianceException::class); |
||
459 | $helper::validateEmail('jo&[email protected]'); |
||
460 | } |
||
461 | |||
462 | public function testValidateEmailWithStarInIt() |
||
463 | { |
||
464 | $helper = $this->mockEmptyMailHelper(); |
||
465 | $this->expectException(\Swift_RfcComplianceException::class); |
||
466 | $helper::validateEmail('jo*[email protected]'); |
||
467 | } |
||
468 | |||
469 | public function testValidateEmailWithPercentInIt() |
||
470 | { |
||
471 | $helper = $this->mockEmptyMailHelper(); |
||
472 | $this->expectException(\Swift_RfcComplianceException::class); |
||
473 | $helper::validateEmail('jo%[email protected]'); |
||
474 | } |
||
475 | |||
476 | public function testQueueModeIsReset() |
||
477 | { |
||
478 | $contacts = $this->contacts; |
||
479 | $contacts[] = [ |
||
480 | 'id' => 5, |
||
481 | 'email' => '[email protected]', |
||
482 | 'firstname' => 'Contact', |
||
483 | 'lastname' => '5', |
||
484 | 'owner_id' => 1, |
||
485 | ]; |
||
486 | |||
487 | $helper = $this->mockEmptyMailHelper(false); |
||
488 | |||
489 | $helper->enableQueue(); |
||
490 | |||
491 | $helper->addTo($contacts[0]['email']); |
||
492 | $helper->addTo($contacts[1]['email']); |
||
493 | $helper->addTo($contacts[2]['email']); |
||
494 | $helper->addTo($contacts[3]['email']); |
||
495 | |||
496 | $exceptionCaught = true; |
||
497 | try { |
||
498 | $helper->addTo($contacts[4]['email']); |
||
499 | $exceptionCaught = false; |
||
500 | } catch (BatchQueueMaxException $exception) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
501 | } |
||
502 | |||
503 | if (!$exceptionCaught) { |
||
504 | $this->fail('BatchQueueMaxException should have been thrown'); |
||
505 | } |
||
506 | |||
507 | // Reset which should now reset qeue mode so that each to address is accepted |
||
508 | $helper->reset(); |
||
509 | |||
510 | try { |
||
511 | foreach ($contacts as $contact) { |
||
512 | $helper->addTo($contact['email']); |
||
513 | } |
||
514 | } catch (BatchQueueMaxException $exception) { |
||
515 | $this->fail('Queue mode was not reset'); |
||
516 | } |
||
517 | |||
518 | $to = $helper->message->getTo(); |
||
519 | |||
520 | $this->assertEquals( |
||
521 | [ |
||
522 | '[email protected]' => null, |
||
523 | '[email protected]' => null, |
||
524 | '[email protected]' => null, |
||
525 | '[email protected]' => null, |
||
526 | '[email protected]' => null, |
||
527 | ], |
||
528 | $to |
||
529 | ); |
||
530 | } |
||
531 | |||
532 | public function testGlobalHeadersAreSet() |
||
533 | { |
||
534 | $parameterMap = [ |
||
535 | ['mailer_custom_headers', [], ['X-Mautic-Test' => 'test', 'X-Mautic-Test2' => 'test']], |
||
536 | ]; |
||
537 | $mockFactory = $this->getMockFactory(true, $parameterMap); |
||
538 | |||
539 | $transport = new SmtpTransport(); |
||
540 | $swiftMailer = new \Swift_Mailer($transport); |
||
541 | |||
542 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
543 | $mailer->setBody('{signature}'); |
||
544 | $mailer->addTo($this->contacts[0]['email']); |
||
545 | $mailer->send(); |
||
546 | |||
547 | $customHeadersFounds = []; |
||
548 | |||
549 | /** @var \Swift_Mime_Headers_ParameterizedHeader[] $headers */ |
||
550 | $headers = $mailer->message->getHeaders()->getAll(); |
||
551 | foreach ($headers as $header) { |
||
552 | if (false !== strpos($header->getFieldName(), 'X-Mautic-Test')) { |
||
553 | $customHeadersFounds[] = $header->getFieldName(); |
||
554 | |||
555 | $this->assertEquals('test', $header->getValue()); |
||
556 | } |
||
557 | } |
||
558 | |||
559 | $this->assertCount(2, $customHeadersFounds); |
||
560 | } |
||
561 | |||
562 | public function testGlobalHeadersAreIgnoredIfEmailEntityIsSet() |
||
563 | { |
||
564 | $parameterMap = [ |
||
565 | ['mailer_custom_headers', [], ['X-Mautic-Test' => 'test', 'X-Mautic-Test2' => 'test']], |
||
566 | ]; |
||
567 | $mockFactory = $this->getMockFactory(true, $parameterMap); |
||
568 | |||
569 | $transport = new SmtpTransport(); |
||
570 | $swiftMailer = new \Swift_Mailer($transport); |
||
571 | |||
572 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
573 | $mailer->addTo($this->contacts[0]['email']); |
||
574 | |||
575 | $email = new Email(); |
||
576 | $email->setSubject('Test'); |
||
577 | $email->setCustomHtml('{signature}'); |
||
578 | $mailer->setEmail($email); |
||
579 | $mailer->send(); |
||
580 | |||
581 | /** @var \Swift_Mime_Headers_ParameterizedHeader[] $headers */ |
||
582 | $headers = $mailer->message->getHeaders()->getAll(); |
||
583 | foreach ($headers as $header) { |
||
584 | $this->assertFalse(strpos($header->getFieldName(), 'X-Mautic-Test'), 'System headers were not supposed to be set'); |
||
585 | } |
||
586 | } |
||
587 | |||
588 | public function testEmailHeadersAreSet() |
||
589 | { |
||
590 | $parameterMap = [ |
||
591 | ['mailer_custom_headers', [], ['X-Mautic-Test' => 'test', 'X-Mautic-Test2' => 'test']], |
||
592 | ]; |
||
593 | $mockFactory = $this->getMockFactory(true, $parameterMap); |
||
594 | |||
595 | $transport = new SmtpTransport(); |
||
596 | $swiftMailer = new \Swift_Mailer($transport); |
||
597 | |||
598 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
599 | $mailer->addTo($this->contacts[0]['email']); |
||
600 | |||
601 | $email = new Email(); |
||
602 | $email->setSubject('Test'); |
||
603 | $email->setCustomHtml('{signature}'); |
||
604 | $email->setHeaders(['X-Mautic-Test3' => 'test2', 'X-Mautic-Test4' => 'test2']); |
||
605 | $mailer->setEmail($email); |
||
606 | $mailer->send(); |
||
607 | |||
608 | $customHeadersFounds = []; |
||
609 | |||
610 | /** @var \Swift_Mime_Headers_ParameterizedHeader[] $headers */ |
||
611 | $headers = $mailer->message->getHeaders()->getAll(); |
||
612 | foreach ($headers as $header) { |
||
613 | if (false !== strpos($header->getFieldName(), 'X-Mautic-Test')) { |
||
614 | $customHeadersFounds[] = $header->getFieldName(); |
||
615 | |||
616 | $this->assertEquals('test2', $header->getValue()); |
||
617 | } |
||
618 | } |
||
619 | |||
620 | $this->assertCount(2, $customHeadersFounds); |
||
621 | } |
||
622 | |||
623 | protected function mockEmptyMailHelper($useSmtp = true) |
||
624 | { |
||
625 | $mockFactory = $this->getMockFactory(); |
||
626 | $transport = ($useSmtp) ? new SmtpTransport() : new BatchTransport(); |
||
627 | $swiftMailer = new \Swift_Mailer($transport); |
||
628 | |||
629 | return new MailHelper($mockFactory, $swiftMailer); |
||
630 | } |
||
631 | |||
632 | protected function getMockFactory($mailIsOwner = true, $parameterMap = []) |
||
633 | { |
||
634 | $mockLeadRepository = $this->getMockBuilder(LeadRepository::class) |
||
635 | ->disableOriginalConstructor() |
||
636 | ->getMock(); |
||
637 | |||
638 | $mockLeadRepository->method('getLeadOwner') |
||
639 | ->will( |
||
640 | $this->returnValueMap( |
||
641 | [ |
||
642 | [1, ['id' => 1, 'email' => '[email protected]', 'first_name' => '', 'last_name' => '', 'signature' => 'owner 1']], |
||
643 | [2, ['id' => 2, 'email' => '[email protected]', 'first_name' => '', 'last_name' => '', 'signature' => 'owner 2']], |
||
644 | [3, ['id' => 3, 'email' => '[email protected]', 'first_name' => 'John', 'last_name' => 'S'mith', 'signature' => 'owner 2']], |
||
645 | ] |
||
646 | ) |
||
647 | ); |
||
648 | |||
649 | $mockLeadModel = $this->getMockBuilder(LeadModel::class) |
||
650 | ->disableOriginalConstructor() |
||
651 | ->getMock(); |
||
652 | |||
653 | $mockLeadModel->method('getRepository') |
||
654 | ->willReturn($mockLeadRepository); |
||
655 | |||
656 | $mockFactory = $this->getMockBuilder(MauticFactory::class) |
||
657 | ->disableOriginalConstructor() |
||
658 | ->getMock(); |
||
659 | |||
660 | $parameterMap = array_merge( |
||
661 | [ |
||
662 | ['mailer_return_path', false, null], |
||
663 | ['mailer_spool_type', false, 'memory'], |
||
664 | ['mailer_is_owner', false, $mailIsOwner], |
||
665 | ], |
||
666 | $parameterMap |
||
667 | ); |
||
668 | |||
669 | $mockFactory->method('getParameter') |
||
670 | ->will( |
||
671 | $this->returnValueMap($parameterMap) |
||
672 | ); |
||
673 | $mockFactory->method('getModel') |
||
674 | ->will( |
||
675 | $this->returnValueMap( |
||
676 | [ |
||
677 | ['lead', $mockLeadModel], |
||
678 | ] |
||
679 | ) |
||
680 | ); |
||
681 | |||
682 | $mockLogger = $this->getMockBuilder(Logger::class) |
||
683 | ->disableOriginalConstructor() |
||
684 | ->getMock(); |
||
685 | $mockFactory->method('getLogger') |
||
686 | ->willReturn($mockLogger); |
||
687 | |||
688 | $mockMailboxHelper = $this->getMockBuilder(Mailbox::class) |
||
689 | ->disableOriginalConstructor() |
||
690 | ->getMock(); |
||
691 | $mockMailboxHelper->method('isConfigured') |
||
692 | ->willReturn(false); |
||
693 | |||
694 | $mockFactory->method('getHelper') |
||
695 | ->will( |
||
696 | $this->returnValueMap( |
||
697 | [ |
||
698 | ['mailbox', $mockMailboxHelper], |
||
699 | ] |
||
700 | ) |
||
701 | ); |
||
702 | |||
703 | return $mockFactory; |
||
704 | } |
||
705 | |||
706 | public function testArrayOfAddressesAreRemappedIntoEmailToNameKeyValuePair() |
||
707 | { |
||
708 | $mockFactory = $this->getMockBuilder(MauticFactory::class) |
||
709 | ->disableOriginalConstructor() |
||
710 | ->getMock(); |
||
711 | $mockFactory->method('getParameter') |
||
712 | ->will( |
||
713 | $this->returnValueMap( |
||
714 | [ |
||
715 | ['mailer_return_path', false, null], |
||
716 | ['mailer_spool_type', false, 'memory'], |
||
717 | ] |
||
718 | ) |
||
719 | ); |
||
720 | |||
721 | $swiftMailer = new \Swift_Mailer(new SmtpTransport()); |
||
722 | |||
723 | $mailer = new MailHelper($mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
724 | |||
725 | $mailer->setTo(['[email protected]', '[email protected]'], 'test'); |
||
726 | |||
727 | $this->assertEquals( |
||
728 | [ |
||
729 | '[email protected]' => 'test', |
||
730 | '[email protected]' => 'test', |
||
731 | ], |
||
732 | $mailer->message->getTo() |
||
733 | ); |
||
734 | } |
||
735 | |||
736 | public function testThatTokenizationIsEnabledIfTransportSupportsTokenization() |
||
737 | { |
||
738 | $swiftMailer = new \Swift_Mailer($this->spoolTransport); |
||
739 | $this->delegatingSpool->expects($this->once()) |
||
740 | ->method('isTokenizationEnabled') |
||
741 | ->willReturn(true); |
||
742 | |||
743 | $mailer = new MailHelper($this->mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
744 | $this->assertTrue($mailer->inTokenizationMode()); |
||
745 | } |
||
746 | |||
747 | public function testThatTokenizationIsDisabledIfTransportDoesnotSupportTokenization() |
||
748 | { |
||
749 | $swiftMailer = new \Swift_Mailer($this->spoolTransport); |
||
750 | $this->delegatingSpool->expects($this->once()) |
||
751 | ->method('isTokenizationEnabled') |
||
752 | ->willReturn(false); |
||
753 | |||
754 | $mailer = new MailHelper($this->mockFactory, $swiftMailer, ['[email protected]' => 'No Body']); |
||
755 | $this->assertFalse($mailer->inTokenizationMode()); |
||
756 | } |
||
757 | } |
||
758 |