7
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
8
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice
13
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
15
* @package Cake.Test.Case.Network.Email
16
* @since CakePHP(tm) v 2.0.0
17
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
19
App::uses('CakeEmail', 'Network/Email');
22
* Help to test CakeEmail
25
class TestCakeEmail extends CakeEmail {
31
protected $_config = array();
34
* Wrap to protected method
37
public function formatAddress($address) {
38
return parent::_formatAddress($address);
42
* Wrap to protected method
45
public function wrap($text) {
46
return parent::_wrap($text);
50
* Get the boundary attribute
54
public function getBoundary() {
55
return $this->_boundary;
59
* Encode to protected method
62
public function encode($text) {
63
return $this->_encode($text);
80
'from' => array('some@example.com' => 'My website'),
81
'to' => array('test@example.com' => 'Testname'),
82
'subject' => 'Test mail subject',
83
'transport' => 'Debug',
84
'theme' => 'TestTheme',
85
'helpers' => array('Html', 'Form'),
91
* ExtendTransport class
92
* test class to ensure the class has send() method
95
class ExtendTransport {
100
* CakeEmailTest class
102
* @package Cake.Test.Case.Network.Email
104
class CakeEmailTest extends CakeTestCase {
111
public function setUp() {
113
$this->CakeEmail = new TestCakeEmail();
116
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
125
public function tearDown() {
135
public function testFrom() {
136
$this->assertSame($this->CakeEmail->from(), array());
138
$this->CakeEmail->from('cake@cakephp.org');
139
$expected = array('cake@cakephp.org' => 'cake@cakephp.org');
140
$this->assertSame($this->CakeEmail->from(), $expected);
142
$this->CakeEmail->from(array('cake@cakephp.org'));
143
$this->assertSame($this->CakeEmail->from(), $expected);
145
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
146
$expected = array('cake@cakephp.org' => 'CakePHP');
147
$this->assertSame($this->CakeEmail->from(), $expected);
149
$result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP'));
150
$this->assertSame($this->CakeEmail->from(), $expected);
151
$this->assertSame($this->CakeEmail, $result);
153
$this->setExpectedException('SocketException');
154
$result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address'));
162
public function testSender() {
163
$this->CakeEmail->reset();
164
$this->assertSame($this->CakeEmail->sender(), array());
166
$this->CakeEmail->sender('cake@cakephp.org', 'Name');
167
$expected = array('cake@cakephp.org' => 'Name');
168
$this->assertSame($this->CakeEmail->sender(), $expected);
170
$headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
171
$this->assertSame($headers['From'], false);
172
$this->assertSame($headers['Sender'], 'Name <cake@cakephp.org>');
174
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
175
$headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
176
$this->assertSame($headers['From'], 'CakePHP <cake@cakephp.org>');
177
$this->assertSame($headers['Sender'], '');
185
public function testTo() {
186
$this->assertSame($this->CakeEmail->to(), array());
188
$result = $this->CakeEmail->to('cake@cakephp.org');
189
$expected = array('cake@cakephp.org' => 'cake@cakephp.org');
190
$this->assertSame($this->CakeEmail->to(), $expected);
191
$this->assertSame($this->CakeEmail, $result);
193
$this->CakeEmail->to('cake@cakephp.org', 'CakePHP');
194
$expected = array('cake@cakephp.org' => 'CakePHP');
195
$this->assertSame($this->CakeEmail->to(), $expected);
198
'cake@cakephp.org' => 'Cake PHP',
199
'cake-php@googlegroups.com' => 'Cake Groups',
202
$this->CakeEmail->to($list);
204
'cake@cakephp.org' => 'Cake PHP',
205
'cake-php@googlegroups.com' => 'Cake Groups',
206
'root@cakephp.org' => 'root@cakephp.org'
208
$this->assertSame($this->CakeEmail->to(), $expected);
210
$this->CakeEmail->addTo('jrbasso@cakephp.org');
211
$this->CakeEmail->addTo('mark_story@cakephp.org', 'Mark Story');
212
$result = $this->CakeEmail->addTo(array('phpnut@cakephp.org' => 'PhpNut', 'jose_zap@cakephp.org'));
214
'cake@cakephp.org' => 'Cake PHP',
215
'cake-php@googlegroups.com' => 'Cake Groups',
216
'root@cakephp.org' => 'root@cakephp.org',
217
'jrbasso@cakephp.org' => 'jrbasso@cakephp.org',
218
'mark_story@cakephp.org' => 'Mark Story',
219
'phpnut@cakephp.org' => 'PhpNut',
220
'jose_zap@cakephp.org' => 'jose_zap@cakephp.org'
222
$this->assertSame($this->CakeEmail->to(), $expected);
223
$this->assertSame($this->CakeEmail, $result);
227
* Data provider function for testBuildInvalidData
231
public static function invalidEmails() {
237
array('some@one.whereis'),
238
array('wrong@key' => 'Name'),
239
array(array('ok@cakephp.org', 1.0, '', 'string'))
244
* testBuildInvalidData
246
* @dataProvider invalidEmails
247
* @expectedException SocketException
250
public function testInvalidEmail($value) {
251
$this->CakeEmail->to($value);
255
* testBuildInvalidData
257
* @dataProvider invalidEmails
258
* @expectedException SocketException
261
public function testInvalidEmailAdd($value) {
262
$this->CakeEmail->addTo($value);
266
* testFormatAddress method
270
public function testFormatAddress() {
271
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org'));
272
$expected = array('cake@cakephp.org');
273
$this->assertSame($expected, $result);
275
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org', 'php@cakephp.org' => 'php@cakephp.org'));
276
$expected = array('cake@cakephp.org', 'php@cakephp.org');
277
$this->assertSame($expected, $result);
279
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'CakePHP', 'php@cakephp.org' => 'Cake'));
280
$expected = array('CakePHP <cake@cakephp.org>', 'Cake <php@cakephp.org>');
281
$this->assertSame($expected, $result);
283
$result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last, First'));
284
$expected = array('"Last, First" <me@example.com>');
285
$this->assertSame($expected, $result);
287
$result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last First'));
288
$expected = array('Last First <me@example.com>');
289
$this->assertSame($expected, $result);
291
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'ÄÖÜTest'));
292
$expected = array('=?UTF-8?B?w4TDlsOcVGVzdA==?= <cake@cakephp.org>');
293
$this->assertSame($expected, $result);
295
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '日本語Test'));
296
$expected = array('=?UTF-8?B?5pel5pys6KqeVGVzdA==?= <cake@cakephp.org>');
297
$this->assertSame($expected, $result);
301
* testFormatAddressJapanese
305
public function testFormatAddressJapanese() {
306
$this->skipIf(!function_exists('mb_convert_encoding'));
308
$this->CakeEmail->headerCharset = 'ISO-2022-JP';
309
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '日本語Test'));
310
$expected = array('=?ISO-2022-JP?B?GyRCRnxLXDhsGyhCVGVzdA==?= <cake@cakephp.org>');
311
$this->assertSame($expected, $result);
313
$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '寿限無寿限無五劫の擦り切れ海砂利水魚の水行末雲来末風来末食う寝る処に住む処やぶら小路の藪柑子パイポパイポパイポのシューリンガンシューリンガンのグーリンダイグーリンダイのポンポコピーのポンポコナーの長久命の長助'));
314
$expected = array("=?ISO-2022-JP?B?GyRCPHc4Qkw1PHc4Qkw1OF45ZSROOyQkakBaJGwzJDo9TXg/ZTV7GyhC?=\r\n" .
315
" =?ISO-2022-JP?B?GyRCJE4/ZTlUS3YxQE1oS3ZJd01oS3Y/KSQmPzIkaz1oJEs9OyRgGyhC?=\r\n" .
316
" =?ISO-2022-JP?B?GyRCPWgkZCRWJGk+Lk8pJE5pLjQ7O1IlUSUkJV0lUSUkJV0lUSUkGyhC?=\r\n" .
317
" =?ISO-2022-JP?B?GyRCJV0kTiU3JWUhPCVqJXMlLCVzJTclZSE8JWolcyUsJXMkTiUwGyhC?=\r\n" .
318
" =?ISO-2022-JP?B?GyRCITwlaiVzJUAlJCUwITwlaiVzJUAlJCROJV0lcyVdJTMlVCE8GyhC?=\r\n" .
319
" =?ISO-2022-JP?B?GyRCJE4lXSVzJV0lMyVKITwkTkQ5NVdMPyRORDk9dRsoQg==?= <cake@cakephp.org>");
320
$this->assertSame($expected, $result);
324
* testAddresses method
328
public function testAddresses() {
329
$this->CakeEmail->reset();
330
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
331
$this->CakeEmail->replyTo('replyto@cakephp.org', 'ReplyTo CakePHP');
332
$this->CakeEmail->readReceipt('readreceipt@cakephp.org', 'ReadReceipt CakePHP');
333
$this->CakeEmail->returnPath('returnpath@cakephp.org', 'ReturnPath CakePHP');
334
$this->CakeEmail->to('to@cakephp.org', 'To CakePHP');
335
$this->CakeEmail->cc('cc@cakephp.org', 'Cc CakePHP');
336
$this->CakeEmail->bcc('bcc@cakephp.org', 'Bcc CakePHP');
337
$this->CakeEmail->addTo('to2@cakephp.org', 'To2 CakePHP');
338
$this->CakeEmail->addCc('cc2@cakephp.org', 'Cc2 CakePHP');
339
$this->CakeEmail->addBcc('bcc2@cakephp.org', 'Bcc2 CakePHP');
341
$this->assertSame($this->CakeEmail->from(), array('cake@cakephp.org' => 'CakePHP'));
342
$this->assertSame($this->CakeEmail->replyTo(), array('replyto@cakephp.org' => 'ReplyTo CakePHP'));
343
$this->assertSame($this->CakeEmail->readReceipt(), array('readreceipt@cakephp.org' => 'ReadReceipt CakePHP'));
344
$this->assertSame($this->CakeEmail->returnPath(), array('returnpath@cakephp.org' => 'ReturnPath CakePHP'));
345
$this->assertSame($this->CakeEmail->to(), array('to@cakephp.org' => 'To CakePHP', 'to2@cakephp.org' => 'To2 CakePHP'));
346
$this->assertSame($this->CakeEmail->cc(), array('cc@cakephp.org' => 'Cc CakePHP', 'cc2@cakephp.org' => 'Cc2 CakePHP'));
347
$this->assertSame($this->CakeEmail->bcc(), array('bcc@cakephp.org' => 'Bcc CakePHP', 'bcc2@cakephp.org' => 'Bcc2 CakePHP'));
349
$headers = $this->CakeEmail->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true));
350
$this->assertSame($headers['From'], 'CakePHP <cake@cakephp.org>');
351
$this->assertSame($headers['Reply-To'], 'ReplyTo CakePHP <replyto@cakephp.org>');
352
$this->assertSame($headers['Disposition-Notification-To'], 'ReadReceipt CakePHP <readreceipt@cakephp.org>');
353
$this->assertSame($headers['Return-Path'], 'ReturnPath CakePHP <returnpath@cakephp.org>');
354
$this->assertSame($headers['To'], 'To CakePHP <to@cakephp.org>, To2 CakePHP <to2@cakephp.org>');
355
$this->assertSame($headers['Cc'], 'Cc CakePHP <cc@cakephp.org>, Cc2 CakePHP <cc2@cakephp.org>');
356
$this->assertSame($headers['Bcc'], 'Bcc CakePHP <bcc@cakephp.org>, Bcc2 CakePHP <bcc2@cakephp.org>');
360
* testMessageId method
364
public function testMessageId() {
365
$this->CakeEmail->messageId(true);
366
$result = $this->CakeEmail->getHeaders();
367
$this->assertTrue(isset($result['Message-ID']));
369
$this->CakeEmail->messageId(false);
370
$result = $this->CakeEmail->getHeaders();
371
$this->assertFalse(isset($result['Message-ID']));
373
$result = $this->CakeEmail->messageId('<my-email@localhost>');
374
$this->assertSame($this->CakeEmail, $result);
375
$result = $this->CakeEmail->getHeaders();
376
$this->assertSame($result['Message-ID'], '<my-email@localhost>');
378
$result = $this->CakeEmail->messageId();
379
$this->assertSame($result, '<my-email@localhost>');
383
* testMessageIdInvalid method
386
* @expectedException SocketException
388
public function testMessageIdInvalid() {
389
$this->CakeEmail->messageId('my-email@localhost');
397
public function testDomain() {
398
$result = $this->CakeEmail->domain();
399
$expected = env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n');
400
$this->assertSame($expected, $result);
402
$this->CakeEmail->domain('example.org');
403
$result = $this->CakeEmail->domain();
404
$expected = 'example.org';
405
$this->assertSame($expected, $result);
409
* testMessageIdWithDomain method
413
public function testMessageIdWithDomain() {
414
$result = $this->CakeEmail->getHeaders();
415
$expected = '@' . (env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n')) . '>';
416
$this->assertTextContains($expected, $result['Message-ID']);
418
$this->CakeEmail->domain('example.org');
419
$result = $this->CakeEmail->getHeaders();
420
$expected = '@example.org>';
421
$this->assertTextContains($expected, $result['Message-ID']);
429
public function testSubject() {
430
$this->CakeEmail->subject('You have a new message.');
431
$this->assertSame($this->CakeEmail->subject(), 'You have a new message.');
433
$this->CakeEmail->subject('You have a new message, I think.');
434
$this->assertSame($this->CakeEmail->subject(), 'You have a new message, I think.');
435
$this->CakeEmail->subject(1);
436
$this->assertSame($this->CakeEmail->subject(), '1');
438
$this->CakeEmail->subject('هذه رسالة بعنوان طويل مرسل للمستلم');
439
$expected = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
440
$this->assertSame($this->CakeEmail->subject(), $expected);
444
* testSubjectJapanese
448
public function testSubjectJapanese() {
449
$this->skipIf(!function_exists('mb_convert_encoding'));
450
mb_internal_encoding('UTF-8');
452
$this->CakeEmail->headerCharset = 'ISO-2022-JP';
453
$this->CakeEmail->subject('日本語のSubjectにも対応するよ');
454
$expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsJE4bKEJTdWJqZWN0GyRCJEskYkJQMX4kOSRrJGgbKEI=?=';
455
$this->assertSame($this->CakeEmail->subject(), $expected);
457
$this->CakeEmail->subject('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?');
458
$expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
459
" =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
460
" =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
461
$this->assertSame($this->CakeEmail->subject(), $expected);
469
public function testHeaders() {
470
$this->CakeEmail->messageId(false);
471
$this->CakeEmail->setHeaders(array('X-Something' => 'nice'));
473
'X-Something' => 'nice',
474
'X-Mailer' => 'CakePHP Email',
475
'Date' => date(DATE_RFC2822),
476
'MIME-Version' => '1.0',
477
'Content-Type' => 'text/plain; charset=UTF-8',
478
'Content-Transfer-Encoding' => '8bit'
480
$this->assertSame($this->CakeEmail->getHeaders(), $expected);
482
$this->CakeEmail->addHeaders(array('X-Something' => 'very nice', 'X-Other' => 'cool'));
484
'X-Something' => 'very nice',
486
'X-Mailer' => 'CakePHP Email',
487
'Date' => date(DATE_RFC2822),
488
'MIME-Version' => '1.0',
489
'Content-Type' => 'text/plain; charset=UTF-8',
490
'Content-Transfer-Encoding' => '8bit'
492
$this->assertSame($this->CakeEmail->getHeaders(), $expected);
494
$this->CakeEmail->from('cake@cakephp.org');
495
$this->assertSame($this->CakeEmail->getHeaders(), $expected);
498
'From' => 'cake@cakephp.org',
499
'X-Something' => 'very nice',
501
'X-Mailer' => 'CakePHP Email',
502
'Date' => date(DATE_RFC2822),
503
'MIME-Version' => '1.0',
504
'Content-Type' => 'text/plain; charset=UTF-8',
505
'Content-Transfer-Encoding' => '8bit'
507
$this->assertSame($this->CakeEmail->getHeaders(array('from' => true)), $expected);
509
$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
510
$expected['From'] = 'CakePHP <cake@cakephp.org>';
511
$this->assertSame($this->CakeEmail->getHeaders(array('from' => true)), $expected);
513
$this->CakeEmail->to(array('cake@cakephp.org', 'php@cakephp.org' => 'CakePHP'));
515
'From' => 'CakePHP <cake@cakephp.org>',
516
'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
517
'X-Something' => 'very nice',
519
'X-Mailer' => 'CakePHP Email',
520
'Date' => date(DATE_RFC2822),
521
'MIME-Version' => '1.0',
522
'Content-Type' => 'text/plain; charset=UTF-8',
523
'Content-Transfer-Encoding' => '8bit'
525
$this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
527
$this->CakeEmail->charset = 'ISO-2022-JP';
529
'From' => 'CakePHP <cake@cakephp.org>',
530
'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
531
'X-Something' => 'very nice',
533
'X-Mailer' => 'CakePHP Email',
534
'Date' => date(DATE_RFC2822),
535
'MIME-Version' => '1.0',
536
'Content-Type' => 'text/plain; charset=ISO-2022-JP',
537
'Content-Transfer-Encoding' => '7bit'
539
$this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
541
$result = $this->CakeEmail->setHeaders(array());
542
$this->assertInstanceOf('CakeEmail', $result);
546
* Data provider function for testInvalidHeaders
550
public static function invalidHeaders() {
563
* @dataProvider invalidHeaders
564
* @expectedException SocketException
567
public function testInvalidHeaders($value) {
568
$this->CakeEmail->setHeaders($value);
572
* testInvalidAddHeaders
574
* @dataProvider invalidHeaders
575
* @expectedException SocketException
578
public function testInvalidAddHeaders($value) {
579
$this->CakeEmail->addHeaders($value);
583
* testTemplate method
587
public function testTemplate() {
588
$this->CakeEmail->template('template', 'layout');
589
$expected = array('template' => 'template', 'layout' => 'layout');
590
$this->assertSame($this->CakeEmail->template(), $expected);
592
$this->CakeEmail->template('new_template');
593
$expected = array('template' => 'new_template', 'layout' => 'layout');
594
$this->assertSame($this->CakeEmail->template(), $expected);
596
$this->CakeEmail->template('template', null);
597
$expected = array('template' => 'template', 'layout' => null);
598
$this->assertSame($this->CakeEmail->template(), $expected);
600
$this->CakeEmail->template(null, null);
601
$expected = array('template' => null, 'layout' => null);
602
$this->assertSame($this->CakeEmail->template(), $expected);
610
public function testTheme() {
611
$this->assertSame(null, $this->CakeEmail->theme());
613
$this->CakeEmail->theme('default');
614
$expected = 'default';
615
$this->assertSame($expected, $this->CakeEmail->theme());
619
* testViewVars method
623
public function testViewVars() {
624
$this->assertSame($this->CakeEmail->viewVars(), array());
626
$this->CakeEmail->viewVars(array('value' => 12345));
627
$this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345));
629
$this->CakeEmail->viewVars(array('name' => 'CakePHP'));
630
$this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345, 'name' => 'CakePHP'));
632
$this->CakeEmail->viewVars(array('value' => 4567));
633
$this->assertSame($this->CakeEmail->viewVars(), array('value' => 4567, 'name' => 'CakePHP'));
637
* testAttachments method
641
public function testAttachments() {
642
$this->CakeEmail->attachments(CAKE . 'basics.php');
643
$expected = array('basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'application/octet-stream'));
644
$this->assertSame($this->CakeEmail->attachments(), $expected);
646
$this->CakeEmail->attachments(array());
647
$this->assertSame($this->CakeEmail->attachments(), array());
649
$this->CakeEmail->attachments(array(array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
650
$this->CakeEmail->addAttachments(CAKE . 'bootstrap.php');
651
$this->CakeEmail->addAttachments(array(CAKE . 'bootstrap.php'));
652
$this->CakeEmail->addAttachments(array('other.txt' => CAKE . 'bootstrap.php', 'license' => CAKE . 'LICENSE.txt'));
654
'basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'),
655
'bootstrap.php' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
656
'other.txt' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
657
'license' => array('file' => CAKE . 'LICENSE.txt', 'mimetype' => 'application/octet-stream')
659
$this->assertSame($this->CakeEmail->attachments(), $expected);
661
$this->setExpectedException('SocketException');
662
$this->CakeEmail->attachments(array(array('nofile' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
666
* testTransport method
670
public function testTransport() {
671
$result = $this->CakeEmail->transport('Debug');
672
$this->assertSame($this->CakeEmail, $result);
673
$this->assertSame($this->CakeEmail->transport(), 'Debug');
675
$result = $this->CakeEmail->transportClass();
676
$this->assertInstanceOf('DebugTransport', $result);
678
$this->setExpectedException('SocketException');
679
$this->CakeEmail->transport('Invalid');
680
$result = $this->CakeEmail->transportClass();
684
* testExtendTransport method
688
public function testExtendTransport() {
689
$this->setExpectedException('SocketException');
690
$this->CakeEmail->transport('Extend');
691
$result = $this->CakeEmail->transportClass();
699
public function testConfig() {
700
$transportClass = $this->CakeEmail->transport('debug')->transportClass();
702
$config = array('test' => 'ok', 'test2' => true);
703
$this->CakeEmail->config($config);
704
$this->assertSame($transportClass->config(), $config);
705
$this->assertSame($this->CakeEmail->config(), $config);
707
$this->CakeEmail->config(array());
708
$this->assertSame($transportClass->config(), array());
712
* testConfigString method
716
public function testConfigString() {
717
$configs = new EmailConfig();
718
$this->CakeEmail->config('test');
720
$result = $this->CakeEmail->to();
721
$this->assertEquals($configs->test['to'], $result);
723
$result = $this->CakeEmail->from();
724
$this->assertEquals($configs->test['from'], $result);
726
$result = $this->CakeEmail->subject();
727
$this->assertEquals($configs->test['subject'], $result);
729
$result = $this->CakeEmail->theme();
730
$this->assertEquals($configs->test['theme'], $result);
732
$result = $this->CakeEmail->transport();
733
$this->assertEquals($configs->test['transport'], $result);
735
$result = $this->CakeEmail->transportClass();
736
$this->assertInstanceOf('DebugTransport', $result);
738
$result = $this->CakeEmail->helpers();
739
$this->assertEquals($configs->test['helpers'], $result);
743
* testSendWithContent method
747
public function testSendWithContent() {
748
$this->CakeEmail->reset();
749
$this->CakeEmail->transport('Debug');
750
$this->CakeEmail->from('cake@cakephp.org');
751
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
752
$this->CakeEmail->subject('My title');
753
$this->CakeEmail->config(array('empty'));
755
$result = $this->CakeEmail->send("Here is my body, with multi lines.\nThis is the second line.\r\n\r\nAnd the last.");
756
$expected = array('headers', 'message');
757
$this->assertEquals($expected, array_keys($result));
758
$expected = "Here is my body, with multi lines.\r\nThis is the second line.\r\n\r\nAnd the last.\r\n\r\n";
760
$this->assertEquals($expected, $result['message']);
761
$this->assertTrue((bool)strpos($result['headers'], 'Date: '));
762
$this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
763
$this->assertTrue((bool)strpos($result['headers'], 'To: '));
765
$result = $this->CakeEmail->send("Other body");
766
$expected = "Other body\r\n\r\n";
767
$this->assertSame($result['message'], $expected);
768
$this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
769
$this->assertTrue((bool)strpos($result['headers'], 'To: '));
771
$this->CakeEmail->reset();
772
$this->CakeEmail->transport('Debug');
773
$this->CakeEmail->from('cake@cakephp.org');
774
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
775
$this->CakeEmail->subject('My title');
776
$this->CakeEmail->config(array('empty'));
777
$result = $this->CakeEmail->send(array('Sending content', 'As array'));
778
$expected = "Sending content\r\nAs array\r\n\r\n\r\n";
779
$this->assertSame($result['message'], $expected);
783
* testSendWithoutFrom method
787
public function testSendWithoutFrom() {
788
$this->CakeEmail->transport('Debug');
789
$this->CakeEmail->to('cake@cakephp.org');
790
$this->CakeEmail->subject('My title');
791
$this->CakeEmail->config(array('empty'));
792
$this->setExpectedException('SocketException');
793
$this->CakeEmail->send("Forgot to set From");
797
* testSendWithoutTo method
801
public function testSendWithoutTo() {
802
$this->CakeEmail->transport('Debug');
803
$this->CakeEmail->from('cake@cakephp.org');
804
$this->CakeEmail->subject('My title');
805
$this->CakeEmail->config(array('empty'));
806
$this->setExpectedException('SocketException');
807
$this->CakeEmail->send("Forgot to set To");
811
* Test send() with no template.
815
public function testSendNoTemplateWithAttachments() {
816
$this->CakeEmail->transport('debug');
817
$this->CakeEmail->from('cake@cakephp.org');
818
$this->CakeEmail->to('cake@cakephp.org');
819
$this->CakeEmail->subject('My title');
820
$this->CakeEmail->emailFormat('text');
821
$this->CakeEmail->attachments(array(CAKE . 'basics.php'));
822
$result = $this->CakeEmail->send('Hello');
824
$boundary = $this->CakeEmail->getBoundary();
825
$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
826
$expected = "--$boundary\r\n" .
827
"Content-Type: text/plain; charset=UTF-8\r\n" .
828
"Content-Transfer-Encoding: 8bit\r\n" .
835
"Content-Type: application/octet-stream\r\n" .
836
"Content-Transfer-Encoding: base64\r\n" .
837
"Content-Disposition: attachment; filename=\"basics.php\"\r\n\r\n";
838
$this->assertContains($expected, $result['message']);
842
* Test send() with no template as both
846
public function testSendNoTemplateWithAttachmentsAsBoth() {
847
$this->CakeEmail->transport('debug');
848
$this->CakeEmail->from('cake@cakephp.org');
849
$this->CakeEmail->to('cake@cakephp.org');
850
$this->CakeEmail->subject('My title');
851
$this->CakeEmail->emailFormat('both');
852
$this->CakeEmail->attachments(array(CAKE . 'VERSION.txt'));
853
$result = $this->CakeEmail->send('Hello');
855
$boundary = $this->CakeEmail->getBoundary();
856
$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
857
$expected = "--$boundary\r\n" .
858
"Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
860
"--alt-$boundary\r\n" .
861
"Content-Type: text/plain; charset=UTF-8\r\n" .
862
"Content-Transfer-Encoding: 8bit\r\n" .
868
"--alt-$boundary\r\n" .
869
"Content-Type: text/html; charset=UTF-8\r\n" .
870
"Content-Transfer-Encoding: 8bit\r\n" .
876
"--alt-{$boundary}--\r\n" .
879
"Content-Type: application/octet-stream\r\n" .
880
"Content-Transfer-Encoding: base64\r\n" .
881
"Content-Disposition: attachment; filename=\"VERSION.txt\"\r\n\r\n";
882
$this->assertContains($expected, $result['message']);
886
* Test setting inline attachments and messages.
890
public function testSendWithInlineAttachments() {
891
$this->CakeEmail->transport('debug');
892
$this->CakeEmail->from('cake@cakephp.org');
893
$this->CakeEmail->to('cake@cakephp.org');
894
$this->CakeEmail->subject('My title');
895
$this->CakeEmail->emailFormat('both');
896
$this->CakeEmail->attachments(array(
898
'file' => CAKE . 'VERSION.txt',
899
'contentId' => 'abc123'
902
$result = $this->CakeEmail->send('Hello');
904
$boundary = $this->CakeEmail->getBoundary();
905
$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
906
$expected = "--$boundary\r\n" .
907
"Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
909
"--rel-$boundary\r\n" .
910
"Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
912
"--alt-$boundary\r\n" .
913
"Content-Type: text/plain; charset=UTF-8\r\n" .
914
"Content-Transfer-Encoding: 8bit\r\n" .
920
"--alt-$boundary\r\n" .
921
"Content-Type: text/html; charset=UTF-8\r\n" .
922
"Content-Transfer-Encoding: 8bit\r\n" .
928
"--alt-{$boundary}--\r\n" .
930
"--rel-$boundary\r\n" .
931
"Content-Type: application/octet-stream\r\n" .
932
"Content-Transfer-Encoding: base64\r\n" .
933
"Content-ID: <abc123>\r\n" .
934
"Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
935
$this->assertContains($expected, $result['message']);
936
$this->assertContains('--rel-' . $boundary . '--', $result['message']);
937
$this->assertContains('--' . $boundary . '--', $result['message']);
941
* testSendWithLog method
945
public function testSendWithLog() {
946
$path = CAKE . 'Test' . DS . 'test_app' . DS . 'tmp' . DS;
947
CakeLog::config('email', array(
948
'engine' => 'FileLog',
951
CakeLog::drop('default');
952
$this->CakeEmail->transport('Debug');
953
$this->CakeEmail->to('me@cakephp.org');
954
$this->CakeEmail->from('cake@cakephp.org');
955
$this->CakeEmail->subject('My title');
956
$this->CakeEmail->config(array('log' => 'cake_test_emails'));
957
$result = $this->CakeEmail->send("Logging This");
959
App::uses('File', 'Utility');
960
$File = new File(TMP . 'cake_test_emails.log');
961
$log = $File->read();
962
$this->assertTrue(strpos($log, $result['headers']) !== false);
963
$this->assertTrue(strpos($log, $result['message']) !== false);
965
CakeLog::drop('email');
969
* testSendRender method
973
public function testSendRender() {
974
$this->CakeEmail->reset();
975
$this->CakeEmail->transport('debug');
977
$this->CakeEmail->from('cake@cakephp.org');
978
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
979
$this->CakeEmail->subject('My title');
980
$this->CakeEmail->config(array('empty'));
981
$this->CakeEmail->template('default', 'default');
982
$result = $this->CakeEmail->send();
984
$this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
985
$this->assertContains('Message-ID: ', $result['headers']);
986
$this->assertContains('To: ', $result['headers']);
990
* testSendRender method for ISO-2022-JP
994
public function testSendRenderJapanese() {
995
$this->skipIf(!function_exists('mb_convert_encoding'));
997
$this->CakeEmail->reset();
998
$this->CakeEmail->transport('debug');
1000
$this->CakeEmail->from('cake@cakephp.org');
1001
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1002
$this->CakeEmail->subject('My title');
1003
$this->CakeEmail->config(array('empty'));
1004
$this->CakeEmail->template('default', 'japanese');
1005
$this->CakeEmail->charset = 'ISO-2022-JP';
1006
$result = $this->CakeEmail->send();
1008
$expected = mb_convert_encoding('CakePHP Framework を使って送信したメールです。 http://cakephp.org.', 'ISO-2022-JP');
1009
$this->assertContains($expected, $result['message']);
1010
$this->assertContains('Message-ID: ', $result['headers']);
1011
$this->assertContains('To: ', $result['headers']);
1015
* testSendRenderThemed method
1019
public function testSendRenderThemed() {
1020
$this->CakeEmail->reset();
1021
$this->CakeEmail->transport('debug');
1023
$this->CakeEmail->from('cake@cakephp.org');
1024
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1025
$this->CakeEmail->subject('My title');
1026
$this->CakeEmail->config(array('empty'));
1027
$this->CakeEmail->theme('TestTheme');
1028
$this->CakeEmail->template('themed', 'default');
1029
$result = $this->CakeEmail->send();
1031
$this->assertContains('In TestTheme', $result['message']);
1032
$this->assertContains('Message-ID: ', $result['headers']);
1033
$this->assertContains('To: ', $result['headers']);
1037
* testSendRenderWithVars method
1041
public function testSendRenderWithVars() {
1042
$this->CakeEmail->reset();
1043
$this->CakeEmail->transport('debug');
1045
$this->CakeEmail->from('cake@cakephp.org');
1046
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1047
$this->CakeEmail->subject('My title');
1048
$this->CakeEmail->config(array('empty'));
1049
$this->CakeEmail->template('custom', 'default');
1050
$this->CakeEmail->viewVars(array('value' => 12345));
1051
$result = $this->CakeEmail->send();
1053
$this->assertContains('Here is your value: 12345', $result['message']);
1057
* testSendRenderWithVars method for ISO-2022-JP
1061
public function testSendRenderWithVarsJapanese() {
1062
$this->skipIf(!function_exists('mb_convert_encoding'));
1063
$this->CakeEmail->reset();
1064
$this->CakeEmail->transport('debug');
1066
$this->CakeEmail->from('cake@cakephp.org');
1067
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1068
$this->CakeEmail->subject('My title');
1069
$this->CakeEmail->config(array('empty'));
1070
$this->CakeEmail->template('japanese', 'default');
1071
$this->CakeEmail->viewVars(array('value' => '日本語の差し込み123'));
1072
$this->CakeEmail->charset = 'ISO-2022-JP';
1073
$result = $this->CakeEmail->send();
1075
$expected = mb_convert_encoding('ここにあなたの設定した値が入ります: 日本語の差し込み123', 'ISO-2022-JP');
1076
$this->assertTrue((bool)strpos($result['message'], $expected));
1080
* testSendRenderWithHelpers method
1084
public function testSendRenderWithHelpers() {
1085
$this->CakeEmail->reset();
1086
$this->CakeEmail->transport('debug');
1088
$timestamp = time();
1089
$this->CakeEmail->from('cake@cakephp.org');
1090
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1091
$this->CakeEmail->subject('My title');
1092
$this->CakeEmail->config(array('empty'));
1093
$this->CakeEmail->template('custom_helper', 'default');
1094
$this->CakeEmail->viewVars(array('time' => $timestamp));
1096
$result = $this->CakeEmail->helpers(array('Time'));
1097
$this->assertInstanceOf('CakeEmail', $result);
1099
$result = $this->CakeEmail->send();
1100
$this->assertTrue((bool)strpos($result['message'], 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
1102
$result = $this->CakeEmail->helpers();
1103
$this->assertEquals(array('Time'), $result);
1107
* testSendRenderWithImage method
1111
public function testSendRenderWithImage() {
1112
$this->CakeEmail->reset();
1113
$this->CakeEmail->transport('Debug');
1115
$this->CakeEmail->from('cake@cakephp.org');
1116
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1117
$this->CakeEmail->subject('My title');
1118
$this->CakeEmail->config(array('empty'));
1119
$this->CakeEmail->template('image');
1120
$this->CakeEmail->emailFormat('html');
1121
$server = env('SERVER_NAME') ? env('SERVER_NAME') : 'localhost';
1123
if (env('SERVER_PORT') != null && env('SERVER_PORT') != 80) {
1124
$server .= ':' . env('SERVER_PORT');
1127
$expected = '<img src="http://' . $server . '/img/image.gif" alt="cool image" width="100" height="100" />';
1128
$result = $this->CakeEmail->send();
1129
$this->assertContains($expected, $result['message']);
1133
* testSendRenderPlugin method
1137
public function testSendRenderPlugin() {
1139
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
1141
CakePlugin::load('TestPlugin');
1143
$this->CakeEmail->reset();
1144
$this->CakeEmail->transport('debug');
1145
$this->CakeEmail->from('cake@cakephp.org');
1146
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1147
$this->CakeEmail->subject('My title');
1148
$this->CakeEmail->config(array('empty'));
1150
$result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'default')->send();
1151
$this->assertContains('Into TestPlugin.', $result['message']);
1152
$this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
1154
$result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'TestPlugin.plug_default')->send();
1155
$this->assertContains('Into TestPlugin.', $result['message']);
1156
$this->assertContains('This email was sent using the TestPlugin.', $result['message']);
1158
$result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'plug_default')->send();
1159
$this->assertContains('Into TestPlugin.', $result['message']);
1160
$this->assertContains('This email was sent using the TestPlugin.', $result['message']);
1162
// test plugin template overridden by theme
1163
$this->CakeEmail->theme('TestTheme');
1164
$result = $this->CakeEmail->send();
1166
$this->assertContains('Into TestPlugin. (themed)', $result['message']);
1168
$this->CakeEmail->viewVars(array('value' => 12345));
1169
$result = $this->CakeEmail->template('custom', 'TestPlugin.plug_default')->send();
1170
$this->assertContains('Here is your value: 12345', $result['message']);
1171
$this->assertContains('This email was sent using the TestPlugin.', $result['message']);
1173
$this->setExpectedException('MissingViewException');
1174
$this->CakeEmail->template('test_plugin_tpl', 'plug_default')->send();
1178
* testSendMultipleMIME method
1182
public function testSendMultipleMIME() {
1183
$this->CakeEmail->reset();
1184
$this->CakeEmail->transport('debug');
1186
$this->CakeEmail->from('cake@cakephp.org');
1187
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1188
$this->CakeEmail->subject('My title');
1189
$this->CakeEmail->template('custom', 'default');
1190
$this->CakeEmail->config(array());
1191
$this->CakeEmail->viewVars(array('value' => 12345));
1192
$this->CakeEmail->emailFormat('both');
1193
$result = $this->CakeEmail->send();
1195
$message = $this->CakeEmail->message();
1196
$boundary = $this->CakeEmail->getBoundary();
1197
$this->assertFalse(empty($boundary));
1198
$this->assertContains('--' . $boundary, $message);
1199
$this->assertContains('--' . $boundary . '--', $message);
1200
$this->assertContains('--alt-' . $boundary, $message);
1201
$this->assertContains('--alt-' . $boundary . '--', $message);
1203
$this->CakeEmail->attachments(array('fake.php' => __FILE__));
1204
$this->CakeEmail->send();
1206
$message = $this->CakeEmail->message();
1207
$boundary = $this->CakeEmail->getBoundary();
1208
$this->assertFalse(empty($boundary));
1209
$this->assertContains('--' . $boundary, $message);
1210
$this->assertContains('--' . $boundary . '--', $message);
1211
$this->assertContains('--alt-' . $boundary, $message);
1212
$this->assertContains('--alt-' . $boundary . '--', $message);
1216
* testSendAttachment method
1220
public function testSendAttachment() {
1221
$this->CakeEmail->reset();
1222
$this->CakeEmail->transport('debug');
1223
$this->CakeEmail->from('cake@cakephp.org');
1224
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1225
$this->CakeEmail->subject('My title');
1226
$this->CakeEmail->config(array());
1227
$this->CakeEmail->attachments(array(CAKE . 'basics.php'));
1228
$result = $this->CakeEmail->send('body');
1229
$this->assertContains("Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"basics.php\"", $result['message']);
1231
$this->CakeEmail->attachments(array('my.file.txt' => CAKE . 'basics.php'));
1232
$result = $this->CakeEmail->send('body');
1233
$this->assertContains("Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"my.file.txt\"", $result['message']);
1235
$this->CakeEmail->attachments(array('file.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
1236
$result = $this->CakeEmail->send('body');
1237
$this->assertContains("Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"file.txt\"", $result['message']);
1239
$this->CakeEmail->attachments(array('file2.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain', 'contentId' => 'a1b1c1')));
1240
$result = $this->CakeEmail->send('body');
1241
$this->assertContains("Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-ID: <a1b1c1>\r\nContent-Disposition: inline; filename=\"file2.txt\"", $result['message']);
1245
* testDeliver method
1249
public function testDeliver() {
1250
$instance = CakeEmail::deliver('all@cakephp.org', 'About', 'Everything ok', array('from' => 'root@cakephp.org'), false);
1251
$this->assertInstanceOf('CakeEmail', $instance);
1252
$this->assertSame($instance->to(), array('all@cakephp.org' => 'all@cakephp.org'));
1253
$this->assertSame($instance->subject(), 'About');
1254
$this->assertSame($instance->from(), array('root@cakephp.org' => 'root@cakephp.org'));
1257
'from' => 'cake@cakephp.org',
1258
'to' => 'debug@cakephp.org',
1259
'subject' => 'Update ok',
1260
'template' => 'custom',
1261
'layout' => 'custom_layout',
1262
'viewVars' => array('value' => 123),
1263
'cc' => array('cake@cakephp.org' => 'Myself')
1265
$instance = CakeEmail::deliver(null, null, array('name' => 'CakePHP'), $config, false);
1266
$this->assertSame($instance->from(), array('cake@cakephp.org' => 'cake@cakephp.org'));
1267
$this->assertSame($instance->to(), array('debug@cakephp.org' => 'debug@cakephp.org'));
1268
$this->assertSame($instance->subject(), 'Update ok');
1269
$this->assertSame($instance->template(), array('template' => 'custom', 'layout' => 'custom_layout'));
1270
$this->assertSame($instance->viewVars(), array('value' => 123, 'name' => 'CakePHP'));
1271
$this->assertSame($instance->cc(), array('cake@cakephp.org' => 'Myself'));
1273
$configs = array('from' => 'root@cakephp.org', 'message' => 'Message from configs', 'transport' => 'Debug');
1274
$instance = CakeEmail::deliver('all@cakephp.org', 'About', null, $configs, true);
1275
$message = $instance->message();
1276
$this->assertEquals($configs['message'], $message[0]);
1280
* testMessage method
1284
public function testMessage() {
1285
$this->CakeEmail->reset();
1286
$this->CakeEmail->transport('debug');
1287
$this->CakeEmail->from('cake@cakephp.org');
1288
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1289
$this->CakeEmail->subject('My title');
1290
$this->CakeEmail->config(array('empty'));
1291
$this->CakeEmail->template('default', 'default');
1292
$this->CakeEmail->emailFormat('both');
1293
$result = $this->CakeEmail->send();
1295
$expected = '<p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>';
1296
$this->assertContains($expected, $this->CakeEmail->message(CakeEmail::MESSAGE_HTML));
1298
$expected = 'This email was sent using the CakePHP Framework, http://cakephp.org.';
1299
$this->assertContains($expected, $this->CakeEmail->message(CakeEmail::MESSAGE_TEXT));
1301
$message = $this->CakeEmail->message();
1302
$this->assertContains('Content-Type: text/plain; charset=UTF-8', $message);
1303
$this->assertContains('Content-Type: text/html; charset=UTF-8', $message);
1306
$this->assertTrue($this->_checkContentTransferEncoding($message, '8bit'));
1308
$this->CakeEmail->charset = 'ISO-2022-JP';
1309
$this->CakeEmail->send();
1310
$message = $this->CakeEmail->message();
1311
$this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $message);
1312
$this->assertContains('Content-Type: text/html; charset=ISO-2022-JP', $message);
1314
// ISO-2022-JP is 7bit
1315
$this->assertTrue($this->_checkContentTransferEncoding($message, '7bit'));
1323
public function testReset() {
1324
$this->CakeEmail->to('cake@cakephp.org');
1325
$this->CakeEmail->theme('TestTheme');
1326
$this->assertSame($this->CakeEmail->to(), array('cake@cakephp.org' => 'cake@cakephp.org'));
1328
$this->CakeEmail->reset();
1329
$this->assertSame($this->CakeEmail->to(), array());
1330
$this->assertSame(null, $this->CakeEmail->theme());
1334
* testReset with charset
1338
public function testResetWithCharset() {
1339
$this->CakeEmail->charset = 'ISO-2022-JP';
1340
$this->CakeEmail->reset();
1342
$this->assertSame($this->CakeEmail->charset, 'utf-8', $this->CakeEmail->charset);
1343
$this->assertSame($this->CakeEmail->headerCharset, null, $this->CakeEmail->headerCharset);
1351
public function testWrap() {
1352
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
1353
$result = $this->CakeEmail->wrap($text);
1355
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
1356
'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
1359
$this->assertSame($expected, $result);
1361
$text = 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan amet.';
1362
$result = $this->CakeEmail->wrap($text);
1364
'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
1365
'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
1369
$this->assertSame($expected, $result);
1371
$text = '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula pellentesque accumsan amet.<hr></p>';
1372
$result = $this->CakeEmail->wrap($text);
1374
'<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac',
1375
'turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula',
1376
'pellentesque accumsan amet.<hr></p>',
1379
$this->assertSame($expected, $result);
1381
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac <a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
1382
$result = $this->CakeEmail->wrap($text);
1384
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac',
1385
'<a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh',
1386
'nisi, vehicula pellentesque accumsan amet.',
1389
$this->assertSame($expected, $result);
1391
$text = 'Lorem ipsum <a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">ok</a>';
1392
$result = $this->CakeEmail->wrap($text);
1395
'<a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">',
1399
$this->assertSame($expected, $result);
1401
$text = 'Lorem ipsum withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite ok.';
1402
$result = $this->CakeEmail->wrap($text);
1405
'withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite',
1409
$this->assertSame($expected, $result);
1413
* testConstructWithConfigArray method
1417
public function testConstructWithConfigArray() {
1419
'from' => array('some@example.com' => 'My website'),
1420
'to' => 'test@example.com',
1421
'subject' => 'Test mail subject',
1422
'transport' => 'Debug',
1424
$this->CakeEmail = new CakeEmail($configs);
1426
$result = $this->CakeEmail->to();
1427
$this->assertEquals(array($configs['to'] => $configs['to']), $result);
1429
$result = $this->CakeEmail->from();
1430
$this->assertEquals($configs['from'], $result);
1432
$result = $this->CakeEmail->subject();
1433
$this->assertEquals($configs['subject'], $result);
1435
$result = $this->CakeEmail->transport();
1436
$this->assertEquals($configs['transport'], $result);
1438
$result = $this->CakeEmail->transportClass();
1439
$this->assertTrue($result instanceof DebugTransport);
1441
$result = $this->CakeEmail->send('This is the message');
1443
$this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
1444
$this->assertTrue((bool)strpos($result['headers'], 'To: '));
1448
* testConstructWithConfigString method
1452
public function testConstructWithConfigString() {
1453
$configs = new EmailConfig();
1454
$this->CakeEmail = new CakeEmail('test');
1456
$result = $this->CakeEmail->to();
1457
$this->assertEquals($configs->test['to'], $result);
1459
$result = $this->CakeEmail->from();
1460
$this->assertEquals($configs->test['from'], $result);
1462
$result = $this->CakeEmail->subject();
1463
$this->assertEquals($configs->test['subject'], $result);
1465
$result = $this->CakeEmail->transport();
1466
$this->assertEquals($configs->test['transport'], $result);
1468
$result = $this->CakeEmail->transportClass();
1469
$this->assertTrue($result instanceof DebugTransport);
1471
$result = $this->CakeEmail->send('This is the message');
1473
$this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
1474
$this->assertTrue((bool)strpos($result['headers'], 'To: '));
1478
* testViewRender method
1482
public function testViewRender() {
1483
$result = $this->CakeEmail->viewRender();
1484
$this->assertEquals('View', $result);
1486
$result = $this->CakeEmail->viewRender('Theme');
1487
$this->assertInstanceOf('CakeEmail', $result);
1489
$result = $this->CakeEmail->viewRender();
1490
$this->assertEquals('Theme', $result);
1494
* testEmailFormat method
1498
public function testEmailFormat() {
1499
$result = $this->CakeEmail->emailFormat();
1500
$this->assertEquals('text', $result);
1502
$result = $this->CakeEmail->emailFormat('html');
1503
$this->assertInstanceOf('CakeEmail', $result);
1505
$result = $this->CakeEmail->emailFormat();
1506
$this->assertEquals('html', $result);
1508
$this->setExpectedException('SocketException');
1509
$result = $this->CakeEmail->emailFormat('invalid');
1513
* Tests that it is possible to add charset configuration to a CakeEmail object
1517
public function testConfigCharset() {
1518
$email = new CakeEmail();
1519
$this->assertEquals(Configure::read('App.encoding'), $email->charset);
1520
$this->assertEquals(Configure::read('App.encoding'), $email->headerCharset);
1522
$email = new CakeEmail(array('charset' => 'iso-2022-jp', 'headerCharset' => 'iso-2022-jp-ms'));
1523
$this->assertEquals('iso-2022-jp', $email->charset);
1524
$this->assertEquals('iso-2022-jp-ms', $email->headerCharset);
1526
$email = new CakeEmail(array('charset' => 'iso-2022-jp'));
1527
$this->assertEquals('iso-2022-jp', $email->charset);
1528
$this->assertEquals('iso-2022-jp', $email->headerCharset);
1530
$email = new CakeEmail(array('headerCharset' => 'iso-2022-jp-ms'));
1531
$this->assertEquals(Configure::read('App.encoding'), $email->charset);
1532
$this->assertEquals('iso-2022-jp-ms', $email->headerCharset);
1536
* Tests that the header is encoded using the configured headerCharset
1540
public function testHeaderEncoding() {
1541
$this->skipIf(!function_exists('mb_convert_encoding'));
1542
$email = new CakeEmail(array('headerCharset' => 'iso-2022-jp-ms', 'transport' => 'Debug'));
1543
$email->subject('あれ?もしかしての前と');
1544
$headers = $email->getHeaders(array('subject'));
1545
$expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
1546
$this->assertContains($expected, $headers['Subject']);
1548
$email->to('someone@example.com')->from('someone@example.com');
1549
$result = $email->send('ってテーブルを作ってやってたらう');
1550
$this->assertContains('ってテーブルを作ってやってたらう', $result['message']);
1554
* Tests that the body is encoded using the configured charset
1558
public function testBodyEncoding() {
1559
$this->skipIf(!function_exists('mb_convert_encoding'));
1560
$email = new CakeEmail(array(
1561
'charset' => 'iso-2022-jp',
1562
'headerCharset' => 'iso-2022-jp-ms',
1563
'transport' => 'Debug'
1565
$email->subject('あれ?もしかしての前と');
1566
$headers = $email->getHeaders(array('subject'));
1567
$expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
1568
$this->assertContains($expected, $headers['Subject']);
1570
$email->to('someone@example.com')->from('someone@example.com');
1571
$result = $email->send('ってテーブルを作ってやってたらう');
1572
$this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $result['headers']);
1573
$this->assertContains(mb_convert_encoding('ってテーブルを作ってやってたらう','ISO-2022-JP'), $result['message']);
1577
* Tests that the body is encoded using the configured charset (Japanese standard encoding)
1581
public function testBodyEncodingIso2022Jp() {
1582
$this->skipIf(!function_exists('mb_convert_encoding'));
1583
$email = new CakeEmail(array(
1584
'charset' => 'iso-2022-jp',
1585
'headerCharset' => 'iso-2022-jp',
1586
'transport' => 'Debug'
1588
$email->subject('あれ?もしかしての前と');
1589
$headers = $email->getHeaders(array('subject'));
1590
$expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
1591
$this->assertContains($expected, $headers['Subject']);
1593
$email->to('someone@example.com')->from('someone@example.com');
1594
$result = $email->send('①㈱');
1595
$this->assertTextContains("Content-Type: text/plain; charset=ISO-2022-JP", $result['headers']);
1596
$this->assertTextNotContains("Content-Type: text/plain; charset=ISO-2022-JP-MS", $result['headers']); // not charset=iso-2022-jp-ms
1597
$this->assertTextNotContains(mb_convert_encoding('①㈱','ISO-2022-JP-MS'), $result['message']);
1601
* Tests that the body is encoded using the configured charset (Japanese irregular encoding, but sometime use this)
1605
public function testBodyEncodingIso2022JpMs() {
1606
$this->skipIf(!function_exists('mb_convert_encoding'));
1607
$email = new CakeEmail(array(
1608
'charset' => 'iso-2022-jp-ms',
1609
'headerCharset' => 'iso-2022-jp-ms',
1610
'transport' => 'Debug'
1612
$email->subject('あれ?もしかしての前と');
1613
$headers = $email->getHeaders(array('subject'));
1614
$expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
1615
$this->assertContains($expected, $headers['Subject']);
1617
$email->to('someone@example.com')->from('someone@example.com');
1618
$result = $email->send('①㈱');
1619
$this->assertTextContains("Content-Type: text/plain; charset=ISO-2022-JP", $result['headers']);
1620
$this->assertTextNotContains("Content-Type: text/plain; charset=iso-2022-jp-ms", $result['headers']); // not charset=iso-2022-jp-ms
1621
$this->assertContains(mb_convert_encoding('①㈱','ISO-2022-JP-MS'), $result['message']);
1624
protected function _checkContentTransferEncoding($message, $charset) {
1625
$boundary = '--alt-' . $this->CakeEmail->getBoundary();
1626
$result['text'] = false;
1627
$result['html'] = false;
1628
$length = count($message);
1629
for ($i = 0; $i < $length; ++$i) {
1630
if ($message[$i] == $boundary) {
1633
while (!preg_match('/^$/', $message[$i])) {
1634
if (preg_match('/^Content-Type: text\/plain/', $message[$i])) {
1637
if (preg_match('/^Content-Type: text\/html/', $message[$i])) {
1640
if ($message[$i] === 'Content-Transfer-Encoding: ' . $charset) {
1645
$result[$type] = $flag;
1648
return $result['text'] && $result['html'];
1652
* Test CakeEmail::_encode function
1656
public function testEncode() {
1657
$this->skipIf(!function_exists('mb_convert_encoding'));
1659
$this->CakeEmail->headerCharset = 'ISO-2022-JP';
1660
$result = $this->CakeEmail->encode('日本語');
1661
$expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?=';
1662
$this->assertSame($expected, $result);
1664
$this->CakeEmail->headerCharset = 'ISO-2022-JP';
1665
$result = $this->CakeEmail->encode('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?');
1666
$expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
1667
" =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
1668
" =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
1669
$this->assertSame($expected, $result);
1673
* Tests charset setter/getter
1677
public function testCharset() {
1678
$this->CakeEmail->charset('UTF-8');
1679
$this->assertSame($this->CakeEmail->charset(), 'UTF-8');
1681
$this->CakeEmail->charset('ISO-2022-JP');
1682
$this->assertSame($this->CakeEmail->charset(), 'ISO-2022-JP');
1684
$charset = $this->CakeEmail->charset('Shift_JIS');
1685
$this->assertSame($charset, 'Shift_JIS');
1689
* Tests headerCharset setter/getter
1693
public function testHeaderCharset() {
1694
$this->CakeEmail->headerCharset('UTF-8');
1695
$this->assertSame($this->CakeEmail->headerCharset(), 'UTF-8');
1697
$this->CakeEmail->headerCharset('ISO-2022-JP');
1698
$this->assertSame($this->CakeEmail->headerCharset(), 'ISO-2022-JP');
1700
$charset = $this->CakeEmail->headerCharset('Shift_JIS');
1701
$this->assertSame($charset, 'Shift_JIS');
1705
* Tests for compatible check.
1706
* charset property and charset() method.
1707
* headerCharset property and headerCharset() method.
1709
public function testCharsetsCompatible() {
1710
$this->skipIf(!function_exists('mb_convert_encoding'));
1712
$checkHeaders = array(
1719
// Header Charset : null (used by default UTF-8)
1720
// Body Charset : ISO-2022-JP
1721
$oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', null);
1722
$oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
1724
$newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', null);
1725
$newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
1727
$this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
1728
$this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
1729
$this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
1730
$this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
1732
// Header Charset : UTF-8
1733
// Boby Charset : ISO-2022-JP
1734
$oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', 'utf-8');
1735
$oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
1737
$newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', 'utf-8');
1738
$newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
1740
$this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
1741
$this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
1742
$this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
1743
$this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
1745
// Header Charset : ISO-2022-JP
1746
// Boby Charset : UTF-8
1747
$oldStyleEmail = $this->_getEmailByOldStyleCharset('utf-8', 'iso-2022-jp');
1748
$oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
1750
$newStyleEmail = $this->_getEmailByNewStyleCharset('utf-8', 'iso-2022-jp');
1751
$newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
1753
$this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
1754
$this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
1755
$this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
1756
$this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
1759
protected function _getEmailByOldStyleCharset($charset, $headerCharset) {
1760
$email = new CakeEmail(array('transport' => 'Debug'));
1762
if (! empty($charset)) {
1763
$email->charset = $charset;
1765
if (! empty($headerCharset)) {
1766
$email->headerCharset = $headerCharset;
1769
$email->from('someone@example.com', 'どこかの誰か');
1770
$email->to('someperson@example.jp', 'どこかのどなたか');
1771
$email->cc('miku@example.net', 'ミク');
1772
$email->subject('テストメール');
1773
$email->send('テストメールの本文');
1778
protected function _getEmailByNewStyleCharset($charset, $headerCharset) {
1779
$email = new CakeEmail(array('transport' => 'Debug'));
1781
if (! empty($charset)) {
1782
$email->charset($charset);
1784
if (! empty($headerCharset)) {
1785
$email->headerCharset($headerCharset);
1788
$email->from('someone@example.com', 'どこかの誰か');
1789
$email->to('someperson@example.jp', 'どこかのどなたか');
1790
$email->cc('miku@example.net', 'ミク');
1791
$email->subject('テストメール');
1792
$email->send('テストメールの本文');