Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * EmailComponentTest file
4
 *
5
 * Series of tests for email component.
6
 *
7
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
16
 * @package       Cake.Test.Case.Controller.Component
17
 * @since         CakePHP(tm) v 1.2.0.5347
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20
 
21
App::uses('Controller', 'Controller');
22
App::uses('EmailComponent', 'Controller/Component');
23
App::uses('AbstractTransport', 'Network/Email');
24
 
25
/**
26
 * EmailTestComponent class
27
 *
28
 * @package       Cake.Test.Case.Controller.Component
29
 */
30
class EmailTestComponent extends EmailComponent {
31
 
32
/**
33
 * Convenience method for testing.
34
 *
35
 * @return string
36
 */
37
	public function strip($content, $message = false) {
38
		return parent::_strip($content, $message);
39
	}
40
 
41
}
42
 
43
/**
44
 * DebugCompTransport class
45
 *
46
 * @package       Cake.Test.Case.Controller.Component
47
 */
48
class DebugCompTransport extends AbstractTransport {
49
 
50
/**
51
 * Last email
52
 *
53
 * @var string
54
 */
55
	public static $lastEmail = null;
56
 
57
/**
58
 * Send mail
59
 *
60
 * @params object $email CakeEmail
61
 * @return boolean
62
 */
63
	public function send(CakeEmail $email) {
64
		$email->addHeaders(array('Date' => EmailComponentTest::$sentDate));
65
		$headers = $email->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), true));
66
		$to = $headers['To'];
67
		$subject = $headers['Subject'];
68
		unset($headers['To'], $headers['Subject']);
69
 
70
		$message = implode("\n", $email->message());
71
 
72
		$last = '<pre>';
73
		$last .= sprintf("%s %s\n", 'To:', $to);
74
		$last .= sprintf("%s %s\n", 'From:', $headers['From']);
75
		$last .= sprintf("%s %s\n", 'Subject:', $subject);
76
		$last .= sprintf("%s\n\n%s", 'Header:', $this->_headersToString($headers, "\n"));
77
		$last .= sprintf("%s\n\n%s", 'Message:', $message);
78
		$last .= '</pre>';
79
 
80
		self::$lastEmail = $last;
81
 
82
		return true;
83
	}
84
 
85
}
86
 
87
/**
88
 * EmailTestController class
89
 *
90
 * @package       Cake.Test.Case.Controller.Component
91
 */
92
class EmailTestController extends Controller {
93
 
94
/**
95
 * uses property
96
 *
97
 * @var mixed null
98
 */
99
	public $uses = null;
100
 
101
/**
102
 * components property
103
 *
104
 * @var array
105
 */
106
	public $components = array('Session', 'EmailTest');
107
 
108
}
109
 
110
/**
111
 * EmailTest class
112
 *
113
 * @package       Cake.Test.Case.Controller.Component
114
 */
115
class EmailComponentTest extends CakeTestCase {
116
 
117
/**
118
 * Controller property
119
 *
120
 * @var EmailTestController
121
 */
122
	public $Controller;
123
 
124
/**
125
 * name property
126
 *
127
 * @var string
128
 */
129
	public $name = 'Email';
130
 
131
/**
132
 * sentDate
133
 *
134
 * @var string
135
 */
136
	public static $sentDate = null;
137
 
138
/**
139
 * setUp method
140
 *
141
 * @return void
142
 */
143
	public function setUp() {
144
		parent::setUp();
145
 
146
		Configure::write('App.encoding', 'UTF-8');
147
 
148
		$this->Controller = new EmailTestController();
149
		$this->Controller->Components->init($this->Controller);
150
		$this->Controller->EmailTest->initialize($this->Controller, array());
151
 
152
		self::$sentDate = date(DATE_RFC2822);
153
 
154
		App::build(array(
155
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
156
		));
157
	}
158
 
159
/**
160
 * testSendFormats method
161
 *
162
 * @return void
163
 */
164
	public function testSendFormats() {
165
		$this->Controller->EmailTest->to = 'postmaster@example.com';
166
		$this->Controller->EmailTest->from = 'noreply@example.com';
167
		$this->Controller->EmailTest->subject = 'Cake SMTP test';
168
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
169
		$this->Controller->EmailTest->template = null;
170
		$this->Controller->EmailTest->delivery = 'DebugComp';
171
		$this->Controller->EmailTest->messageId = false;
172
 
173
		$date = self::$sentDate;
174
		$message = <<<MSGBLOC
175
<pre>To: postmaster@example.com
176
From: noreply@example.com
177
Subject: Cake SMTP test
178
Header:
179
 
180
From: noreply@example.com
181
Reply-To: noreply@example.com
182
X-Mailer: CakePHP Email Component
183
Date: $date
184
MIME-Version: 1.0
185
Content-Type: {CONTENTTYPE}
186
Content-Transfer-Encoding: 8bitMessage:
187
 
188
This is the body of the message
189
 
190
</pre>
191
MSGBLOC;
192
 
193
		$this->Controller->EmailTest->sendAs = 'text';
194
		$expect = str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $message);
195
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
196
		$this->assertTextEquals(DebugCompTransport::$lastEmail, $expect);
197
 
198
		$this->Controller->EmailTest->sendAs = 'html';
199
		$expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $message);
