~artur-barczynski/azsystem/trunk

« back to all changes in this revision

Viewing changes to lib/Cake/Test/Case/Network/Email/CakeEmailTest.php

  • Committer: Artur Barczynski
  • Date: 2012-09-20 16:31:07 UTC
  • Revision ID: artur@arturkb.pl-20120920163107-oakeg1a4h9e6d37f
Init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CakeEmailTest file
 
4
 *
 
5
 * PHP 5
 
6
 *
 
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)
 
9
 *
 
10
 * Licensed under The MIT License
 
11
 * Redistributions of files must retain the above copyright notice
 
12
 *
 
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)
 
18
 */
 
19
App::uses('CakeEmail', 'Network/Email');
 
20
 
 
21
/**
 
22
 * Help to test CakeEmail
 
23
 *
 
24
 */
 
25
class TestCakeEmail extends CakeEmail {
 
26
 
 
27
/**
 
28
 * Config
 
29
 *
 
30
 */
 
31
        protected $_config = array();
 
32
 
 
33
/**
 
34
 * Wrap to protected method
 
35
 *
 
36
 */
 
37
        public function formatAddress($address) {
 
38
                return parent::_formatAddress($address);
 
39
        }
 
40
 
 
41
/**
 
42
 * Wrap to protected method
 
43
 *
 
44
 */
 
45
        public function wrap($text) {
 
46
                return parent::_wrap($text);
 
47
        }
 
48
 
 
49
/**
 
50
 * Get the boundary attribute
 
51
 *
 
52
 * @return string
 
53
 */
 
54
        public function getBoundary() {
 
55
                return $this->_boundary;
 
56
        }
 
57
 
 
58
/**
 
59
 * Encode to protected method
 
60
 *
 
61
 */
 
62
        public function encode($text) {
 
63
                return $this->_encode($text);
 
64
        }
 
65
 
 
66
}
 
67
 
 
68
/*
 
69
 * EmailConfig class
 
70
 *
 
71
 */
 
72
class EmailConfig {
 
73
 
 
74
/**
 
75
 * test config
 
76
 *
 
77
 * @var string
 
78
 */
 
79
        public $test = array(
 
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'),
 
86
        );
 
87
 
 
88
}
 
89
 
 
90
/*
 
91
 * ExtendTransport class
 
92
 * test class to ensure the class has send() method
 
93
 *
 
94
 */
 
95
class ExtendTransport {
 
96
 
 
97
}
 
98
 
 
99
/**
 
100
 * CakeEmailTest class
 
101
 *
 
102
 * @package       Cake.Test.Case.Network.Email
 
103
 */
 
104
class CakeEmailTest extends CakeTestCase {
 
105
 
 
106
/**
 
107
 * setUp
 
108
 *
 
109
 * @return void
 
110
 */
 
111
        public function setUp() {
 
112
                parent::setUp();
 
113
                $this->CakeEmail = new TestCakeEmail();
 
114
 
 
115
                App::build(array(
 
116
                        'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
 
117
                ));
 
118
        }
 
119
 
 
120
/**
 
121
 * tearDown method
 
122
 *
 
123
 * @return void
 
124
 */
 
125
        public function tearDown() {
 
126
                parent::tearDown();
 
127
                App::build();
 
128
        }
 
129
 
 
130
/**
 
131
 * testFrom method
 
132
 *
 
133
 * @return void
 
134
 */
 
135
        public function testFrom() {
 
136
                $this->assertSame($this->CakeEmail->from(), array());
 
137
 
 
138
                $this->CakeEmail->from('cake@cakephp.org');
 
139
                $expected = array('cake@cakephp.org' => 'cake@cakephp.org');
 
140
                $this->assertSame($this->CakeEmail->from(), $expected);
 
141
 
 
142
                $this->CakeEmail->from(array('cake@cakephp.org'));
 
143
                $this->assertSame($this->CakeEmail->from(), $expected);
 
144
 
 
145
                $this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
 
146
                $expected = array('cake@cakephp.org' => 'CakePHP');
 
147
                $this->assertSame($this->CakeEmail->from(), $expected);
 
148
 
 
149
                $result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP'));
 
150
                $this->assertSame($this->CakeEmail->from(), $expected);
 
151
                $this->assertSame($this->CakeEmail, $result);
 
152
 
 
153
                $this->setExpectedException('SocketException');
 
154
                $result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address'));
 
155
        }
 
156
 
 
157
/**
 
158
 * testSender method
 
159
 *
 
160
 * @return void
 
161
 */
 
162
        public function testSender() {
 
163
                $this->CakeEmail->reset();
 
164
                $this->assertSame($this->CakeEmail->sender(), array());
 
165
 
 
166
                $this->CakeEmail->sender('cake@cakephp.org', 'Name');
 
167
                $expected = array('cake@cakephp.org' => 'Name');
 
168
                $this->assertSame($this->CakeEmail->sender(), $expected);
 
169
 
 
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>');
 
173
 
 
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'], '');
 
178
        }
 
179
 
 
180
/**
 
181
 * testTo method
 
182
 *
 
183
 * @return void
 
184
 */
 
185
        public function testTo() {
 
186
                $this->assertSame($this->CakeEmail->to(), array());
 
187
 
 
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);
 
192
 
 
193
                $this->CakeEmail->to('cake@cakephp.org', 'CakePHP');
 
194
                $expected = array('cake@cakephp.org' => 'CakePHP');
 
195
                $this->assertSame($this->CakeEmail->to(), $expected);
 
196
 
 
197
                $list = array(
 
198
                        'cake@cakephp.org' => 'Cake PHP',
 
199
                        'cake-php@googlegroups.com' => 'Cake Groups',
 
200
                        'root@cakephp.org'
 
201
                );
 
202
                $this->CakeEmail->to($list);
 
203
                $expected = array(
 
204
                        'cake@cakephp.org' => 'Cake PHP',
 
205
                        'cake-php@googlegroups.com' => 'Cake Groups',
 
206
                        'root@cakephp.org' => 'root@cakephp.org'
 
207
                );
 
208
                $this->assertSame($this->CakeEmail->to(), $expected);
 
209
 
 
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'));
 
213
                $expected = array(
 
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'
 
221
                );
 
222
                $this->assertSame($this->CakeEmail->to(), $expected);
 
223
                $this->assertSame($this->CakeEmail, $result);
 
224
        }
 
