EmailComponentTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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/2.0/en/development/testing.html>
  10. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * For full copyright and license information, please see the LICENSE.txt
  14. * Redistributions of files must retain the above copyright notice
  15. *
  16. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  18. * @package Cake.Test.Case.Controller.Component
  19. * @since CakePHP(tm) v 1.2.0.5347
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('Controller', 'Controller');
  23. App::uses('EmailComponent', 'Controller/Component');
  24. App::uses('AbstractTransport', 'Network/Email');
  25. /**
  26. * EmailTestComponent class
  27. *
  28. * @package Cake.Test.Case.Controller.Component
  29. */
  30. class EmailTestComponent extends EmailComponent {
  31. /**
  32. * Convenience method for testing.
  33. *
  34. * @return string
  35. */
  36. public function strip($content, $message = false) {
  37. return parent::_strip($content, $message);
  38. }
  39. }
  40. /**
  41. * DebugCompTransport class
  42. *
  43. * @package Cake.Test.Case.Controller.Component
  44. */
  45. class DebugCompTransport extends AbstractTransport {
  46. /**
  47. * Last email
  48. *
  49. * @var string
  50. */
  51. public static $lastEmail = null;
  52. /**
  53. * Send mail
  54. *
  55. * @params object $email CakeEmail
  56. * @return boolean
  57. */
  58. public function send(CakeEmail $email) {
  59. $email->addHeaders(array('Date' => EmailComponentTest::$sentDate));
  60. $headers = $email->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), true));
  61. $to = $headers['To'];
  62. $subject = $headers['Subject'];
  63. unset($headers['To'], $headers['Subject']);
  64. $message = implode("\n", $email->message());
  65. $last = '<pre>';
  66. $last .= sprintf("%s %s\n", 'To:', $to);
  67. $last .= sprintf("%s %s\n", 'From:', $headers['From']);
  68. $last .= sprintf("%s %s\n", 'Subject:', $subject);
  69. $last .= sprintf("%s\n\n%s", 'Header:', $this->_headersToString($headers, "\n"));
  70. $last .= sprintf("%s\n\n%s", 'Message:', $message);
  71. $last .= '</pre>';
  72. self::$lastEmail = $last;
  73. return true;
  74. }
  75. }
  76. /**
  77. * EmailTestController class
  78. *
  79. * @package Cake.Test.Case.Controller.Component
  80. */
  81. class EmailTestController extends Controller {
  82. /**
  83. * name property
  84. *
  85. * @var string 'EmailTest'
  86. */
  87. public $name = 'EmailTest';
  88. /**
  89. * uses property
  90. *
  91. * @var mixed null
  92. */
  93. public $uses = null;
  94. /**
  95. * components property
  96. *
  97. * @var array
  98. */
  99. public $components = array('Session', 'EmailTest');
  100. }
  101. /**
  102. * EmailTest class
  103. *
  104. * @package Cake.Test.Case.Controller.Component
  105. */
  106. class EmailComponentTest extends CakeTestCase {
  107. /**
  108. * Controller property
  109. *
  110. * @var EmailTestController
  111. */
  112. public $Controller;
  113. /**
  114. * name property
  115. *
  116. * @var string 'Email'
  117. */
  118. public $name = 'Email';
  119. /**
  120. * sentDate
  121. *
  122. * @var string
  123. */
  124. public static $sentDate = null;
  125. /**
  126. * setUp method
  127. *
  128. * @return void
  129. */
  130. public function setUp() {
  131. parent::setUp();
  132. Configure::write('App.encoding', 'UTF-8');
  133. $this->Controller = new EmailTestController();
  134. $this->Controller->Components->init($this->Controller);
  135. $this->Controller->EmailTest->initialize($this->Controller, array());
  136. self::$sentDate = date(DATE_RFC2822);
  137. App::build(array(
  138. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  139. ));
  140. }
  141. /**
  142. * testSendFormats method
  143. *
  144. * @return void
  145. */
  146. public function testSendFormats() {
  147. $this->Controller->EmailTest->to = 'postmaster@example.com';
  148. $this->Controller->EmailTest->from = 'noreply@example.com';
  149. $this->Controller->EmailTest->subject = 'Cake SMTP test';
  150. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  151. $this->Controller->EmailTest->template = null;
  152. $this->Controller->EmailTest->delivery = 'DebugComp';
  153. $this->Controller->EmailTest->messageId = false;
  154. $date = self::$sentDate;
  155. $message = <<<MSGBLOC
  156. <pre>To: postmaster@example.com
  157. From: noreply@example.com
  158. Subject: Cake SMTP test
  159. Header:
  160. From: noreply@example.com
  161. Reply-To: noreply@example.com
  162. X-Mailer: CakePHP Email Component
  163. Date: $date
  164. MIME-Version: 1.0
  165. Content-Type: {CONTENTTYPE}
  166. Content-Transfer-Encoding: 8bitMessage:
  167. This is the body of the message
  168. </pre>
  169. MSGBLOC;
  170. $this->Controller->EmailTest->sendAs = 'text';
  171. $expect = str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $message);
  172. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  173. $this->assertTextEquals(DebugCompTransport::$lastEmail, $expect);
  174. $this->Controller->EmailTest->sendAs = 'html';
  175. $expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $message);
  176. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  177. $this->assertTextEquals(DebugCompTransport::$lastEmail, $expect);
  178. }
  179. /**
  180. * testTemplates method
  181. *
  182. * @return void
  183. */
  184. public function testTemplates() {
  185. ClassRegistry::flush();
  186. $this->Controller->EmailTest->to = 'postmaster@example.com';
  187. $this->Controller->EmailTest->from = 'noreply@example.com';
  188. $this->Controller->EmailTest->subject = 'Cake SMTP test';
  189. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  190. $this->Controller->EmailTest->delivery = 'DebugComp';
  191. $this->Controller->EmailTest->messageId = false;
  192. $date = self::$sentDate;
  193. $header = <<<HEADBLOC
  194. To: postmaster@example.com
  195. From: noreply@example.com
  196. Subject: Cake SMTP test
  197. Header:
  198. From: noreply@example.com
  199. Reply-To: noreply@example.com
  200. X-Mailer: CakePHP Email Component
  201. Date: $date
  202. MIME-Version: 1.0
  203. Content-Type: {CONTENTTYPE}
  204. Content-Transfer-Encoding: 8bitMessage:
  205. HEADBLOC;
  206. $this->Controller->EmailTest->layout = 'default';
  207. $this->Controller->EmailTest->template = 'default';
  208. $this->Controller->set('title_for_layout', 'Email Test');
  209. $text = <<<TEXTBLOC
  210. This is the body of the message
  211. This email was sent using the CakePHP Framework, http://cakephp.org.
  212. TEXTBLOC;
  213. $html = <<<HTMLBLOC
  214. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  215. <html>
  216. <head>
  217. <title>Email Test</title>
  218. </head>
  219. <body>
  220. <p> This is the body of the message</p><p> </p>
  221. <p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
  222. </body>
  223. </html>
  224. HTMLBLOC;
  225. $this->Controller->EmailTest->sendAs = 'text';
  226. $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $header) . $text . "\n" . '</pre>';
  227. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  228. $this->assertTextEquals(DebugCompTransport::$lastEmail, $expect);
  229. $this->Controller->EmailTest->sendAs = 'html';
  230. $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
  231. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  232. $this->assertTextEquals(DebugCompTransport::$lastEmail, $expect);
  233. $this->Controller->EmailTest->sendAs = 'both';
  234. $expect = str_replace('{CONTENTTYPE}', 'multipart/mixed; boundary="{boundary}"', $header);
  235. $expect .= "--{boundary}\n" .
  236. 'Content-Type: multipart/alternative; boundary="alt-{boundary}"' . "\n\n" .
  237. '--alt-{boundary}' . "\n" .
  238. 'Content-Type: text/plain; charset=UTF-8' . "\n" .
  239. 'Content-Transfer-Encoding: 8bit' . "\n\n" .
  240. $text .
  241. "\n\n" .
  242. '--alt-{boundary}' . "\n" .
  243. 'Content-Type: text/html; charset=UTF-8' . "\n" .
  244. 'Content-Transfer-Encoding: 8bit' . "\n\n" .
  245. $html .
  246. "\n\n" .
  247. '--alt-{boundary}--' . "\n\n\n" .
  248. '--{boundary}--' . "\n";
  249. $expect = '<pre>' . $expect . '</pre>';
  250. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  251. $this->assertTextEquals(
  252. $expect,
  253. preg_replace('/[a-z0-9]{32}/i', '{boundary}', DebugCompTransport::$lastEmail)
  254. );
  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->assertTextEquals(DebugCompTransport::$lastEmail, $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->assertRegExp('/Test/', $result);
  291. $this->assertRegExp('/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->assertRegExp('/To: postmaster@example.com\n/', $result);
  310. $this->assertRegExp('/Subject: Cake Debug Test\n/', $result);
  311. $this->assertRegExp('/Reply-To: noreply@example.com\n/', $result);
  312. $this->assertRegExp('/From: noreply@example.com\n/', $result);
  313. $this->assertRegExp('/Cc: cc@example.com\n/', $result);
  314. $this->assertRegExp('/Bcc: bcc@example.com\n/', $result);
  315. $this->assertRegExp('/Date: ' . preg_quote(self::$sentDate) . '\n/', $result);
  316. $this->assertRegExp('/X-Mailer: CakePHP Email Component\n/', $result);
  317. $this->assertRegExp('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
  318. $this->assertRegExp('/Content-Transfer-Encoding: 8bitMessage:\n/', $result);
  319. $this->assertRegExp('/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->assertRegExp('/To: postmaster@example.com\n/', $result);
  338. $this->assertRegExp('/Subject: Cake Debug Test\n/', $result);
  339. $this->assertRegExp('/Reply-To: noreply@example.com\n/', $result);
  340. $this->assertRegExp('/From: noreply@example.com\n/', $result);
  341. $this->assertRegExp('/Date: ' . preg_quote(self::$sentDate) . '\n/', $result);
  342. $this->assertRegExp('/X-Mailer: CakePHP Email Component\n/', $result);
  343. $this->assertRegExp('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
  344. $this->assertRegExp('/Content-Transfer-Encoding: 8bitMessage:\n/', $result);
  345. $this->assertRegExp('/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\n";
  365. $this->Controller->EmailTest->sendAs = 'both';
  366. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  367. $this->assertTextEquals($this->Controller->EmailTest->textMessage, $text);
  368. $this->assertTextEquals($this->Controller->EmailTest->htmlMessage, $html);
  369. $this->Controller->EmailTest->sendAs = 'text';
  370. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  371. $this->assertTextEquals($this->Controller->EmailTest->textMessage, $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->assertTextEquals($this->Controller->EmailTest->htmlMessage, $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->assertTextEquals($this->Controller->EmailTest->textMessage, $text);
  415. $this->assertTextEquals($this->Controller->EmailTest->htmlMessage, $html);
  416. $this->Controller->EmailTest->sendAs = 'text';
  417. $this->assertTrue($this->Controller->EmailTest->send());
  418. $this->assertTextEquals($this->Controller->EmailTest->textMessage, $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->assertTextEquals($this->Controller->EmailTest->htmlMessage, $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->assertRegExp('/To: postmaster@example.com\n/', $result);
  465. $this->assertRegExp('/Subject: Cake Debug Test\n/', $result);
  466. $this->assertRegExp('/Reply-To: noreply@example.com\n/', $result);
  467. $this->assertRegExp('/From: noreply@example.com\n/', $result);
  468. $this->assertRegExp('/X-Mailer: CakePHP Email Component\n/', $result);
  469. $this->assertRegExp('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
  470. $this->assertRegExp('/Content-Transfer-Encoding: 8bitMessage:\n/', $result);
  471. $this->assertRegExp('/First line\n/', $result);
  472. $this->assertRegExp('/Second line\n/', $result);
  473. $this->assertRegExp('/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 = self::$sentDate = '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->assertRegExp('/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->assertEquals($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->assertEquals($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->assertEquals($expected, $result);
  511. }
  512. /**
  513. * test that the _encode() will set mb_internal_encoding.
  514. *
  515. * @return void
  516. */
  517. public function testEncodeSettingInternalCharset() {
  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->assertEquals(trim($matches[1]), $subject);
  533. $result = mb_internal_encoding();
  534. $this->assertEquals('ISO-8859-1', $result);
  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->assertEquals(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->assertEquals(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->assertEquals(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->assertRegExp('/' . preg_quote('Content-Disposition: attachment; filename="EmailComponentTest.php"') . '/', $msg);
  585. $this->assertRegExp('/' . 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->assertNotRegExp('/text\/plain/', $msg);
  605. $this->assertRegExp('/text\/html/', $msg);
  606. $this->Controller->EmailTest->sendAs = 'text';
  607. $this->assertTrue($this->Controller->EmailTest->send($body));
  608. $msg = DebugCompTransport::$lastEmail;
  609. $this->assertRegExp('/text\/plain/', $msg);
  610. $this->assertNotRegExp('/text\/html/', $msg);
  611. $this->Controller->EmailTest->sendAs = 'both';
  612. $this->assertTrue($this->Controller->EmailTest->send($body));
  613. $msg = DebugCompTransport::$lastEmail;
  614. $this->assertRegExp('/text\/plain/', $msg);
  615. $this->assertRegExp('/text\/html/', $msg);
  616. $this->assertRegExp('/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->assertNotRegExp('/\n\nContent-Transfer-Encoding/', $msg);
  635. $this->assertRegExp('/\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->assertSame($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->assertSame($this->Controller->EmailTest->cc, array());
  673. $this->assertSame($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->assertSame($this->Controller->EmailTest->attachments, array());
  679. $this->assertNull($this->Controller->EmailTest->textMessage);
  680. $this->assertTrue($this->Controller->EmailTest->messageId);
  681. $this->assertEquals('mail', $this->Controller->EmailTest->delivery);
  682. }
  683. public function testPluginCustomViewClass() {
  684. App::build(array(
  685. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  686. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  687. ));
  688. $this->Controller->view = 'TestPlugin.Email';
  689. $this->Controller->EmailTest->to = 'postmaster@example.com';
  690. $this->Controller->EmailTest->from = 'noreply@example.com';
  691. $this->Controller->EmailTest->subject = 'CustomViewClass test';
  692. $this->Controller->EmailTest->delivery = 'DebugComp';
  693. $body = 'Body of message';
  694. $this->assertTrue($this->Controller->EmailTest->send($body));
  695. $result = DebugCompTransport::$lastEmail;
  696. $this->assertRegExp('/Body of message/', $result);
  697. }
  698. /**
  699. * testStartup method
  700. *
  701. * @return void
  702. */
  703. public function testStartup() {
  704. $this->assertNull($this->Controller->EmailTest->startup($this->Controller));
  705. }
  706. /**
  707. * testMessageId method
  708. *
  709. * @return void
  710. */
  711. public function testMessageId() {
  712. $this->Controller->EmailTest->to = 'postmaster@example.com';
  713. $this->Controller->EmailTest->from = 'noreply@example.com';
  714. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  715. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  716. $this->Controller->EmailTest->template = null;
  717. $this->Controller->EmailTest->delivery = 'DebugComp';
  718. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  719. $result = DebugCompTransport::$lastEmail;
  720. $host = env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n');
  721. $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);
  722. $this->Controller->EmailTest->messageId = '<22091985.998877@example.com>';
  723. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  724. $result = DebugCompTransport::$lastEmail;
  725. $this->assertRegExp('/Message-ID: <22091985.998877@example.com>\n/', $result);
  726. $this->Controller->EmailTest->messageId = false;
  727. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  728. $result = DebugCompTransport::$lastEmail;
  729. $this->assertNotRegExp('/Message-ID:/', $result);
  730. }
  731. /**
  732. * Make sure from/to are not double encoded when UTF-8 is present
  733. */
  734. public function testEncodingFrom() {
  735. $this->Controller->EmailTest->to = 'Teßt <test@example.com>';
  736. $this->Controller->EmailTest->from = 'Teßt <test@example.com>';
  737. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  738. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  739. $this->Controller->EmailTest->template = null;
  740. $this->Controller->EmailTest->delivery = 'DebugComp';
  741. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  742. $result = DebugCompTransport::$lastEmail;
  743. $this->assertContains('From: =?UTF-8?B?VGXDn3Qg?= <test@example.com>', $result);
  744. $this->assertContains('To: =?UTF-8?B?VGXDn3Qg?= <test@example.com>', $result);
  745. }
  746. }