EmailComponentTest.php 30 KB

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