Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * CakeEmailTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Network.Email
15
 * @since         CakePHP(tm) v 2.0.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
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 class name.
29
 *
30
 * Use a the testing config class in this file.
31
 *
32
 * @var string
33
 */
34
	protected $_configClass = 'TestEmailConfig';
35
 
36
/**
37
 * Config
38
 *
39
 */
40
	protected $_config = array();
41
 
42
/**
43
 * Wrap to protected method
44
 *
45
 * @return array
46
 */
47
	public function formatAddress($address) {
48
		return parent::_formatAddress($address);
49
	}
50
 
51
/**
52
 * Wrap to protected method
53
 *
54
 * @return array
55
 */
56
	public function wrap($text, $length = CakeEmail::LINE_LENGTH_MUST) {
57
		return parent::_wrap($text, $length);
58
	}
59
 
60
/**
61
 * Get the boundary attribute
62
 *
63
 * @return string
64
 */
65
	public function getBoundary() {
66
		return $this->_boundary;
67
	}
68
 
69
/**
70
 * Encode to protected method
71
 *
72
 * @return string
73
 */
74
	public function encode($text) {
75
		return $this->_encode($text);
76
	}
77
 
78
/**
79
 * Render to protected method
80
 *
81
 * @return array
82
 */
83
	public function render($content) {
84
		return $this->_render($content);
85
	}
86
 
87
}
88
 
89
/**
90
 * EmailConfig class
91
 *
92
 */
93
class TestEmailConfig {
94
 
95
/**
96
 * test config
97
 *
98
 * @var array
99
 */
100
	public $test = array(
101
		'from' => array('some@example.com' => 'My website'),
102
		'to' => array('test@example.com' => 'Testname'),
103
		'subject' => 'Test mail subject',
104
		'transport' => 'Debug',
105
		'theme' => 'TestTheme',
106
		'helpers' => array('Html', 'Form'),
107
	);
108
 
109
/**
110
 * test config 2
111
 *
112
 * @var array
113
 */
114
	public $test2 = array(
115
		'from' => array('some@example.com' => 'My website'),
116
		'to' => array('test@example.com' => 'Testname'),
117
		'subject' => 'Test mail subject',
118
		'transport' => 'Smtp',
119
		'host' => 'cakephp.org',
120
		'timeout' => 60
121
	);
122
 
123
}
124
 
125
/**
126
 * ExtendTransport class
127
 * test class to ensure the class has send() method
128
 *
129
 */
130
class ExtendTransport {
131
 
132
}
133
 
134
/**
135
 * CakeEmailTest class
136
 *
137
 * @package       Cake.Test.Case.Network.Email
138
 */
139
class CakeEmailTest extends CakeTestCase {
140
 
141
/**
142
 * setUp
143
 *
144
 * @return void
145
 */
146
	public function setUp() {
147
		parent::setUp();
148
		$this->CakeEmail = new TestCakeEmail();
149
 
150
		App::build(array(
151
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
152
		));
153
	}
154
 
155
/**
156
 * tearDown method
157
 *
158
 * @return void
159
 */
160
	public function tearDown() {
161
		parent::tearDown();
162
		App::build();
163
	}
164
 
165
/**
166
 * testFrom method
167
 *
168
 * @return void
169
 */
170
	public function testFrom() {
171
		$this->assertSame($this->CakeEmail->from(), array());
172
 
173
		$this->CakeEmail->from('cake@cakephp.org');
174
		$expected = array('cake@cakephp.org' => 'cake@cakephp.org');
175
		$this->assertSame($this->CakeEmail->from(), $expected);
176
 
177
		$this->CakeEmail->from(array('cake@cakephp.org'));
178
		$this->assertSame($this->CakeEmail->from(), $expected);
179
 
180
		$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
181
		$expected = array('cake@cakephp.org' => 'CakePHP');
182
		$this->assertSame($this->CakeEmail->from(), $expected);
183
 
184
		$result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP'));
185
		$this->assertSame($this->CakeEmail->from(), $expected);
186
		$this->assertSame($this->CakeEmail, $result);
187
 
188
		$this->setExpectedException('SocketException');
189
		$result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address'));
190
	}
191
 
192
/**
193
 * Test that from addresses using colons work.
194
 *
195
 * @return void
196
 */
197
	public function testFromWithColonsAndQuotes() {
198
		$address = array(
199
			'info@example.com' => '70:20:00 " Forum'
200
		);
201
		$this->CakeEmail->from($address);
202
		$this->assertEquals($address, $this->CakeEmail->from());
203
		$this->CakeEmail->to('info@example.com')
204
			->subject('Test email')
205
			->transport('Debug');
206
 
207
		$result = $this->CakeEmail->send();
208
		$this->assertContains('From: "70:20:00 \" Forum" <info@example.com>', $result['headers']);
209
	}
210
 
211
/**
212
 * testSender method
213
 *
214
 * @return void
215
 */
216
	public function testSender() {
217
		$this->CakeEmail->reset();
218
		$this->assertSame($this->CakeEmail->sender(), array());
219
 
220
		$this->CakeEmail->sender('cake@cakephp.org', 'Name');
221
		$expected = array('cake@cakephp.org' => 'Name');
222
		$this->assertSame($this->CakeEmail->sender(), $expected);
223
 
224
		$headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
225
		$this->assertFalse($headers['From']);
226
		$this->assertSame($headers['Sender'], 'Name <cake@cakephp.org>');
227
 
228
		$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
229
		$headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
230
		$this->assertSame($headers['From'], 'CakePHP <cake@cakephp.org>');
231
		$this->assertSame($headers['Sender'], '');
232
	}
233
 
234
/**
235
 * testTo method
236
 *
237
 * @return void
238
 */
239
	public function testTo() {
240
		$this->assertSame($this->CakeEmail->to(), array());
241
 
242
		$result = $this->CakeEmail->to('cake@cakephp.org');
243
		$expected = array('cake@cakephp.org' => 'cake@cakephp.org');
244
		$this->assertSame($this->CakeEmail->to(), $expected);
245
		$this->assertSame($this->CakeEmail, $result);
246
 
247
		$this->CakeEmail->to('cake@cakephp.org', 'CakePHP');
248
		$expected = array('cake@cakephp.org' => 'CakePHP');
249
		$this->assertSame($this->CakeEmail->to(), $expected);
250
 
251
		$list = array(
252
			'root@localhost' => 'root',
253
			'bjørn@hammeröath.com' => 'Bjorn',
254
			'cake.php@cakephp.org' => 'Cake PHP',
255
			'cake-php@googlegroups.com' => 'Cake Groups',
256
			'root@cakephp.org'
257
		);
258
		$this->CakeEmail->to($list);
259
		$expected = array(
260
			'root@localhost' => 'root',
261
			'bjørn@hammeröath.com' => 'Bjorn',
262
			'cake.php@cakephp.org' => 'Cake PHP',
263
			'cake-php@googlegroups.com' => 'Cake Groups',
264
			'root@cakephp.org' => 'root@cakephp.org'
265
		);
266
		$this->assertSame($this->CakeEmail->to(), $expected);
267
 
268
		$this->CakeEmail->addTo('jrbasso@cakephp.org');
269
		$this->CakeEmail->addTo('mark_story@cakephp.org', 'Mark Story');
270
		$result = $this->CakeEmail->addTo(array('phpnut@cakephp.org' => 'PhpNut', 'jose_zap@cakephp.org'));
271
		$expected = array(
272
			'root@localhost' => 'root',
273
			'bjørn@hammeröath.com' => 'Bjorn',
274
			'cake.php@cakephp.org' => 'Cake PHP',
275
			'cake-php@googlegroups.com' => 'Cake Groups',
276
			'root@cakephp.org' => 'root@cakephp.org',
277
			'jrbasso@cakephp.org' => 'jrbasso@cakephp.org',
278
			'mark_story@cakephp.org' => 'Mark Story',
279
			'phpnut@cakephp.org' => 'PhpNut',
280
			'jose_zap@cakephp.org' => 'jose_zap@cakephp.org'
281
		);
282
		$this->assertSame($this->CakeEmail->to(), $expected);
283
		$this->assertSame($this->CakeEmail, $result);
284
	}
285
 
286
/**
287
 * Data provider function for testBuildInvalidData
288
 *
289
 * @return array
290
 */
291
	public static function invalidEmails() {
292
		return array(
293
			array(1.0),
294
			array(''),
295
			array('string'),
296
			array('<tag>'),
297
			array(array('ok@cakephp.org', 1.0, '', 'string'))
298
		);
299
	}
300
 
301
/**
302
 * testBuildInvalidData
303
 *
304
 * @dataProvider invalidEmails
305
 * @expectedException SocketException
306
 * @return void
307
 */
308
	public function testInvalidEmail($value) {
309
		$this->CakeEmail->to($value);
310
	}
311
 
312
/**
313
 * testBuildInvalidData
314
 *
315
 * @dataProvider invalidEmails
316
 * @expectedException SocketException
317
 * @return void
318
 */
319
	public function testInvalidEmailAdd($value) {
320
		$this->CakeEmail->addTo($value);
321
	}
322
 
323
/**
324
 * test emailPattern method
325
 *
326
 * @return void
327
 */
328
	public function testEmailPattern() {
329
		$regex = '/.+@.+\..+/i';
330
		$this->assertSame($regex, $this->CakeEmail->emailPattern($regex)->emailPattern());
331
	}
332
 
333
/**
334
 * Tests that it is possible to set email regex configuration to a CakeEmail object
335
 *
336
 * @return void
337
 */
338
	public function testConfigEmailPattern() {
339
		$regex = '/.+@.+\..+/i';
340
		$email = new CakeEmail(array('emailPattern' => $regex));
341
		$this->assertSame($regex, $email->emailPattern());
342
	}
343
 
344
/**
345
 * Tests that it is possible set custom email validation
346
 *
347
 * @return void
348
 */
349
	public function testCustomEmailValidation() {
350
		$regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}$/i';
351
 
352
		$this->CakeEmail->emailPattern($regex)->to('pass.@example.com');
353
		$this->assertSame(array(
354
			'pass.@example.com' => 'pass.@example.com',
355
		), $this->CakeEmail->to());
356
 
357
		$this->CakeEmail->addTo('pass..old.docomo@example.com');
358
		$this->assertSame(array(
359
			'pass.@example.com' => 'pass.@example.com',
360
			'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
361
		), $this->CakeEmail->to());
362
 
363
		$this->CakeEmail->reset();
364
		$emails = array(
365
			'pass.@example.com',
366
			'pass..old.docomo@example.com'
367
		);
368
		$additionalEmails = array(
369
			'.extend.@example.com',
370
			'.docomo@example.com'
371
		);
372
		$this->CakeEmail->emailPattern($regex)->to($emails);
373
		$this->assertSame(array(
374
			'pass.@example.com' => 'pass.@example.com',
375
			'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
376
		), $this->CakeEmail->to());
377
 
378
		$this->CakeEmail->addTo($additionalEmails);
379
		$this->assertSame(array(
380
			'pass.@example.com' => 'pass.@example.com',
381
			'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
382
			'.extend.@example.com' => '.extend.@example.com',
383
			'.docomo@example.com' => '.docomo@example.com',
384
		), $this->CakeEmail->to());
385
	}
386
 
387
/**
388
 * testFormatAddress method
389
 *
390
 * @return void
391
 */
392
	public function testFormatAddress() {
393
		$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org'));
394
		$expected = array('cake@cakephp.org');
395
		$this->assertSame($expected, $result);
396
 
397
		$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org', 'php@cakephp.org' => 'php@cakephp.org'));
398
		$expected = array('cake@cakephp.org', 'php@cakephp.org');
399
		$this->assertSame($expected, $result);
400
 
401
		$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'CakePHP', 'php@cakephp.org' => 'Cake'));
402
		$expected = array('CakePHP <cake@cakephp.org>', 'Cake <php@cakephp.org>');
403
		$this->assertSame($expected, $result);
404
 
405
		$result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last, First'));
406
		$expected = array('"Last, First" <me@example.com>');
407
		$this->assertSame($expected, $result);