225
 
 
226
/**
 
227
 * Data provider function for testBuildInvalidData
 
228
 *
 
229
 * @return array
 
230
 */
 
231
        public static function invalidEmails() {
 
232
                return array(
 
233
                        array(1.0),
 
234
                        array(''),
 
235
                        array('string'),
 
236
                        array('<tag>'),
 
237
                        array('some@one.whereis'),
 
238
                        array('wrong@key' => 'Name'),
 
239
                        array(array('ok@cakephp.org', 1.0, '', 'string'))
 
240
                );
 
241
        }
 
242
 
 
243
/**
 
244
 * testBuildInvalidData
 
245
 *
 
246
 * @dataProvider invalidEmails
 
247
 * @expectedException SocketException
 
248
 * @return void
 
249
 */
 
250
        public function testInvalidEmail($value) {
 
251
                $this->CakeEmail->to($value);
 
252
        }
 
253
 
 
254
/**
 
255
 * testBuildInvalidData
 
256
 *
 
257
 * @dataProvider invalidEmails
 
258
 * @expectedException SocketException
 
259
 * @return void
 
260
 */
 
261
        public function testInvalidEmailAdd($value) {
 
262
                $this->CakeEmail->addTo($value);
 
263
        }
 
264
 
 
265
/**
 
266
 * testFormatAddress method
 
267
 *
 
268
 * @return void
 
269
 */
 
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);
 
274
 
 
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);
 
278
 
 
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);
 
282
 
 
283
                $result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last, First'));
 
284
                $expected = array('"Last, First" <me@example.com>');
 
285
                $this->assertSame($expected, $result);
 
286
 
 
287
                $result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last First'));
 
288
                $expected = array('Last First <me@example.com>');
 
289
                $this->assertSame($expected, $result);
 
290
 
 
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);
 
294
 
 
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);
 
298
        }
 
299
 
 
300
/**
 
301
 * testFormatAddressJapanese
 
302
 *
 
303
 * @return void
 
304
 */
 
305
        public function testFormatAddressJapanese() {
 
306
                $this->skipIf(!function_exists('mb_convert_encoding'));
 
307
 
 
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);
 
312
 
 
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);
 
321
        }
 
322
 
 
323
/**
 
324
 * testAddresses method
 
325
 *
 
326
 * @return void
 
327
 */
 
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');
 
340
 
 
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'));
 
348
 
 
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>');
 
357
        }
 
358
 
 
359
/**
 
360
 * testMessageId method
 
361
 *
 
362
 * @return void
 
363
 */
 
364
        public function testMessageId() {
 
365
                $this->CakeEmail->messageId(true);
 
366
                $result = $this->CakeEmail->getHeaders();
 
367
                $this->assertTrue(isset($result['Message-ID']));
 
368
 
 
369
                $this->CakeEmail->messageId(false);
 
370
                $result = $this->CakeEmail->getHeaders();
 
371
                $this->assertFalse(isset($result['Message-ID']));
 
372
 
 
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>');
 
377
 
 
378
                $result = $this->CakeEmail->messageId();
 
379
                $this->assertSame($result, '<my-email@localhost>');
 
380
        }
 
381
 
 
382
/**
 
383
 * testMessageIdInvalid method
 
384
 *
 
385
 * @return void
 
386
 * @expectedException SocketException
 
387
 */
 
388
        public function testMessageIdInvalid() {
 
389
                $this->CakeEmail->messageId('my-email@localhost');
 
390
        }
 
391
 
 
392
/**
 
393
 * testDomain method
 
394
 *
 
395
 * @return void
 
396
 */
 
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);
 
401
 
 
402
                $this->CakeEmail->domain('example.org');
 
403
                $result = $this->CakeEmail->domain();
 
404
                $expected = 'example.org';
 
405
                $this->assertSame($expected, $result);
 
406
        }
 
407
 
 
408
/**
 
409
 * testMessageIdWithDomain method
 
410
 *
 
411
 * @return void
 
412
 */
 
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']);
 
417
 
 
418
                $this->CakeEmail->domain('example.org');
 
419
                $result = $this->CakeEmail->getHeaders();
 
420
                $expected = '@example.org>';
 
421
                $this->assertTextContains($expected, $result['Message-ID']);
 
422
        }
 
423
 
 
424
/**
 
425
 * testSubject method
 
426
 *
 
427
 * @return void
 
428
 */
 
429
        public function testSubject() {
 
430
                $this->CakeEmail->subject('You have a new message.');
 
431
                $this->assertSame($this->CakeEmail->subject(), 'You have a new message.');
 
432
 
 
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');
 
437
 
 
438
                $this->CakeEmail->subject('هذه رسالة بعنوان طويل مرسل للمستلم');
 
439
                $expected = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
 
440
                $this->assertSame($this->CakeEmail->subject(), $expected);
 
441
        }
 
442
 
 
443
/**
 
444
 * testSubjectJapanese
 
445
 *
 
446
 * @return void
 
447
 */
 
448
        public function testSubjectJapanese() {
 
449
                $this->skipIf(!function_exists('mb_convert_encoding'));
 
450
                mb_internal_encoding('UTF-8');
 
451
 
 
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);
 
456
 
 
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);
 
462
        }
 