200
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
201
		$this->assertTextEquals(DebugCompTransport::$lastEmail, $expect);
202
	}
203
 
204
/**
205
 * testTemplates method
206
 *
207
 * @return void
208
 */
209
	public function testTemplates() {
210
		ClassRegistry::flush();
211
 
212
		$this->Controller->EmailTest->to = 'postmaster@example.com';
213
		$this->Controller->EmailTest->from = 'noreply@example.com';
214
		$this->Controller->EmailTest->subject = 'Cake SMTP test';
215
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
216
 
217
		$this->Controller->EmailTest->delivery = 'DebugComp';
218
		$this->Controller->EmailTest->messageId = false;
219
 
220
		$date = self::$sentDate;
221
		$header = <<<HEADBLOC
222
To: postmaster@example.com
223
From: noreply@example.com
224
Subject: Cake SMTP test
225
Header:
226
 
227
From: noreply@example.com
228
Reply-To: noreply@example.com
229
X-Mailer: CakePHP Email Component
230
Date: $date
231
MIME-Version: 1.0
232
Content-Type: {CONTENTTYPE}
233
Content-Transfer-Encoding: 8bitMessage:
234
 
235
 
236
HEADBLOC;
237
 
238
		$this->Controller->EmailTest->layout = 'default';
239
		$this->Controller->EmailTest->template = 'default';
240
		$this->Controller->set('title_for_layout', 'Email Test');
241
 
242
		$text = <<<TEXTBLOC
243
 
244
This is the body of the message
245
 
246
This email was sent using the CakePHP Framework, http://cakephp.org.
247
TEXTBLOC;
248
 
249
		$html = <<<HTMLBLOC
250
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
251
 
252
<html>
253
<head>
254
	<title>Email Test</title>
255
</head>
256
 
257
<body>
258
	<p> This is the body of the message</p><p> </p>
259
	<p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
260
</body>
261
</html>
262
HTMLBLOC;
263
 
264
		$this->Controller->EmailTest->sendAs = 'text';
265
		$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $header) . $text . "\n" . '</pre>';
266
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
267
		$this->assertTextEquals(DebugCompTransport::$lastEmail, $expect);
268
 
269
		$this->Controller->EmailTest->sendAs = 'html';
270
		$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
271
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
272
		$this->assertTextEquals(DebugCompTransport::$lastEmail, $expect);
273
 
274
		$this->Controller->EmailTest->sendAs = 'both';
275
		$expect = str_replace('{CONTENTTYPE}', 'multipart/mixed; boundary="{boundary}"', $header);
276
		$expect .= "--{boundary}\n" .
277
			'Content-Type: multipart/alternative; boundary="alt-{boundary}"' . "\n\n" .
278
			'--alt-{boundary}' . "\n" .
279
			'Content-Type: text/plain; charset=UTF-8' . "\n" .
280
			'Content-Transfer-Encoding: 8bit' . "\n\n" .
281
			$text .
282
			"\n\n" .
283
			'--alt-{boundary}' . "\n" .
284
			'Content-Type: text/html; charset=UTF-8' . "\n" .
285
			'Content-Transfer-Encoding: 8bit' . "\n\n" .
286
			$html .
287
			"\n\n" .
