EmailComponentTest.php 30 KB

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