463
 
 
464
/**
 
465
 * testHeaders method
 
466
 *
 
467
 * @return void
 
468
 */
 
469
        public function testHeaders() {
 
470
                $this->CakeEmail->messageId(false);
 
471
                $this->CakeEmail->setHeaders(array('X-Something' => 'nice'));
 
472
                $expected = array(
 
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'
 
479
                );
 
480
                $this->assertSame($this->CakeEmail->getHeaders(), $expected);
 
481
 
 
482
                $this->CakeEmail->addHeaders(array('X-Something' => 'very nice', 'X-Other' => 'cool'));
 
483
                $expected = array(
 
484
                        'X-Something' => 'very nice',
 
485
                        'X-Other' => 'cool',
 
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'
 
491
                );
 
492
                $this->assertSame($this->CakeEmail->getHeaders(), $expected);
 
493
 
 
494
                $this->CakeEmail->from('cake@cakephp.org');
 
495
                $this->assertSame($this->CakeEmail->getHeaders(), $expected);
 
496
 
 
497
                $expected = array(
 
498
                        'From' => 'cake@cakephp.org',
 
499
                        'X-Something' => 'very nice',
 
500
                        'X-Other' => 'cool',
 
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'
 
506
                );
 
507
                $this->assertSame($this->CakeEmail->getHeaders(array('from' => true)), $expected);
 
508
 
 
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);
 
512
 
 
513
                $this->CakeEmail->to(array('cake@cakephp.org', 'php@cakephp.org' => 'CakePHP'));
 
514
                $expected = array(
 
515
                        'From' => 'CakePHP <cake@cakephp.org>',
 
516
                        'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
 
517
                        'X-Something' => 'very nice',
 
518
                        'X-Other' => 'cool',
 
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'
 
524
                );
 
525
                $this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
 
526
 
 
527
                $this->CakeEmail->charset = 'ISO-2022-JP';
 
528
                $expected = array(
 
529
                        'From' => 'CakePHP <cake@cakephp.org>',
 
530
                        'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
 
531
                        'X-Something' => 'very nice',
 
532
                        'X-Other' => 'cool',
 
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'
 
538
                );
 
539
                $this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
 
540
 
 
541
                $result = $this->CakeEmail->setHeaders(array());
 
542
                $this->assertInstanceOf('CakeEmail', $result);
 
543
        }
 
544
 
 
545
/**
 
546
 * Data provider function for testInvalidHeaders
 
547
 *
 
548
 * @return array
 
549
 */
 
550
        public static function invalidHeaders() {
 
551
                return array(
 
552
                        array(10),
 
553
                        array(''),
 
554
                        array('string'),
 
555
                        array(false),
 
556
                        array(null)
 
557
                );
 
558
        }
 
559
 
 
560
/**
 
561
 * testInvalidHeaders
 
562
 *
 
563
 * @dataProvider invalidHeaders
 
564
 * @expectedException SocketException
 
565
 * @return void
 
566
 */
 
567
        public function testInvalidHeaders($value) {
 
568
                $this->CakeEmail->setHeaders($value);
 
569
        }
 
570
 
 
571
/**
 
572
 * testInvalidAddHeaders
 
573
 *
 
574
 * @dataProvider invalidHeaders
 
575
 * @expectedException SocketException
 
576
 * @return void
 
577
 */
 
578
        public function testInvalidAddHeaders($value) {
 
579
                $this->CakeEmail->addHeaders($value);
 
580
        }
 
581
 
 
582
/**
 
583
 * testTemplate method
 
584
 *
 
585
 * @return void
 
586
 */
 
587
        public function testTemplate() {
 
588
                $this->CakeEmail->template('template', 'layout');
 
589
                $expected = array('template' => 'template', 'layout' => 'layout');
 
590
                $this->assertSame($this->CakeEmail->template(), $expected);
 
591
 
 
592
                $this->CakeEmail->template('new_template');
 
593
                $expected = array('template' => 'new_template', 'layout' => 'layout');
 
594
                $this->assertSame($this->CakeEmail->template(), $expected);
 
595
 
 
596
                $this->CakeEmail->template('template', null);
 
597
                $expected = array('template' => 'template', 'layout' => null);
 
598
                $this->assertSame($this->CakeEmail->template(), $expected);
 
599
 
 
600
                $this->CakeEmail->template(null, null);
 
601
                $expected = array('template' => null, 'layout' => null);
 
602
                $this->assertSame($this->CakeEmail->template(), $expected);
 
603
        }
 
604
 
 
605
/**
 
606
 * testTheme method
 
607
 *
 
608
 * @return void
 
609
 */
 
610
        public function testTheme() {
 
611
                $this->assertSame(null, $this->CakeEmail->theme());
 
612
 
 
613
                $this->CakeEmail->theme('default');
 
614
                $expected = 'default';
 
615
                $this->assertSame($expected, $this->CakeEmail->theme());
 
616
        }
 
617
 
 
618
/**
 
619
 * testViewVars method
 
620
 *
 
621
 * @return void
 
622
 */
 
623
        public function testViewVars() {
 
624
                $this->assertSame($this->CakeEmail->viewVars(), array());
 
625
 
 
626
                $this->CakeEmail->viewVars(array('value' => 12345));
 
627
                $this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345));
 
628
 
 
629
                $this->CakeEmail->viewVars(array('name' => 'CakePHP'));
 
630
                $this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345, 'name' => 'CakePHP'));
 
631
 
 
632
                $this->CakeEmail->viewVars(array('value' => 4567));
 
633
                $this->assertSame($this->CakeEmail->viewVars(), array('value' => 4567, 'name' => 'CakePHP'));
 
634
        }
 
635
 
 
636
/**
 
637
 * testAttachments method
 
638
 *
 
639
 * @return void
 
640
 */
 
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);
 
645
 
 
646
                $this->CakeEmail->attachments(array());
 