288
			'--alt-{boundary}--' . "\n\n\n" .
289
			'--{boundary}--' . "\n";
290
 
291
		$expect = '<pre>' . $expect . '</pre>';
292
 
293
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
294
		$this->assertTextEquals(
295
			$expect,
296
			preg_replace('/[a-z0-9]{32}/i', '{boundary}', DebugCompTransport::$lastEmail)
297
		);
298
 
299
		$html = <<<HTMLBLOC
300
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
301
 
302
<html>
303
<head>
304
	<title>Email Test</title>
305
</head>
306
 
307
<body>
308
	<p> This is the body of the message</p><p> </p>
309
	<p>This email was sent using the CakePHP Framework</p>
310
</body>
311
</html>
312
 
313
HTMLBLOC;
314
 
315
		$this->Controller->EmailTest->sendAs = 'html';
316
		$expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . '</pre>';
317
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message', 'default', 'thin'));
318
		$this->assertTextEquals(DebugCompTransport::$lastEmail, $expect);
319
	}
320
 
321
/**
322
 * test that elements used in email templates get helpers.
323
 *
324
 * @return void
325
 */
326
	public function testTemplateNestedElements() {
327
		$this->Controller->EmailTest->to = 'postmaster@example.com';
328
		$this->Controller->EmailTest->from = 'noreply@example.com';
329
		$this->Controller->EmailTest->subject = 'Cake SMTP test';
330
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
331
 
332
		$this->Controller->EmailTest->delivery = 'DebugComp';
333
		$this->Controller->EmailTest->messageId = false;
334
		$this->Controller->EmailTest->layout = 'default';
335
		$this->Controller->EmailTest->template = 'nested_element';
336
		$this->Controller->EmailTest->sendAs = 'html';
337
		$this->Controller->helpers = array('Html');
338
 
339
		$this->Controller->EmailTest->send();
340
		$result = DebugCompTransport::$lastEmail;
341
		$this->assertRegExp('/Test/', $result);
342
		$this->assertRegExp('/http\:\/\/example\.com/', $result);
343
	}
344
 
345
/**
346
 * testSendDebug method
347
 *
348
 * @return void
349
 */
