1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Control\Tests\Email; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
7
|
|
|
use SilverStripe\Control\Director; |
8
|
|
|
use SilverStripe\Control\Email\Email; |
9
|
|
|
use SilverStripe\Control\Email\Mailer; |
10
|
|
|
use SilverStripe\Control\Email\SwiftMailer; |
11
|
|
|
use SilverStripe\Control\Tests\Email\EmailTest\EmailSubClass; |
12
|
|
|
use SilverStripe\Core\Injector\Injector; |
13
|
|
|
use SilverStripe\Core\Manifest\ModuleResourceLoader; |
14
|
|
|
use SilverStripe\Dev\SapphireTest; |
15
|
|
|
use SilverStripe\Dev\TestMailer; |
16
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
17
|
|
|
use SilverStripe\Security\Member; |
18
|
|
|
use SilverStripe\View\SSViewer; |
19
|
|
|
use Swift_Attachment; |
20
|
|
|
use Swift_Mailer; |
21
|
|
|
use Swift_Message; |
22
|
|
|
use Swift_NullTransport; |
23
|
|
|
use Swift_RfcComplianceException; |
24
|
|
|
|
25
|
|
|
class EmailTest extends SapphireTest |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
protected function setUp() |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
// This is done so that the Director::host() and thus Email::getDefaultFrom() |
32
|
|
|
// have a host within a CI context |
33
|
|
|
Director::config()->set('alternate_base_url', 'http://www.mysite.com/'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testAddAttachment() |
37
|
|
|
{ |
38
|
|
|
$email = new Email(); |
39
|
|
|
|
40
|
|
|
$email->addAttachment(__DIR__ . '/EmailTest/attachment.txt', null, 'text/plain'); |
41
|
|
|
|
42
|
|
|
$children = $email->getSwiftMessage()->getChildren(); |
43
|
|
|
$this->assertCount(1, $children); |
44
|
|
|
|
45
|
|
|
/** @var Swift_Attachment $child */ |
46
|
|
|
$child = reset($children); |
47
|
|
|
|
48
|
|
|
$this->assertInstanceOf(Swift_Attachment::class, $child); |
49
|
|
|
$this->assertEquals('text/plain', $child->getContentType()); |
50
|
|
|
$this->assertEquals('attachment.txt', $child->getFilename()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testAddAttachmentFromData() |
54
|
|
|
{ |
55
|
|
|
$email = new Email(); |
56
|
|
|
|
57
|
|
|
$email->addAttachmentFromData('foo bar', 'foo.txt', 'text/plain'); |
58
|
|
|
$children = $email->getSwiftMessage()->getChildren(); |
59
|
|
|
|
60
|
|
|
$this->assertCount(1, $children); |
61
|
|
|
|
62
|
|
|
/** @var Swift_Attachment $child */ |
63
|
|
|
$child = reset($children); |
64
|
|
|
|
65
|
|
|
$this->assertInstanceOf(Swift_Attachment::class, $child); |
66
|
|
|
$this->assertEquals('foo bar', $child->getBody()); |
67
|
|
|
$this->assertEquals('text/plain', $child->getContentType()); |
68
|
|
|
$this->assertEquals('foo.txt', $child->getFilename()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @dataProvider provideValidEmailAddresses |
73
|
|
|
*/ |
74
|
|
|
public function testValidEmailAddress($email) |
75
|
|
|
{ |
76
|
|
|
$this->assertTrue(Email::is_valid_address($email)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @dataProvider provideInvalidEmailAddresses |
81
|
|
|
*/ |
82
|
|
|
public function testInvalidEmailAddress($email) |
83
|
|
|
{ |
84
|
|
|
$this->assertFalse(Email::is_valid_address($email)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function provideValidEmailAddresses() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
['[email protected]', '[email protected]'], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function provideInvalidEmailAddresses() |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
['foo.bar@', '@example.com', 'foo@'], |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testObfuscate() |
102
|
|
|
{ |
103
|
|
|
$emailAddress = '[email protected]'; |
104
|
|
|
|
105
|
|
|
$direction = Email::obfuscate($emailAddress, 'direction'); |
106
|
|
|
$visible = Email::obfuscate($emailAddress, 'visible'); |
107
|
|
|
$hex = Email::obfuscate($emailAddress, 'hex'); |
108
|
|
|
|
109
|
|
|
$this->assertEquals('<span class="codedirection">moc.elpmaxe@1-tset</span>', $direction); |
110
|
|
|
$this->assertEquals('test [dash] 1 [at] example [dot] com', $visible); |
111
|
|
|
$this->assertEquals( |
112
|
|
|
'test-1@example.com', |
113
|
|
|
$hex |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testSendPlain() |
118
|
|
|
{ |
119
|
|
|
$email = $this->makeEmailMock('Test send plain'); |
120
|
|
|
|
121
|
|
|
// email should not call render if a body is supplied |
122
|
|
|
$email->expects($this->never())->method('renderWith'); |
123
|
|
|
$successful = $email->sendPlain(); |
124
|
|
|
|
125
|
|
|
$this->assertTrue($successful); |
126
|
|
|
$this->assertEmpty($email->getFailedRecipients()); |
127
|
|
|
|
128
|
|
|
/** @var TestMailer $mailer */ |
129
|
|
|
$mailer = Injector::inst()->get(Mailer::class); |
130
|
|
|
$sentMail = $mailer->findEmail('[email protected]'); |
131
|
|
|
|
132
|
|
|
$this->assertTrue(is_array($sentMail)); |
133
|
|
|
|
134
|
|
|
$this->assertEquals('[email protected]', $sentMail['To']); |
135
|
|
|
$this->assertEquals('[email protected]', $sentMail['From']); |
136
|
|
|
$this->assertEquals('Test send plain', $sentMail['Subject']); |
137
|
|
|
$this->assertEquals('Body for Test send plain', $sentMail['Content']); |
138
|
|
|
|
139
|
|
|
$this->assertCount(1, $sentMail['AttachedFiles']); |
140
|
|
|
$child = reset($sentMail['AttachedFiles']); |
141
|
|
|
$this->assertEquals('text/plain', $child['mimetype']); |
142
|
|
|
$this->assertEquals('attachment.txt', $child['filename']); |
143
|
|
|
$this->assertEquals('Hello, I\'m a text document.', $child['contents']); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testSend() |
147
|
|
|
{ |
148
|
|
|
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */ |
149
|
|
|
$email = $this->makeEmailMock('Test send HTML'); |
150
|
|
|
|
151
|
|
|
// email should not call render if a body is supplied |
152
|
|
|
$email->expects($this->never())->method('renderWith'); |
153
|
|
|
$successful = $email->send(); |
154
|
|
|
|
155
|
|
|
$this->assertTrue($successful); |
156
|
|
|
$this->assertEmpty($email->getFailedRecipients()); |
157
|
|
|
|
158
|
|
|
/** @var TestMailer $mailer */ |
159
|
|
|
$mailer = Injector::inst()->get(Mailer::class); |
160
|
|
|
$sentMail = $mailer->findEmail('[email protected]'); |
161
|
|
|
|
162
|
|
|
$this->assertTrue(is_array($sentMail)); |
163
|
|
|
|
164
|
|
|
$this->assertEquals('[email protected]', $sentMail['To']); |
165
|
|
|
$this->assertEquals('[email protected]', $sentMail['From']); |
166
|
|
|
$this->assertEquals('Test send HTML', $sentMail['Subject']); |
167
|
|
|
$this->assertEquals('Body for Test send HTML', $sentMail['Content']); |
168
|
|
|
|
169
|
|
|
$this->assertCount(1, $sentMail['AttachedFiles']); |
170
|
|
|
$child = reset($sentMail['AttachedFiles']); |
171
|
|
|
$this->assertEquals('text/plain', $child['mimetype']); |
172
|
|
|
$this->assertEquals('attachment.txt', $child['filename']); |
173
|
|
|
$this->assertEquals('Hello, I\'m a text document.', $child['contents']); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testRenderedSend() |
177
|
|
|
{ |
178
|
|
|
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */ |
179
|
|
|
$email = $this->getMockBuilder(Email::class) |
180
|
|
|
->enableProxyingToOriginalMethods() |
181
|
|
|
->getMock(); |
182
|
|
|
$email->setFrom('[email protected]'); |
183
|
|
|
$email->setTo('[email protected]'); |
184
|
|
|
$email->setData([ |
185
|
|
|
'EmailContent' => 'test', |
186
|
|
|
]); |
187
|
|
|
$this->assertFalse($email->hasPlainPart()); |
188
|
|
|
$this->assertEmpty($email->getBody()); |
189
|
|
|
// these seem to fail for some reason :/ |
190
|
|
|
//$email->expects($this->once())->method('render'); |
191
|
|
|
//$email->expects($this->once())->method('generatePlainPartFromBody'); |
192
|
|
|
$email->send(); |
193
|
|
|
$this->assertTrue($email->hasPlainPart()); |
194
|
|
|
$this->assertNotEmpty($email->getBody()); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testRenderedSendSubclass() |
198
|
|
|
{ |
199
|
|
|
// Include dev theme |
200
|
|
|
SSViewer::set_themes([ |
201
|
|
|
'silverstripe/framework:/tests/php/Control/Email/EmailTest', |
202
|
|
|
'$default', |
203
|
|
|
]); |
204
|
|
|
|
205
|
|
|
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */ |
206
|
|
|
$email = $this->getMockBuilder(EmailSubClass::class) |
207
|
|
|
->enableProxyingToOriginalMethods() |
208
|
|
|
->getMock(); |
209
|
|
|
$email->setFrom('[email protected]'); |
210
|
|
|
$email->setTo('[email protected]'); |
211
|
|
|
$email->setData([ |
212
|
|
|
'EmailContent' => 'test', |
213
|
|
|
]); |
214
|
|
|
$this->assertFalse($email->hasPlainPart()); |
215
|
|
|
$this->assertEmpty($email->getBody()); |
216
|
|
|
$email->send(); |
217
|
|
|
$this->assertTrue($email->hasPlainPart()); |
218
|
|
|
$this->assertNotEmpty($email->getBody()); |
219
|
|
|
$this->assertContains('<h1>Email Sub-class</h1>', $email->getBody()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function testConsturctor() |
223
|
|
|
{ |
224
|
|
|
$email = new Email( |
225
|
|
|
'[email protected]', |
226
|
|
|
'[email protected]', |
227
|
|
|
'subject', |
228
|
|
|
'body', |
229
|
|
|
'[email protected]', |
230
|
|
|
'[email protected]', |
231
|
|
|
'[email protected]' |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
$this->assertCount(1, $email->getFrom()); |
235
|
|
|
$this->assertContains('[email protected]', array_keys($email->getFrom())); |
236
|
|
|
$this->assertCount(1, $email->getTo()); |
237
|
|
|
$this->assertContains('[email protected]', array_keys($email->getTo())); |
238
|
|
|
$this->assertEquals('subject', $email->getSubject()); |
239
|
|
|
$this->assertEquals('body', $email->getBody()); |
240
|
|
|
$this->assertCount(1, $email->getCC()); |
241
|
|
|
$this->assertContains('[email protected]', array_keys($email->getCC())); |
242
|
|
|
$this->assertCount(1, $email->getBCC()); |
243
|
|
|
$this->assertContains('[email protected]', array_keys($email->getBCC())); |
244
|
|
|
$this->assertEquals('[email protected]', $email->getReturnPath()); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function testGetSwiftMessage() |
248
|
|
|
{ |
249
|
|
|
$email = new Email( |
250
|
|
|
'[email protected]', |
251
|
|
|
'[email protected]', |
252
|
|
|
'subject', |
253
|
|
|
'body', |
254
|
|
|
'[email protected]', |
255
|
|
|
'[email protected]', |
256
|
|
|
'[email protected]' |
257
|
|
|
); |
258
|
|
|
$swiftMessage = $email->getSwiftMessage(); |
259
|
|
|
|
260
|
|
|
$this->assertInstanceOf(Swift_Message::class, $swiftMessage); |
261
|
|
|
|
262
|
|
|
$this->assertCount(1, $swiftMessage->getFrom()); |
263
|
|
|
$this->assertContains('[email protected]', array_keys($swiftMessage->getFrom())); |
|
|
|
|
264
|
|
|
$this->assertCount(1, $swiftMessage->getTo()); |
265
|
|
|
$this->assertContains('[email protected]', array_keys($swiftMessage->getTo())); |
266
|
|
|
$this->assertEquals('subject', $swiftMessage->getSubject()); |
267
|
|
|
$this->assertEquals('body', $swiftMessage->getBody()); |
268
|
|
|
$this->assertCount(1, $swiftMessage->getCC()); |
269
|
|
|
$this->assertContains('[email protected]', array_keys($swiftMessage->getCc())); |
270
|
|
|
$this->assertCount(1, $swiftMessage->getBCC()); |
271
|
|
|
$this->assertContains('[email protected]', array_keys($swiftMessage->getBcc())); |
272
|
|
|
$this->assertEquals('[email protected]', $swiftMessage->getReturnPath()); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function testSetSwiftMessage() |
276
|
|
|
{ |
277
|
|
|
Email::config()->update('admin_email', '[email protected]'); |
278
|
|
|
DBDatetime::set_mock_now('2017-01-01 07:00:00'); |
279
|
|
|
$email = new Email(); |
280
|
|
|
$swiftMessage = new Swift_Message(); |
281
|
|
|
$email->setSwiftMessage($swiftMessage); |
282
|
|
|
$dateTime = new DateTime(); |
283
|
|
|
$dateTime->setTimestamp(DBDatetime::now()->getTimestamp()); |
284
|
|
|
$email->getSwiftMessage()->setDate($dateTime); |
285
|
|
|
$this->assertCount(1, $email->getFrom()); |
286
|
|
|
$this->assertContains('[email protected]', array_keys($swiftMessage->getFrom())); |
|
|
|
|
287
|
|
|
$this->assertEquals(strtotime('2017-01-01 07:00:00'), $swiftMessage->getDate()->getTimestamp()); |
288
|
|
|
$this->assertEquals($swiftMessage, $email->getSwiftMessage()); |
289
|
|
|
|
290
|
|
|
// check from field is retained |
291
|
|
|
$swiftMessage = new Swift_Message(); |
292
|
|
|
$swiftMessage->setFrom('[email protected]'); |
293
|
|
|
$email->setSwiftMessage($swiftMessage); |
294
|
|
|
$this->assertCount(1, $email->getFrom()); |
295
|
|
|
$this->assertContains('[email protected]', array_keys($email->getFrom())); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
public function testAdminEmailApplied() |
299
|
|
|
{ |
300
|
|
|
Email::config()->update('admin_email', '[email protected]'); |
301
|
|
|
$email = new Email(); |
302
|
|
|
|
303
|
|
|
$this->assertCount(1, $email->getFrom()); |
304
|
|
|
$this->assertContains('[email protected]', array_keys($email->getFrom())); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function testGetFrom() |
308
|
|
|
{ |
309
|
|
|
$email = new Email('[email protected]'); |
310
|
|
|
$this->assertCount(1, $email->getFrom()); |
311
|
|
|
$this->assertContains('[email protected]', array_keys($email->getFrom())); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function testSetFrom() |
315
|
|
|
{ |
316
|
|
|
$email = new Email('[email protected]'); |
317
|
|
|
$this->assertCount(1, $email->getFrom()); |
318
|
|
|
$this->assertContains('[email protected]', array_keys($email->getFrom())); |
319
|
|
|
$email->setFrom('[email protected]'); |
320
|
|
|
$this->assertCount(1, $email->getFrom()); |
321
|
|
|
$this->assertContains('[email protected]', array_keys($email->getFrom())); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function testAddFrom() |
325
|
|
|
{ |
326
|
|
|
$email = new Email('[email protected]'); |
327
|
|
|
$this->assertCount(1, $email->getFrom()); |
328
|
|
|
$this->assertContains('[email protected]', array_keys($email->getFrom())); |
329
|
|
|
$email->addFrom('[email protected]'); |
330
|
|
|
$this->assertCount(2, $email->getFrom()); |
331
|
|
|
$this->assertContains('[email protected]', array_keys($email->getFrom())); |
332
|
|
|
$this->assertContains('[email protected]', array_keys($email->getFrom())); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function testSetGetSender() |
336
|
|
|
{ |
337
|
|
|
$email = new Email(); |
338
|
|
|
$this->assertEmpty($email->getSender()); |
339
|
|
|
$email->setSender('[email protected]', 'Silver Stripe'); |
340
|
|
|
$this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getSender()); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function testSetGetReturnPath() |
344
|
|
|
{ |
345
|
|
|
$email = new Email(); |
346
|
|
|
$this->assertEmpty($email->getReturnPath()); |
347
|
|
|
$email->setReturnPath('[email protected]'); |
348
|
|
|
$this->assertEquals('[email protected]', $email->getReturnPath()); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function testSetGetTo() |
352
|
|
|
{ |
353
|
|
|
$email = new Email('[email protected]', '[email protected]'); |
354
|
|
|
$this->assertCount(1, $email->getTo()); |
355
|
|
|
$this->assertContains('[email protected]', array_keys($email->getTo())); |
356
|
|
|
$email->setTo('[email protected]', 'Silver Stripe'); |
357
|
|
|
$this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getTo()); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
public function testAddTo() |
361
|
|
|
{ |
362
|
|
|
$email = new Email('[email protected]', '[email protected]'); |
363
|
|
|
$this->assertCount(1, $email->getTo()); |
364
|
|
|
$this->assertContains('[email protected]', array_keys($email->getTo())); |
365
|
|
|
$email->addTo('[email protected]'); |
366
|
|
|
$this->assertCount(2, $email->getTo()); |
367
|
|
|
$this->assertContains('[email protected]', array_keys($email->getTo())); |
368
|
|
|
$this->assertContains('[email protected]', array_keys($email->getTo())); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function testSetGetCC() |
372
|
|
|
{ |
373
|
|
|
$email = new Email('[email protected]', '[email protected]', 'subject', 'body', '[email protected]'); |
374
|
|
|
$this->assertCount(1, $email->getCC()); |
375
|
|
|
$this->assertContains('[email protected]', array_keys($email->getCC())); |
376
|
|
|
$email->setCC('[email protected]', 'Silver Stripe'); |
377
|
|
|
$this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getCC()); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
public function testAddCC() |
381
|
|
|
{ |
382
|
|
|
$email = new Email('[email protected]', '[email protected]', 'subject', 'body', '[email protected]'); |
383
|
|
|
$this->assertCount(1, $email->getCC()); |
384
|
|
|
$this->assertContains('[email protected]', array_keys($email->getCC())); |
385
|
|
|
$email->addCC('[email protected]', 'Silver Stripe'); |
386
|
|
|
$this->assertCount(2, $email->getCC()); |
387
|
|
|
$this->assertContains('[email protected]', array_keys($email->getCC())); |
388
|
|
|
$this->assertContains('[email protected]', array_keys($email->getCC())); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
public function testSetGetBCC() |
392
|
|
|
{ |
393
|
|
|
$email = new Email( |
394
|
|
|
'[email protected]', |
395
|
|
|
'[email protected]', |
396
|
|
|
'subject', |
397
|
|
|
'body', |
398
|
|
|
'[email protected]', |
399
|
|
|
'[email protected]' |
400
|
|
|
); |
401
|
|
|
$this->assertCount(1, $email->getBCC()); |
402
|
|
|
$this->assertContains('[email protected]', array_keys($email->getBCC())); |
403
|
|
|
$email->setBCC('[email protected]', 'Silver Stripe'); |
404
|
|
|
$this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getBCC()); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
public function testAddBCC() |
408
|
|
|
{ |
409
|
|
|
$email = new Email( |
410
|
|
|
'[email protected]', |
411
|
|
|
'[email protected]', |
412
|
|
|
'subject', |
413
|
|
|
'body', |
414
|
|
|
'[email protected]', |
415
|
|
|
'[email protected]' |
416
|
|
|
); |
417
|
|
|
$this->assertCount(1, $email->getBCC()); |
418
|
|
|
$this->assertContains('[email protected]', array_keys($email->getBCC())); |
419
|
|
|
$email->addBCC('[email protected]', 'Silver Stripe'); |
420
|
|
|
$this->assertCount(2, $email->getBCC()); |
421
|
|
|
$this->assertContains('[email protected]', array_keys($email->getBCC())); |
422
|
|
|
$this->assertContains('[email protected]', array_keys($email->getBCC())); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
public function testReplyTo() |
426
|
|
|
{ |
427
|
|
|
$email = new Email(); |
428
|
|
|
$this->assertEmpty($email->getReplyTo()); |
429
|
|
|
$email->setReplyTo('[email protected]', 'Silver Stripe'); |
430
|
|
|
$this->assertEquals(['[email protected]' => 'Silver Stripe'], $email->getReplyTo()); |
431
|
|
|
$email->addReplyTo('[email protected]'); |
432
|
|
|
$this->assertCount(2, $email->getReplyTo()); |
433
|
|
|
$this->assertContains('[email protected]', array_keys($email->getReplyTo())); |
|
|
|
|
434
|
|
|
$this->assertContains('[email protected]', array_keys($email->getReplyTo())); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
public function testSubject() |
438
|
|
|
{ |
439
|
|
|
$email = new Email('[email protected]', '[email protected]', 'subject'); |
440
|
|
|
$this->assertEquals('subject', $email->getSubject()); |
441
|
|
|
$email->setSubject('new subject'); |
442
|
|
|
$this->assertEquals('new subject', $email->getSubject()); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
public function testPriority() |
446
|
|
|
{ |
447
|
|
|
$email = new Email(); |
448
|
|
|
$this->assertEquals(3, $email->getPriority()); |
449
|
|
|
$email->setPriority(5); |
450
|
|
|
$this->assertEquals(5, $email->getPriority()); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
public function testData() |
454
|
|
|
{ |
455
|
|
|
$email = new Email(); |
456
|
|
|
$this->assertEmpty($email->getData()); |
457
|
|
|
$email->setData([ |
458
|
|
|
'Title' => 'My Title', |
459
|
|
|
]); |
460
|
|
|
$this->assertCount(1, $email->getData()); |
461
|
|
|
$this->assertEquals(['Title' => 'My Title'], $email->getData()); |
462
|
|
|
|
463
|
|
|
$email->addData('Content', 'My content'); |
464
|
|
|
$this->assertCount(2, $email->getData()); |
465
|
|
|
$this->assertEquals([ |
466
|
|
|
'Title' => 'My Title', |
467
|
|
|
'Content' => 'My content', |
468
|
|
|
], $email->getData()); |
469
|
|
|
$email->removeData('Title'); |
470
|
|
|
$this->assertEquals(['Content' => 'My content'], $email->getData()); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
public function testDataWithViewableData() |
474
|
|
|
{ |
475
|
|
|
$member = new Member(); |
476
|
|
|
$member->FirstName = 'First Name'; |
477
|
|
|
$email = new Email(); |
478
|
|
|
$this->assertEmpty($email->getData()); |
479
|
|
|
$email->setData($member); |
480
|
|
|
$this->assertEquals($member, $email->getData()); |
481
|
|
|
$email->addData('Test', 'Test value'); |
482
|
|
|
$this->assertEquals('Test value', $email->getData()->Test); |
|
|
|
|
483
|
|
|
$email->removeData('Test'); |
484
|
|
|
$this->assertNull($email->getData()->Test); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
public function testBody() |
488
|
|
|
{ |
489
|
|
|
$email = new Email(); |
490
|
|
|
$this->assertEmpty($email->getBody()); |
491
|
|
|
$email->setBody('<h1>Title</h1>'); |
492
|
|
|
$this->assertEquals('<h1>Title</h1>', $email->getBody()); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
public function testHTMLTemplate() |
496
|
|
|
{ |
497
|
|
|
// Include dev theme |
498
|
|
|
SSViewer::set_themes([ |
499
|
|
|
'silverstripe/framework:/tests/php/Control/Email/EmailTest', |
500
|
|
|
'$default', |
501
|
|
|
]); |
502
|
|
|
|
503
|
|
|
// Find template on disk |
504
|
|
|
$emailTemplate = ModuleResourceLoader::singleton()->resolveResource( |
505
|
|
|
'silverstripe/framework:templates/SilverStripe/Control/Email/Email.ss' |
506
|
|
|
); |
507
|
|
|
$subClassTemplate = ModuleResourceLoader::singleton()->resolveResource( |
508
|
|
|
'silverstripe/framework:tests/php/Control/Email/EmailTest/templates/' |
509
|
|
|
. str_replace('\\', '/', EmailSubClass::class) |
510
|
|
|
. '.ss' |
511
|
|
|
); |
512
|
|
|
$this->assertTrue($emailTemplate->exists()); |
513
|
|
|
$this->assertTrue($subClassTemplate->exists()); |
514
|
|
|
|
515
|
|
|
// Check template is auto-found |
516
|
|
|
$email = new Email(); |
517
|
|
|
$this->assertEquals($emailTemplate->getPath(), $email->getHTMLTemplate()); |
518
|
|
|
$email->setHTMLTemplate('MyTemplate'); |
519
|
|
|
$this->assertEquals('MyTemplate', $email->getHTMLTemplate()); |
520
|
|
|
|
521
|
|
|
// Check subclass template is found |
522
|
|
|
$email2 = new EmailSubClass(); |
523
|
|
|
$this->assertEquals($subClassTemplate->getPath(), $email2->getHTMLTemplate()); |
524
|
|
|
$email->setHTMLTemplate('MyTemplate'); |
525
|
|
|
$this->assertEquals('MyTemplate', $email->getHTMLTemplate()); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
public function testPlainTemplate() |
529
|
|
|
{ |
530
|
|
|
$email = new Email(); |
531
|
|
|
$this->assertEmpty($email->getPlainTemplate()); |
532
|
|
|
$email->setPlainTemplate('MyTemplate'); |
533
|
|
|
$this->assertEquals('MyTemplate', $email->getPlainTemplate()); |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
public function testGetFailedRecipients() |
537
|
|
|
{ |
538
|
|
|
$mailer = new SwiftMailer(); |
539
|
|
|
/** @var Swift_NullTransport|PHPUnit_Framework_MockObject_MockObject $transport */ |
540
|
|
|
$transport = $this->getMockBuilder(Swift_NullTransport::class)->getMock(); |
541
|
|
|
$transport->expects($this->once()) |
542
|
|
|
->method('send') |
543
|
|
|
->willThrowException(new Swift_RfcComplianceException('Bad email')); |
544
|
|
|
$mailer->setSwiftMailer(new Swift_Mailer($transport)); |
545
|
|
|
$email = new Email(); |
546
|
|
|
$email->setTo('[email protected]'); |
547
|
|
|
$email->setFrom('[email protected]'); |
548
|
|
|
$mailer->send($email); |
549
|
|
|
$this->assertCount(1, $email->getFailedRecipients()); |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
public function testIsEmail() |
553
|
|
|
{ |
554
|
|
|
$this->assertTrue((new Email)->IsEmail()); |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
public function testRenderAgain() |
558
|
|
|
{ |
559
|
|
|
$email = new Email(); |
560
|
|
|
$email->setData([ |
561
|
|
|
'EmailContent' => 'my content', |
562
|
|
|
]); |
563
|
|
|
$email->render(); |
564
|
|
|
$this->assertContains('my content', $email->getBody()); |
565
|
|
|
$children = $email->getSwiftMessage()->getChildren(); |
566
|
|
|
$this->assertCount(1, $children); |
567
|
|
|
$plainPart = reset($children); |
568
|
|
|
$this->assertEquals('my content', $plainPart->getBody()); |
569
|
|
|
|
570
|
|
|
// ensure repeat renders don't add multiple plain parts |
571
|
|
|
$email->render(); |
572
|
|
|
$this->assertCount(1, $email->getSwiftMessage()->getChildren()); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
public function testRerender() |
576
|
|
|
{ |
577
|
|
|
$email = new Email(); |
578
|
|
|
$email->setData([ |
579
|
|
|
'EmailContent' => 'my content', |
580
|
|
|
]); |
581
|
|
|
$email->render(); |
582
|
|
|
$this->assertContains('my content', $email->getBody()); |
583
|
|
|
$children = $email->getSwiftMessage()->getChildren(); |
584
|
|
|
$this->assertCount(1, $children); |
585
|
|
|
$plainPart = reset($children); |
586
|
|
|
$this->assertEquals('my content', $plainPart->getBody()); |
587
|
|
|
|
588
|
|
|
// Ensure setting data causes a rerender |
589
|
|
|
$email->setData([ |
590
|
|
|
'EmailContent' => 'your content' |
591
|
|
|
]); |
592
|
|
|
$email->render(); |
593
|
|
|
$this->assertContains('your content', $email->getBody()); |
594
|
|
|
|
595
|
|
|
// Ensure removing data causes a rerender |
596
|
|
|
$email->removeData('EmailContent'); |
597
|
|
|
$email->render(); |
598
|
|
|
$this->assertNotContains('your content', $email->getBody()); |
599
|
|
|
|
600
|
|
|
// Ensure adding data causes a rerender |
601
|
|
|
$email->addData([ |
602
|
|
|
'EmailContent' => 'their content' |
603
|
|
|
]); |
604
|
|
|
$email->render(); |
605
|
|
|
$this->assertContains('their content', $email->getBody()); |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
public function testRenderPlainOnly() |
609
|
|
|
{ |
610
|
|
|
$email = new Email(); |
611
|
|
|
$email->setData([ |
612
|
|
|
'EmailContent' => 'test content', |
613
|
|
|
]); |
614
|
|
|
$email->render(true); |
615
|
|
|
$this->assertEquals('text/plain', $email->getSwiftMessage()->getContentType()); |
616
|
|
|
$this->assertEmpty($email->getSwiftMessage()->getChildren()); |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
public function testHasPlainPart() |
620
|
|
|
{ |
621
|
|
|
$email = new Email(); |
622
|
|
|
$email->setData([ |
623
|
|
|
'EmailContent' => 'test', |
624
|
|
|
]); |
625
|
|
|
//emails are assumed to be HTML by default |
626
|
|
|
$this->assertFalse($email->hasPlainPart()); |
627
|
|
|
//make sure plain attachments aren't picked up as a plain part |
628
|
|
|
$email->addAttachmentFromData('data', 'attachent.txt', 'text/plain'); |
629
|
|
|
$this->assertFalse($email->hasPlainPart()); |
630
|
|
|
$email->getSwiftMessage()->addPart('plain', 'text/plain'); |
631
|
|
|
$this->assertTrue($email->hasPlainPart()); |
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
public function testGeneratePlainPartFromBody() |
635
|
|
|
{ |
636
|
|
|
$email = new Email(); |
637
|
|
|
$email->setBody('<h1>Test</h1>'); |
638
|
|
|
$this->assertEmpty($email->getSwiftMessage()->getChildren()); |
639
|
|
|
$email->generatePlainPartFromBody(); |
640
|
|
|
$children = $email->getSwiftMessage()->getChildren(); |
641
|
|
|
$this->assertCount(1, $children); |
642
|
|
|
$plainPart = reset($children); |
643
|
|
|
$this->assertContains('Test', $plainPart->getBody()); |
644
|
|
|
$this->assertNotContains('<h1>Test</h1>', $plainPart->getBody()); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
public function testMultipleEmailSends() |
648
|
|
|
{ |
649
|
|
|
$email = new Email(); |
650
|
|
|
$email->setData([ |
651
|
|
|
'EmailContent' => 'Test', |
652
|
|
|
]); |
653
|
|
|
$this->assertEmpty($email->getBody()); |
654
|
|
|
$this->assertEmpty($email->getSwiftMessage()->getChildren()); |
655
|
|
|
$email->send(); |
656
|
|
|
$this->assertContains('Test', $email->getBody()); |
657
|
|
|
$this->assertCount(1, $email->getSwiftMessage()->getChildren()); |
658
|
|
|
$children = $email->getSwiftMessage()->getChildren(); |
659
|
|
|
/** @var \Swift_MimePart $plainPart */ |
660
|
|
|
$plainPart = reset($children); |
661
|
|
|
$this->assertContains('Test', $plainPart->getBody()); |
662
|
|
|
|
663
|
|
|
|
664
|
|
|
//send again |
665
|
|
|
$email->send(); |
666
|
|
|
$this->assertContains('Test', $email->getBody()); |
667
|
|
|
$this->assertCount(1, $email->getSwiftMessage()->getChildren()); |
668
|
|
|
$children = $email->getSwiftMessage()->getChildren(); |
669
|
|
|
/** @var \Swift_MimePart $plainPart */ |
670
|
|
|
$plainPart = reset($children); |
671
|
|
|
$this->assertContains('Test', $plainPart->getBody()); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
public function testGetDefaultFrom() |
675
|
|
|
{ |
676
|
|
|
$email = new Email(); |
677
|
|
|
$class = new \ReflectionClass(Email::class); |
678
|
|
|
$method = $class->getMethod('getDefaultFrom'); |
679
|
|
|
$method->setAccessible(true); |
680
|
|
|
|
681
|
|
|
// default to [email protected] if admin_email config not set |
682
|
|
|
$host = parse_url(Director::host(), PHP_URL_HOST) ?: 'example.com'; |
683
|
|
|
$expected = sprintf('no-reply@%s', $host); |
684
|
|
|
$this->assertSame($expected, $method->invokeArgs($email, [])); |
685
|
|
|
|
686
|
|
|
// use admin_email config |
687
|
|
|
Email::config()->set('admin_email', '[email protected]'); |
688
|
|
|
$this->assertSame('[email protected]', $method->invokeArgs($email, [])); |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
/** |
692
|
|
|
* @return PHPUnit_Framework_MockObject_MockObject|Email |
693
|
|
|
*/ |
694
|
|
|
protected function makeEmailMock($subject) |
695
|
|
|
{ |
696
|
|
|
/** @var Email|PHPUnit_Framework_MockObject_MockObject $email */ |
697
|
|
|
$email = $this->getMockBuilder(Email::class) |
698
|
|
|
->enableProxyingToOriginalMethods() |
699
|
|
|
->getMock(); |
700
|
|
|
|
701
|
|
|
$email->setFrom('[email protected]'); |
702
|
|
|
$email->setTo('[email protected]'); |
703
|
|
|
$email->setSubject($subject); |
704
|
|
|
$email->setBody("Body for {$subject}"); |
705
|
|
|
$email->setCC('[email protected]'); |
706
|
|
|
$email->setBCC('[email protected]'); |
707
|
|
|
$email->addAttachment(__DIR__ . '/EmailTest/attachment.txt', null, 'text/plain'); |
708
|
|
|
return $email; |
709
|
|
|
} |
710
|
|
|
} |
711
|
|
|
|