647
                $this->assertSame($this->CakeEmail->attachments(), array());
 
648
 
 
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'));
 
653
                $expected = array(
 
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')
 
658
                );
 
659
                $this->assertSame($this->CakeEmail->attachments(), $expected);
 
660
 
 
661
                $this->setExpectedException('SocketException');
 
662
                $this->CakeEmail->attachments(array(array('nofile' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
 
663
        }
 
664
 
 
665
/**
 
666
 * testTransport method
 
667
 *
 
668
 * @return void
 
669
 */
 
670
        public function testTransport() {
 
671
                $result = $this->CakeEmail->transport('Debug');
 
672
                $this->assertSame($this->CakeEmail, $result);
 
673
                $this->assertSame($this->CakeEmail->transport(), 'Debug');
 
674
 
 
675
                $result = $this->CakeEmail->transportClass();
 
676
                $this->assertInstanceOf('DebugTransport', $result);
 
677
 
 
678
                $this->setExpectedException('SocketException');
 
679
                $this->CakeEmail->transport('Invalid');
 
680
                $result = $this->CakeEmail->transportClass();
 
681
        }
 
682
 
 
683
/**
 
684
 * testExtendTransport method
 
685
 *
 
686
 * @return void
 
687
 */
 
688
        public function testExtendTransport() {
 
689
                $this->setExpectedException('SocketException');
 
690
                $this->CakeEmail->transport('Extend');
 
691
                $result = $this->CakeEmail->transportClass();
 
692
        }
 
693
 
 
694
/**
 
695
 * testConfig method
 
696
 *
 
697
 * @return void
 
698
 */
 
699
        public function testConfig() {
 
700
                $transportClass = $this->CakeEmail->transport('debug')->transportClass();
 
701
 
 
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);
 
706
 
 
707
                $this->CakeEmail->config(array());
 
708
                $this->assertSame($transportClass->config(), array());
 
709
        }
 
710
 
 
711
/**
 
712
 * testConfigString method
 
713
 *
 
714
 * @return void
 
715
 */
 
716
        public function testConfigString() {
 
717
                $configs = new EmailConfig();
 
718
                $this->CakeEmail->config('test');
 
719
 
 
720
                $result = $this->CakeEmail->to();
 
721
                $this->assertEquals($configs->test['to'], $result);
 
722
 
 
723
                $result = $this->CakeEmail->from();
 
724
                $this->assertEquals($configs->test['from'], $result);
 
725
 
 
726
                $result = $this->CakeEmail->subject();
 
727
                $this->assertEquals($configs->test['subject'], $result);
 
728
 
 
729
                $result = $this->CakeEmail->theme();
 
730
                $this->assertEquals($configs->test['theme'], $result);
 
731
 
 
732
                $result = $this->CakeEmail->transport();
 
733
                $this->assertEquals($configs->test['transport'], $result);
 
734
 
 
735
                $result = $this->CakeEmail->transportClass();
 
736
                $this->assertInstanceOf('DebugTransport', $result);
 
737
 
 
738
                $result = $this->CakeEmail->helpers();
 
739
                $this->assertEquals($configs->test['helpers'], $result);
 
740
        }
 
741
 
 
742
/**
 
743
 * testSendWithContent method
 
744
 *
 
745
 * @return void
 
746
 */
 
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'));
 
754
 
 
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";
 
759
 
 
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: '));
 
764
 
 
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: '));
 
770
 
 
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);
 
780
        }
 
781
 
 
782
/**
 
783
 * testSendWithoutFrom method
 
784
 *
 
785
 * @return void
 
786
 */
 
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");
 
794
        }
 
795
 
 
796
/**
 
797
 * testSendWithoutTo method
 
798
 *
 
799
 * @return void
 
800
 */
 
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");
 
808
        }
 
809
 
 
810
/**
 
811
 * Test send() with no template.
 
812
 *
 
813
 * @return void
 
814
 */
 
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');
 
823
 
 
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" .
 
829
                        "\r\n" .
 
830
                        "Hello" .
 
831
                        "\r\n" .
 
832
                        "\r\n" .
 
833
                        "\r\n" .
 
834
                        "--$boundary\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']);
 
839
        }
 
840
 
 
841
/**
 
842
 * Test send() with no template as both
 
843
 *
 
844
 * @return void
 
845
 */
 
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');
 
854
 
 
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" .
 
859
                        "\r\n" .
 
860
                        "--alt-$boundary\r\n" .
 
861
                        "Content-Type: text/plain; charset=UTF-8\r\n" .
 
862
                        "Content-Transfer-Encoding: 8bit\r\n" .
 
863
                        "\r\n" .
 
864
                        "Hello" .
 
865
                        "\r\n" .
 
866
                        "\r\n" .
 
867
                        "\r\n" .
 
868
                        "--alt-$boundary\r\n" .
 
869
                        "Content-Type: text/html; charset=UTF-8\r\n" .
 
870
                        "Content-Transfer-Encoding: 8bit\r\n" .
 
871
                        "\r\n" .
 
872
                        "Hello" .
 
873
                        "\r\n" .
 
874
                        "\r\n" .
 
875
                        "\r\n" .
 
876
                        "--alt-{$boundary}--\r\n" .
 
877
                        "\r\n" .
 
878
                        "--$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']);
 
883
        }
 
884
 
 
885
/**
 
886
 * Test setting inline attachments and messages.
 
887
 *
 
888
 * @return void
 
889
 */
 
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(
 
897
                        'cake.png' => array(
 
898
                                'file' => CAKE . 'VERSION.txt',
 
899
                                'contentId' => 'abc123'
 
900
                        )
 
901
                ));
 
902
                $result = $this->CakeEmail->send('Hello');
 
903
 
 
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" .
 
908
                        "\r\n" .
 
909
                        "--rel-$boundary\r\n" .
 