408
 
409
		$result = $this->CakeEmail->formatAddress(array('me@example.com' => '"Last" First'));
410
		$expected = array('"\"Last\" First" <me@example.com>');
411
		$this->assertSame($expected, $result);
412
 
413
		$result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last First'));
414
		$expected = array('Last First <me@example.com>');
415
		$this->assertSame($expected, $result);
416
 
417
		$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'ÄÖÜTest'));
418
		$expected = array('=?UTF-8?B?w4TDlsOcVGVzdA==?= <cake@cakephp.org>');
419
		$this->assertSame($expected, $result);
420
 
421
		$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '日本語Test'));
422
		$expected = array('=?UTF-8?B?5pel5pys6KqeVGVzdA==?= <cake@cakephp.org>');
423
		$this->assertSame($expected, $result);
424
	}
425
 
426
/**
427
 * testFormatAddressJapanese
428
 *
429
 * @return void
430
 */
431
	public function testFormatAddressJapanese() {
432
		$this->skipIf(!function_exists('mb_convert_encoding'));
433
 
434
		$this->CakeEmail->headerCharset = 'ISO-2022-JP';
435
		$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '日本語Test'));
436
		$expected = array('=?ISO-2022-JP?B?GyRCRnxLXDhsGyhCVGVzdA==?= <cake@cakephp.org>');
437
		$this->assertSame($expected, $result);
438
 
439
		$result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '寿限無寿限無五劫の擦り切れ海砂利水魚の水行末雲来末風来末食う寝る処に住む処やぶら小路の藪柑子パイポパイポパイポのシューリンガンシューリンガンのグーリンダイグーリンダイのポンポコピーのポンポコナーの長久命の長助'));
440
		$expected = array("=?ISO-2022-JP?B?GyRCPHc4Qkw1PHc4Qkw1OF45ZSROOyQkakBaJGwzJDo9TXg/ZTV7GyhC?=\r\n" .
441
			" =?ISO-2022-JP?B?GyRCJE4/ZTlUS3YxQE1oS3ZJd01oS3Y/KSQmPzIkaz1oJEs9OyRgGyhC?=\r\n" .
442
			" =?ISO-2022-JP?B?GyRCPWgkZCRWJGk+Lk8pJE5pLjQ7O1IlUSUkJV0lUSUkJV0lUSUkGyhC?=\r\n" .
443
			" =?ISO-2022-JP?B?GyRCJV0kTiU3JWUhPCVqJXMlLCVzJTclZSE8JWolcyUsJXMkTiUwGyhC?=\r\n" .
444
			" =?ISO-2022-JP?B?GyRCITwlaiVzJUAlJCUwITwlaiVzJUAlJCROJV0lcyVdJTMlVCE8GyhC?=\r\n" .
445
			" =?ISO-2022-JP?B?GyRCJE4lXSVzJV0lMyVKITwkTkQ5NVdMPyRORDk9dRsoQg==?= <cake@cakephp.org>");
446
		$this->assertSame($expected, $result);
447
	}
448
 
449
/**
450
 * testAddresses method
451
 *
452
 * @return void
453
 */
454
	public function testAddresses() {
455
		$this->CakeEmail->reset();
456
		$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
457
		$this->CakeEmail->replyTo('replyto@cakephp.org', 'ReplyTo CakePHP');
458
		$this->CakeEmail->readReceipt('readreceipt@cakephp.org', 'ReadReceipt CakePHP');
459
		$this->CakeEmail->returnPath('returnpath@cakephp.org', 'ReturnPath CakePHP');
460
		$this->CakeEmail->to('to@cakephp.org', 'To, CakePHP');
461
		$this->CakeEmail->cc('cc@cakephp.org', 'Cc CakePHP');
462
		$this->CakeEmail->bcc('bcc@cakephp.org', 'Bcc CakePHP');
463
		$this->CakeEmail->addTo('to2@cakephp.org', 'To2 CakePHP');
464
		$this->CakeEmail->addCc('cc2@cakephp.org', 'Cc2 CakePHP');
465
		$this->CakeEmail->addBcc('bcc2@cakephp.org', 'Bcc2 CakePHP');
466
 
467
		$this->assertSame($this->CakeEmail->from(), array('cake@cakephp.org' => 'CakePHP'));
468
		$this->assertSame($this->CakeEmail->replyTo(), array('replyto@cakephp.org' => 'ReplyTo CakePHP'));
469
		$this->assertSame($this->CakeEmail->readReceipt(), array('readreceipt@cakephp.org' => 'ReadReceipt CakePHP'));
470
		$this->assertSame($this->CakeEmail->returnPath(), array('returnpath@cakephp.org' => 'ReturnPath CakePHP'));
471
		$this->assertSame($this->CakeEmail->to(), array('to@cakephp.org' => 'To, CakePHP', 'to2@cakephp.org' => 'To2 CakePHP'));
472
		$this->assertSame($this->CakeEmail->cc(), array('cc@cakephp.org' => 'Cc CakePHP', 'cc2@cakephp.org' => 'Cc2 CakePHP'));
473
		$this->assertSame($this->CakeEmail->bcc(), array('bcc@cakephp.org' => 'Bcc CakePHP', 'bcc2@cakephp.org' => 'Bcc2 CakePHP'));
474
 
475
		$headers = $this->CakeEmail->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true));
476
		$this->assertSame($headers['From'], 'CakePHP <cake@cakephp.org>');
477
		$this->assertSame($headers['Reply-To'], 'ReplyTo CakePHP <replyto@cakephp.org>');
478
		$this->assertSame($headers['Disposition-Notification-To'], 'ReadReceipt CakePHP <readreceipt@cakephp.org>');
479
		$this->assertSame($headers['Return-Path'], 'ReturnPath CakePHP <returnpath@cakephp.org>');
480
		$this->assertSame($headers['To'], '"To, CakePHP" <to@cakephp.org>, To2 CakePHP <to2@cakephp.org>');
481
		$this->assertSame($headers['Cc'], 'Cc CakePHP <cc@cakephp.org>, Cc2 CakePHP <cc2@cakephp.org>');
482
		$this->assertSame($headers['Bcc'], 'Bcc CakePHP <bcc@cakephp.org>, Bcc2 CakePHP <bcc2@cakephp.org>');
483
	}
484
 
485
/**
486
 * testMessageId method
487
 *
488
 * @return void
489
 */
490
	public function testMessageId() {
491
		$this->CakeEmail->messageId(true);
492
		$result = $this->CakeEmail->getHeaders();
493
		$this->assertTrue(isset($result['Message-ID']));
494
 
495
		$this->CakeEmail->messageId(false);
496
		$result = $this->CakeEmail->getHeaders();
497
		$this->assertFalse(isset($result['Message-ID']));
498
 
499
		$result = $this->CakeEmail->messageId('<my-email@localhost>');
500
		$this->assertSame($this->CakeEmail, $result);
501
		$result = $this->CakeEmail->getHeaders();
502
		$this->assertSame($result['Message-ID'], '<my-email@localhost>');
503
 
504
		$result = $this->CakeEmail->messageId();
505
		$this->assertSame($result, '<my-email@localhost>');
506
	}
507
 
508
/**
509
 * testMessageIdInvalid method
510
 *
511
 * @return void
512
 * @expectedException SocketException
513
 */
514
	public function testMessageIdInvalid() {
515
		$this->CakeEmail->messageId('my-email@localhost');
516
	}
517
 
518
/**
519
 * testDomain method
520
 *
521
 * @return void
522
 */
523
	public function testDomain() {
524
		$result = $this->CakeEmail->domain();
525
		$expected = env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n');
526
		$this->assertSame($expected, $result);
527
 
528
		$this->CakeEmail->domain('example.org');
529
		$result = $this->CakeEmail->domain();
530
		$expected = 'example.org';
531
		$this->assertSame($expected, $result);
532
	}
533
 
534
/**
535
 * testMessageIdWithDomain method
536
 *
537
 * @return void
538
 */
539
	public function testMessageIdWithDomain() {
540
		$this->CakeEmail->domain('example.org');
541
		$result = $this->CakeEmail->getHeaders();
542
		$expected = '@example.org>';
543
		$this->assertTextContains($expected, $result['Message-ID']);
544
 
545
		$_SERVER['HTTP_HOST'] = 'example.org';
546
		$result = $this->CakeEmail->getHeaders();
547
		$this->assertTextContains('example.org', $result['Message-ID']);
548
 
549
		$_SERVER['HTTP_HOST'] = 'example.org:81';
550
		$result = $this->CakeEmail->getHeaders();
551
		$this->assertTextNotContains(':81', $result['Message-ID']);
552
	}
553
 
554
/**
555
 * testSubject method
556
 *
557
 * @return void
558
 */
559
	public function testSubject() {
560
		$this->CakeEmail->subject('You have a new message.');
561
		$this->assertSame($this->CakeEmail->subject(), 'You have a new message.');
562
 
563
		$this->CakeEmail->subject('You have a new message, I think.');
564
		$this->assertSame($this->CakeEmail->subject(), 'You have a new message, I think.');
565
		$this->CakeEmail->subject(1);
566
		$this->assertSame($this->CakeEmail->subject(), '1');
567
 
568
		$this->CakeEmail->subject('هذه رسالة بعنوان طويل مرسل للمستلم');
569
		$expected = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
570
		$this->assertSame($this->CakeEmail->subject(), $expected);
571
	}
572
 
573
/**
574
 * testSubjectJapanese
575
 *
576
 * @return void
577
 */
578
	public function testSubjectJapanese() {
579
		$this->skipIf(!function_exists('mb_convert_encoding'));
580
		mb_internal_encoding('UTF-8');
581
 
582
		$this->CakeEmail->headerCharset = 'ISO-2022-JP';
583
		$this->CakeEmail->subject('日本語のSubjectにも対応するよ');
584
		$expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsJE4bKEJTdWJqZWN0GyRCJEskYkJQMX4kOSRrJGgbKEI=?=';
585
		$this->assertSame($this->CakeEmail->subject(), $expected);
586
 
587
		$this->CakeEmail->subject('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?');
588
		$expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
589
			" =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
590
			" =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
591
		$this->assertSame($this->CakeEmail->subject(), $expected);
592
	}
593
 
594
/**
595
 * testHeaders method
596
 *
597
 * @return void
598
 */
599
	public function testHeaders() {
600
		$this->CakeEmail->messageId(false);
601
		$this->CakeEmail->setHeaders(array('X-Something' => 'nice'));
602
		$expected = array(
603
			'X-Something' => 'nice',
604
			'X-Mailer' => 'CakePHP Email',
605
			'Date' => date(DATE_RFC2822),
606
			'MIME-Version' => '1.0',
607
			'Content-Type' => 'text/plain; charset=UTF-8',
608
			'Content-Transfer-Encoding' => '8bit'
609
		);
610
		$this->assertSame($this->CakeEmail->getHeaders(), $expected);
611
 
612
		$this->CakeEmail->addHeaders(array('X-Something' => 'very nice', 'X-Other' => 'cool'));
613
		$expected = array(
614
			'X-Something' => 'very nice',
615
			'X-Other' => 'cool',
616
			'X-Mailer' => 'CakePHP Email',
617
			'Date' => date(DATE_RFC2822),
618
			'MIME-Version' => '1.0',
619
			'Content-Type' => 'text/plain; charset=UTF-8',
620
			'Content-Transfer-Encoding' => '8bit'
621
		);
622
		$this->assertSame($this->CakeEmail->getHeaders(), $expected);
623
 
624
		$this->CakeEmail->from('cake@cakephp.org');
625
		$this->assertSame($this->CakeEmail->getHeaders(), $expected);
626
 
627
		$expected = array(
628
			'From' => 'cake@cakephp.org',
629
			'X-Something' => 'very nice',
630
			'X-Other' => 'cool',
631
			'X-Mailer' => 'CakePHP Email',
632
			'Date' => date(DATE_RFC2822),
633
			'MIME-Version' => '1.0',
634
			'Content-Type' => 'text/plain; charset=UTF-8',
635
			'Content-Transfer-Encoding' => '8bit'
636
		);
637
		$this->assertSame($this->CakeEmail->getHeaders(array('from' => true)), $expected);
638
 
639
		$this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
640
		$expected['From'] = 'CakePHP <cake@cakephp.org>';
641
		$this->assertSame($this->CakeEmail->getHeaders(array('from' => true)), $expected);
642
 
643
		$this->CakeEmail->to(array('cake@cakephp.org', 'php@cakephp.org' => 'CakePHP'));
644
		$expected = array(
645
			'From' => 'CakePHP <cake@cakephp.org>',
646
			'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
647
			'X-Something' => 'very nice',
648
			'X-Other' => 'cool',
649
			'X-Mailer' => 'CakePHP Email',
650
			'Date' => date(DATE_RFC2822),
651
			'MIME-Version' => '1.0',
652
			'Content-Type' => 'text/plain; charset=UTF-8',
653
			'Content-Transfer-Encoding' => '8bit'
654
		);
655
		$this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
656
 
657
		$this->CakeEmail->charset = 'ISO-2022-JP';
658
		$expected = array(
659
			'From' => 'CakePHP <cake@cakephp.org>',
660
			'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
661
			'X-Something' => 'very nice',
662
			'X-Other' => 'cool',
663
			'X-Mailer' => 'CakePHP Email',
664
			'Date' => date(DATE_RFC2822),
665
			'MIME-Version' => '1.0',
666
			'Content-Type' => 'text/plain; charset=ISO-2022-JP',
667
			'Content-Transfer-Encoding' => '7bit'
668
		);
669
		$this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
670
 
671
		$result = $this->CakeEmail->setHeaders(array());
672
		$this->assertInstanceOf('CakeEmail', $result);
673
	}
