-
Notifications
You must be signed in to change notification settings - Fork 823
/
Copy pathEmailTest.php
224 lines (196 loc) · 5.95 KB
/
EmailTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* @package framework
* @subpackage tests
*/
class EmailTest extends SapphireTest {
public function testAttachFiles() {
$email = new Email();
$email->attachFileFromString('foo bar', 'foo.txt', 'text/plain');
$email->attachFile(__DIR__ . '/fixtures/attachment.txt', null, 'text/plain');
$this->assertEquals(
array('contents'=>'foo bar', 'filename'=>'foo.txt', 'mimetype'=>'text/plain'),
$email->attachments[0],
'File is attached correctly from string'
);
$this->assertEquals(
array('contents'=>'Hello, I\'m a text document.', 'filename'=>'attachment.txt', 'mimetype'=>'text/plain'),
$email->attachments[1],
'File is attached correctly from file'
);
}
public function testCustomHeaders() {
$email = new Email();
$email->addCustomHeader('Cc', '[email protected]');
$email->addCustomHeader('Bcc', '[email protected]');
$this->assertEmpty(
$email->customHeaders,
'addCustomHeader() doesn\'t add Cc and Bcc headers'
);
$email->addCustomHeader('Reply-To', '[email protected]');
$this->assertEquals(
array('Reply-To' => '[email protected]'),
$email->customHeaders,
'addCustomHeader() adds headers'
);
$email->addCustomHeader('Reply-To', '[email protected]');
$this->assertEquals(
array('Reply-To' => '[email protected], [email protected]'),
$email->customHeaders,
'addCustomHeader() appends data to existing headers'
);
}
public function testValidEmailAddress() {
$validEmails = array('[email protected]', '[email protected]');
$invalidEmails = array('foo.bar@', '@example.com', 'foo@');
foreach ($validEmails as $email) {
$this->assertTrue(
Email::is_valid_address($email),
'is_valid_address() returns true for a valid email address'
);
}
foreach ($invalidEmails as $email) {
$this->assertFalse(
Email::is_valid_address($email),
'is_valid_address() returns false for an invalid email address'
);
}
}
public function testObfuscate() {
$emailAddress = '[email protected]';
$direction = Email::obfuscate($emailAddress, 'direction');
$visible = Email::obfuscate($emailAddress, 'visible');
$hex = Email::obfuscate($emailAddress, 'hex');
$this->assertEquals(
'<span class="codedirection">moc.elpmaxe@1-tset</span>',
$direction,
'obfuscate() correctly reverses the email direction'
);
$this->assertEquals(
'test [dash] 1 [at] example [dot] com',
$visible,
'obfuscate() correctly obfuscates email characters'
);
$this->assertEquals(
'test-1@examp'
. 'le.com',
$hex,
'obfuscate() correctly returns hex representation of email'
);
}
public function testSendPlain() {
// Set custom $project - used in email headers
global $project;
$oldProject = $project;
$project = 'emailtest';
Injector::inst()->registerService(new EmailTest_Mailer(), 'Mailer');
$email = new Email(
'Test send plain',
'Testing Email->sendPlain()',
null,
);
$email->attachFile(__DIR__ . '/fixtures/attachment.txt', null, 'text/plain');
$email->addCustomHeader('foo', 'bar');
$sent = $email->sendPlain(123);
// Restore old project name after sending
$project = $oldProject;
$this->assertEquals('[email protected]', $sent['to']);
$this->assertEquals('[email protected]', $sent['from']);
$this->assertEquals('Test send plain', $sent['subject']);
$this->assertEquals('Testing Email->sendPlain()', $sent['content']);
$this->assertEquals(
array(
0 => array(
'contents'=>'Hello, I\'m a text document.',
'filename'=>'attachment.txt',
'mimetype'=>'text/plain'
)
),
$sent['files']
);
$this->assertEquals(
array(
'foo' => 'bar',
'X-SilverStripeMessageID' => 'emailtest.123',
'X-SilverStripeSite' => 'emailtest',
'Cc' => '[email protected]',
'Bcc' => '[email protected]'
),
$sent['customheaders']
);
}
public function testSendHTML() {
// Set custom $project - used in email headers
global $project;
$oldProject = $project;
$project = 'emailtest';
Injector::inst()->registerService(new EmailTest_Mailer(), 'Mailer');
$email = new Email(
'Test send plain',
'Testing Email->sendPlain()',
null,
);
$email->attachFile(__DIR__ . '/fixtures/attachment.txt', null, 'text/plain');
$email->addCustomHeader('foo', 'bar');
$sent = $email->send(123);
// Restore old project name after sending
$project = $oldProject;
$this->assertEquals('[email protected]', $sent['to']);
$this->assertEquals('[email protected]', $sent['from']);
$this->assertEquals('Test send plain', $sent['subject']);
$this->assertContains('Testing Email->sendPlain()', $sent['content']);
$this->assertNull($sent['plaincontent']);
$this->assertEquals(
array(
0 => array(
'contents'=>'Hello, I\'m a text document.',
'filename'=>'attachment.txt',
'mimetype'=>'text/plain'
)
),
$sent['files']
);
$this->assertEquals(
array(
'foo' => 'bar',
'X-SilverStripeMessageID' => 'emailtest.123',
'X-SilverStripeSite' => 'emailtest',
'Cc' => '[email protected]',
'Bcc' => '[email protected]'
),
$sent['customheaders']
);
}
}
class EmailTest_Mailer extends Mailer {
public function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false,
$plainContent = false) {
return array(
'to' => $to,
'from' => $from,
'subject' => $subject,
'content' => $htmlContent,
'files' => $attachedFiles,
'customheaders' => $customheaders,
'plaincontent' => $plainContent
);
}
public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customheaders = false) {
return array(
'to' => $to,
'from' => $from,
'subject' => $subject,
'content' => $plainContent,
'files' => $attachedFiles,
'customheaders' => $customheaders
);
}
}