910
                        "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
 
911
                        "\r\n" .
 
912
                        "--alt-$boundary\r\n" .
 
913
                        "Content-Type: text/plain; charset=UTF-8\r\n" .
 
914
                        "Content-Transfer-Encoding: 8bit\r\n" .
 
915
                        "\r\n" .
 
916
                        "Hello" .
 
917
                        "\r\n" .
 
918
                        "\r\n" .
 
919
                        "\r\n" .
 
920
                        "--alt-$boundary\r\n" .
 
921
                        "Content-Type: text/html; charset=UTF-8\r\n" .
 
922
                        "Content-Transfer-Encoding: 8bit\r\n" .
 
923
                        "\r\n" .
 
924
                        "Hello" .
 
925
                        "\r\n" .
 
926
                        "\r\n" .
 
927
                        "\r\n" .
 
928
                        "--alt-{$boundary}--\r\n" .
 
929
                        "\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']);
 
938
        }
 
939
 
 
940
/**
 
941
 * testSendWithLog method
 
942
 *
 
943
 * @return void
 
944
 */
 
945
        public function testSendWithLog() {
 
946
                $path = CAKE . 'Test' . DS . 'test_app' . DS . 'tmp' . DS;
 
947
                CakeLog::config('email', array(
 
948
                        'engine' => 'FileLog',
 
949
                        'path' => TMP
 
950
                ));
 
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");
 
958
 
 
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);
 
964
                $File->delete();
 
965
                CakeLog::drop('email');
 
966
        }
 
967
 
 
968
/**
 
969
 * testSendRender method
 
970
 *
 
971
 * @return void
 
972
 */
 
973
        public function testSendRender() {
 
974
                $this->CakeEmail->reset();
 
975
                $this->CakeEmail->transport('debug');
 
976
 
 
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();
 
983
 
 
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']);
 
987
        }
 
988
 
 
989
/**
 
990
 * testSendRender method for ISO-2022-JP
 
991
 *
 
992
 * @return void
 
993
 */
 
994
        public function testSendRenderJapanese() {
 
995
                $this->skipIf(!function_exists('mb_convert_encoding'));
 
996
 
 
997
                $this->CakeEmail->reset();
 
998
                $this->CakeEmail->transport('debug');
 
999
 
 
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();
 
1007
 
 
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']);
 
1012
        }
 
1013
 
 
1014
/**
 
1015
 * testSendRenderThemed method
 
1016
 *
 
1017
 * @return void
 
1018
 */
 
1019
        public function testSendRenderThemed() {
 
1020
                $this->CakeEmail->reset();
 
1021
                $this->CakeEmail->transport('debug');
 
1022
 
 
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();
 
1030
 
 
1031
                $this->assertContains('In TestTheme', $result['message']);
 
1032
                $this->assertContains('Message-ID: ', $result['headers']);
 
1033
                $this->assertContains('To: ', $result['headers']);
 
1034
        }
 
1035
 
 
1036
/**
 
1037
 * testSendRenderWithVars method
 
1038
 *
 
1039
 * @return void
 
1040
 */
 
1041
        public function testSendRenderWithVars() {
 
1042
                $this->CakeEmail->reset();
 
1043
                $this->CakeEmail->transport('debug');
 
1044
 
 
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();
 
1052
 
 
1053
                $this->assertContains('Here is your value: 12345', $result['message']);
 
1054
        }
 
1055
 
 
1056
/**
 
1057
 * testSendRenderWithVars method for ISO-2022-JP
 
1058
 *
 
1059
 * @return void
 
1060
 */
 
1061
        public function testSendRenderWithVarsJapanese() {
 
1062
                $this->skipIf(!function_exists('mb_convert_encoding'));
 
1063
                $this->CakeEmail->reset();
 
1064
                $this->CakeEmail->transport('debug');
 
1065
 
 
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();
 
1074
 
 
1075
                $expected = mb_convert_encoding('ここにあなたの設定した値が入ります: 日本語の差し込み123', 'ISO-2022-JP');
 
1076
                $this->assertTrue((bool)strpos($result['message'], $expected));
 
1077
        }
 
1078
 
 
1079
/**
 
1080
 * testSendRenderWithHelpers method
 
1081
 *
 
1082
 * @return void
 
1083
 */
 
1084
        public function testSendRenderWithHelpers() {
 
1085
                $this->CakeEmail->reset();
 
1086
                $this->CakeEmail->transport('debug');
 
1087
 
 
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));
 
1095
 
 
1096
                $result = $this->CakeEmail->helpers(array('Time'));
 
1097
                $this->assertInstanceOf('CakeEmail', $result);
 
1098
 
 
1099
                $result = $this->CakeEmail->send();
 