674
 
675
/**
676
 * Data provider function for testInvalidHeaders
677
 *
678
 * @return array
679
 */
680
	public static function invalidHeaders() {
681
		return array(
682
			array(10),
683
			array(''),
684
			array('string'),
685
			array(false),
686
			array(null)
687
		);
688
	}
689
 
690
/**
691
 * testInvalidHeaders
692
 *
693
 * @dataProvider invalidHeaders
694
 * @expectedException SocketException
695
 * @return void
696
 */
697
	public function testInvalidHeaders($value) {
698
		$this->CakeEmail->setHeaders($value);
699
	}
700
 
701
/**
702
 * testInvalidAddHeaders
703
 *
704
 * @dataProvider invalidHeaders
705
 * @expectedException SocketException
706
 * @return void
707
 */
708
	public function testInvalidAddHeaders($value) {
709
		$this->CakeEmail->addHeaders($value);
710
	}
711
 
712
/**
713
 * testTemplate method
714
 *
715
 * @return void
716
 */
717
	public function testTemplate() {
718
		$this->CakeEmail->template('template', 'layout');
719
		$expected = array('template' => 'template', 'layout' => 'layout');
720
		$this->assertSame($this->CakeEmail->template(), $expected);
721
 
722
		$this->CakeEmail->template('new_template');
723
		$expected = array('template' => 'new_template', 'layout' => 'layout');
724
		$this->assertSame($this->CakeEmail->template(), $expected);
725
 
726
		$this->CakeEmail->template('template', null);
727
		$expected = array('template' => 'template', 'layout' => null);
728
		$this->assertSame($this->CakeEmail->template(), $expected);
729
 
730
		$this->CakeEmail->template(null, null);
731
		$expected = array('template' => null, 'layout' => null);
732
		$this->assertSame($this->CakeEmail->template(), $expected);
733
	}
734
 
735
/**
736
 * testTheme method
737
 *
738
 * @return void
739
 */
740
	public function testTheme() {
741
		$this->assertSame(null, $this->CakeEmail->theme());
742
 
743
		$this->CakeEmail->theme('default');
744
		$expected = 'default';
745
		$this->assertSame($expected, $this->CakeEmail->theme());
746
	}
747
 
748
/**
749
 * testViewVars method
750
 *
751
 * @return void
752
 */
753
	public function testViewVars() {
754
		$this->assertSame($this->CakeEmail->viewVars(), array());
755
 
756
		$this->CakeEmail->viewVars(array('value' => 12345));
757
		$this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345));
758
 
759
		$this->CakeEmail->viewVars(array('name' => 'CakePHP'));
760
		$this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345, 'name' => 'CakePHP'));
761
 
762
		$this->CakeEmail->viewVars(array('value' => 4567));
763
		$this->assertSame($this->CakeEmail->viewVars(), array('value' => 4567, 'name' => 'CakePHP'));
764
	}
765
 
766
/**
767
 * testAttachments method
768
 *
769
 * @return void
770
 */
771
	public function testAttachments() {
772
		$this->CakeEmail->attachments(CAKE . 'basics.php');
773
		$expected = array(
774
			'basics.php' => array(
775
				'file' => CAKE . 'basics.php',
776
				'mimetype' => 'application/octet-stream'
777
			)
778
		);
779
		$this->assertSame($this->CakeEmail->attachments(), $expected);
780
 
781
		$this->CakeEmail->attachments(array());
782
		$this->assertSame($this->CakeEmail->attachments(), array());
783
 
784
		$this->CakeEmail->attachments(array(
785
			array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')
786
		));
787
		$this->CakeEmail->addAttachments(CAKE . 'bootstrap.php');
788
		$this->CakeEmail->addAttachments(array(CAKE . 'bootstrap.php'));
789
		$this->CakeEmail->addAttachments(array('other.txt' => CAKE . 'bootstrap.php', 'license' => CAKE . 'LICENSE.txt'));
790
		$expected = array(
791
			'basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'),
792
			'bootstrap.php' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
793
			'other.txt' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
794
			'license' => array('file' => CAKE . 'LICENSE.txt', 'mimetype' => 'application/octet-stream')
795
		);
796
		$this->assertSame($this->CakeEmail->attachments(), $expected);
797
 
798
		$this->setExpectedException('SocketException');
