Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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