350
	public function testSendDebug() {
351
		$this->Controller->EmailTest->to = 'postmaster@example.com';
352
		$this->Controller->EmailTest->from = 'noreply@example.com';
353
		$this->Controller->EmailTest->cc = 'cc@example.com';
354
		$this->Controller->EmailTest->bcc = 'bcc@example.com';
355
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
356
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
357
		$this->Controller->EmailTest->template = null;
358
 
359
		$this->Controller->EmailTest->delivery = 'DebugComp';
360
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
361
		$result = DebugCompTransport::$lastEmail;
362
 
363
		$this->assertRegExp('/To: postmaster@example.com\n/', $result);
364
		$this->assertRegExp('/Subject: Cake Debug Test\n/', $result);
365
		$this->assertRegExp('/Reply-To: noreply@example.com\n/', $result);
366
		$this->assertRegExp('/From: noreply@example.com\n/', $result);
367
		$this->assertRegExp('/Cc: cc@example.com\n/', $result);
368
		$this->assertRegExp('/Bcc: bcc@example.com\n/', $result);
369
		$this->assertRegExp('/Date: ' . preg_quote(self::$sentDate) . '\n/', $result);
370
		$this->assertRegExp('/X-Mailer: CakePHP Email Component\n/', $result);
371
		$this->assertRegExp('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
372
		$this->assertRegExp('/Content-Transfer-Encoding: 8bitMessage:\n/', $result);
373
		$this->assertRegExp('/This is the body of the message/', $result);
374
	}
375
 
376
/**
377
 * test send with delivery = debug and not using sessions.
378
 *
379
 * @return void
380
 */
381
	public function testSendDebugWithNoSessions() {
382
		$session = $this->Controller->Session;
383
		unset($this->Controller->Session);
384
		$this->Controller->EmailTest->to = 'postmaster@example.com';
385
		$this->Controller->EmailTest->from = 'noreply@example.com';
386
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
387
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
388
		$this->Controller->EmailTest->template = null;
389
 
390
		$this->Controller->EmailTest->delivery = 'DebugComp';
391
		$this->Controller->EmailTest->send('This is the body of the message');
392
		$result = DebugCompTransport::$lastEmail;
393
 
394
		$this->assertRegExp('/To: postmaster@example.com\n/', $result);
395
		$this->assertRegExp('/Subject: Cake Debug Test\n/', $result);
396
		$this->assertRegExp('/Reply-To: noreply@example.com\n/', $result);
397
		$this->assertRegExp('/From: noreply@example.com\n/', $result);
398
		$this->assertRegExp('/Date: ' . preg_quote(self::$sentDate) . '\n/', $result);
399
		$this->assertRegExp('/X-Mailer: CakePHP Email Component\n/', $result);
400
		$this->assertRegExp('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
401
		$this->assertRegExp('/Content-Transfer-Encoding: 8bitMessage:\n/', $result);
402
		$this->assertRegExp('/This is the body of the message/', $result);
403
		$this->Controller->Session = $session;
404
	}
405
 
406
/**
407
 * testMessageRetrievalWithoutTemplate method
408
 *
409
 * @return void
410
 */
411
	public function testMessageRetrievalWithoutTemplate() {
412
		App::build(array(
413
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
414
		));
415
 
416
		$this->Controller->EmailTest->to = 'postmaster@example.com';
417
		$this->Controller->EmailTest->from = 'noreply@example.com';
418
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
419
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
420
		$this->Controller->EmailTest->layout = 'default';
421
		$this->Controller->EmailTest->template = null;
422
 
423
		$this->Controller->EmailTest->delivery = 'DebugComp';
424
 
425
		$text = $html = "This is the body of the message\n";
426
 
427
		$this->Controller->EmailTest->sendAs = 'both';
428
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
429
		$this->assertTextEquals($this->Controller->EmailTest->textMessage, $text);
430
		$this->assertTextEquals($this->Controller->EmailTest->htmlMessage, $html);
431
 
432
		$this->Controller->EmailTest->sendAs = 'text';
433
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
434
		$this->assertTextEquals($this->Controller->EmailTest->textMessage, $text);
435
		$this->assertNull($this->Controller->EmailTest->htmlMessage);
436
 
437
		$this->Controller->EmailTest->sendAs = 'html';
438
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
439
		$this->assertNull($this->Controller->EmailTest->textMessage);
440
		$this->assertTextEquals($this->Controller->EmailTest->htmlMessage, $html);
441
	}
442
 
443
/**
444
 * testMessageRetrievalWithTemplate method
445
 *
446
 * @return void
447
 */
448
	public function testMessageRetrievalWithTemplate() {
449
		App::build(array(
450
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
451
		));
452
 
453
		$this->Controller->set('value', 22091985);
454
		$this->Controller->set('title_for_layout', 'EmailTest');
455
 
456
		$this->Controller->EmailTest->to = 'postmaster@example.com';
457
		$this->Controller->EmailTest->from = 'noreply@example.com';
458
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
459
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
460
		$this->Controller->EmailTest->layout = 'default';
461
		$this->Controller->EmailTest->template = 'custom';
462
 
463
		$this->Controller->EmailTest->delivery = 'DebugComp';
464
 
465
		$text = <<<TEXTBLOC
466
 
467
Here is your value: 22091985
468
This email was sent using the CakePHP Framework, http://cakephp.org.
469
TEXTBLOC;
470
 
471
		$html = <<<HTMLBLOC
472
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
473
 
474
<html>
475
<head>
476
	<title>EmailTest</title>
477
</head>
478
 
479
<body>
480
	<p>Here is your value: <b>22091985</b></p>
481
 
482
	<p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
483
</body>
484
</html>
485
HTMLBLOC;
486
 
487
		$this->Controller->EmailTest->sendAs = 'both';
488
		$this->assertTrue($this->Controller->EmailTest->send());
489
		$this->assertTextEquals($this->Controller->EmailTest->textMessage, $text);
490
		$this->assertTextEquals($this->Controller->EmailTest->htmlMessage, $html);
491
 
492
		$this->Controller->EmailTest->sendAs = 'text';
493
		$this->assertTrue($this->Controller->EmailTest->send());
494
		$this->assertTextEquals($this->Controller->EmailTest->textMessage, $text);
495
		$this->assertNull($this->Controller->EmailTest->htmlMessage);
496
 
497
		$this->Controller->EmailTest->sendAs = 'html';
498
		$this->assertTrue($this->Controller->EmailTest->send());
499
		$this->assertNull($this->Controller->EmailTest->textMessage);
500
		$this->assertTextEquals($this->Controller->EmailTest->htmlMessage, $html);
501
	}
502
 
503
/**
504
 * testMessageRetrievalWithHelper method
505
 *
506
 * @return void
507
 */
508
	public function testMessageRetrievalWithHelper() {
509
		App::build(array(
510
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
511
		));
512
 
513
		$timestamp = time();
514
		$this->Controller->set('time', $timestamp);
515
		$this->Controller->set('title_for_layout', 'EmailTest');
516
		$this->Controller->helpers = array('Time');
517
 
518
		$this->Controller->EmailTest->to = 'postmaster@example.com';
519
		$this->Controller->EmailTest->from = 'noreply@example.com';
520
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
521
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
522
		$this->Controller->EmailTest->layout = 'default';
523
		$this->Controller->EmailTest->template = 'custom_helper';
524
		$this->Controller->EmailTest->sendAs = 'text';
525
		$this->Controller->EmailTest->delivery = 'DebugComp';
526
 
527
		$this->assertTrue($this->Controller->EmailTest->send());
528
		$this->assertTrue((bool)strpos($this->Controller->EmailTest->textMessage, 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
529
	}
530
 
531
/**
532
 * testContentArray method
533
 *
534
 * @return void
535
 */
536
	public function testSendContentArray() {
537
		$this->Controller->EmailTest->to = 'postmaster@example.com';
538
		$this->Controller->EmailTest->from = 'noreply@example.com';
539
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
540
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
541
		$this->Controller->EmailTest->template = null;
542
		$this->Controller->EmailTest->delivery = 'DebugComp';
543
 
544
		$content = array('First line', 'Second line', 'Third line');
545
		$this->assertTrue($this->Controller->EmailTest->send($content));
546
		$result = DebugCompTransport::$lastEmail;
547
 
548
		$this->assertRegExp('/To: postmaster@example.com\n/', $result);
549
		$this->assertRegExp('/Subject: Cake Debug Test\n/', $result);
550
		$this->assertRegExp('/Reply-To: noreply@example.com\n/', $result);
551
		$this->assertRegExp('/From: noreply@example.com\n/', $result);
552
		$this->assertRegExp('/X-Mailer: CakePHP Email Component\n/', $result);
553
		$this->assertRegExp('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
554
		$this->assertRegExp('/Content-Transfer-Encoding: 8bitMessage:\n/', $result);
555
		$this->assertRegExp('/First line\n/', $result);
556
		$this->assertRegExp('/Second line\n/', $result);
557
		$this->assertRegExp('/Third line\n/', $result);
558
	}
559
 
560
/**
561
 * test setting a custom date.
562
 *
563
 * @return void
564
 */
565
	public function testDateProperty() {
566
		$this->Controller->EmailTest->to = 'postmaster@example.com';
567
		$this->Controller->EmailTest->from = 'noreply@example.com';
568
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
569
		$this->Controller->EmailTest->date = self::$sentDate = 'Today!';
570
		$this->Controller->EmailTest->template = null;
571
		$this->Controller->EmailTest->delivery = 'DebugComp';
572
 
573
		$this->assertTrue($this->Controller->EmailTest->send('test message'));
574
		$result = DebugCompTransport::$lastEmail;
575
		$this->assertRegExp('/Date: Today!\n/', $result);
576
	}
577
 
578
/**
579
 * testContentStripping method
580
 *
581
 * @return void
582
 */
583
	public function testContentStripping() {
584
		$content = "Previous content\n--alt-\nContent-TypeContent-Type:: text/html; charsetcharset==utf-8\nContent-Transfer-Encoding: 8bit";
585
		$content .= "\n\n<p>My own html content</p>";
586
 
587
		$result = $this->Controller->EmailTest->strip($content, true);
588
		$expected = "Previous content\n--alt-\n text/html; utf-8\n 8bit\n\n<p>My own html content</p>";
589
		$this->assertEquals($expected, $result);
590
 
591
		$content = '<p>Some HTML content with an <a href="mailto:test@example.com">email link</a>';
592
		$result = $this->Controller->EmailTest->strip($content, true);
593
		$expected = $content;
594
		$this->assertEquals($expected, $result);
595
 
596
		$content = '<p>Some HTML content with an ';
597
		$content .= '<a href="mailto:test@example.com,test2@example.com">email link</a>';
598
		$result = $this->Controller->EmailTest->strip($content, true);
599
		$expected = $content;
600
		$this->assertEquals($expected, $result);
601
	}
602
 
603
/**
604
 * test that the _encode() will set mb_internal_encoding.
605
 *
606
 * @return void
607
 */
608
	public function testEncodeSettingInternalCharset() {
609
		$this->skipIf(!function_exists('mb_internal_encoding'), 'Missing mb_* functions, cannot run test.');
610
 
611
		$restore = mb_internal_encoding();
612
		mb_internal_encoding('ISO-8859-1');
613
 
614
		$this->Controller->charset = 'UTF-8';
615
		$this->Controller->EmailTest->to = 'postmaster@example.com';
616
		$this->Controller->EmailTest->from = 'noreply@example.com';
617
		$this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
618
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
619
		$this->Controller->EmailTest->template = null;
620
		$this->Controller->EmailTest->delivery = 'DebugComp';
621
 
622
		$this->Controller->EmailTest->sendAs = 'text';
623
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
624
 
625
		$subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
626
 
627
		preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
628
		$this->assertEquals(trim($matches[1]), $subject);
629
 
630
		$result = mb_internal_encoding();
631
		$this->assertEquals('ISO-8859-1', $result);
632
 
633
		mb_internal_encoding($restore);
634
	}
635
 
636
/**
637
 * testMultibyte method
638
 *
639
 * @return void
640
 */
641
	public function testMultibyte() {
642
		$this->Controller->charset = 'UTF-8';
643
		$this->Controller->EmailTest->to = 'postmaster@example.com';
644
		$this->Controller->EmailTest->from = 'noreply@example.com';
645
		$this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
646
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
647
		$this->Controller->EmailTest->template = null;
648
		$this->Controller->EmailTest->delivery = 'DebugComp';
649
 
650
		$subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
651
 
652
		$this->Controller->EmailTest->sendAs = 'text';
653
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
654
		preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
655
		$this->assertEquals(trim($matches[1]), $subject);
656
 
657
		$this->Controller->EmailTest->sendAs = 'html';
658
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
659
		preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
660
		$this->assertEquals(trim($matches[1]), $subject);
661
 
662
		$this->Controller->EmailTest->sendAs = 'both';
663
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
664
		preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
665
		$this->assertEquals(trim($matches[1]), $subject);
666
	}
667
 
668
/**
669
 * undocumented function
670
 *
671
 * @return void
672
 */
673
	public function testSendWithAttachments() {
674
		$this->Controller->EmailTest->to = 'postmaster@example.com';
675
		$this->Controller->EmailTest->from = 'noreply@example.com';
676
		$this->Controller->EmailTest->subject = 'Attachment Test';
677
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
678
		$this->Controller->EmailTest->template = null;
679
		$this->Controller->EmailTest->delivery = 'DebugComp';
680
		$this->Controller->EmailTest->attachments = array(
681
			__FILE__,
682
			'some-name.php' => __FILE__
683
		);
684
		$body = '<p>This is the body of the message</p>';
685
 
686
		$this->Controller->EmailTest->sendAs = 'text';
687
		$this->assertTrue($this->Controller->EmailTest->send($body));
688
		$msg = DebugCompTransport::$lastEmail;
689
		$this->assertRegExp('/' . preg_quote('Content-Disposition: attachment; filename="EmailComponentTest.php"') . '/', $msg);
690
		$this->assertRegExp('/' . preg_quote('Content-Disposition: attachment; filename="some-name.php"') . '/', $msg);
691
	}
692
 
693
/**
694
 * testSendAsIsNotIgnoredIfAttachmentsPresent method
695
 *
696
 * @return void
697
 */
698
	public function testSendAsIsNotIgnoredIfAttachmentsPresent() {
699
		$this->Controller->EmailTest->to = 'postmaster@example.com';
700
		$this->Controller->EmailTest->from = 'noreply@example.com';
701
		$this->Controller->EmailTest->subject = 'Attachment Test';
702
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
703
		$this->Controller->EmailTest->template = null;
704
		$this->Controller->EmailTest->delivery = 'DebugComp';
705
		$this->Controller->EmailTest->attachments = array(__FILE__);
706
		$body = '<p>This is the body of the message</p>';
707
 
708
		$this->Controller->EmailTest->sendAs = 'html';
709
		$this->assertTrue($this->Controller->EmailTest->send($body));
710
		$msg = DebugCompTransport::$lastEmail;
711
		$this->assertNotRegExp('/text\/plain/', $msg);
712
		$this->assertRegExp('/text\/html/', $msg);
713
 
714
		$this->Controller->EmailTest->sendAs = 'text';
715
		$this->assertTrue($this->Controller->EmailTest->send($body));
716
		$msg = DebugCompTransport::$lastEmail;
717
		$this->assertRegExp('/text\/plain/', $msg);
718
		$this->assertNotRegExp('/text\/html/', $msg);
719
 
720
		$this->Controller->EmailTest->sendAs = 'both';
721
		$this->assertTrue($this->Controller->EmailTest->send($body));
722
		$msg = DebugCompTransport::$lastEmail;
723
 
724
		$this->assertRegExp('/text\/plain/', $msg);
725
		$this->assertRegExp('/text\/html/', $msg);
726
		$this->assertRegExp('/multipart\/alternative/', $msg);
727
	}
728
 
729
/**
730
 * testNoDoubleNewlinesInHeaders function
731
 *
732
 * @return void
733
 */
734
	public function testNoDoubleNewlinesInHeaders() {
735
		$this->Controller->EmailTest->to = 'postmaster@example.com';
736
		$this->Controller->EmailTest->from = 'noreply@example.com';
737
		$this->Controller->EmailTest->subject = 'Attachment Test';
738
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
739
		$this->Controller->EmailTest->template = null;
740
		$this->Controller->EmailTest->delivery = 'DebugComp';
741
		$body = '<p>This is the body of the message</p>';
742
 
743
		$this->Controller->EmailTest->sendAs = 'both';
744
		$this->assertTrue($this->Controller->EmailTest->send($body));
745
		$msg = DebugCompTransport::$lastEmail;
746
 
747
		$this->assertNotRegExp('/\n\nContent-Transfer-Encoding/', $msg);
748
		$this->assertRegExp('/\nContent-Transfer-Encoding/', $msg);
749
	}
750
 
751
/**
752
 * testReset method
753
 *
754
 * @return void
755
 */
756
	public function testReset() {
757
		$this->Controller->EmailTest->template = 'default';
758
		$this->Controller->EmailTest->to = 'test.recipient@example.com';
759
		$this->Controller->EmailTest->from = 'test.sender@example.com';
760
		$this->Controller->EmailTest->replyTo = 'test.replyto@example.com';
761
		$this->Controller->EmailTest->return = 'test.return@example.com';
762
		$this->Controller->EmailTest->cc = array('cc1@example.com', 'cc2@example.com');
763
		$this->Controller->EmailTest->bcc = array('bcc1@example.com', 'bcc2@example.com');
764
		$this->Controller->EmailTest->date = 'Today!';
765
		$this->Controller->EmailTest->subject = 'Test subject';
766
		$this->Controller->EmailTest->additionalParams = 'X-additional-header';
767
		$this->Controller->EmailTest->delivery = 'smtp';
768
		$this->Controller->EmailTest->smtpOptions['host'] = 'blah';
769
		$this->Controller->EmailTest->smtpOptions['timeout'] = 0.2;
770
		$this->Controller->EmailTest->attachments = array('attachment1', 'attachment2');
771
		$this->Controller->EmailTest->textMessage = 'This is the body of the message';
772
		$this->Controller->EmailTest->htmlMessage = 'This is the body of the message';
773
		$this->Controller->EmailTest->messageId = false;
774
 
775
		try {
776
			$this->Controller->EmailTest->send('Should not work');
777
			$this->fail('No exception');
778
		} catch (SocketException $e) {
779
			$this->assertTrue(true, 'SocketException raised');
780
		}
781
 
782
		$this->Controller->EmailTest->reset();
783
 
784
		$this->assertNull($this->Controller->EmailTest->template);
785
		$this->assertSame($this->Controller->EmailTest->to, array());
786
		$this->assertNull($this->Controller->EmailTest->from);
787
		$this->assertNull($this->Controller->EmailTest->replyTo);
788
		$this->assertNull($this->Controller->EmailTest->return);
789
		$this->assertSame($this->Controller->EmailTest->cc, array());
790
		$this->assertSame($this->Controller->EmailTest->bcc, array());
791
		$this->assertNull($this->Controller->EmailTest->date);
792
		$this->assertNull($this->Controller->EmailTest->subject);
793
		$this->assertNull($this->Controller->EmailTest->additionalParams);
794
		$this->assertNull($this->Controller->EmailTest->smtpError);
795
		$this->assertSame($this->Controller->EmailTest->attachments, array());
796
		$this->assertNull($this->Controller->EmailTest->textMessage);
797
		$this->assertTrue($this->Controller->EmailTest->messageId);
798
		$this->assertEquals('mail', $this->Controller->EmailTest->delivery);
799
	}
800
 
801
	public function testPluginCustomViewClass() {
802
		App::build(array(
803
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
804
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
805
		));
806
 
807
		$this->Controller->view = 'TestPlugin.Email';
808
 
809
		$this->Controller->EmailTest->to = 'postmaster@example.com';
810
		$this->Controller->EmailTest->from = 'noreply@example.com';
811
		$this->Controller->EmailTest->subject = 'CustomViewClass test';
812
		$this->Controller->EmailTest->delivery = 'DebugComp';
813
		$body = 'Body of message';
814
 
815
		$this->assertTrue($this->Controller->EmailTest->send($body));
816
		$result = DebugCompTransport::$lastEmail;
817
 
818
		$this->assertRegExp('/Body of message/', $result);
819
	}
820
 
821
/**
822
 * testStartup method
823
 *
824
 * @return void
825
 */
826
	public function testStartup() {
827
		$this->assertNull($this->Controller->EmailTest->startup($this->Controller));
828
	}
829
 
830
/**
831
 * testMessageId method
832
 *
833
 * @return void
834
 */
835
	public function testMessageId() {
836
		$this->Controller->EmailTest->to = 'postmaster@example.com';
837
		$this->Controller->EmailTest->from = 'noreply@example.com';
838
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
839
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
840
		$this->Controller->EmailTest->template = null;
841
 
842
		$this->Controller->EmailTest->delivery = 'DebugComp';
843
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
844
		$result = DebugCompTransport::$lastEmail;
845
 
846
		$host = env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n');
847
		$this->assertRegExp('/Message-ID: \<[a-f0-9]{8}[a-f0-9]{4}[a-f0-9]{4}[a-f0-9]{4}[a-f0-9]{12}@' . $host . '\>\n/', $result);
848
 
849
		$this->Controller->EmailTest->messageId = '<22091985.998877@example.com>';
850
 
851
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
852
		$result = DebugCompTransport::$lastEmail;
853
 
854
		$this->assertRegExp('/Message-ID: <22091985.998877@example.com>\n/', $result);
855
 
856
		$this->Controller->EmailTest->messageId = false;
857
 
858
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
859
		$result = DebugCompTransport::$lastEmail;
860
 
861
		$this->assertNotRegExp('/Message-ID:/', $result);
862
	}
863
 
864
/**
865
 * Make sure from/to are not double encoded when UTF-8 is present
866
 */
867
	public function testEncodingFrom() {
868
		$this->Controller->EmailTest->to = 'Teßt <test@example.com>';
869
		$this->Controller->EmailTest->from = 'Teßt <test@example.com>';
870
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
871
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
872
		$this->Controller->EmailTest->template = null;
873
 
874
		$this->Controller->EmailTest->delivery = 'DebugComp';
875
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
876
		$result = DebugCompTransport::$lastEmail;
877
 
878
		$this->assertContains('From: =?UTF-8?B?VGXDn3Qg?= <test@example.com>', $result);
879
		$this->assertContains('To: =?UTF-8?B?VGXDn3Qg?= <test@example.com>', $result);
880
	}
881
 
882
}