799
		$this->CakeEmail->attachments(array(array('nofile' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
800
	}
801
 
802
/**
803
 * testTransport method
804
 *
805
 * @return void
806
 */
807
	public function testTransport() {
808
		$result = $this->CakeEmail->transport('Debug');
809
		$this->assertSame($this->CakeEmail, $result);
810
		$this->assertSame($this->CakeEmail->transport(), 'Debug');
811
 
812
		$result = $this->CakeEmail->transportClass();
813
		$this->assertInstanceOf('DebugTransport', $result);
814
 
815
		$this->setExpectedException('SocketException');
816
		$this->CakeEmail->transport('Invalid');
817
		$result = $this->CakeEmail->transportClass();
818
	}
819
 
820
/**
821
 * testExtendTransport method
822
 *
823
 * @return void
824
 */
825
	public function testExtendTransport() {
826
		$this->setExpectedException('SocketException');
827
		$this->CakeEmail->transport('Extend');
828
		$this->CakeEmail->transportClass();
829
	}
830
 
831
/**
832
 * testConfig method
833
 *
834
 * @return void
835
 */
836
	public function testConfig() {
837
		$transportClass = $this->CakeEmail->transport('debug')->transportClass();
838
 
839
		$config = array('test' => 'ok', 'test2' => true);
840
		$this->CakeEmail->config($config);
841
		$this->assertSame($transportClass->config(), $config);
842
		$this->assertSame($this->CakeEmail->config(), $config);
843
 
844
		$this->CakeEmail->config(array());
845
		$this->assertSame($transportClass->config(), $config);
846
 
847
		$config = array('test' => 'test@example.com');
848
		$this->CakeEmail->config($config);
849
		$expected = array('test' => 'test@example.com', 'test2' => true);
850
		$this->assertSame($expected, $this->CakeEmail->config());
851
		$this->assertSame($expected, $transportClass->config());
852
	}
853
 
854
/**
855
 * testConfigString method
856
 *
857
 * @return void
858
 */
859
	public function testConfigString() {
860
		$configs = new TestEmailConfig();
861
		$this->CakeEmail->config('test');
862
 
863
		$result = $this->CakeEmail->to();
864
		$this->assertEquals($configs->test['to'], $result);
865
 
866
		$result = $this->CakeEmail->from();
867
		$this->assertEquals($configs->test['from'], $result);
868
 
869
		$result = $this->CakeEmail->subject();
870
		$this->assertEquals($configs->test['subject'], $result);
871
 
872
		$result = $this->CakeEmail->theme();
873
		$this->assertEquals($configs->test['theme'], $result);
874
 
875
		$result = $this->CakeEmail->transport();
876
		$this->assertEquals($configs->test['transport'], $result);
877
 
878
		$result = $this->CakeEmail->transportClass();
879
		$this->assertInstanceOf('DebugTransport', $result);
880
 
881
		$result = $this->CakeEmail->helpers();
882
		$this->assertEquals($configs->test['helpers'], $result);
883
	}
884
 
885
/**
886
 * Test updating config doesn't reset transport's config.
887
 *
888
 * @return void
889
 */
890
	public function testConfigMerge() {
891
		$this->CakeEmail->config('test2');
892
 
893
		$expected = array(
894
			'host' => 'cakephp.org',
895
			'port' => 25,
896
			'timeout' => 60,
897
			'username' => null,
898
			'password' => null,
899
			'client' => null,
900
			'tls' => false
901
		);
902
		$this->assertEquals($expected, $this->CakeEmail->transportClass()->config());
903
 
904
		$this->CakeEmail->config(array('log' => true));
905
		$result = $this->CakeEmail->transportClass()->config();
906
		$expected += array('log' => true);
907
		$this->assertEquals($expected, $this->CakeEmail->transportClass()->config());
908
 
909
		$this->CakeEmail->config(array('timeout' => 45));
910
		$result = $this->CakeEmail->transportClass()->config();
911
		$this->assertEquals(45, $result['timeout']);
912
	}
913
 
914
/**
915
 * Calling send() with no parameters should not overwrite the view variables.
916
 *
917
 * @return void
918
 */
919
	public function testSendWithNoContentDoesNotOverwriteViewVar() {
920
		$this->CakeEmail->reset();
921
		$this->CakeEmail->transport('Debug');
922
		$this->CakeEmail->from('cake@cakephp.org');
923
		$this->CakeEmail->to('you@cakephp.org');
924
		$this->CakeEmail->subject('My title');
925
		$this->CakeEmail->emailFormat('text');
926
		$this->CakeEmail->template('default');
927
		$this->CakeEmail->viewVars(array(
928
			'content' => 'A message to you',
929
		));
930
 
931
		$result = $this->CakeEmail->send();
932
		$this->assertContains('A message to you', $result['message']);
933
	}
934
 
935
/**
936
 * testSendWithContent method
937
 *
938
 * @return void
939
 */
940
	public function testSendWithContent() {
941
		$this->CakeEmail->reset();
942
		$this->CakeEmail->transport('Debug');
943
		$this->CakeEmail->from('cake@cakephp.org');
944
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
945
		$this->CakeEmail->subject('My title');
946
		$this->CakeEmail->config(array('empty'));
947
 
948
		$result = $this->CakeEmail->send("Here is my body, with multi lines.\nThis is the second line.\r\n\r\nAnd the last.");
949
		$expected = array('headers', 'message');
950
		$this->assertEquals($expected, array_keys($result));
951
		$expected = "Here is my body, with multi lines.\r\nThis is the second line.\r\n\r\nAnd the last.\r\n\r\n";
952
 
953
		$this->assertEquals($expected, $result['message']);
954
		$this->assertTrue((bool)strpos($result['headers'], 'Date: '));
955
		$this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
956
		$this->assertTrue((bool)strpos($result['headers'], 'To: '));
957
 
958
		$result = $this->CakeEmail->send("Other body");
959
		$expected = "Other body\r\n\r\n";
960
		$this->assertSame($result['message'], $expected);
961
		$this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
962
		$this->assertTrue((bool)strpos($result['headers'], 'To: '));
963
 
964
		$this->CakeEmail->reset();
965
		$this->CakeEmail->transport('Debug');
966
		$this->CakeEmail->from('cake@cakephp.org');
967
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
968
		$this->CakeEmail->subject('My title');
969
		$this->CakeEmail->config(array('empty'));
970
		$result = $this->CakeEmail->send(array('Sending content', 'As array'));
971
		$expected = "Sending content\r\nAs array\r\n\r\n\r\n";
972
		$this->assertSame($result['message'], $expected);
973
	}
974
 
975
/**
976
 * testSendWithoutFrom method
977
 *
978
 * @return void
979
 */
980
	public function testSendWithoutFrom() {
981
		$this->CakeEmail->transport('Debug');
982
		$this->CakeEmail->to('cake@cakephp.org');
983
		$this->CakeEmail->subject('My title');
984
		$this->CakeEmail->config(array('empty'));
985
		$this->setExpectedException('SocketException');
986
		$this->CakeEmail->send("Forgot to set From");
987
	}
988
 
989
/**
990
 * testSendWithoutTo method
991
 *
992
 * @return void
993
 */
994
	public function testSendWithoutTo() {
995
		$this->CakeEmail->transport('Debug');
996
		$this->CakeEmail->from('cake@cakephp.org');
997
		$this->CakeEmail->subject('My title');
998
		$this->CakeEmail->config(array('empty'));
999
		$this->setExpectedException('SocketException');
1000
		$this->CakeEmail->send("Forgot to set To");
1001
	}
1002
 
1003
/**
1004
 * Test send() with no template.
1005
 *
1006
 * @return void
1007
 */
1008
	public function testSendNoTemplateWithAttachments() {
1009
		$this->CakeEmail->transport('debug');
1010
		$this->CakeEmail->from('cake@cakephp.org');
1011
		$this->CakeEmail->to('cake@cakephp.org');
1012
		$this->CakeEmail->subject('My title');
1013
		$this->CakeEmail->emailFormat('text');
1014
		$this->CakeEmail->attachments(array(CAKE . 'basics.php'));
1015
		$result = $this->CakeEmail->send('Hello');
1016
 
1017
		$boundary = $this->CakeEmail->getBoundary();
1018
		$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
1019
		$expected = "--$boundary\r\n" .
1020
			"Content-Type: text/plain; charset=UTF-8\r\n" .
1021
			"Content-Transfer-Encoding: 8bit\r\n" .
1022
			"\r\n" .
1023
			"Hello" .
1024
			"\r\n" .
1025
			"\r\n" .
1026
			"\r\n" .
1027
			"--$boundary\r\n" .
1028
			"Content-Type: application/octet-stream\r\n" .
1029
			"Content-Transfer-Encoding: base64\r\n" .
1030
			"Content-Disposition: attachment; filename=\"basics.php\"\r\n\r\n";
1031
		$this->assertContains($expected, $result['message']);
1032
	}
1033
 
1034
/**
1035
 * Test send() with no template and data string attachment
1036
 *
1037
 * @return void
1038
 */
1039
 
1040
	public function testSendNoTemplateWithDataStringAttachment() {
1041
		$this->CakeEmail->transport('debug');
1042
		$this->CakeEmail->from('cake@cakephp.org');
1043
		$this->CakeEmail->to('cake@cakephp.org');
1044
		$this->CakeEmail->subject('My title');
1045
		$this->CakeEmail->emailFormat('text');
1046
		$data = file_get_contents(CAKE . 'Console/Templates/skel/webroot/img/cake.icon.png');
1047
		$this->CakeEmail->attachments(array('cake.icon.png' => array(
1048
				'data' => $data,
1049
				'mimetype' => 'image/png'
1050
		)));
1051
		$result = $this->CakeEmail->send('Hello');
1052
 
1053
		$boundary = $this->CakeEmail->getBoundary();
1054
		$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
1055
		$expected = "--$boundary\r\n" .
1056
				"Content-Type: text/plain; charset=UTF-8\r\n" .
1057
				"Content-Transfer-Encoding: 8bit\r\n" .
1058
				"\r\n" .
1059
				"Hello" .
1060
				"\r\n" .
1061
				"\r\n" .
1062
				"\r\n" .
1063
				"--$boundary\r\n" .
1064
				"Content-Type: image/png\r\n" .
1065
				"Content-Transfer-Encoding: base64\r\n" .
1066
				"Content-Disposition: attachment; filename=\"cake.icon.png\"\r\n\r\n";
1067
		$expected .= chunk_split(base64_encode($data), 76, "\r\n");
1068
		$this->assertContains($expected, $result['message']);
1069
	}
1070
 
1071
/**
1072
 * Test send() with no template as both
1073
 *
1074
 * @return void
1075
 */
1076
	public function testSendNoTemplateWithAttachmentsAsBoth() {
1077
		$this->CakeEmail->transport('debug');
1078
		$this->CakeEmail->from('cake@cakephp.org');
1079
		$this->CakeEmail->to('cake@cakephp.org');
1080
		$this->CakeEmail->subject('My title');
1081
		$this->CakeEmail->emailFormat('both');
1082
		$this->CakeEmail->attachments(array(CAKE . 'VERSION.txt'));
1083
		$result = $this->CakeEmail->send('Hello');
1084
 
1085
		$boundary = $this->CakeEmail->getBoundary();
1086
		$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
1087
		$expected = "--$boundary\r\n" .
1088
			"Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
1089
			"\r\n" .
1090
			"--alt-$boundary\r\n" .
1091
			"Content-Type: text/plain; charset=UTF-8\r\n" .
1092
			"Content-Transfer-Encoding: 8bit\r\n" .
1093
			"\r\n" .
1094
			"Hello" .
1095
			"\r\n" .
1096
			"\r\n" .
1097
			"\r\n" .
1098
			"--alt-$boundary\r\n" .
1099
			"Content-Type: text/html; charset=UTF-8\r\n" .
1100
			"Content-Transfer-Encoding: 8bit\r\n" .
1101
			"\r\n" .
1102
			"Hello" .
1103
			"\r\n" .
1104
			"\r\n" .
1105
			"\r\n" .
1106
			"--alt-{$boundary}--\r\n" .
1107
			"\r\n" .
1108
			"--$boundary\r\n" .
1109
			"Content-Type: application/octet-stream\r\n" .
1110
			"Content-Transfer-Encoding: base64\r\n" .
1111
			"Content-Disposition: attachment; filename=\"VERSION.txt\"\r\n\r\n";
1112
		$this->assertContains($expected, $result['message']);
1113
	}
1114
 
1115
/**
1116
 * Test setting inline attachments and messages.
1117
 *
1118
 * @return void
1119
 */
1120
	public function testSendWithInlineAttachments() {
1121
		$this->CakeEmail->transport('debug');
1122
		$this->CakeEmail->from('cake@cakephp.org');
1123
		$this->CakeEmail->to('cake@cakephp.org');
1124
		$this->CakeEmail->subject('My title');
1125
		$this->CakeEmail->emailFormat('both');
1126
		$this->CakeEmail->attachments(array(
1127
			'cake.png' => array(
1128
				'file' => CAKE . 'VERSION.txt',
1129
				'contentId' => 'abc123'
1130
			)
1131
		));
1132
		$result = $this->CakeEmail->send('Hello');
1133
 
1134
		$boundary = $this->CakeEmail->getBoundary();
1135
		$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
1136
		$expected = "--$boundary\r\n" .
1137
			"Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
1138
			"\r\n" .
1139
			"--rel-$boundary\r\n" .
1140
			"Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
1141
			"\r\n" .
1142
			"--alt-$boundary\r\n" .
1143
			"Content-Type: text/plain; charset=UTF-8\r\n" .
1144
			"Content-Transfer-Encoding: 8bit\r\n" .
1145
			"\r\n" .
1146
			"Hello" .
1147
			"\r\n" .
1148
			"\r\n" .
1149
			"\r\n" .
1150
			"--alt-$boundary\r\n" .
1151
			"Content-Type: text/html; charset=UTF-8\r\n" .
1152
			"Content-Transfer-Encoding: 8bit\r\n" .
1153
			"\r\n" .
1154
			"Hello" .
1155
			"\r\n" .
1156
			"\r\n" .
1157
			"\r\n" .
1158
			"--alt-{$boundary}--\r\n" .
1159
			"\r\n" .
1160
			"--rel-$boundary\r\n" .
1161
			"Content-Type: application/octet-stream\r\n" .
1162
			"Content-Transfer-Encoding: base64\r\n" .
1163
			"Content-ID: <abc123>\r\n" .
1164
			"Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
1165
		$this->assertContains($expected, $result['message']);
1166
		$this->assertContains('--rel-' . $boundary . '--', $result['message']);
1167
		$this->assertContains('--' . $boundary . '--', $result['message']);
1168
	}
1169
 
1170
/**
1171
 * Test setting inline attachments and HTML only messages.
1172
 *
1173
 * @return void
1174
 */
1175
	public function testSendWithInlineAttachmentsHtmlOnly() {
1176
		$this->CakeEmail->transport('debug');
1177
		$this->CakeEmail->from('cake@cakephp.org');
1178
		$this->CakeEmail->to('cake@cakephp.org');
1179
		$this->CakeEmail->subject('My title');
1180
		$this->CakeEmail->emailFormat('html');
1181
		$this->CakeEmail->attachments(array(
1182
			'cake.png' => array(
1183
				'file' => CAKE . 'VERSION.txt',
1184
				'contentId' => 'abc123'
1185
			)
1186
		));
1187
		$result = $this->CakeEmail->send('Hello');
1188
 
1189
		$boundary = $this->CakeEmail->getBoundary();
1190
		$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
1191
		$expected = "--$boundary\r\n" .
1192
			"Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
1193
			"\r\n" .
1194
			"--rel-$boundary\r\n" .
1195
			"Content-Type: text/html; charset=UTF-8\r\n" .
1196
			"Content-Transfer-Encoding: 8bit\r\n" .
1197
			"\r\n" .
1198
			"Hello" .
1199
			"\r\n" .
1200
			"\r\n" .
1201
			"\r\n" .
1202
			"--rel-$boundary\r\n" .
1203
			"Content-Type: application/octet-stream\r\n" .
1204
			"Content-Transfer-Encoding: base64\r\n" .
1205
			"Content-ID: <abc123>\r\n" .
1206
			"Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
1207
		$this->assertContains($expected, $result['message']);
1208
		$this->assertContains('--rel-' . $boundary . '--', $result['message']);
1209
		$this->assertContains('--' . $boundary . '--', $result['message']);
1210
	}
1211
 
1212
/**
1213
 * Test disabling content-disposition.
1214
 *
1215
 * @return void
1216
 */
1217
	public function testSendWithNoContentDispositionAttachments() {
1218
		$this->CakeEmail->transport('debug');
1219
		$this->CakeEmail->from('cake@cakephp.org');
1220
		$this->CakeEmail->to('cake@cakephp.org');
1221
		$this->CakeEmail->subject('My title');
1222
		$this->CakeEmail->emailFormat('text');
1223
		$this->CakeEmail->attachments(array(
1224
			'cake.png' => array(
1225
				'file' => CAKE . 'VERSION.txt',
1226
				'contentDisposition' => false
1227
			)
1228
		));
1229
		$result = $this->CakeEmail->send('Hello');
1230
 
1231
		$boundary = $this->CakeEmail->getBoundary();
1232
		$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
1233
		$expected = "--$boundary\r\n" .
1234
			"Content-Type: text/plain; charset=UTF-8\r\n" .
1235
			"Content-Transfer-Encoding: 8bit\r\n" .
1236
			"\r\n" .
1237
			"Hello" .
1238
			"\r\n" .
1239
			"\r\n" .
1240
			"\r\n" .
1241
			"--{$boundary}\r\n" .
1242
			"Content-Type: application/octet-stream\r\n" .
1243
			"Content-Transfer-Encoding: base64\r\n" .
1244
			"\r\n";
1245
 
1246
		$this->assertContains($expected, $result['message']);
1247
		$this->assertContains('--' . $boundary . '--', $result['message']);
1248
	}
1249
/**
1250
 * testSendWithLog method
1251
 *
1252
 * @return void
1253
 */
1254
	public function testSendWithLog() {
1255
		CakeLog::config('email', array(
1256
			'engine' => 'File',
1257
			'path' => TMP
1258
		));
1259
		CakeLog::drop('default');
1260
		$this->CakeEmail->transport('Debug');
1261
		$this->CakeEmail->to('me@cakephp.org');
1262
		$this->CakeEmail->from('cake@cakephp.org');
1263
		$this->CakeEmail->subject('My title');
1264
		$this->CakeEmail->config(array('log' => 'cake_test_emails'));
1265
		$result = $this->CakeEmail->send("Logging This");
1266
 
1267
		App::uses('File', 'Utility');
1268
		$File = new File(TMP . 'cake_test_emails.log');
1269
		$log = $File->read();
1270
		$this->assertTrue(strpos($log, $result['headers']) !== false);
1271
		$this->assertTrue(strpos($log, $result['message']) !== false);
1272
		$File->delete();
1273
		CakeLog::drop('email');
1274
	}
1275
 
1276
/**
1277
 * testSendWithLogAndScope method
1278
 *
1279
 * @return void
1280
 */
1281
	public function testSendWithLogAndScope() {
1282
		CakeLog::config('email', array(
1283
			'engine' => 'File',
1284
			'path' => TMP,
1285
			'types' => array('cake_test_emails'),
1286
			'scopes' => array('email')
1287
		));
1288
		CakeLog::drop('default');
1289
		$this->CakeEmail->transport('Debug');
1290
		$this->CakeEmail->to('me@cakephp.org');
1291
		$this->CakeEmail->from('cake@cakephp.org');
1292
		$this->CakeEmail->subject('My title');
1293
		$this->CakeEmail->config(array('log' => array('level' => 'cake_test_emails', 'scope' => 'email')));
1294
		$result = $this->CakeEmail->send("Logging This");
1295
 
1296
		App::uses('File', 'Utility');
1297
		$File = new File(TMP . 'cake_test_emails.log');
1298
		$log = $File->read();
1299
		$this->assertTrue(strpos($log, $result['headers']) !== false);
1300
		$this->assertTrue(strpos($log, $result['message']) !== false);
1301
		$File->delete();
1302
		CakeLog::drop('email');
1303
	}
1304
 
1305
/**
1306
 * testSendRender method
1307
 *
1308
 * @return void
1309
 */
1310
	public function testSendRender() {
1311
		$this->CakeEmail->reset();
1312
		$this->CakeEmail->transport('debug');
1313
 
1314
		$this->CakeEmail->from('cake@cakephp.org');
1315
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1316
		$this->CakeEmail->subject('My title');
1317
		$this->CakeEmail->config(array('empty'));
1318
		$this->CakeEmail->template('default', 'default');
1319
		$result = $this->CakeEmail->send();
1320
 
1321
		$this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
1322
		$this->assertContains('Message-ID: ', $result['headers']);
1323
		$this->assertContains('To: ', $result['headers']);
1324
	}
1325
 
1326
/**
1327
 * test sending and rendering with no layout
1328
 *
1329
 * @return void
1330
 */
1331
	public function testSendRenderNoLayout() {
1332
		$this->CakeEmail->reset();
1333
		$this->CakeEmail->transport('debug');
1334
 
1335
		$this->CakeEmail->from('cake@cakephp.org');
1336
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1337
		$this->CakeEmail->subject('My title');
1338
		$this->CakeEmail->config(array('empty'));
1339
		$this->CakeEmail->template('default', null);
1340
		$result = $this->CakeEmail->send('message body.');
1341
 
1342
		$this->assertContains('message body.', $result['message']);
1343
		$this->assertNotContains('This email was sent using the CakePHP Framework', $result['message']);
1344
	}
1345
 
1346
/**
1347
 * testSendRender both method
1348
 *
1349
 * @return void
1350
 */
1351
	public function testSendRenderBoth() {
1352
		$this->CakeEmail->reset();
1353
		$this->CakeEmail->transport('debug');
1354
 
1355
		$this->CakeEmail->from('cake@cakephp.org');
1356
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1357
		$this->CakeEmail->subject('My title');
1358
		$this->CakeEmail->config(array('empty'));
1359
		$this->CakeEmail->template('default', 'default');
1360
		$this->CakeEmail->emailFormat('both');
1361
		$result = $this->CakeEmail->send();
1362
 
1363
		$this->assertContains('Message-ID: ', $result['headers']);
1364
		$this->assertContains('To: ', $result['headers']);
1365
 
1366
		$boundary = $this->CakeEmail->getBoundary();
1367
		$this->assertContains('Content-Type: multipart/alternative; boundary="' . $boundary . '"', $result['headers']);
1368
 
1369
		$expected = "--$boundary\r\n" .
1370
			"Content-Type: text/plain; charset=UTF-8\r\n" .
1371
			"Content-Transfer-Encoding: 8bit\r\n" .
1372
			"\r\n" .
1373
			"\r\n" .
1374
			"\r\n" .
1375
			"This email was sent using the CakePHP Framework, http://cakephp.org." .
1376
			"\r\n" .
1377
			"\r\n" .
1378
			"--$boundary\r\n" .
1379
			"Content-Type: text/html; charset=UTF-8\r\n" .
1380
			"Content-Transfer-Encoding: 8bit\r\n" .
1381
			"\r\n" .
1382
			"<!DOCTYPE html";
1383
		$this->assertStringStartsWith($expected, $result['message']);
1384
 
1385
		$expected = "</html>\r\n" .
1386
			"\r\n" .
1387
			"\r\n" .
1388
			"--$boundary--\r\n";
1389
		$this->assertStringEndsWith($expected, $result['message']);
1390
	}
1391
 
1392
/**
1393
 * testSendRender method for ISO-2022-JP
1394
 *
1395
 * @return void
1396
 */
1397
	public function testSendRenderJapanese() {
1398
		$this->skipIf(!function_exists('mb_convert_encoding'));
1399
 
1400
		$this->CakeEmail->reset();
1401
		$this->CakeEmail->transport('debug');
1402
 
1403
		$this->CakeEmail->from('cake@cakephp.org');
1404
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1405
		$this->CakeEmail->subject('My title');
1406
		$this->CakeEmail->config(array('empty'));
1407
		$this->CakeEmail->template('default', 'japanese');
1408
		$this->CakeEmail->charset = 'ISO-2022-JP';
1409
		$result = $this->CakeEmail->send();
1410
 
1411
		$expected = mb_convert_encoding('CakePHP Framework を使って送信したメールです。 http://cakephp.org.', 'ISO-2022-JP');
1412
		$this->assertContains($expected, $result['message']);
1413
		$this->assertContains('Message-ID: ', $result['headers']);
1414
		$this->assertContains('To: ', $result['headers']);
1415
	}
1416
 
1417
/**
1418
 * testSendRenderThemed method
1419
 *
1420
 * @return void
1421
 */
1422
	public function testSendRenderThemed() {
1423
		$this->CakeEmail->reset();
1424
		$this->CakeEmail->transport('debug');
1425
 
1426
		$this->CakeEmail->from('cake@cakephp.org');
1427
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1428
		$this->CakeEmail->subject('My title');
1429
		$this->CakeEmail->config(array('empty'));
1430
		$this->CakeEmail->theme('TestTheme');
1431
		$this->CakeEmail->template('themed', 'default');
1432
		$result = $this->CakeEmail->send();
1433
 
1434
		$this->assertContains('In TestTheme', $result['message']);
1435
		$this->assertContains('Message-ID: ', $result['headers']);
1436
		$this->assertContains('To: ', $result['headers']);
1437
		$this->assertContains('/theme/TestTheme/img/test.jpg', $result['message']);
1438
	}
1439
 
1440
/**
1441
 * testSendRenderWithHTML method and assert line length is kept below the required limit
1442
 *
1443
 * @return void
1444
 */
1445
	public function testSendRenderWithHTML() {
1446
		$this->CakeEmail->reset();
1447
		$this->CakeEmail->transport('debug');
1448
 
1449
		$this->CakeEmail->from('cake@cakephp.org');
1450
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1451
		$this->CakeEmail->subject('My title');
1452
		$this->CakeEmail->config(array('empty'));
1453
		$this->CakeEmail->emailFormat('html');
1454
		$this->CakeEmail->template('html', 'default');
1455
		$result = $this->CakeEmail->send();
1456
 
1457
		$this->assertTextContains('<h1>HTML Ipsum Presents</h1>', $result['message']);
1458
		$this->assertLineLengths($result['message']);
1459
	}
1460
 
1461
/**
1462
 * testSendRenderWithVars method
1463
 *
1464
 * @return void
1465
 */
1466
	public function testSendRenderWithVars() {
1467
		$this->CakeEmail->reset();
1468
		$this->CakeEmail->transport('debug');
1469
 
1470
		$this->CakeEmail->from('cake@cakephp.org');
1471
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1472
		$this->CakeEmail->subject('My title');
1473
		$this->CakeEmail->config(array('empty'));
1474
		$this->CakeEmail->template('custom', 'default');
1475
		$this->CakeEmail->viewVars(array('value' => 12345));
1476
		$result = $this->CakeEmail->send();
1477
 
1478
		$this->assertContains('Here is your value: 12345', $result['message']);
1479
	}
1480
 
1481
/**
1482
 * testSendRenderWithVars method for ISO-2022-JP
1483
 *
1484
 * @return void
1485
 */
1486
	public function testSendRenderWithVarsJapanese() {
1487
		$this->skipIf(!function_exists('mb_convert_encoding'));
1488
		$this->CakeEmail->reset();
1489
		$this->CakeEmail->transport('debug');
1490
 
1491
		$this->CakeEmail->from('cake@cakephp.org');
1492
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1493
		$this->CakeEmail->subject('My title');
1494
		$this->CakeEmail->config(array('empty'));
1495
		$this->CakeEmail->template('japanese', 'default');
1496
		$this->CakeEmail->viewVars(array('value' => '日本語の差し込み123'));
1497
		$this->CakeEmail->charset = 'ISO-2022-JP';
1498
		$result = $this->CakeEmail->send();
1499
 
1500
		$expected = mb_convert_encoding('ここにあなたの設定した値が入ります: 日本語の差し込み123', 'ISO-2022-JP');
1501
		$this->assertTrue((bool)strpos($result['message'], $expected));
1502
	}
1503
 
1504
/**
1505
 * testSendRenderWithHelpers method
1506
 *
1507
 * @return void
1508
 */
1509
	public function testSendRenderWithHelpers() {
1510
		$this->CakeEmail->reset();
1511
		$this->CakeEmail->transport('debug');
1512
 
1513
		$timestamp = time();
1514
		$this->CakeEmail->from('cake@cakephp.org');
1515
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1516
		$this->CakeEmail->subject('My title');
1517
		$this->CakeEmail->config(array('empty'));
1518
		$this->CakeEmail->template('custom_helper', 'default');
1519
		$this->CakeEmail->viewVars(array('time' => $timestamp));
1520
 
1521
		$result = $this->CakeEmail->helpers(array('Time'));
1522
		$this->assertInstanceOf('CakeEmail', $result);
1523
 
1524
		$result = $this->CakeEmail->send();
1525
		$this->assertTrue((bool)strpos($result['message'], 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
1526
 
1527
		$result = $this->CakeEmail->helpers();
1528
		$this->assertEquals(array('Time'), $result);
1529
	}
1530
 
1531
/**
1532
 * testSendRenderWithImage method
1533
 *
1534
 * @return void
1535
 */
1536
	public function testSendRenderWithImage() {
1537
		$this->CakeEmail->reset();
1538
		$this->CakeEmail->transport('Debug');
1539
 
1540
		$this->CakeEmail->from('cake@cakephp.org');
1541
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1542
		$this->CakeEmail->subject('My title');
1543
		$this->CakeEmail->config(array('empty'));
1544
		$this->CakeEmail->template('image');
1545
		$this->CakeEmail->emailFormat('html');
1546
		$server = env('SERVER_NAME') ? env('SERVER_NAME') : 'localhost';
1547
 
1548
		if (env('SERVER_PORT') && env('SERVER_PORT') != 80) {
1549
			$server .= ':' . env('SERVER_PORT');
1550
		}
1551
 
1552
		$expected = '<img src="http://' . $server . '/img/image.gif" alt="cool image" width="100" height="100" />';
1553
		$result = $this->CakeEmail->send();
1554
		$this->assertContains($expected, $result['message']);
1555
	}
1556
 
1557
/**
1558
 * testSendRenderPlugin method
1559
 *
1560
 * @return void
1561
 */
1562
	public function testSendRenderPlugin() {
1563
		App::build(array(
1564
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
1565
		));
1566
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
1567
 
1568
		$this->CakeEmail->reset();
1569
		$this->CakeEmail->transport('debug');
1570
		$this->CakeEmail->from('cake@cakephp.org');
1571
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1572
		$this->CakeEmail->subject('My title');
1573
		$this->CakeEmail->config(array('empty'));
1574
 
1575
		$result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'default')->send();
1576
		$this->assertContains('Into TestPlugin.', $result['message']);
1577
		$this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
1578
 
1579
		$result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'TestPlugin.plug_default')->send();
1580
		$this->assertContains('Into TestPlugin.', $result['message']);
1581
		$this->assertContains('This email was sent using the TestPlugin.', $result['message']);
1582
 
1583
		$result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'plug_default')->send();
1584
		$this->assertContains('Into TestPlugin.', $result['message']);
1585
		$this->assertContains('This email was sent using the TestPlugin.', $result['message']);
1586
 
1587
		$this->CakeEmail->template(
1588
			'TestPlugin.test_plugin_tpl',
1589
			'TestPluginTwo.default'
1590
		);
1591
		$result = $this->CakeEmail->send();
1592
		$this->assertContains('Into TestPlugin.', $result['message']);
1593
		$this->assertContains('This email was sent using TestPluginTwo.', $result['message']);
1594
 
1595
		// test plugin template overridden by theme
1596
		$this->CakeEmail->theme('TestTheme');
1597
		$result = $this->CakeEmail->send();
1598
 
1599
		$this->assertContains('Into TestPlugin. (themed)', $result['message']);
1600
 
1601
		$this->CakeEmail->viewVars(array('value' => 12345));
1602
		$result = $this->CakeEmail->template('custom', 'TestPlugin.plug_default')->send();
1603
		$this->assertContains('Here is your value: 12345', $result['message']);
1604
		$this->assertContains('This email was sent using the TestPlugin.', $result['message']);
1605
 
1606
		$this->setExpectedException('MissingViewException');
1607
		$this->CakeEmail->template('test_plugin_tpl', 'plug_default')->send();
1608
	}
1609
 
1610
/**
1611
 * testSendMultipleMIME method
1612
 *
1613
 * @return void
1614
 */
1615
	public function testSendMultipleMIME() {
1616
		$this->CakeEmail->reset();
1617
		$this->CakeEmail->transport('debug');
1618
 
1619
		$this->CakeEmail->from('cake@cakephp.org');
1620
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1621
		$this->CakeEmail->subject('My title');
1622
		$this->CakeEmail->template('custom', 'default');
1623
		$this->CakeEmail->config(array());
1624
		$this->CakeEmail->viewVars(array('value' => 12345));
1625
		$this->CakeEmail->emailFormat('both');
1626
		$this->CakeEmail->send();
1627
 
1628
		$message = $this->CakeEmail->message();
1629
		$boundary = $this->CakeEmail->getBoundary();
1630
		$this->assertFalse(empty($boundary));
1631
		$this->assertContains('--' . $boundary, $message);
1632
		$this->assertContains('--' . $boundary . '--', $message);
1633
 
1634
		$this->CakeEmail->attachments(array('fake.php' => __FILE__));
1635
		$this->CakeEmail->send();
1636
 
1637
		$message = $this->CakeEmail->message();
1638
		$boundary = $this->CakeEmail->getBoundary();
1639
		$this->assertFalse(empty($boundary));
1640
		$this->assertContains('--' . $boundary, $message);
1641
		$this->assertContains('--' . $boundary . '--', $message);
1642
		$this->assertContains('--alt-' . $boundary, $message);
1643
		$this->assertContains('--alt-' . $boundary . '--', $message);
1644
	}
1645
 
1646
/**
1647
 * testSendAttachment method
1648
 *
1649
 * @return void
1650
 */
1651
	public function testSendAttachment() {
1652
		$this->CakeEmail->reset();
1653
		$this->CakeEmail->transport('debug');
1654
		$this->CakeEmail->from('cake@cakephp.org');
1655
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1656
		$this->CakeEmail->subject('My title');
1657
		$this->CakeEmail->config(array());
1658
		$this->CakeEmail->attachments(array(CAKE . 'basics.php'));
1659
		$result = $this->CakeEmail->send('body');
1660
		$this->assertContains("Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"basics.php\"", $result['message']);
1661
 
1662
		$this->CakeEmail->attachments(array('my.file.txt' => CAKE . 'basics.php'));
1663
		$result = $this->CakeEmail->send('body');
1664
		$this->assertContains("Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"my.file.txt\"", $result['message']);
1665
 
1666
		$this->CakeEmail->attachments(array('file.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
1667
		$result = $this->CakeEmail->send('body');
1668
		$this->assertContains("Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"file.txt\"", $result['message']);
1669
 
1670
		$this->CakeEmail->attachments(array('file2.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain', 'contentId' => 'a1b1c1')));
1671
		$result = $this->CakeEmail->send('body');
1672
		$this->assertContains("Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-ID: <a1b1c1>\r\nContent-Disposition: inline; filename=\"file2.txt\"", $result['message']);
1673
	}
1674
 
1675
/**
1676
 * testDeliver method
1677
 *
1678
 * @return void
1679
 */
1680
	public function testDeliver() {
1681
		$instance = CakeEmail::deliver('all@cakephp.org', 'About', 'Everything ok', array('from' => 'root@cakephp.org'), false);
1682
		$this->assertInstanceOf('CakeEmail', $instance);
1683
		$this->assertSame($instance->to(), array('all@cakephp.org' => 'all@cakephp.org'));
1684
		$this->assertSame($instance->subject(), 'About');
1685
		$this->assertSame($instance->from(), array('root@cakephp.org' => 'root@cakephp.org'));
1686
 
1687
		$config = array(
1688
			'from' => 'cake@cakephp.org',
1689
			'to' => 'debug@cakephp.org',
1690
			'subject' => 'Update ok',
1691
			'template' => 'custom',
1692
			'layout' => 'custom_layout',
1693
			'viewVars' => array('value' => 123),
1694
			'cc' => array('cake@cakephp.org' => 'Myself')
1695
		);
1696
		$instance = CakeEmail::deliver(null, null, array('name' => 'CakePHP'), $config, false);
1697
		$this->assertSame($instance->from(), array('cake@cakephp.org' => 'cake@cakephp.org'));
1698
		$this->assertSame($instance->to(), array('debug@cakephp.org' => 'debug@cakephp.org'));
1699
		$this->assertSame($instance->subject(), 'Update ok');
1700
		$this->assertSame($instance->template(), array('template' => 'custom', 'layout' => 'custom_layout'));
1701
		$this->assertSame($instance->viewVars(), array('value' => 123, 'name' => 'CakePHP'));
1702
		$this->assertSame($instance->cc(), array('cake@cakephp.org' => 'Myself'));
1703
 
1704
		$configs = array('from' => 'root@cakephp.org', 'message' => 'Message from configs', 'transport' => 'Debug');
1705
		$instance = CakeEmail::deliver('all@cakephp.org', 'About', null, $configs, true);
1706
		$message = $instance->message();
1707
		$this->assertEquals($configs['message'], $message[0]);
1708
	}
1709
 
1710
/**
1711
 * testMessage method
1712
 *
1713
 * @return void
1714
 */
1715
	public function testMessage() {
1716
		$this->CakeEmail->reset();
1717
		$this->CakeEmail->transport('debug');
1718
		$this->CakeEmail->from('cake@cakephp.org');
1719
		$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
1720
		$this->CakeEmail->subject('My title');
1721
		$this->CakeEmail->config(array('empty'));
1722
		$this->CakeEmail->template('default', 'default');
1723
		$this->CakeEmail->emailFormat('both');
1724
		$this->CakeEmail->send();
1725
 
1726
		$expected = '<p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>';
1727
		$this->assertContains($expected, $this->CakeEmail->message(CakeEmail::MESSAGE_HTML));
1728
 
1729
		$expected = 'This email was sent using the CakePHP Framework, http://cakephp.org.';
1730
		$this->assertContains($expected, $this->CakeEmail->message(CakeEmail::MESSAGE_TEXT));
1731
 
1732
		$message = $this->CakeEmail->message();
1733
		$this->assertContains('Content-Type: text/plain; charset=UTF-8', $message);
1734
		$this->assertContains('Content-Type: text/html; charset=UTF-8', $message);
1735
 
1736
		// UTF-8 is 8bit
1737
		$this->assertTrue($this->_checkContentTransferEncoding($message, '8bit'));
1738
 
1739
		$this->CakeEmail->charset = 'ISO-2022-JP';
1740
		$this->CakeEmail->send();
1741
		$message = $this->CakeEmail->message();
1742
		$this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $message);
1743
		$this->assertContains('Content-Type: text/html; charset=ISO-2022-JP', $message);
1744
 
1745
		// ISO-2022-JP is 7bit
1746
		$this->assertTrue($this->_checkContentTransferEncoding($message, '7bit'));
1747
	}
1748
 
1749
/**
1750
 * testReset method
1751
 *
1752
 * @return void
1753
 */
1754
	public function testReset() {
1755
		$this->CakeEmail->to('cake@cakephp.org');
1756
		$this->CakeEmail->theme('TestTheme');
1757
		$this->CakeEmail->emailPattern('/.+@.+\..+/i');
1758
		$this->assertSame($this->CakeEmail->to(), array('cake@cakephp.org' => 'cake@cakephp.org'));
1759
 
1760
		$this->CakeEmail->reset();
1761
		$this->assertSame($this->CakeEmail->to(), array());
1762
		$this->assertSame(null, $this->CakeEmail->theme());
1763
		$this->assertSame(null, $this->CakeEmail->emailPattern());
1764
	}
1765
 
1766
/**
1767
 * testReset with charset
1768
 *
1769
 * @return void
1770
 */
1771
	public function testResetWithCharset() {
1772
		$this->CakeEmail->charset = 'ISO-2022-JP';
1773
		$this->CakeEmail->reset();
1774
 
1775
		$this->assertSame($this->CakeEmail->charset, 'utf-8', $this->CakeEmail->charset);
1776
		$this->assertSame($this->CakeEmail->headerCharset, null, $this->CakeEmail->headerCharset);
1777
	}
1778
 
1779
/**
1780
 * testWrap method
1781
 *
1782
 * @return void
1783
 */
1784
	public function testWrap() {
1785
		$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
1786
		$result = $this->CakeEmail->wrap($text, CakeEmail::LINE_LENGTH_SHOULD);
1787
		$expected = array(
1788
			'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
1789
			'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
1790
			''
1791
		);
1792
		$this->assertSame($expected, $result);
1793
 
1794
		$text = 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan amet.';
1795
		$result = $this->CakeEmail->wrap($text, CakeEmail::LINE_LENGTH_SHOULD);
1796
		$expected = array(
1797
			'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
1798
			'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
1799
			'amet.',
1800
			''
1801
		);
1802
		$this->assertSame($expected, $result);
1803
 
1804
		$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>';
1805
		$result = $this->CakeEmail->wrap($text, CakeEmail::LINE_LENGTH_SHOULD);
1806
		$expected = array(
1807
			'<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac',
1808
			'turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula',
1809
			'pellentesque accumsan amet.<hr></p>',
1810
			''
1811
		);
1812
		$this->assertSame($expected, $result);
1813
 
1814
		$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.';
1815
		$result = $this->CakeEmail->wrap($text, CakeEmail::LINE_LENGTH_SHOULD);
1816
		$expected = array(
1817
			'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac',
1818
			'<a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh',
1819
			'nisi, vehicula pellentesque accumsan amet.',
1820
			''
1821
		);
1822
		$this->assertSame($expected, $result);
1823
 
1824
		$text = 'Lorem ipsum <a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">ok</a>';
1825
		$result = $this->CakeEmail->wrap($text, CakeEmail::LINE_LENGTH_SHOULD);
1826
		$expected = array(
1827
			'Lorem ipsum',
1828
			'<a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">',
1829
			'ok</a>',
1830
			''
1831
		);
1832
		$this->assertSame($expected, $result);
1833
 
1834
		$text = 'Lorem ipsum withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite ok.';
1835
		$result = $this->CakeEmail->wrap($text, CakeEmail::LINE_LENGTH_SHOULD);
1836
		$expected = array(
1837
			'Lorem ipsum',
1838
			'withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite',
1839
			'ok.',
1840
			''
1841
		);
1842
		$this->assertSame($expected, $result);
1843
	}
1844
 
1845
/**
1846
 * testRender method
1847
 *
1848
 * @return void
1849
 */
1850
	public function testRenderWithLayoutAndAttachment() {
1851
		$this->CakeEmail->emailFormat('html');
1852
		$this->CakeEmail->template('html', 'default');
1853
		$this->CakeEmail->attachments(array(CAKE . 'basics.php'));
1854
		$result = $this->CakeEmail->render(array());
1855
		$this->assertNotEmpty($result);
1856
 
1857
		$result = $this->CakeEmail->getBoundary();
1858
		$this->assertNotEmpty($result);
1859
	}
1860
 
1861
/**
1862
 * testConstructWithConfigArray method
1863
 *
1864
 * @return void
1865
 */
1866
	public function testConstructWithConfigArray() {
1867
		$configs = array(
1868
			'from' => array('some@example.com' => 'My website'),
1869
			'to' => 'test@example.com',
1870
			'subject' => 'Test mail subject',
1871
			'transport' => 'Debug',
1872
		);
1873
		$this->CakeEmail = new CakeEmail($configs);
1874
 
1875
		$result = $this->CakeEmail->to();
1876
		$this->assertEquals(array($configs['to'] => $configs['to']), $result);
1877
 
1878
		$result = $this->CakeEmail->from();
1879
		$this->assertEquals($configs['from'], $result);
1880
 
1881
		$result = $this->CakeEmail->subject();
1882
		$this->assertEquals($configs['subject'], $result);
1883
 
1884
		$result = $this->CakeEmail->transport();
1885
		$this->assertEquals($configs['transport'], $result);
1886
 
1887
		$result = $this->CakeEmail->transportClass();
1888
		$this->assertTrue($result instanceof DebugTransport);
1889
 
1890
		$result = $this->CakeEmail->send('This is the message');
1891
 
1892
		$this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
1893
		$this->assertTrue((bool)strpos($result['headers'], 'To: '));
1894
	}
1895
 
1896
/**
1897
 * testConfigArrayWithLayoutWithoutTemplate method
1898
 *
1899
 * @return void
1900
 */
1901
	public function testConfigArrayWithLayoutWithoutTemplate() {
1902
		$configs = array(
1903
			'from' => array('some@example.com' => 'My website'),
1904
			'to' => 'test@example.com',
1905
			'subject' => 'Test mail subject',
1906
			'transport' => 'Debug',
1907
			'layout' => 'custom'
1908
		);
1909
		$this->CakeEmail = new CakeEmail($configs);
1910
 
1911
		$result = $this->CakeEmail->template();
1912
		$this->assertEquals('', $result['template']);
1913
		$this->assertEquals($configs['layout'], $result['layout']);
1914
	}
1915
 
1916
/**
1917
 * testConstructWithConfigString method
1918
 *
1919
 * @return void
1920
 */
1921
	public function testConstructWithConfigString() {
1922
		$configs = new TestEmailConfig();
1923
		$this->CakeEmail = new TestCakeEmail('test');
1924
 
1925
		$result = $this->CakeEmail->to();
1926
		$this->assertEquals($configs->test['to'], $result);
1927
 
1928
		$result = $this->CakeEmail->from();
1929
		$this->assertEquals($configs->test['from'], $result);
1930
 
1931
		$result = $this->CakeEmail->subject();
1932
		$this->assertEquals($configs->test['subject'], $result);
1933
 
1934
		$result = $this->CakeEmail->transport();
1935
		$this->assertEquals($configs->test['transport'], $result);
1936
 
1937
		$result = $this->CakeEmail->transportClass();
1938
		$this->assertTrue($result instanceof DebugTransport);
1939
 
1940
		$result = $this->CakeEmail->send('This is the message');
1941
 
1942
		$this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
1943
		$this->assertTrue((bool)strpos($result['headers'], 'To: '));
1944
	}
1945
 
1946
/**
1947
 * testViewRender method
1948
 *
1949
 * @return void
1950
 */
1951
	public function testViewRender() {
1952
		$result = $this->CakeEmail->viewRender();
1953
		$this->assertEquals('View', $result);
1954
 
1955
		$result = $this->CakeEmail->viewRender('Theme');
1956
		$this->assertInstanceOf('CakeEmail', $result);
1957
 
1958
		$result = $this->CakeEmail->viewRender();
1959
		$this->assertEquals('Theme', $result);
1960
	}
1961
 
1962
/**
1963
 * testEmailFormat method
1964
 *
1965
 * @return void
1966
 */
1967
	public function testEmailFormat() {
1968
		$result = $this->CakeEmail->emailFormat();
1969
		$this->assertEquals('text', $result);
1970
 
1971
		$result = $this->CakeEmail->emailFormat('html');
1972
		$this->assertInstanceOf('CakeEmail', $result);
1973
 
1974
		$result = $this->CakeEmail->emailFormat();
1975
		$this->assertEquals('html', $result);
1976
 
1977
		$this->setExpectedException('SocketException');
1978
		$result = $this->CakeEmail->emailFormat('invalid');
1979
	}
1980
 
1981
/**
1982
 * Tests that it is possible to add charset configuration to a CakeEmail object
1983
 *
1984
 * @return void
1985
 */
1986
	public function testConfigCharset() {
1987
		$email = new CakeEmail();
1988
		$this->assertEquals(Configure::read('App.encoding'), $email->charset);
1989
		$this->assertEquals(Configure::read('App.encoding'), $email->headerCharset);
1990
 
1991
		$email = new CakeEmail(array('charset' => 'iso-2022-jp', 'headerCharset' => 'iso-2022-jp-ms'));
1992
		$this->assertEquals('iso-2022-jp', $email->charset);
1993
		$this->assertEquals('iso-2022-jp-ms', $email->headerCharset);
1994
 
1995
		$email = new CakeEmail(array('charset' => 'iso-2022-jp'));
1996
		$this->assertEquals('iso-2022-jp', $email->charset);
1997
		$this->assertEquals('iso-2022-jp', $email->headerCharset);
1998
 
1999
		$email = new CakeEmail(array('headerCharset' => 'iso-2022-jp-ms'));
2000
		$this->assertEquals(Configure::read('App.encoding'), $email->charset);
2001
		$this->assertEquals('iso-2022-jp-ms', $email->headerCharset);
2002
	}
2003
 
2004
/**
2005
 * Tests that the header is encoded using the configured headerCharset
2006
 *
2007
 * @return void
2008
 */
2009
	public function testHeaderEncoding() {
2010
		$this->skipIf(!function_exists('mb_convert_encoding'));
2011
		$email = new CakeEmail(array('headerCharset' => 'iso-2022-jp-ms', 'transport' => 'Debug'));
2012
		$email->subject('あれ?もしかしての前と');
2013
		$headers = $email->getHeaders(array('subject'));
2014
		$expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
2015
		$this->assertContains($expected, $headers['Subject']);
2016
 
2017
		$email->to('someone@example.com')->from('someone@example.com');
2018
		$result = $email->send('ってテーブルを作ってやってたらう');
2019
		$this->assertContains('ってテーブルを作ってやってたらう', $result['message']);
2020
	}
2021
 
2022
/**
2023
 * Tests that the body is encoded using the configured charset
2024
 *
2025
 * @return void
2026
 */
2027
	public function testBodyEncoding() {
2028
		$this->skipIf(!function_exists('mb_convert_encoding'));
2029
		$email = new CakeEmail(array(
2030
			'charset' => 'iso-2022-jp',
2031
			'headerCharset' => 'iso-2022-jp-ms',
2032
			'transport' => 'Debug'
2033
		));
2034
		$email->subject('あれ?もしかしての前と');
2035
		$headers = $email->getHeaders(array('subject'));
2036
		$expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
2037
		$this->assertContains($expected, $headers['Subject']);
2038
 
2039
		$email->to('someone@example.com')->from('someone@example.com');
2040
		$result = $email->send('ってテーブルを作ってやってたらう');
2041
		$this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $result['headers']);
2042
		$this->assertContains(mb_convert_encoding('ってテーブルを作ってやってたらう', 'ISO-2022-JP'), $result['message']);
2043
	}
