Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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