Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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