EmailComponentTest.php 30 KB

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