2044
 
2045
/**
2046
 * Tests that the body is encoded using the configured charset (Japanese standard encoding)
2047
 *
2048
 * @return void
2049
 */
2050
	public function testBodyEncodingIso2022Jp() {
2051
		$this->skipIf(!function_exists('mb_convert_encoding'));
2052
		$email = new CakeEmail(array(
2053
			'charset' => 'iso-2022-jp',
2054
			'headerCharset' => 'iso-2022-jp',
2055
			'transport' => 'Debug'
2056
		));
2057
		$email->subject('あれ?もしかしての前と');
2058
		$headers = $email->getHeaders(array('subject'));
2059
		$expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
2060
		$this->assertContains($expected, $headers['Subject']);
2061
 
2062
		$email->to('someone@example.com')->from('someone@example.com');
2063
		$result = $email->send('①㈱');
2064
		$this->assertTextContains("Content-Type: text/plain; charset=ISO-2022-JP", $result['headers']);
2065
		$this->assertTextNotContains("Content-Type: text/plain; charset=ISO-2022-JP-MS", $result['headers']); // not charset=iso-2022-jp-ms
2066
		$this->assertTextNotContains(mb_convert_encoding('①㈱', 'ISO-2022-JP-MS'), $result['message']);
2067
	}
2068
 
