Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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 bool
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
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
		$expected = 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($expected, DebugCompTransport::$lastEmail);
197
 
198
		$this->Controller->EmailTest->sendAs = 'html';
199
		$expected = 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($expected, DebugCompTransport::$lastEmail);
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
		$expected = '<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($expected, DebugCompTransport::$lastEmail);
268
 
269
		$this->Controller->EmailTest->sendAs = 'html';
270
		$expected = '<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($expected, DebugCompTransport::$lastEmail);
273
 
274
		$this->Controller->EmailTest->sendAs = 'both';
275
		$expected = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="{boundary}"', $header);
276
		$expected .= "--{boundary}\n" .
277
			'Content-Type: text/plain; charset=UTF-8' . "\n" .
278
			'Content-Transfer-Encoding: 8bit' . "\n\n" .
279
			$text .
280
			"\n\n" .
281
			'--{boundary}' . "\n" .
282
			'Content-Type: text/html; charset=UTF-8' . "\n" .
283
			'Content-Transfer-Encoding: 8bit' . "\n\n" .
284
			$html .
285
			"\n\n\n" .
286
			'--{boundary}--' . "\n";
287
 
288
		$expected = '<pre>' . $expected . '</pre>';
289
 
290
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
291
		$this->assertTextEquals(
292
			$expected,
293
			preg_replace('/[a-z0-9]{32}/i', '{boundary}', DebugCompTransport::$lastEmail)
294
		);
295
 
296
		$html = <<<HTMLBLOC
297
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
298
 
299
<html>
300
<head>
301
	<title>Email Test</title>
302
</head>
303
 
304
<body>
305
	<p> This is the body of the message</p><p> </p>
306
	<p>This email was sent using the CakePHP Framework</p>
307
</body>
308
</html>
309
 
310
HTMLBLOC;
311
 
312
		$this->Controller->EmailTest->sendAs = 'html';
313
		$expected = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . '</pre>';
314
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message', 'default', 'thin'));
315
		$this->assertTextEquals($expected, DebugCompTransport::$lastEmail);
316
	}
317
 
318
/**
319
 * test that elements used in email templates get helpers.
320
 *
321
 * @return void
322
 */
323
	public function testTemplateNestedElements() {
324
		$this->Controller->EmailTest->to = 'postmaster@example.com';
325
		$this->Controller->EmailTest->from = 'noreply@example.com';
326
		$this->Controller->EmailTest->subject = 'Cake SMTP test';
327
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
328
 
329
		$this->Controller->EmailTest->delivery = 'DebugComp';
330
		$this->Controller->EmailTest->messageId = false;
331
		$this->Controller->EmailTest->layout = 'default';
332
		$this->Controller->EmailTest->template = 'nested_element';
333
		$this->Controller->EmailTest->sendAs = 'html';
334
		$this->Controller->helpers = array('Html');
335
 
336
		$this->Controller->EmailTest->send();
337
		$result = DebugCompTransport::$lastEmail;
338
		$this->assertRegExp('/Test/', $result);
339
		$this->assertRegExp('/http\:\/\/example\.com/', $result);
340
	}
341
 
342
/**
343
 * testSendDebug method
344
 *
345
 * @return void
346
 */