1100
                $this->assertTrue((bool)strpos($result['message'], 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
 
1101
 
 
1102
                $result = $this->CakeEmail->helpers();
 
1103
                $this->assertEquals(array('Time'), $result);
 
1104
        }
 
1105
 
 
1106
/**
 
1107
 * testSendRenderWithImage method
 
1108
 *
 
1109
 * @return void
 
1110
 */
 
1111
        public function testSendRenderWithImage() {
 
1112
                $this->CakeEmail->reset();
 
1113
                $this->CakeEmail->transport('Debug');
 
1114
 
 
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';
 
1122
 
 
1123
                if (env('SERVER_PORT') != null && env('SERVER_PORT') != 80) {
 
1124
                        $server .= ':' . env('SERVER_PORT');
 
1125
                }
 
1126
 
 
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']);
 
1130
        }
 
1131
 
 
1132
/**
 
1133
 * testSendRenderPlugin method
 
1134
 *
 
1135
 * @return void
 
1136
 */
 
1137
        public function testSendRenderPlugin() {
 
1138
                App::build(array(
 
1139
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
 
1140
                ));
 
1141
                CakePlugin::load('TestPlugin');
 
1142
 
 
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'));
 
1149
 
 
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']);
 
1153
 
 
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']);
 
1157
 
 
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']);
 
1161
 
 
1162
                // test plugin template overridden by theme
 
1163
                $this->CakeEmail->theme('TestTheme');
 
1164
                $result = $this->CakeEmail->send();
 
1165
 
 
1166
                $this->assertContains('Into TestPlugin. (themed)', $result['message']);
 
1167
 
 
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']);
 
1172
 
 
1173
                $this->setExpectedException('MissingViewException');
 
1174
                $this->CakeEmail->template('test_plugin_tpl', 'plug_default')->send();
 
1175
        }
 
1176
 
 
1177
/**
 
1178
 * testSendMultipleMIME method
 
1179
 *
 
1180
 * @return void
 
1181
 */
 
1182
        public function testSendMultipleMIME() {
 
1183
                $this->CakeEmail->reset();
 
1184
                $this->CakeEmail->transport('debug');
 
1185
 
 
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();
 
1194
 
 
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);
 
1202
 
 
1203
                $this->CakeEmail->attachments(array('fake.php' => __FILE__));
 
1204
                $this->CakeEmail->send();
 
1205
 
 
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);
 
1213
        }
 
1214
 
 
1215
/**
 
1216
 * testSendAttachment method
 
1217
 *
 
1218
 * @return void
 
1219
 */
 
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']);
 
1230
 
 
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']);
 
1234
 
 
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']);
 
1238
 
 
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']);
 
1242
        }
 
1243
 
 
1244
/**
 
1245
 * testDeliver method
 
1246
 *
 
1247
 * @return void
 
1248
 */
 
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'));
 
1255
 
 
1256
                $config = array(
 
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')
 
1264
                );
 
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'));
 
1272
 
 
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]);
 
1277
        }
 
1278
 
 
1279
/**
 
1280
 * testMessage method
 
1281
 *
 
1282
 * @return void
 
1283
 */
 
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();
 
1294
 
 
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));
 
1297
 
 
1298
                $expected = 'This email was sent using the CakePHP Framework, http://cakephp.org.';
 
1299
                $this->assertContains($expected, $this->CakeEmail->message(CakeEmail::MESSAGE_TEXT));
 
1300
 
 
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);
 
1304
 
 
1305
                // UTF-8 is 8bit
 
1306
                $this->assertTrue($this->_checkContentTransferEncoding($message, '8bit'));
 
1307
 
 
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);
 
1313
 
 
1314
                // ISO-2022-JP is 7bit
 
1315
                $this->assertTrue($this->_checkContentTransferEncoding($message, '7bit'));
 
1316
        }
 
1317
 
 
1318
/**
 
1319
 * testReset method
 
1320
 *
 
1321
 * @return void
 
1322
 */
 
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'));
 
1327
 
 
1328
                $this->CakeEmail->reset();
 
1329
                $this->assertSame($this->CakeEmail->to(), array());
 
1330
                $this->assertSame(null, $this->CakeEmail->theme());
 
1331
        }
 
1332
 
 
1333
/**
 
1334
 * testReset with charset
 
1335
 *
 
1336
 * @return void
 
1337
 */
 
1338
        public function testResetWithCharset() {
 
1339
                $this->CakeEmail->charset = 'ISO-2022-JP';
 
1340
                $this->CakeEmail->reset();
 
1341
 
 
1342
                $this->assertSame($this->CakeEmail->charset, 'utf-8', $this->CakeEmail->charset);
 
1343
                $this->assertSame($this->CakeEmail->headerCharset, null, $this->CakeEmail->headerCharset);
 
1344
        }
 
1345
 
 
1346
/**
 
1347
 * testWrap method
 
1348
 *
 
1349
 * @return void
 
1350
 */
 
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);
 
1354
                $expected = array(
 
1355
                        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
 
1356
                        'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
 
1357
                        ''
 
1358
                );
 
1359
                $this->assertSame($expected, $result);
 
1360
 
 
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);
 
1363
                $expected = array(
 
1364
                        'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
 
1365
                        'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
 
1366
                        'amet.',
 
1367
                        ''
 
1368
                );
 
1369
                $this->assertSame($expected, $result);
 
1370
 
 
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);
 
1373
                $expected = array(
 
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>',
 
1377
                        ''
 
1378
                );
 
1379
                $this->assertSame($expected, $result);
 
1380
 
 
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);
 
1383
                $expected = array(
 
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.',
 
1387
                        ''
 
1388
                );
 
1389
                $this->assertSame($expected, $result);
 
1390
 
 
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);
 
1393
                $expected = array(
 
1394
                        'Lorem ipsum',
 
1395
                        '<a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">',
 
1396
                        'ok</a>',
 
1397
                        ''
 
1398
                );
 
1399
                $this->assertSame($expected, $result);
 
1400
 
 
1401
                $text = 'Lorem ipsum withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite ok.';
 
1402
                $result = $this->CakeEmail->wrap($text);
 
1403
                $expected = array(
 
1404
                        'Lorem ipsum',
 
1405
                        'withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite',
 
1406
                        'ok.',
 
1407
                        ''
 
1408
                );
 
1409
                $this->assertSame($expected, $result);
 
1410
        }
 
1411
 
 
1412
/**
 
1413
 * testConstructWithConfigArray method
 
1414
 *
 
1415
 * @return void
 
1416
 */
 
