EmailComponentTest.php 30 KB

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