347
	public function testSendDebug() {
348
		$this->Controller->EmailTest->to = 'postmaster@example.com';
349
		$this->Controller->EmailTest->from = 'noreply@example.com';
350
		$this->Controller->EmailTest->cc = 'cc@example.com';
351
		$this->Controller->EmailTest->bcc = 'bcc@example.com';
352
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
353
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
354
		$this->Controller->EmailTest->template = null;
355
 
356
		$this->Controller->EmailTest->delivery = 'DebugComp';
357
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
358
		$result = DebugCompTransport::$lastEmail;
359
 
360
		$this->assertRegExp('/To: postmaster@example.com\n/', $result);
361
		$this->assertRegExp('/Subject: Cake Debug Test\n/', $result);
362
		$this->assertRegExp('/Reply-To: noreply@example.com\n/', $result);
363
		$this->assertRegExp('/From: noreply@example.com\n/', $result);
364
		$this->assertRegExp('/Cc: cc@example.com\n/', $result);
365
		$this->assertRegExp('/Bcc: bcc@example.com\n/', $result);
366
		$this->assertRegExp('/Date: ' . preg_quote(self::$sentDate) . '\n/', $result);
367
		$this->assertRegExp('/X-Mailer: CakePHP Email Component\n/', $result);
368
		$this->assertRegExp('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
369
		$this->assertRegExp('/Content-Transfer-Encoding: 8bitMessage:\n/', $result);
370
		$this->assertRegExp('/This is the body of the message/', $result);
371
	}
372
 
373
/**
374
 * test send with delivery = debug and not using sessions.
375
 *
376
 * @return void
377
 */
378
	public function testSendDebugWithNoSessions() {
379
		$session = $this->Controller->Session;
380
		unset($this->Controller->Session);
381
		$this->Controller->EmailTest->to = 'postmaster@example.com';
382
		$this->Controller->EmailTest->from = 'noreply@example.com';
383
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
384
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
385
		$this->Controller->EmailTest->template = null;
386
 
387
		$this->Controller->EmailTest->delivery = 'DebugComp';
388
		$this->Controller->EmailTest->send('This is the body of the message');
389
		$result = DebugCompTransport::$lastEmail;
390
 
391
		$this->assertRegExp('/To: postmaster@example.com\n/', $result);
392
		$this->assertRegExp('/Subject: Cake Debug Test\n/', $result);
393
		$this->assertRegExp('/Reply-To: noreply@example.com\n/', $result);
394
		$this->assertRegExp('/From: noreply@example.com\n/', $result);
395
		$this->assertRegExp('/Date: ' . preg_quote(self::$sentDate) . '\n/', $result);
396
		$this->assertRegExp('/X-Mailer: CakePHP Email Component\n/', $result);
397
		$this->assertRegExp('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
398
		$this->assertRegExp('/Content-Transfer-Encoding: 8bitMessage:\n/', $result);
399
		$this->assertRegExp('/This is the body of the message/', $result);
400
		$this->Controller->Session = $session;
401
	}
402
 
403
/**
404
 * testMessageRetrievalWithoutTemplate method
405
 *
406
 * @return void
407
 */
408
	public function testMessageRetrievalWithoutTemplate() {
409
		App::build(array(
410
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
411
		));
412
 
413
		$this->Controller->EmailTest->to = 'postmaster@example.com';
414
		$this->Controller->EmailTest->from = 'noreply@example.com';
415
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
416
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
417
		$this->Controller->EmailTest->layout = 'default';
418
		$this->Controller->EmailTest->template = null;
419
 
420
		$this->Controller->EmailTest->delivery = 'DebugComp';
421
 
422
		$text = $html = "This is the body of the message\n";
423
 
424
		$this->Controller->EmailTest->sendAs = 'both';
425
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
426
		$this->assertTextEquals($this->Controller->EmailTest->textMessage, $text);
427
		$this->assertTextEquals($this->Controller->EmailTest->htmlMessage, $html);
428
 
429
		$this->Controller->EmailTest->sendAs = 'text';
430
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
431
		$this->assertTextEquals($this->Controller->EmailTest->textMessage, $text);
432
		$this->assertNull($this->Controller->EmailTest->htmlMessage);
433
 
434
		$this->Controller->EmailTest->sendAs = 'html';
435
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
436
		$this->assertNull($this->Controller->EmailTest->textMessage);
437
		$this->assertTextEquals($this->Controller->EmailTest->htmlMessage, $html);
438
	}
439
 
440
/**
441
 * testMessageRetrievalWithTemplate method
442
 *
443
 * @return void
444
 */
445
	public function testMessageRetrievalWithTemplate() {
446
		App::build(array(
447
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
448
		));
449
 
450
		$this->Controller->set('value', 22091985);
451
		$this->Controller->set('title_for_layout', 'EmailTest');
452
 
453
		$this->Controller->EmailTest->to = 'postmaster@example.com';
454
		$this->Controller->EmailTest->from = 'noreply@example.com';
455
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
456
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
457
		$this->Controller->EmailTest->layout = 'default';
458
		$this->Controller->EmailTest->template = 'custom';
459
 
460
		$this->Controller->EmailTest->delivery = 'DebugComp';
461
 
462
		$text = <<<TEXTBLOC
463
 
464
Here is your value: 22091985
465
This email was sent using the CakePHP Framework, http://cakephp.org.
466
TEXTBLOC;
467
 
468
		$html = <<<HTMLBLOC
469
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
470
 
471
<html>
472
<head>
473
	<title>EmailTest</title>
474
</head>
475
 
476
<body>
477
	<p>Here is your value: <b>22091985</b></p>
478
 
479
	<p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
480
</body>
481
</html>
482
HTMLBLOC;
483
 
484
		$this->Controller->EmailTest->sendAs = 'both';
485
		$this->assertTrue($this->Controller->EmailTest->send());
486
		$this->assertTextEquals($this->Controller->EmailTest->textMessage, $text);
487
		$this->assertTextEquals($this->Controller->EmailTest->htmlMessage, $html);
488
 
489
		$this->Controller->EmailTest->sendAs = 'text';
490
		$this->assertTrue($this->Controller->EmailTest->send());
491
		$this->assertTextEquals($this->Controller->EmailTest->textMessage, $text);
492
		$this->assertNull($this->Controller->EmailTest->htmlMessage);
493
 
494
		$this->Controller->EmailTest->sendAs = 'html';
495
		$this->assertTrue($this->Controller->EmailTest->send());
496
		$this->assertNull($this->Controller->EmailTest->textMessage);
497
		$this->assertTextEquals($this->Controller->EmailTest->htmlMessage, $html);
498
	}
499
 
500
/**
501
 * testMessageRetrievalWithHelper method
502
 *
503
 * @return void
504
 */
505
	public function testMessageRetrievalWithHelper() {
506
		App::build(array(
507
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
508
		));
509
 
510
		$timestamp = time();
511
		$this->Controller->set('time', $timestamp);
512
		$this->Controller->set('title_for_layout', 'EmailTest');
513
		$this->Controller->helpers = array('Time');
514
 
515
		$this->Controller->EmailTest->to = 'postmaster@example.com';
516
		$this->Controller->EmailTest->from = 'noreply@example.com';
517
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
518
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
519
		$this->Controller->EmailTest->layout = 'default';
520
		$this->Controller->EmailTest->template = 'custom_helper';
521
		$this->Controller->EmailTest->sendAs = 'text';
522
		$this->Controller->EmailTest->delivery = 'DebugComp';
523
 
524
		$this->assertTrue($this->Controller->EmailTest->send());
525
		$this->assertTrue((bool)strpos($this->Controller->EmailTest->textMessage, 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
526
	}
527
 
528
/**
529
 * testContentArray method
530
 *
531
 * @return void
532
 */
533
	public function testSendContentArray() {
534
		$this->Controller->EmailTest->to = 'postmaster@example.com';
535
		$this->Controller->EmailTest->from = 'noreply@example.com';
536
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
537
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
538
		$this->Controller->EmailTest->template = null;
539
		$this->Controller->EmailTest->delivery = 'DebugComp';
540
 
541
		$content = array('First line', 'Second line', 'Third line');
542
		$this->assertTrue($this->Controller->EmailTest->send($content));
543
		$result = DebugCompTransport::$lastEmail;
544
 
545
		$this->assertRegExp('/To: postmaster@example.com\n/', $result);
546
		$this->assertRegExp('/Subject: Cake Debug Test\n/', $result);
547
		$this->assertRegExp('/Reply-To: noreply@example.com\n/', $result);
548
		$this->assertRegExp('/From: noreply@example.com\n/', $result);
549
		$this->assertRegExp('/X-Mailer: CakePHP Email Component\n/', $result);
550
		$this->assertRegExp('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
551
		$this->assertRegExp('/Content-Transfer-Encoding: 8bitMessage:\n/', $result);
552
		$this->assertRegExp('/First line\n/', $result);
553
		$this->assertRegExp('/Second line\n/', $result);
554
		$this->assertRegExp('/Third line\n/', $result);
555
	}
556
 
557
/**
558
 * test setting a custom date.
559
 *
560
 * @return void
561
 */
562
	public function testDateProperty() {
563
		$this->Controller->EmailTest->to = 'postmaster@example.com';
564
		$this->Controller->EmailTest->from = 'noreply@example.com';
565
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
566
		$this->Controller->EmailTest->date = self::$sentDate = 'Today!';
567
		$this->Controller->EmailTest->template = null;
568
		$this->Controller->EmailTest->delivery = 'DebugComp';
569
 
570
		$this->assertTrue($this->Controller->EmailTest->send('test message'));
571
		$result = DebugCompTransport::$lastEmail;
572
		$this->assertRegExp('/Date: Today!\n/', $result);
573
	}
574
 
575
/**
576
 * testContentStripping method
577
 *
578
 * @return void
579
 */
580
	public function testContentStripping() {
581
		$content = "Previous content\n--alt-\nContent-TypeContent-Type:: text/html; charsetcharset==utf-8\nContent-Transfer-Encoding: 8bit";
582
		$content .= "\n\n<p>My own html content</p>";
583
 
584
		$result = $this->Controller->EmailTest->strip($content, true);
585
		$expected = "Previous content\n--alt-\n text/html; utf-8\n 8bit\n\n<p>My own html content</p>";
586
		$this->assertEquals($expected, $result);
587
 
588
		$content = '<p>Some HTML content with an <a href="mailto:test@example.com">email link</a>';
589
		$result = $this->Controller->EmailTest->strip($content, true);
590
		$expected = $content;
591
		$this->assertEquals($expected, $result);
592
 
593
		$content = '<p>Some HTML content with an ';
594
		$content .= '<a href="mailto:test@example.com,test2@example.com">email link</a>';
595
		$result = $this->Controller->EmailTest->strip($content, true);
596
		$expected = $content;
597
		$this->assertEquals($expected, $result);
598
	}
599
 
600
/**
601
 * test that the _encode() will set mb_internal_encoding.
602
 *
603
 * @return void
604
 */
605
	public function testEncodeSettingInternalCharset() {
606
		$this->skipIf(!function_exists('mb_internal_encoding'), 'Missing mb_* functions, cannot run test.');
607
 
608
		$restore = mb_internal_encoding();
609
		mb_internal_encoding('ISO-8859-1');
610
 
611
		$this->Controller->charset = 'UTF-8';
612
		$this->Controller->EmailTest->to = 'postmaster@example.com';
613
		$this->Controller->EmailTest->from = 'noreply@example.com';
614
		$this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
615
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
616
		$this->Controller->EmailTest->template = null;
617
		$this->Controller->EmailTest->delivery = 'DebugComp';
618
 
619
		$this->Controller->EmailTest->sendAs = 'text';
620
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
621
 
622
		$subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
623
 
624
		preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
625
		$this->assertEquals(trim($matches[1]), $subject);
626
 
627
		$result = mb_internal_encoding();
628
		$this->assertEquals('ISO-8859-1', $result);
629
 
630
		mb_internal_encoding($restore);
631
	}
632
 
633
/**
634
 * testMultibyte method
635
 *
636
 * @return void
637
 */
638
	public function testMultibyte() {
639
		$this->Controller->charset = 'UTF-8';
640
		$this->Controller->EmailTest->to = 'postmaster@example.com';
641
		$this->Controller->EmailTest->from = 'noreply@example.com';
642
		$this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
643
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
644
		$this->Controller->EmailTest->template = null;
645
		$this->Controller->EmailTest->delivery = 'DebugComp';
646
 
647
		$subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
648
 
649
		$this->Controller->EmailTest->sendAs = 'text';
650
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
651
		preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
652
		$this->assertEquals(trim($matches[1]), $subject);
653
 
654
		$this->Controller->EmailTest->sendAs = 'html';
655
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
656
		preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
657
		$this->assertEquals(trim($matches[1]), $subject);
658
 
659
		$this->Controller->EmailTest->sendAs = 'both';
660
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
661
		preg_match('/Subject: (.*)Header:/s', DebugCompTransport::$lastEmail, $matches);
662
		$this->assertEquals(trim($matches[1]), $subject);
663
	}
664
 
665
/**
666
 * undocumented function
667
 *
668
 * @return void
669
 */
670
	public function testSendWithAttachments() {
671
		$this->Controller->EmailTest->to = 'postmaster@example.com';
672
		$this->Controller->EmailTest->from = 'noreply@example.com';
673
		$this->Controller->EmailTest->subject = 'Attachment Test';
674
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
675
		$this->Controller->EmailTest->template = null;
676
		$this->Controller->EmailTest->delivery = 'DebugComp';
677
		$this->Controller->EmailTest->attachments = array(
678
			__FILE__,
679
			'some-name.php' => __FILE__
680
		);
681
		$body = '<p>This is the body of the message</p>';
682
 
683
		$this->Controller->EmailTest->sendAs = 'text';
684
		$this->assertTrue($this->Controller->EmailTest->send($body));
685
		$msg = DebugCompTransport::$lastEmail;
686
		$this->assertRegExp('/' . preg_quote('Content-Disposition: attachment; filename="EmailComponentTest.php"') . '/', $msg);
687
		$this->assertRegExp('/' . preg_quote('Content-Disposition: attachment; filename="some-name.php"') . '/', $msg);
688
	}
689
 
690
/**
691
 * testSendAsIsNotIgnoredIfAttachmentsPresent method
692
 *
693
 * @return void
694
 */
695
	public function testSendAsIsNotIgnoredIfAttachmentsPresent() {
696
		$this->Controller->EmailTest->to = 'postmaster@example.com';
697
		$this->Controller->EmailTest->from = 'noreply@example.com';
698
		$this->Controller->EmailTest->subject = 'Attachment Test';
699
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
700
		$this->Controller->EmailTest->template = null;
701
		$this->Controller->EmailTest->delivery = 'DebugComp';
702
		$this->Controller->EmailTest->attachments = array(__FILE__);
703
		$body = '<p>This is the body of the message</p>';
704
 
705
		$this->Controller->EmailTest->sendAs = 'html';
706
		$this->assertTrue($this->Controller->EmailTest->send($body));
707
		$msg = DebugCompTransport::$lastEmail;
708
		$this->assertNotRegExp('/text\/plain/', $msg);
709
		$this->assertRegExp('/text\/html/', $msg);
710
 
711
		$this->Controller->EmailTest->sendAs = 'text';
712
		$this->assertTrue($this->Controller->EmailTest->send($body));
713
		$msg = DebugCompTransport::$lastEmail;
714
		$this->assertRegExp('/text\/plain/', $msg);
715
		$this->assertNotRegExp('/text\/html/', $msg);
716
 
717
		$this->Controller->EmailTest->sendAs = 'both';
718
		$this->assertTrue($this->Controller->EmailTest->send($body));
719
		$msg = DebugCompTransport::$lastEmail;
720
 
721
		$this->assertRegExp('/text\/plain/', $msg);
722
		$this->assertRegExp('/text\/html/', $msg);
723
		$this->assertRegExp('/multipart\/alternative/', $msg);
724
	}
725
 
726
/**
727
 * testNoDoubleNewlinesInHeaders function
728
 *
729
 * @return void
730
 */
731
	public function testNoDoubleNewlinesInHeaders() {
732
		$this->Controller->EmailTest->to = 'postmaster@example.com';
733
		$this->Controller->EmailTest->from = 'noreply@example.com';
734
		$this->Controller->EmailTest->subject = 'Attachment Test';
735
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
736
		$this->Controller->EmailTest->template = null;
737
		$this->Controller->EmailTest->delivery = 'DebugComp';
738
		$body = '<p>This is the body of the message</p>';
739
 
740
		$this->Controller->EmailTest->sendAs = 'both';
741
		$this->assertTrue($this->Controller->EmailTest->send($body));
742
		$msg = DebugCompTransport::$lastEmail;
743
 
744
		$this->assertNotRegExp('/\n\nContent-Transfer-Encoding/', $msg);
745
		$this->assertRegExp('/\nContent-Transfer-Encoding/', $msg);
746
	}
747
 
748
/**
749
 * testReset method
750
 *
751
 * @return void
752
 */
753
	public function testReset() {
754
		$this->Controller->EmailTest->template = 'default';
755
		$this->Controller->EmailTest->to = 'test.recipient@example.com';
756
		$this->Controller->EmailTest->from = 'test.sender@example.com';
757
		$this->Controller->EmailTest->replyTo = 'test.replyto@example.com';
758
		$this->Controller->EmailTest->return = 'test.return@example.com';
759
		$this->Controller->EmailTest->cc = array('cc1@example.com', 'cc2@example.com');
760
		$this->Controller->EmailTest->bcc = array('bcc1@example.com', 'bcc2@example.com');
761
		$this->Controller->EmailTest->date = 'Today!';
762
		$this->Controller->EmailTest->subject = 'Test subject';
763
		$this->Controller->EmailTest->additionalParams = 'X-additional-header';
764
		$this->Controller->EmailTest->delivery = 'smtp';
765
		$this->Controller->EmailTest->smtpOptions['host'] = 'blah';
766
		$this->Controller->EmailTest->smtpOptions['timeout'] = 0.2;
767
		$this->Controller->EmailTest->attachments = array('attachment1', 'attachment2');
768
		$this->Controller->EmailTest->textMessage = 'This is the body of the message';
769
		$this->Controller->EmailTest->htmlMessage = 'This is the body of the message';
770
		$this->Controller->EmailTest->messageId = false;
771
 
772
		try {
773
			$this->Controller->EmailTest->send('Should not work');
774
			$this->fail('No exception');
775
		} catch (SocketException $e) {
776
			$this->assertTrue(true, 'SocketException raised');
777
		}
778
 
779
		$this->Controller->EmailTest->reset();
780
 
781
		$this->assertNull($this->Controller->EmailTest->template);
782
		$this->assertSame($this->Controller->EmailTest->to, array());
783
		$this->assertNull($this->Controller->EmailTest->from);
784
		$this->assertNull($this->Controller->EmailTest->replyTo);
785
		$this->assertNull($this->Controller->EmailTest->return);
786
		$this->assertSame($this->Controller->EmailTest->cc, array());
787
		$this->assertSame($this->Controller->EmailTest->bcc, array());
788
		$this->assertNull($this->Controller->EmailTest->date);
789
		$this->assertNull($this->Controller->EmailTest->subject);
790
		$this->assertNull($this->Controller->EmailTest->additionalParams);
791
		$this->assertNull($this->Controller->EmailTest->smtpError);
792
		$this->assertSame($this->Controller->EmailTest->attachments, array());
793
		$this->assertNull($this->Controller->EmailTest->textMessage);
794
		$this->assertTrue($this->Controller->EmailTest->messageId);
795
		$this->assertEquals('mail', $this->Controller->EmailTest->delivery);
796
	}
797
 
798
	public function testPluginCustomViewClass() {
799
		App::build(array(
800
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
801
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
802
		));
803
 
804
		$this->Controller->view = 'TestPlugin.Email';
805
 
806
		$this->Controller->EmailTest->to = 'postmaster@example.com';
807
		$this->Controller->EmailTest->from = 'noreply@example.com';
808
		$this->Controller->EmailTest->subject = 'CustomViewClass test';
809
		$this->Controller->EmailTest->delivery = 'DebugComp';
810
		$body = 'Body of message';
811
 
812
		$this->assertTrue($this->Controller->EmailTest->send($body));
813
		$result = DebugCompTransport::$lastEmail;
814
 
815
		$this->assertRegExp('/Body of message/', $result);
816
	}
817
 
818
/**
819
 * testStartup method
820
 *
821
 * @return void
822
 */
823
	public function testStartup() {
824
		$this->assertNull($this->Controller->EmailTest->startup($this->Controller));
825
	}
826
 
827
/**
828
 * testMessageId method
829
 *
830
 * @return void
831
 */
832
	public function testMessageId() {
833
		$this->Controller->EmailTest->to = 'postmaster@example.com';
834
		$this->Controller->EmailTest->from = 'noreply@example.com';
835
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
836
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
837
		$this->Controller->EmailTest->template = null;
838
 
839
		$this->Controller->EmailTest->delivery = 'DebugComp';
840
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
841
		$result = DebugCompTransport::$lastEmail;
842
 
843
		$host = env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n');
844
		$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);
845
 
846
		$this->Controller->EmailTest->messageId = '<22091985.998877@example.com>';
847
 
848
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
849
		$result = DebugCompTransport::$lastEmail;
850
 
851
		$this->assertRegExp('/Message-ID: <22091985.998877@example.com>\n/', $result);
852
 
853
		$this->Controller->EmailTest->messageId = false;
854
 
855
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
856
		$result = DebugCompTransport::$lastEmail;
857
 
858
		$this->assertNotRegExp('/Message-ID:/', $result);
859
	}
860
 
861
/**
862
 * Make sure from/to are not double encoded when UTF-8 is present
863
 *
864
 * @return void
865
 */
866
	public function testEncodingFrom() {
867
		$this->Controller->EmailTest->to = 'Teßt <test@example.com>';
868
		$this->Controller->EmailTest->from = 'Teßt <test@example.com>';
869
		$this->Controller->EmailTest->subject = 'Cake Debug Test';
870
		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
871
		$this->Controller->EmailTest->template = null;
872
 
873
		$this->Controller->EmailTest->delivery = 'DebugComp';
874
		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
875
		$result = DebugCompTransport::$lastEmail;
876
 
877
		$this->assertContains('From: =?UTF-8?B?VGXDn3Qg?= <test@example.com>', $result);
878
		$this->assertContains('To: =?UTF-8?B?VGXDn3Qg?= <test@example.com>', $result);
879
	}
880
 
881
}