1417
        public function testConstructWithConfigArray() {
 
1418
                $configs = array(
 
1419
                        'from' => array('some@example.com' => 'My website'),
 
1420
                        'to' => 'test@example.com',
 
1421
                        'subject' => 'Test mail subject',
 
1422
                        'transport' => 'Debug',
 
1423
                );
 
1424
                $this->CakeEmail = new CakeEmail($configs);
 
1425
 
 
1426
                $result = $this->CakeEmail->to();
 
1427
                $this->assertEquals(array($configs['to'] => $configs['to']), $result);
 
1428
 
 
1429
                $result = $this->CakeEmail->from();
 
1430
                $this->assertEquals($configs['from'], $result);
 
1431
 
 
1432
                $result = $this->CakeEmail->subject();
 
1433
                $this->assertEquals($configs['subject'], $result);
 
1434
 
 
1435
                $result = $this->CakeEmail->transport();
 
1436
                $this->assertEquals($configs['transport'], $result);
 
1437
 
 
1438
                $result = $this->CakeEmail->transportClass();
 
1439
                $this->assertTrue($result instanceof DebugTransport);
 
1440
 
 
1441
                $result = $this->CakeEmail->send('This is the message');
 
1442
 
 
1443
                $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
 
1444
                $this->assertTrue((bool)strpos($result['headers'], 'To: '));
 
1445
        }
 
1446
 
 
1447
/**
 
1448
 * testConstructWithConfigString method
 
1449
 *
 
1450
 * @return void
 
1451
 */
 
1452
        public function testConstructWithConfigString() {
 
1453
                $configs = new EmailConfig();
 
1454
                $this->CakeEmail = new CakeEmail('test');
 
1455
 
 
1456
                $result = $this->CakeEmail->to();
 
1457
                $this->assertEquals($configs->test['to'], $result);
 
1458
 
 
1459
                $result = $this->CakeEmail->from();
 
1460
                $this->assertEquals($configs->test['from'], $result);
 
1461
 
 
1462
                $result = $this->CakeEmail->subject();
 
1463
                $this->assertEquals($configs->test['subject'], $result);
 
1464
 
 
1465
                $result = $this->CakeEmail->transport();
 
1466
                $this->assertEquals($configs->test['transport'], $result);
 
1467
 
 
1468
                $result = $this->CakeEmail->transportClass();
 
1469
                $this->assertTrue($result instanceof DebugTransport);
 
1470
 
 
1471
                $result = $this->CakeEmail->send('This is the message');
 
1472
 
 
1473
                $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
 
1474
                $this->assertTrue((bool)strpos($result['headers'], 'To: '));
 
1475
        }
 
1476
 
 
1477
/**
 
1478
 * testViewRender method
 
1479
 *
 
1480
 * @return void
 
1481
 */
 
1482
        public function testViewRender() {
 
1483
                $result = $this->CakeEmail->viewRender();
 
1484
                $this->assertEquals('View', $result);
 
1485
 
 
1486
                $result = $this->CakeEmail->viewRender('Theme');
 
1487
                $this->assertInstanceOf('CakeEmail', $result);
 
1488
 
 
1489
                $result = $this->CakeEmail->viewRender();
 
1490
                $this->assertEquals('Theme', $result);
 
1491
        }
 
1492
 
 
1493
/**
 
1494
 * testEmailFormat method
 
1495
 *
 
1496
 * @return void
 
1497
 */
 
1498
        public function testEmailFormat() {
 
1499
                $result = $this->CakeEmail->emailFormat();
 
1500
                $this->assertEquals('text', $result);
 
1501
 
 
1502
                $result = $this->CakeEmail->emailFormat('html');
 
1503
                $this->assertInstanceOf('CakeEmail', $result);
 
1504
 
 
1505
                $result = $this->CakeEmail->emailFormat();
 
1506
                $this->assertEquals('html', $result);
 
1507
 
 
1508
                $this->setExpectedException('SocketException');
 
1509
                $result = $this->CakeEmail->emailFormat('invalid');
 
1510
        }
 
1511
 
 
1512
/**
 
1513
 * Tests that it is possible to add charset configuration to a CakeEmail object
 
1514
 *
 
1515
 * @return void
 
1516
 */
 
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);
 
1521
 
 
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);
 
1525
 
 
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);
 
1529
 
 
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);
 
1533
        }
 
1534
 
 
1535
/**
 
1536
 * Tests that the header is encoded using the configured headerCharset
 
1537
 *
 
1538
 * @return void
 
1539
 */
 
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']);
 
1547
 
 
1548
                $email->to('someone@example.com')->from('someone@example.com');
 
1549
                $result = $email->send('ってテーブルを作ってやってたらう');
 
1550
                $this->assertContains('ってテーブルを作ってやってたらう', $result['message']);
 
1551
        }
 
1552
 
 
1553
/**
 
1554
 * Tests that the body is encoded using the configured charset
 
1555
 *
 
1556
 * @return void
 
1557
 */
 
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'
 
1564
                ));
 
1565
                $email->subject('あれ?もしかしての前と');
 
1566
                $headers = $email->getHeaders(array('subject'));
 
1567
                $expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
 
1568
                $this->assertContains($expected, $headers['Subject']);
 
1569
 
 
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']);
 
1574
        }
 
1575
 
 
1576
/**
 
1577
 * Tests that the body is encoded using the configured charset (Japanese standard encoding)
 
1578
 *
 
1579
 * @return void
 
1580
 */
 
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'
 
1587
                ));
 
1588
                $email->subject('あれ?もしかしての前と');
 
1589
                $headers = $email->getHeaders(array('subject'));
 
1590
                $expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
 
1591
                $this->assertContains($expected, $headers['Subject']);
 
1592
 
 
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']);
 
1598
        }
 
1599
 
 
1600
/**
 
1601
 * Tests that the body is encoded using the configured charset (Japanese irregular encoding, but sometime use this)
 
1602
 *
 
1603
 * @return void
 
1604
 */
 
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'
 
1611
                ));
 
1612
                $email->subject('あれ?もしかしての前と');
 
1613
                $headers = $email->getHeaders(array('subject'));
 
1614
                $expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
 
1615
                $this->assertContains($expected, $headers['Subject']);
 