2069
/**
2070
 * Tests that the body is encoded using the configured charset (Japanese irregular encoding, but sometime use this)
2071
 *
2072
 * @return void
2073
 */
2074
	public function testBodyEncodingIso2022JpMs() {
2075
		$this->skipIf(!function_exists('mb_convert_encoding'));
2076
		$email = new CakeEmail(array(
2077
			'charset' => 'iso-2022-jp-ms',
2078
			'headerCharset' => 'iso-2022-jp-ms',
2079
			'transport' => 'Debug'
2080
		));
2081
		$email->subject('あれ?もしかしての前と');
2082
		$headers = $email->getHeaders(array('subject'));
2083
		$expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
2084
		$this->assertContains($expected, $headers['Subject']);
2085
 
2086
		$email->to('someone@example.com')->from('someone@example.com');
2087
		$result = $email->send('①㈱');
2088
		$this->assertTextContains("Content-Type: text/plain; charset=ISO-2022-JP", $result['headers']);
2089
		$this->assertTextNotContains("Content-Type: text/plain; charset=iso-2022-jp-ms", $result['headers']); // not charset=iso-2022-jp-ms
2090
		$this->assertContains(mb_convert_encoding('①㈱', 'ISO-2022-JP-MS'), $result['message']);
2091
	}
