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