1616
 
 
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']);
 
1622
        }
 
1623
 
 
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) {
 
1631
                                $flag = false;
 
1632
                                $type = '';
 
1633
                                while (!preg_match('/^$/', $message[$i])) {
 
1634
                                        if (preg_match('/^Content-Type: text\/plain/', $message[$i])) {
 
1635
                                                $type = 'text';
 
1636
                                        }
 
1637
                                        if (preg_match('/^Content-Type: text\/html/', $message[$i])) {
 
1638
                                                $type = 'html';
 
1639
                                        }
 
1640
                                        if ($message[$i] === 'Content-Transfer-Encoding: ' . $charset) {
 
1641
                                                $flag = true;
 
1642
                                        }
 
1643
                                        ++$i;
 
1644
                                }
 
1645
                                $result[$type] = $flag;
 
1646
                        }
 
1647
                }
 
1648
                return $result['text'] && $result['html'];
 
1649
        }
 
1650
 
 
1651
/**
 
1652
 * Test CakeEmail::_encode function
 
1653
 *
 
1654
 * @return void
 
1655
 */
 
1656
        public function testEncode() {
 
1657
                $this->skipIf(!function_exists('mb_convert_encoding'));
 
1658
 
 
1659
                $this->CakeEmail->headerCharset = 'ISO-2022-JP';
 
1660
                $result = $this->CakeEmail->encode('日本語');
 
1661
                $expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?=';
 
1662
                $this->assertSame($expected, $result);
 
1663
 
 
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);
 
1670
        }
 
1671
 
 
1672
/**
 
1673
 * Tests charset setter/getter
 
1674
 *
 
1675
 * @return void
 
1676
 */
 
1677
        public function testCharset() {
 
1678
                $this->CakeEmail->charset('UTF-8');
 
1679
                $this->assertSame($this->CakeEmail->charset(), 'UTF-8');
 
1680
 
 
1681
                $this->CakeEmail->charset('ISO-2022-JP');
 
1682
                $this->assertSame($this->CakeEmail->charset(), 'ISO-2022-JP');
 
1683
 
 
1684
                $charset = $this->CakeEmail->charset('Shift_JIS');
 
1685
                $this->assertSame($charset, 'Shift_JIS');
 
1686
        }
 
1687
 
 
1688
/**
 
1689
 * Tests headerCharset setter/getter
 
1690
 *
 
1691
 * @return void
 
1692
 */
 
1693
        public function testHeaderCharset() {
 
1694
                $this->CakeEmail->headerCharset('UTF-8');
 
1695
                $this->assertSame($this->CakeEmail->headerCharset(), 'UTF-8');
 
1696
 
 
1697
                $this->CakeEmail->headerCharset('ISO-2022-JP');
 
1698
                $this->assertSame($this->CakeEmail->headerCharset(), 'ISO-2022-JP');
 
1699
 
 
1700
                $charset = $this->CakeEmail->headerCharset('Shift_JIS');
 
1701
                $this->assertSame($charset, 'Shift_JIS');
 
1702
        }
 
1703
 
 
1704
/**
 
1705
 * Tests for compatible check.
 
1706
 *          charset property and       charset() method.
 
1707
 *    headerCharset property and headerCharset() method.
 
1708
 */
 
1709
        public function testCharsetsCompatible() {
 
1710
                $this->skipIf(!function_exists('mb_convert_encoding'));
 
1711
 
 
1712
                $checkHeaders = array(
 
1713
                        'from' => true,
 
1714
                        'to' => true,
 
1715
                        'cc' => true,
 
1716
                        'subject' => true,
 
1717
                );
 
1718
 
 
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);
 
1723
 
 
1724
                $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', null);
 
1725
                $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
 
1726
 
 
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']);
 
1731
 
 
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);
 
1736
 
 
1737
                $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', 'utf-8');
 
1738
                $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
 
1739
 
 
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']);
 
1744
 
 
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);
 
1749
 
 
1750
                $newStyleEmail = $this->_getEmailByNewStyleCharset('utf-8', 'iso-2022-jp');
 
1751
                $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
 
1752
 
 
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']);
 
1757
        }
 
1758
 
 
1759
        protected function _getEmailByOldStyleCharset($charset, $headerCharset) {
 
1760
                $email = new CakeEmail(array('transport' => 'Debug'));
 
1761
 
 
1762
                if (! empty($charset)) {
 
1763
                        $email->charset = $charset;
 
1764
                }
 
1765
                if (! empty($headerCharset)) {
 
1766
                        $email->headerCharset = $headerCharset;
 
1767
                }
 
1768
 
 
1769
                $email->from('someone@example.com', 'どこかの誰か');
 
1770
                $email->to('someperson@example.jp', 'どこかのどなたか');
 
1771
                $email->cc('miku@example.net', 'ミク');
 
1772
                $email->subject('テストメール');
 
1773
                $email->send('テストメールの本文');
 
1774
 
 
1775
                return $email;
 
1776
        }
 
1777
 
 
1778
        protected function _getEmailByNewStyleCharset($charset, $headerCharset) {
 
1779
                $email = new CakeEmail(array('transport' => 'Debug'));
 
1780
 
 
1781
                if (! empty($charset)) {
 
1782
                        $email->charset($charset);
 
1783
                }
 
1784
                if (! empty($headerCharset)) {
 
1785
                        $email->headerCharset($headerCharset);
 
1786
                }
 
1787
 
 
1788
                $email->from('someone@example.com', 'どこかの誰か');
 
1789
                $email->to('someperson@example.jp', 'どこかのどなたか');
 
1790
                $email->cc('miku@example.net', 'ミク');
 
1791
                $email->subject('テストメール');
 
1792
                $email->send('テストメールの本文');
 
1793
 
 
1794
                return $email;
 
1795
        }
 
1796
 
 
1797
}