2092
 
2093
	protected function _checkContentTransferEncoding($message, $charset) {
2094
		$boundary = '--' . $this->CakeEmail->getBoundary();
2095
		$result['text'] = false;
2096
		$result['html'] = false;
2097
		$length = count($message);
2098
		for ($i = 0; $i < $length; ++$i) {
2099
			if ($message[$i] == $boundary) {
2100
				$flag = false;
2101
				$type = '';
2102
				while (!preg_match('/^$/', $message[$i])) {
2103
					if (preg_match('/^Content-Type: text\/plain/', $message[$i])) {
2104
						$type = 'text';
2105
					}
2106
					if (preg_match('/^Content-Type: text\/html/', $message[$i])) {
2107
						$type = 'html';
2108
					}
2109
					if ($message[$i] === 'Content-Transfer-Encoding: ' . $charset) {
2110
						$flag = true;
2111
					}
2112
					++$i;
2113
				}
2114
				$result[$type] = $flag;
2115
			}
2116
		}
2117
		return $result['text'] && $result['html'];
2118
	}
2119
 
2120
/**
2121
 * Test CakeEmail::_encode function
2122
 *
2123
 * @return void
2124
 */
2125
	public function testEncode() {
2126
		$this->skipIf(!function_exists('mb_convert_encoding'));
2127
 
2128
		$this->CakeEmail->headerCharset = 'ISO-2022-JP';
2129
		$result = $this->CakeEmail->encode('日本語');
2130
		$expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?=';
2131
		$this->assertSame($expected, $result);
2132
 
2133
		$this->CakeEmail->headerCharset = 'ISO-2022-JP';
2134
		$result = $this->CakeEmail->encode('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?');
2135
		$expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
2136
			" =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
2137
			" =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
2138
		$this->assertSame($expected, $result);
2139
	}
