|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests\Email; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Email\Email; |
|
6
|
|
|
use SilverStripe\Control\Email\SwiftPlugin; |
|
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
8
|
|
|
|
|
9
|
|
|
class SwiftPluginTest extends SapphireTest |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
public function setUp() |
|
13
|
|
|
{ |
|
14
|
|
|
parent::setUp(); |
|
15
|
|
|
|
|
16
|
|
|
//clean the config |
|
17
|
|
|
Email::config()->remove('send_all_emails_to'); |
|
18
|
|
|
Email::config()->remove('cc_all_emails_to'); |
|
19
|
|
|
Email::config()->remove('bcc_all_emails_to'); |
|
20
|
|
|
Email::config()->remove('send_all_emails_from'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected function getEmail() |
|
24
|
|
|
{ |
|
25
|
|
|
return (new Email()) |
|
26
|
|
|
->setTo('[email protected]') |
|
27
|
|
|
->setCC('[email protected]') |
|
28
|
|
|
->setBCC('[email protected]') |
|
29
|
|
|
->setFrom('[email protected]'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function getMailer() |
|
33
|
|
|
{ |
|
34
|
|
|
$mailer = new \Swift_Mailer(new \Swift_NullTransport()); |
|
35
|
|
|
$mailer->registerPlugin(new SwiftPlugin()); |
|
36
|
|
|
|
|
37
|
|
|
return $mailer; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testSendAllEmailsTo() |
|
41
|
|
|
{ |
|
42
|
|
|
Email::config()->update('send_all_emails_to', '[email protected]'); |
|
43
|
|
|
$email = $this->getEmail(); |
|
44
|
|
|
$this->getMailer()->send($email->getSwiftMessage()); |
|
45
|
|
|
$headers = $email->getSwiftMessage()->getHeaders(); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertCount(1, $email->getTo()); |
|
48
|
|
|
$this->assertContains('[email protected]', array_keys($email->getTo())); |
|
49
|
|
|
$this->assertCount(1, $email->getFrom()); |
|
50
|
|
|
$this->assertContains('[email protected]', array_keys($email->getFrom())); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertTrue($headers->has('X-Original-To')); |
|
53
|
|
|
$this->assertTrue($headers->has('X-Original-Cc')); |
|
54
|
|
|
$this->assertTrue($headers->has('X-Original-Bcc')); |
|
55
|
|
|
$this->assertFalse($headers->has('X-Original-From')); |
|
56
|
|
|
|
|
57
|
|
|
$originalTo = array_keys($headers->get('X-Original-To')->getFieldBodyModel()); |
|
58
|
|
|
$originalCc = array_keys($headers->get('X-Original-Cc')->getFieldBodyModel()); |
|
59
|
|
|
$originalBcc = array_keys($headers->get('X-Original-Bcc')->getFieldBodyModel()); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertCount(1, $originalTo); |
|
62
|
|
|
$this->assertContains('[email protected]', $originalTo); |
|
63
|
|
|
$this->assertCount(1, $originalCc); |
|
64
|
|
|
$this->assertContains('[email protected]', $originalCc); |
|
65
|
|
|
$this->assertCount(1, $originalBcc); |
|
66
|
|
|
$this->assertContains('[email protected]', $originalBcc); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testSendAllEmailsFrom() |
|
70
|
|
|
{ |
|
71
|
|
|
Email::config()->update('send_all_emails_from', '[email protected]'); |
|
72
|
|
|
$email = $this->getEmail(); |
|
73
|
|
|
$this->getMailer()->send($email->getSwiftMessage()); |
|
74
|
|
|
|
|
75
|
|
|
$headers = $email->getSwiftMessage()->getHeaders(); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertFalse($headers->has('X-Original-To')); |
|
78
|
|
|
$this->assertFalse($headers->has('X-Original-Cc')); |
|
79
|
|
|
$this->assertFalse($headers->has('X-Original-Bcc')); |
|
80
|
|
|
$this->assertTrue($headers->has('X-Original-From')); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertCount(1, $email->getFrom()); |
|
83
|
|
|
$this->assertContains('[email protected]', array_keys($email->getFrom())); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertCount(1, $headers->get('X-Original-From')->getFieldBodyModel()); |
|
86
|
|
|
$this->assertContains('[email protected]', array_keys($headers->get('X-Original-From')->getFieldBodyModel())); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testCCAllEmailsTo() |
|
90
|
|
|
{ |
|
91
|
|
|
Email::config()->update('cc_all_emails_to', '[email protected]'); |
|
92
|
|
|
$email = $this->getEmail(); |
|
93
|
|
|
$this->getMailer()->send($email->getSwiftMessage()); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertCount(2, $email->getCC()); |
|
96
|
|
|
$this->assertContains('[email protected]', array_keys($email->getCC())); |
|
97
|
|
|
$this->assertContains('[email protected]', array_keys($email->getCC())); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testBCCAllEmailsTo() |
|
101
|
|
|
{ |
|
102
|
|
|
Email::config()->update('bcc_all_emails_to', '[email protected]'); |
|
103
|
|
|
$email = $this->getEmail(); |
|
104
|
|
|
$this->getMailer()->send($email->getSwiftMessage()); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertCount(2, $email->getBCC()); |
|
107
|
|
|
$this->assertContains('[email protected]', array_keys($email->getBCC())); |
|
108
|
|
|
$this->assertContains('[email protected]', array_keys($email->getBCC())); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|