2140
 
2141
/**
2142
 * Tests charset setter/getter
2143
 *
2144
 * @return void
2145
 */
2146
	public function testCharset() {
2147
		$this->CakeEmail->charset('UTF-8');
2148
		$this->assertSame($this->CakeEmail->charset(), 'UTF-8');
2149
 
2150
		$this->CakeEmail->charset('ISO-2022-JP');
2151
		$this->assertSame($this->CakeEmail->charset(), 'ISO-2022-JP');
2152
 
2153
		$charset = $this->CakeEmail->charset('Shift_JIS');
2154
		$this->assertSame($charset, 'Shift_JIS');
2155
	}
2156
 
2157
/**
2158
 * Tests headerCharset setter/getter
2159
 *
2160
 * @return void
2161
 */
2162
	public function testHeaderCharset() {
2163
		$this->CakeEmail->headerCharset('UTF-8');
2164
		$this->assertSame($this->CakeEmail->headerCharset(), 'UTF-8');
2165
 
2166
		$this->CakeEmail->headerCharset('ISO-2022-JP');
2167
		$this->assertSame($this->CakeEmail->headerCharset(), 'ISO-2022-JP');
2168
 
2169
		$charset = $this->CakeEmail->headerCharset('Shift_JIS');
2170
		$this->assertSame($charset, 'Shift_JIS');
2171
	}
2172
 
2173
/**
2174
 * Tests for compatible check.
2175
 *          charset property and       charset() method.
2176
 *    headerCharset property and headerCharset() method.
2177
 *
2178
 * @return void
2179
 */
2180
	public function testCharsetsCompatible() {
2181
		$this->skipIf(!function_exists('mb_convert_encoding'));
2182
 
2183
		$checkHeaders = array(
2184
			'from' => true,
2185
			'to' => true,
2186
			'cc' => true,
2187
			'subject' => true,
2188
		);
2189
 
2190
		// Header Charset : null (used by default UTF-8)
2191
		//   Body Charset : ISO-2022-JP
2192
		$oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', null);
2193
		$oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
2194
 
2195
		$newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', null);
2196
		$newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
2197
 
2198
		$this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
2199
		$this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
2200
		$this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
2201
		$this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
2202
 
2203
		// Header Charset : UTF-8
2204
		//   Boby Charset : ISO-2022-JP
2205
		$oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', 'utf-8');
2206
		$oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
2207
 
2208
		$newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', 'utf-8');
2209
		$newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
2210
 
2211
		$this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
2212
		$this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
2213
		$this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
2214
		$this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
2215
 
2216
		// Header Charset : ISO-2022-JP
2217
		//   Boby Charset : UTF-8
2218
		$oldStyleEmail = $this->_getEmailByOldStyleCharset('utf-8', 'iso-2022-jp');
2219
		$oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
2220
 
2221
		$newStyleEmail = $this->_getEmailByNewStyleCharset('utf-8', 'iso-2022-jp');
2222
		$newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
2223
 
2224
		$this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
2225
		$this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
2226
		$this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
2227
		$this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
2228
	}
2229
 
2230
/**
2231
 * @param mixed $charset
2232
 * @param mixed $headerCharset
2233
 * @return CakeEmail
2234
 */
2235
	protected function _getEmailByOldStyleCharset($charset, $headerCharset) {
2236
		$email = new CakeEmail(array('transport' => 'Debug'));
2237
 
2238
		if (! empty($charset)) {
2239
			$email->charset = $charset;
2240
		}
2241
		if (! empty($headerCharset)) {
2242
			$email->headerCharset = $headerCharset;
2243
		}
2244
 
2245
		$email->from('someone@example.com', 'どこかの誰か');
2246
		$email->to('someperson@example.jp', 'どこかのどなたか');
2247
		$email->cc('miku@example.net', 'ミク');
2248
		$email->subject('テストメール');
2249
		$email->send('テストメールの本文');
2250
 
2251
		return $email;
2252
	}
2253
 
2254
/**
2255
 * @param mixed $charset
2256
 * @param mixed $headerCharset
2257
 * @return CakeEmail
2258
 */
2259
	protected function _getEmailByNewStyleCharset($charset, $headerCharset) {
2260
		$email = new CakeEmail(array('transport' => 'Debug'));
2261
 
2262
		if (! empty($charset)) {
2263
			$email->charset($charset);
2264
		}
2265
		if (! empty($headerCharset)) {
2266
			$email->headerCharset($headerCharset);
2267
		}
2268
 
2269
		$email->from('someone@example.com', 'どこかの誰か');
2270
		$email->to('someperson@example.jp', 'どこかのどなたか');
2271
		$email->cc('miku@example.net', 'ミク');
2272
		$email->subject('テストメール');
2273
		$email->send('テストメールの本文');
2274
 
2275
		return $email;
2276
	}
2277
 
2278
/**
2279
 * testWrapLongLine()
2280
 *
2281
 * @return void
2282
 */
2283
	public function testWrapLongLine() {
2284
		$message = '<a href="http://cakephp.org">' . str_repeat('x', CakeEmail::LINE_LENGTH_MUST) . "</a>";
2285
 
2286
		$this->CakeEmail->reset();
2287
		$this->CakeEmail->transport('Debug');
2288
		$this->CakeEmail->from('cake@cakephp.org');
2289
		$this->CakeEmail->to('cake@cakephp.org');
2290
		$this->CakeEmail->subject('Wordwrap Test');
2291
		$this->CakeEmail->config(array('empty'));
2292
		$result = $this->CakeEmail->send($message);
2293
		$expected = "<a\r\n" . 'href="http://cakephp.org">' . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - 26) . "\r\n" .
2294
			str_repeat('x', 26) . "\r\n</a>\r\n\r\n";
2295
		$this->assertEquals($expected, $result['message']);
2296
		$this->assertLineLengths($result['message']);
2297
 
2298
		$str1 = "a ";
2299
		$str2 = " b";
2300
		$length = strlen($str1) + strlen($str2);
2301
		$message = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $length - 1) . $str2;
2302
 
2303
		$result = $this->CakeEmail->send($message);
2304
		$expected = "{$message}\r\n\r\n";
2305
		$this->assertEquals($expected, $result['message']);
2306
		$this->assertLineLengths($result['message']);
2307
 
2308
		$message = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $length) . $str2;
2309
 
2310
		$result = $this->CakeEmail->send($message);
2311
		$expected = "{$message}\r\n\r\n";
2312
		$this->assertEquals($expected, $result['message']);
2313
		$this->assertLineLengths($result['message']);
2314
 
2315
		$message = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $length + 1) . $str2;
2316
 
2317
		$result = $this->CakeEmail->send($message);
2318
		$expected = $str1 . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $length + 1) . sprintf("\r\n%s\r\n\r\n", trim($str2));
2319
		$this->assertEquals($expected, $result['message']);
2320
		$this->assertLineLengths($result['message']);
2321
	}
2322
 
2323
/**
2324
 * testWrapWithTagsAcrossLines()
2325
 *
2326
 * @return void
2327
 */
2328
	public function testWrapWithTagsAcrossLines() {
2329
		$str = <<<HTML
2330
<table>
2331
<th align="right" valign="top"
2332
        style="font-weight: bold">The tag is across multiple lines</th>
2333
</table>
2334
HTML;
2335
		$length = strlen($str);
2336
		$message = $str . str_repeat('x', CakeEmail::LINE_LENGTH_MUST + 1);
2337
 
2338
		$this->CakeEmail->reset();
2339
		$this->CakeEmail->transport('Debug');
2340
		$this->CakeEmail->from('cake@cakephp.org');
2341
		$this->CakeEmail->to('cake@cakephp.org');
2342
		$this->CakeEmail->subject('Wordwrap Test');
2343
		$this->CakeEmail->config(array('empty'));
2344
		$result = $this->CakeEmail->send($message);
2345
		$message = str_replace("\r\n", "\n", substr($message, 0, -9));
2346
		$message = str_replace("\n", "\r\n", $message);
2347
		$expected = "{$message}\r\nxxxxxxxxx\r\n\r\n";
2348
		$this->assertEquals($expected, $result['message']);
2349
		$this->assertLineLengths($result['message']);
2350
	}
2351
 
2352
/**
2353
 * CakeEmailTest::testWrapIncludeLessThanSign()
2354
 *
2355
 * @return void
2356
 */
2357
	public function testWrapIncludeLessThanSign() {
2358
		$str = 'foo<bar';
2359
		$length = strlen($str);
2360
		$message = $str . str_repeat('x', CakeEmail::LINE_LENGTH_MUST - $length + 1);
2361
 
2362
		$this->CakeEmail->reset();
2363
		$this->CakeEmail->transport('Debug');
2364
		$this->CakeEmail->from('cake@cakephp.org');
2365
		$this->CakeEmail->to('cake@cakephp.org');
2366
		$this->CakeEmail->subject('Wordwrap Test');
2367
		$this->CakeEmail->config(array('empty'));
2368
		$result = $this->CakeEmail->send($message);
2369
		$message = substr($message, 0, -1);
2370
		$expected = "{$message}\r\nx\r\n\r\n";
2371
		$this->assertEquals($expected, $result['message']);
2372
		$this->assertLineLengths($result['message']);
2373
	}
2374
 
2375
/**
2376
 * CakeEmailTest::testWrapForJapaneseEncoding()
2377
 *
2378
 * @return void
2379
 */
2380
	public function testWrapForJapaneseEncoding() {
2381
		$this->skipIf(!function_exists('mb_convert_encoding'));
2382
 
2383
		$message = mb_convert_encoding('受け付けました', 'iso-2022-jp', 'UTF-8');
2384
 
2385
		$this->CakeEmail->reset();
2386
		$this->CakeEmail->transport('Debug');
2387
		$this->CakeEmail->from('cake@cakephp.org');
2388
		$this->CakeEmail->to('cake@cakephp.org');
2389
		$this->CakeEmail->subject('Wordwrap Test');
2390
		$this->CakeEmail->config(array('empty'));
2391
		$this->CakeEmail->charset('iso-2022-jp');
2392
		$this->CakeEmail->headerCharset('iso-2022-jp');
2393
		$result = $this->CakeEmail->send($message);
2394
		$expected = "{$message}\r\n\r\n";
2395
		$this->assertEquals($expected, $result['message']);
2396
	}
2397
 
2398
/**
2399
 * CakeEmailTest::assertLineLengths()
2400
 *
2401
 * @param string $message
2402
 * @return void
2403
 */
2404
	public function assertLineLengths($message) {
2405
		$lines = explode("\r\n", $message);
2406
		foreach ($lines as $line) {
2407
			$this->assertTrue(strlen($line) <= CakeEmail::LINE_LENGTH_MUST,
2408
				'Line length exceeds the max. limit of CakeEmail::LINE_LENGTH_MUST');
2409
		}
2410
	}
2411
 
2412
}