PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/controller/components/email.test.php

http://github.com/Datawalke/Coordino
PHP | 1331 lines | 794 code | 200 blank | 337 comment | 10 complexity | 0140ac28cfcd78303b6fbdc4ab476dc3 MD5 | raw file
  1. <?php
  2. /**
  3. * EmailComponentTest file
  4. *
  5. * Series of tests for email component.
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  10. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The Open Group Test Suite 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/view/1196/Testing CakePHP(tm) Tests
  17. * @package cake
  18. * @subpackage cake.cake.tests.cases.libs.controller.components
  19. * @since CakePHP(tm) v 1.2.0.5347
  20. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  21. */
  22. App::import('Component', 'Email');
  23. App::import('Core', 'CakeSocket');
  24. Mock::generate('CakeSocket', 'MockEmailSocket');
  25. /**
  26. * EmailTestComponent class
  27. *
  28. * @package cake
  29. * @subpackage cake.tests.cases.libs.controller.components
  30. */
  31. class EmailTestComponent extends EmailComponent {
  32. var $smtpSend = '';
  33. /**
  34. * smtpSend method override for testing
  35. *
  36. * @access public
  37. * @return mixed
  38. */
  39. function smtpSend($data, $code = '250') {
  40. return parent::_smtpSend($data, $code);
  41. }
  42. /**
  43. * undocumented function
  44. *
  45. * @return void
  46. */
  47. function _smtpSend($data, $code = '250') {
  48. if ($this->_debug) {
  49. $this->smtpSend .= $data . "\n";
  50. return true;
  51. }
  52. return parent::_smtpSend($data, $code);
  53. }
  54. /**
  55. * Convenience setter method for testing.
  56. *
  57. * @access public
  58. * @return void
  59. */
  60. function setConnectionSocket(&$socket) {
  61. $this->__smtpConnection = $socket;
  62. }
  63. /**
  64. * Allows mocks to be used with tests.
  65. *
  66. * @param array $config
  67. * @return void
  68. */
  69. function _getSocket($config) {
  70. if (empty($this->__smtpConnection)) {
  71. parent::_getSocket($config);
  72. }
  73. }
  74. /**
  75. * Convenience getter method for testing.
  76. *
  77. * @access public
  78. * @return mixed
  79. */
  80. function getConnectionSocket() {
  81. return $this->__smtpConnection;
  82. }
  83. /**
  84. * Convenience setter for testing.
  85. *
  86. * @access public
  87. * @return void
  88. */
  89. function setHeaders($headers) {
  90. $this->__header += $headers;
  91. }
  92. /**
  93. * Convenience getter for testing.
  94. *
  95. * @access public
  96. * @return array
  97. */
  98. function getHeaders() {
  99. return $this->__header;
  100. }
  101. /**
  102. * Convenience setter for testing.
  103. *
  104. * @access public
  105. * @return void
  106. */
  107. function setBoundary() {
  108. $this->__createBoundary();
  109. }
  110. /**
  111. * Convenience getter for testing.
  112. *
  113. * @access public
  114. * @return string
  115. */
  116. function getBoundary() {
  117. return $this->__boundary;
  118. }
  119. /**
  120. * Convenience getter for testing.
  121. *
  122. * @access public
  123. * @return string
  124. */
  125. function getMessage() {
  126. return $this->__message;
  127. }
  128. /**
  129. * Convenience getter for testing.
  130. *
  131. * @access protected
  132. * @return string
  133. */
  134. function _getMessage() {
  135. return $this->__message;
  136. }
  137. /**
  138. * Convenience method for testing.
  139. *
  140. * @access public
  141. * @return string
  142. */
  143. function strip($content, $message = false) {
  144. return parent::_strip($content, $message);
  145. }
  146. /**
  147. * Wrapper for testing.
  148. *
  149. * @return void
  150. */
  151. function formatAddress($string, $smtp = false) {
  152. return parent::_formatAddress($string, $smtp);
  153. }
  154. }
  155. /**
  156. * EmailTestController class
  157. *
  158. * @package cake
  159. * @subpackage cake.tests.cases.libs.controller.components
  160. */
  161. class EmailTestController extends Controller {
  162. /**
  163. * name property
  164. *
  165. * @var string 'EmailTest'
  166. * @access public
  167. */
  168. var $name = 'EmailTest';
  169. /**
  170. * uses property
  171. *
  172. * @var mixed null
  173. * @access public
  174. */
  175. var $uses = null;
  176. /**
  177. * components property
  178. *
  179. * @var array
  180. * @access public
  181. */
  182. var $components = array('Session', 'EmailTest');
  183. /**
  184. * pageTitle property
  185. *
  186. * @var string
  187. * @access public
  188. */
  189. var $pageTitle = 'EmailTest';
  190. }
  191. /**
  192. * EmailTest class
  193. *
  194. * @package cake
  195. * @subpackage cake.tests.cases.libs.controller.components
  196. */
  197. class EmailComponentTest extends CakeTestCase {
  198. /**
  199. * Controller property
  200. *
  201. * @var EmailTestController
  202. * @access public
  203. */
  204. var $Controller;
  205. /**
  206. * name property
  207. *
  208. * @var string 'Email'
  209. * @access public
  210. */
  211. var $name = 'Email';
  212. /**
  213. * setUp method
  214. *
  215. * @access public
  216. * @return void
  217. */
  218. function setUp() {
  219. $this->_appEncoding = Configure::read('App.encoding');
  220. Configure::write('App.encoding', 'UTF-8');
  221. $this->Controller =& new EmailTestController();
  222. restore_error_handler();
  223. @$this->Controller->Component->init($this->Controller);
  224. set_error_handler('simpleTestErrorHandler');
  225. $this->Controller->EmailTest->initialize($this->Controller, array());
  226. ClassRegistry::addObject('view', new View($this->Controller));
  227. App::build(array(
  228. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  229. ));
  230. }
  231. /**
  232. * tearDown method
  233. *
  234. * @access public
  235. * @return void
  236. */
  237. function tearDown() {
  238. Configure::write('App.encoding', $this->_appEncoding);
  239. App::build();
  240. $this->Controller->Session->delete('Message');
  241. restore_error_handler();
  242. ClassRegistry::flush();
  243. }
  244. /**
  245. * osFix method
  246. *
  247. * @param string $string
  248. * @access private
  249. * @return string
  250. */
  251. function __osFix($string) {
  252. return str_replace(array("\r\n", "\r"), "\n", $string);
  253. }
  254. /**
  255. * testSmtpConfig method
  256. *
  257. * @access public
  258. * @return void
  259. */
  260. function testSmtpConfig() {
  261. if ($this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) {
  262. return;
  263. }
  264. $this->Controller->EmailTest->delivery = 'smtp';
  265. $this->Controller->EmailTest->smtpOptions = array();
  266. $this->Controller->EmailTest->send('anything');
  267. $config = array(
  268. 'host' => 'localhost',
  269. 'port' => 25,
  270. 'protocol' => 'smtp',
  271. 'timeout' => 30
  272. );
  273. $this->assertEqual($config, $this->Controller->EmailTest->smtpOptions);
  274. $this->Controller->EmailTest->smtpOptions = array('port' => 80);
  275. $this->Controller->EmailTest->send('anything');
  276. $config['port'] = 80;
  277. $this->assertEqual($config, $this->Controller->EmailTest->smtpOptions);
  278. }
  279. /**
  280. * testBadSmtpSend method
  281. *
  282. * @access public
  283. * @return void
  284. */
  285. function testBadSmtpSend() {
  286. if ($this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) {
  287. return;
  288. }
  289. $this->Controller->EmailTest->smtpOptions['host'] = 'blah';
  290. $this->Controller->EmailTest->delivery = 'smtp';
  291. $this->assertFalse($this->Controller->EmailTest->send('Should not work'));
  292. }
  293. /**
  294. * testSmtpSend method
  295. *
  296. * @access public
  297. * @return void
  298. */
  299. function testSmtpSend() {
  300. if ($this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) {
  301. return;
  302. }
  303. $this->Controller->EmailTest->to = 'postmaster@localhost';
  304. $this->Controller->EmailTest->from = 'noreply@example.com';
  305. $this->Controller->EmailTest->subject = 'Cake SMTP test';
  306. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  307. $this->Controller->EmailTest->template = null;
  308. $this->Controller->EmailTest->delivery = 'smtp';
  309. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  310. $this->Controller->EmailTest->_debug = true;
  311. $this->Controller->EmailTest->sendAs = 'text';
  312. $expect = <<<TEMPDOC
  313. <pre>Host: localhost
  314. Port: 25
  315. Timeout: 30
  316. To: postmaster@localhost
  317. From: noreply@example.com
  318. Subject: Cake SMTP test
  319. Header:
  320. To: postmaster@localhost
  321. From: noreply@example.com
  322. Reply-To: noreply@example.com
  323. Subject: Cake SMTP test
  324. X-Mailer: CakePHP Email Component
  325. Content-Type: text/plain; charset=UTF-8
  326. Content-Transfer-Encoding: 7bitParameters:
  327. Message:
  328. This is the body of the message
  329. </pre>
  330. TEMPDOC;
  331. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  332. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  333. }
  334. /**
  335. * testSmtpEhlo method
  336. *
  337. * @access public
  338. * @return void
  339. */
  340. function testSmtpEhlo() {
  341. if ($this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) {
  342. return;
  343. }
  344. $connection =& new CakeSocket(array('protocol'=>'smtp', 'host' => 'localhost', 'port' => 25));
  345. $this->Controller->EmailTest->setConnectionSocket($connection);
  346. $this->Controller->EmailTest->smtpOptions['timeout'] = 10;
  347. $this->assertTrue($connection->connect());
  348. $this->assertTrue($this->Controller->EmailTest->smtpSend(null, '220') !== false);
  349. $this->skipIf($this->Controller->EmailTest->smtpSend('EHLO locahost', '250') === false, '%s do not support EHLO.');
  350. $connection->disconnect();
  351. $this->Controller->EmailTest->to = 'postmaster@localhost';
  352. $this->Controller->EmailTest->from = 'noreply@example.com';
  353. $this->Controller->EmailTest->subject = 'Cake SMTP test';
  354. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  355. $this->Controller->EmailTest->template = null;
  356. $this->Controller->EmailTest->delivery = 'smtp';
  357. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  358. $this->Controller->EmailTest->_debug = true;
  359. $this->Controller->EmailTest->sendAs = 'text';
  360. $expect = <<<TEMPDOC
  361. <pre>Host: localhost
  362. Port: 25
  363. Timeout: 30
  364. To: postmaster@localhost
  365. From: noreply@example.com
  366. Subject: Cake SMTP test
  367. Header:
  368. To: postmaster@localhost
  369. From: noreply@example.com
  370. Reply-To: noreply@example.com
  371. Subject: Cake SMTP test
  372. X-Mailer: CakePHP Email Component
  373. Content-Type: text/plain; charset=UTF-8
  374. Content-Transfer-Encoding: 7bitParameters:
  375. Message:
  376. This is the body of the message
  377. </pre>
  378. TEMPDOC;
  379. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  380. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  381. }
  382. /**
  383. * testSmtpSendMultipleTo method
  384. *
  385. * @access public
  386. * @return void
  387. */
  388. function testSmtpSendMultipleTo() {
  389. if ($this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) {
  390. return;
  391. }
  392. $this->Controller->EmailTest->reset();
  393. $this->Controller->EmailTest->to = array('postmaster@localhost', 'root@localhost');
  394. $this->Controller->EmailTest->from = 'noreply@example.com';
  395. $this->Controller->EmailTest->subject = 'Cake SMTP multiple To test';
  396. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  397. $this->Controller->EmailTest->template = null;
  398. $this->Controller->EmailTest->_debug = true;
  399. $this->Controller->EmailTest->sendAs = 'text';
  400. $this->Controller->EmailTest->delivery = 'smtp';
  401. $socket = new MockEmailSocket();
  402. $socket->setReturnValue('connect', true);
  403. $this->Controller->EmailTest->setConnectionSocket($socket);
  404. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  405. $this->assertPattern('/EHLO localhost\n/', $this->Controller->EmailTest->smtpSend);
  406. $this->assertPattern('/MAIL FROM: <noreply@example\.com>\n/', $this->Controller->EmailTest->smtpSend);
  407. $this->assertPattern('/RCPT TO: <postmaster@localhost>\n/', $this->Controller->EmailTest->smtpSend);
  408. $this->assertPattern('/RCPT TO: <root@localhost>\n/', $this->Controller->EmailTest->smtpSend);
  409. $this->assertPattern(
  410. '/To: postmaster@localhost, root@localhost[\n\r]/',
  411. $this->Controller->EmailTest->smtpSend
  412. );
  413. }
  414. /**
  415. * test sending smtp from a host using a port.
  416. *
  417. * @return void
  418. */
  419. function testSmtpSendHostWithPort() {
  420. $bkp = env('HTTP_HOST');
  421. $_SERVER['HTTP_HOST'] = 'localhost:8080';
  422. $this->Controller->EmailTest->reset();
  423. $this->Controller->EmailTest->to = array('root@localhost');
  424. $this->Controller->EmailTest->from = 'noreply@example.com';
  425. $this->Controller->EmailTest->subject = 'Cake SMTP host test';
  426. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  427. $this->Controller->EmailTest->template = null;
  428. $this->Controller->EmailTest->delivery = 'smtp';
  429. $this->Controller->EmailTest->sendAs = 'text';
  430. $this->Controller->EmailTest->_debug = true;
  431. $socket = new MockEmailSocket();
  432. $socket->setReturnValue('connect', true);
  433. $this->Controller->EmailTest->setConnectionSocket($socket);
  434. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  435. $this->assertPattern('/EHLO localhost\n/', $this->Controller->EmailTest->smtpSend);
  436. $_SERVER['HTTP_HOST'] = $bkp;
  437. }
  438. /**
  439. * testAuthenticatedSmtpSend method
  440. *
  441. * @access public
  442. * @return void
  443. */
  444. function testAuthenticatedSmtpSend() {
  445. if ($this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) {
  446. return;
  447. }
  448. $this->Controller->EmailTest->to = 'postmaster@localhost';
  449. $this->Controller->EmailTest->from = 'noreply@example.com';
  450. $this->Controller->EmailTest->subject = 'Cake SMTP test';
  451. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  452. $this->Controller->EmailTest->template = null;
  453. $this->Controller->EmailTest->smtpOptions['username'] = 'test';
  454. $this->Controller->EmailTest->smtpOptions['password'] = 'testing';
  455. $this->Controller->EmailTest->delivery = 'smtp';
  456. $result = $this->Controller->EmailTest->send('This is the body of the message');
  457. $code = substr($this->Controller->EmailTest->smtpError, 0, 3);
  458. $this->skipIf(!$code, '%s Authentication not enabled on server');
  459. $this->assertFalse($result);
  460. $this->assertEqual($code, '535');
  461. }
  462. /**
  463. * testSendFormats method
  464. *
  465. * @access public
  466. * @return void
  467. */
  468. function testSendFormats() {
  469. $this->Controller->EmailTest->to = 'postmaster@localhost';
  470. $this->Controller->EmailTest->from = 'noreply@example.com';
  471. $this->Controller->EmailTest->subject = 'Cake SMTP test';
  472. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  473. $this->Controller->EmailTest->template = null;
  474. $this->Controller->EmailTest->delivery = 'debug';
  475. $this->Controller->EmailTest->messageId = false;
  476. $date = date(DATE_RFC2822);
  477. $message = <<<MSGBLOC
  478. <pre>To: postmaster@localhost
  479. From: noreply@example.com
  480. Subject: Cake SMTP test
  481. Header:
  482. From: noreply@example.com
  483. Reply-To: noreply@example.com
  484. Date: $date
  485. X-Mailer: CakePHP Email Component
  486. Content-Type: {CONTENTTYPE}
  487. Content-Transfer-Encoding: 7bitParameters:
  488. Message:
  489. This is the body of the message
  490. </pre>
  491. MSGBLOC;
  492. $this->Controller->EmailTest->sendAs = 'text';
  493. $expect = str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $message);
  494. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  495. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  496. $this->Controller->EmailTest->sendAs = 'html';
  497. $expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $message);
  498. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  499. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  500. // TODO: better test for format of message sent?
  501. $this->Controller->EmailTest->sendAs = 'both';
  502. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  503. $boundary = $this->Controller->EmailTest->getBoundary();
  504. $expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-' . $boundary . '"', $message);
  505. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  506. }
  507. /**
  508. * testTemplates method
  509. *
  510. * @access public
  511. * @return void
  512. */
  513. function testTemplates() {
  514. ClassRegistry::flush();
  515. $this->Controller->EmailTest->to = 'postmaster@localhost';
  516. $this->Controller->EmailTest->from = 'noreply@example.com';
  517. $this->Controller->EmailTest->subject = 'Cake SMTP test';
  518. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  519. $this->Controller->EmailTest->delivery = 'debug';
  520. $this->Controller->EmailTest->messageId = false;
  521. $date = date(DATE_RFC2822);
  522. $header = <<<HEADBLOC
  523. To: postmaster@localhost
  524. From: noreply@example.com
  525. Subject: Cake SMTP test
  526. Header:
  527. From: noreply@example.com
  528. Reply-To: noreply@example.com
  529. Date: $date
  530. X-Mailer: CakePHP Email Component
  531. Content-Type: {CONTENTTYPE}
  532. Content-Transfer-Encoding: 7bitParameters:
  533. Message:
  534. HEADBLOC;
  535. $this->Controller->EmailTest->layout = 'default';
  536. $this->Controller->EmailTest->template = 'default';
  537. $text = <<<TEXTBLOC
  538. This is the body of the message
  539. This email was sent using the CakePHP Framework, http://cakephp.org.
  540. TEXTBLOC;
  541. $html = <<<HTMLBLOC
  542. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  543. <html>
  544. <head>
  545. <title>Email Test</title>
  546. </head>
  547. <body>
  548. <p> This is the body of the message</p><p> </p>
  549. <p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
  550. </body>
  551. </html>
  552. HTMLBLOC;
  553. $this->Controller->EmailTest->sendAs = 'text';
  554. $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $header) . $text . "\n" . '</pre>';
  555. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  556. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  557. $this->Controller->EmailTest->sendAs = 'html';
  558. $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
  559. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  560. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  561. $this->Controller->EmailTest->sendAs = 'both';
  562. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  563. $boundary = $this->Controller->EmailTest->getBoundary();
  564. $expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-' . $boundary . '"', $header);
  565. $expect .= '--alt-' . $boundary . "\n" . 'Content-Type: text/plain; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $text . "\n\n";
  566. $expect .= '--alt-' . $boundary . "\n" . 'Content-Type: text/html; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $html . "\n\n";
  567. $expect = '<pre>' . $expect . "--alt-$boundary--" . "\n\n" . '</pre>';
  568. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  569. $this->Controller->EmailTest->reset();
  570. $this->Controller->EmailTest->to = 'postmaster@localhost';
  571. $this->Controller->EmailTest->from = 'noreply@example.com';
  572. $this->Controller->EmailTest->subject = 'Cake SMTP test';
  573. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  574. $this->Controller->EmailTest->delivery = 'debug';
  575. $this->Controller->EmailTest->messageId = false;
  576. $html = <<<HTMLBLOC
  577. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  578. <html>
  579. <head>
  580. <title>Email Test</title>
  581. </head>
  582. <body>
  583. <p> This is the body of the message</p><p> </p>
  584. <p>This email was sent using the CakePHP Framework</p>
  585. </body>
  586. </html>
  587. HTMLBLOC;
  588. $this->Controller->EmailTest->sendAs = 'html';
  589. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message', 'default', 'thin'));
  590. $boundary = $this->Controller->EmailTest->getBoundary();
  591. $expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html;
  592. $expect = '<pre>' . $expect . '</pre>';
  593. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  594. $result = ClassRegistry::getObject('view');
  595. $this->assertFalse($result);
  596. }
  597. /**
  598. * test that elements used in email templates get helpers.
  599. *
  600. * @return void
  601. */
  602. function testTemplateNestedElements() {
  603. $this->Controller->EmailTest->to = 'postmaster@localhost';
  604. $this->Controller->EmailTest->from = 'noreply@example.com';
  605. $this->Controller->EmailTest->subject = 'Cake SMTP test';
  606. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  607. $this->Controller->EmailTest->delivery = 'debug';
  608. $this->Controller->EmailTest->messageId = false;
  609. $this->Controller->EmailTest->layout = 'default';
  610. $this->Controller->EmailTest->template = 'nested_element';
  611. $this->Controller->EmailTest->sendAs = 'html';
  612. $this->Controller->helpers = array('Html');
  613. $this->Controller->EmailTest->send();
  614. $result = $this->Controller->Session->read('Message.email.message');
  615. $this->assertPattern('/Test/', $result);
  616. $this->assertPattern('/http\:\/\/example\.com/', $result);
  617. }
  618. /**
  619. * testSmtpSendSocket method
  620. *
  621. * @access public
  622. * @return void
  623. */
  624. function testSmtpSendSocket() {
  625. if ($this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) {
  626. return;
  627. }
  628. $this->Controller->EmailTest->smtpOptions['timeout'] = 10;
  629. $socket =& new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->Controller->EmailTest->smtpOptions));
  630. $this->Controller->EmailTest->setConnectionSocket($socket);
  631. $this->assertTrue($this->Controller->EmailTest->getConnectionSocket());
  632. $response = $this->Controller->EmailTest->smtpSend('HELO', '250');
  633. $this->assertPattern('/501 Syntax: HELO hostname/', $this->Controller->EmailTest->smtpError);
  634. $this->Controller->EmailTest->reset();
  635. $response = $this->Controller->EmailTest->smtpSend('HELO somehostname', '250');
  636. $this->assertNoPattern('/501 Syntax: HELO hostname/', $this->Controller->EmailTest->smtpError);
  637. }
  638. /**
  639. * testSendDebug method
  640. *
  641. * @access public
  642. * @return void
  643. */
  644. function testSendDebug() {
  645. $this->Controller->EmailTest->to = 'postmaster@localhost';
  646. $this->Controller->EmailTest->from = 'noreply@example.com';
  647. $this->Controller->EmailTest->cc = 'cc@example.com';
  648. $this->Controller->EmailTest->bcc = 'bcc@example.com';
  649. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  650. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  651. $this->Controller->EmailTest->template = null;
  652. $this->Controller->EmailTest->delivery = 'debug';
  653. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  654. $result = $this->Controller->Session->read('Message.email.message');
  655. $this->assertPattern('/To: postmaster@localhost\n/', $result);
  656. $this->assertPattern('/Subject: Cake Debug Test\n/', $result);
  657. $this->assertPattern('/Reply-To: noreply@example.com\n/', $result);
  658. $this->assertPattern('/From: noreply@example.com\n/', $result);
  659. $this->assertPattern('/Cc: cc@example.com\n/', $result);
  660. $this->assertPattern('/Bcc: bcc@example.com\n/', $result);
  661. $this->assertPattern('/Date: ' . preg_quote(date(DATE_RFC2822)) . '\n/', $result);
  662. $this->assertPattern('/X-Mailer: CakePHP Email Component\n/', $result);
  663. $this->assertPattern('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
  664. $this->assertPattern('/Content-Transfer-Encoding: 7bitParameters:\n/', $result);
  665. $this->assertPattern('/This is the body of the message/', $result);
  666. }
  667. /**
  668. * test send with delivery = debug and not using sessions.
  669. *
  670. * @return void
  671. */
  672. function testSendDebugWithNoSessions() {
  673. $session =& $this->Controller->Session;
  674. unset($this->Controller->Session);
  675. $this->Controller->EmailTest->to = 'postmaster@localhost';
  676. $this->Controller->EmailTest->from = 'noreply@example.com';
  677. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  678. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  679. $this->Controller->EmailTest->template = null;
  680. $this->Controller->EmailTest->delivery = 'debug';
  681. $result = $this->Controller->EmailTest->send('This is the body of the message');
  682. $this->assertPattern('/To: postmaster@localhost\n/', $result);
  683. $this->assertPattern('/Subject: Cake Debug Test\n/', $result);
  684. $this->assertPattern('/Reply-To: noreply@example.com\n/', $result);
  685. $this->assertPattern('/From: noreply@example.com\n/', $result);
  686. $this->assertPattern('/Date: ' . preg_quote(date(DATE_RFC2822)) . '\n/', $result);
  687. $this->assertPattern('/X-Mailer: CakePHP Email Component\n/', $result);
  688. $this->assertPattern('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
  689. $this->assertPattern('/Content-Transfer-Encoding: 7bitParameters:\n/', $result);
  690. $this->assertPattern('/This is the body of the message/', $result);
  691. $this->Controller->Session = $session;
  692. }
  693. /**
  694. * testMessageRetrievalWithoutTemplate method
  695. *
  696. * @access public
  697. * @return void
  698. */
  699. function testMessageRetrievalWithoutTemplate() {
  700. App::build(array(
  701. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  702. ));
  703. $this->Controller->EmailTest->to = 'postmaster@localhost';
  704. $this->Controller->EmailTest->from = 'noreply@example.com';
  705. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  706. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  707. $this->Controller->EmailTest->layout = 'default';
  708. $this->Controller->EmailTest->template = null;
  709. $this->Controller->EmailTest->delivery = 'debug';
  710. $text = $html = 'This is the body of the message';
  711. $this->Controller->EmailTest->sendAs = 'both';
  712. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  713. $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
  714. $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
  715. $this->Controller->EmailTest->sendAs = 'text';
  716. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  717. $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
  718. $this->assertNull($this->Controller->EmailTest->htmlMessage);
  719. $this->Controller->EmailTest->sendAs = 'html';
  720. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  721. $this->assertNull($this->Controller->EmailTest->textMessage);
  722. $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
  723. }
  724. /**
  725. * testMessageRetrievalWithTemplate method
  726. *
  727. * @access public
  728. * @return void
  729. */
  730. function testMessageRetrievalWithTemplate() {
  731. App::build(array(
  732. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  733. ));
  734. $this->Controller->set('value', 22091985);
  735. $this->Controller->set('title_for_layout', 'EmailTest');
  736. $this->Controller->EmailTest->to = 'postmaster@localhost';
  737. $this->Controller->EmailTest->from = 'noreply@example.com';
  738. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  739. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  740. $this->Controller->EmailTest->layout = 'default';
  741. $this->Controller->EmailTest->template = 'custom';
  742. $this->Controller->EmailTest->delivery = 'debug';
  743. $text = <<<TEXTBLOC
  744. Here is your value: 22091985
  745. This email was sent using the CakePHP Framework, http://cakephp.org.
  746. TEXTBLOC;
  747. $html = <<<HTMLBLOC
  748. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  749. <html>
  750. <head>
  751. <title>EmailTest</title>
  752. </head>
  753. <body>
  754. <p>Here is your value: <b>22091985</b></p>
  755. <p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
  756. </body>
  757. </html>
  758. HTMLBLOC;
  759. $this->Controller->EmailTest->sendAs = 'both';
  760. $this->assertTrue($this->Controller->EmailTest->send());
  761. $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
  762. $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
  763. $this->Controller->EmailTest->sendAs = 'text';
  764. $this->assertTrue($this->Controller->EmailTest->send());
  765. $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
  766. $this->assertNull($this->Controller->EmailTest->htmlMessage);
  767. $this->Controller->EmailTest->sendAs = 'html';
  768. $this->assertTrue($this->Controller->EmailTest->send());
  769. $this->assertNull($this->Controller->EmailTest->textMessage);
  770. $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
  771. }
  772. /**
  773. * testContentArray method
  774. *
  775. * @access public
  776. * @return void
  777. */
  778. function testSendContentArray() {
  779. $this->Controller->EmailTest->to = 'postmaster@localhost';
  780. $this->Controller->EmailTest->from = 'noreply@example.com';
  781. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  782. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  783. $this->Controller->EmailTest->template = null;
  784. $this->Controller->EmailTest->delivery = 'debug';
  785. $content = array('First line', 'Second line', 'Third line');
  786. $this->assertTrue($this->Controller->EmailTest->send($content));
  787. $result = $this->Controller->Session->read('Message.email.message');
  788. $this->assertPattern('/To: postmaster@localhost\n/', $result);
  789. $this->assertPattern('/Subject: Cake Debug Test\n/', $result);
  790. $this->assertPattern('/Reply-To: noreply@example.com\n/', $result);
  791. $this->assertPattern('/From: noreply@example.com\n/', $result);
  792. $this->assertPattern('/X-Mailer: CakePHP Email Component\n/', $result);
  793. $this->assertPattern('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
  794. $this->assertPattern('/Content-Transfer-Encoding: 7bitParameters:\n/', $result);
  795. $this->assertPattern('/First line\n/', $result);
  796. $this->assertPattern('/Second line\n/', $result);
  797. $this->assertPattern('/Third line\n/', $result);
  798. }
  799. /**
  800. * test setting a custom date.
  801. *
  802. * @return void
  803. */
  804. function testDateProperty() {
  805. $this->Controller->EmailTest->to = 'postmaster@localhost';
  806. $this->Controller->EmailTest->from = 'noreply@example.com';
  807. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  808. $this->Controller->EmailTest->date = 'Today!';
  809. $this->Controller->EmailTest->template = null;
  810. $this->Controller->EmailTest->delivery = 'debug';
  811. $this->assertTrue($this->Controller->EmailTest->send('test message'));
  812. $result = $this->Controller->Session->read('Message.email.message');
  813. $this->assertPattern('/Date: Today!\n/', $result);
  814. }
  815. /**
  816. * testContentStripping method
  817. *
  818. * @access public
  819. * @return void
  820. */
  821. function testContentStripping() {
  822. $content = "Previous content\n--alt-\nContent-TypeContent-Type:: text/html; charsetcharset==utf-8\nContent-Transfer-Encoding: 7bit";
  823. $content .= "\n\n<p>My own html content</p>";
  824. $result = $this->Controller->EmailTest->strip($content, true);
  825. $expected = "Previous content\n--alt-\n text/html; utf-8\n 7bit\n\n<p>My own html content</p>";
  826. $this->assertEqual($result, $expected);
  827. $content = '<p>Some HTML content with an <a href="mailto:test@example.com">email link</a>';
  828. $result = $this->Controller->EmailTest->strip($content, true);
  829. $expected = $content;
  830. $this->assertEqual($result, $expected);
  831. $content = '<p>Some HTML content with an ';
  832. $content .= '<a href="mailto:test@example.com,test2@example.com">email link</a>';
  833. $result = $this->Controller->EmailTest->strip($content, true);
  834. $expected = $content;
  835. $this->assertEqual($result, $expected);
  836. $content = 'This is a test email to: you and whomever you forward it to';
  837. $result = $this->Controller->EmailTest->strip($content, true);
  838. $expected = $content;
  839. $this->assertEqual($result, $expected);
  840. }
  841. /**
  842. * test that the _encode() will set mb_internal_encoding.
  843. *
  844. * @return void
  845. */
  846. function test_encodeSettingInternalCharset() {
  847. $skip = !function_exists('mb_internal_encoding');
  848. if ($this->skipIf($skip, 'Missing mb_* functions, cannot run test.')) {
  849. return;
  850. }
  851. mb_internal_encoding('ISO-8859-1');
  852. $this->Controller->charset = 'UTF-8';
  853. $this->Controller->EmailTest->to = 'postmaster@localhost';
  854. $this->Controller->EmailTest->from = 'noreply@example.com';
  855. $this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
  856. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  857. $this->Controller->EmailTest->template = null;
  858. $this->Controller->EmailTest->delivery = 'debug';
  859. $this->Controller->EmailTest->sendAs = 'text';
  860. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  861. $subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
  862. preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
  863. $this->assertEqual(trim($matches[1]), $subject);
  864. $result = mb_internal_encoding();
  865. $this->assertEqual($result, 'ISO-8859-1');
  866. }
  867. /**
  868. * testMultibyte method
  869. *
  870. * @access public
  871. * @return void
  872. */
  873. function testMultibyte() {
  874. $this->Controller->charset = 'UTF-8';
  875. $this->Controller->EmailTest->to = 'postmaster@localhost';
  876. $this->Controller->EmailTest->from = 'noreply@example.com';
  877. $this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
  878. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  879. $this->Controller->EmailTest->template = null;
  880. $this->Controller->EmailTest->delivery = 'debug';
  881. $subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
  882. $this->Controller->EmailTest->sendAs = 'text';
  883. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  884. preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
  885. $this->assertEqual(trim($matches[1]), $subject);
  886. $this->Controller->EmailTest->sendAs = 'html';
  887. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  888. preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
  889. $this->assertEqual(trim($matches[1]), $subject);
  890. $this->Controller->EmailTest->sendAs = 'both';
  891. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  892. preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
  893. $this->assertEqual(trim($matches[1]), $subject);
  894. }
  895. /**
  896. * undocumented function
  897. *
  898. * @return void
  899. * @access public
  900. */
  901. function testSendWithAttachments() {
  902. $this->Controller->EmailTest->to = 'postmaster@localhost';
  903. $this->Controller->EmailTest->from = 'noreply@example.com';
  904. $this->Controller->EmailTest->subject = 'Attachment Test';
  905. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  906. $this->Controller->EmailTest->template = null;
  907. $this->Controller->EmailTest->delivery = 'debug';
  908. $this->Controller->EmailTest->attachments = array(
  909. __FILE__,
  910. 'some-name.php' => __FILE__
  911. );
  912. $body = '<p>This is the body of the message</p>';
  913. $this->Controller->EmailTest->sendAs = 'text';
  914. $this->assertTrue($this->Controller->EmailTest->send($body));
  915. $msg = $this->Controller->Session->read('Message.email.message');
  916. $this->assertPattern('/' . preg_quote('Content-Disposition: attachment; filename="email.test.php"') . '/', $msg);
  917. $this->assertPattern('/' . preg_quote('Content-Disposition: attachment; filename="some-name.php"') . '/', $msg);
  918. }
  919. /**
  920. * testSendAsIsNotIgnoredIfAttachmentsPresent method
  921. *
  922. * @return void
  923. * @access public
  924. */
  925. function testSendAsIsNotIgnoredIfAttachmentsPresent() {
  926. $this->Controller->EmailTest->to = 'postmaster@localhost';
  927. $this->Controller->EmailTest->from = 'noreply@example.com';
  928. $this->Controller->EmailTest->subject = 'Attachment Test';
  929. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  930. $this->Controller->EmailTest->template = null;
  931. $this->Controller->EmailTest->delivery = 'debug';
  932. $this->Controller->EmailTest->attachments = array(__FILE__);
  933. $body = '<p>This is the body of the message</p>';
  934. $this->Controller->EmailTest->sendAs = 'html';
  935. $this->assertTrue($this->Controller->EmailTest->send($body));
  936. $msg = $this->Controller->Session->read('Message.email.message');
  937. $this->assertNoPattern('/text\/plain/', $msg);
  938. $this->assertPattern('/text\/html/', $msg);
  939. $this->Controller->EmailTest->sendAs = 'text';
  940. $this->assertTrue($this->Controller->EmailTest->send($body));
  941. $msg = $this->Controller->Session->read('Message.email.message');
  942. $this->assertPattern('/text\/plain/', $msg);
  943. $this->assertNoPattern('/text\/html/', $msg);
  944. $this->Controller->EmailTest->sendAs = 'both';
  945. $this->assertTrue($this->Controller->EmailTest->send($body));
  946. $msg = $this->Controller->Session->read('Message.email.message');
  947. $this->assertNoPattern('/text\/plain/', $msg);
  948. $this->assertNoPattern('/text\/html/', $msg);
  949. $this->assertPattern('/multipart\/alternative/', $msg);
  950. }
  951. /**
  952. * testNoDoubleNewlinesInHeaders function
  953. *
  954. * @return void
  955. * @access public
  956. */
  957. function testNoDoubleNewlinesInHeaders() {
  958. $this->Controller->EmailTest->to = 'postmaster@localhost';
  959. $this->Controller->EmailTest->from = 'noreply@example.com';
  960. $this->Controller->EmailTest->subject = 'Attachment Test';
  961. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  962. $this->Controller->EmailTest->template = null;
  963. $this->Controller->EmailTest->delivery = 'debug';
  964. $body = '<p>This is the body of the message</p>';
  965. $this->Controller->EmailTest->sendAs = 'both';
  966. $this->assertTrue($this->Controller->EmailTest->send($body));
  967. $msg = $this->Controller->Session->read('Message.email.message');
  968. $this->assertNoPattern('/\n\nContent-Transfer-Encoding/', $msg);
  969. $this->assertPattern('/\nContent-Transfer-Encoding/', $msg);
  970. }
  971. /**
  972. * testReset method
  973. *
  974. * @access public
  975. * @return void
  976. */
  977. function testReset() {
  978. $this->Controller->EmailTest->template = 'test_template';
  979. $this->Controller->EmailTest->to = 'test.recipient@example.com';
  980. $this->Controller->EmailTest->from = 'test.sender@example.com';
  981. $this->Controller->EmailTest->replyTo = 'test.replyto@example.com';
  982. $this->Controller->EmailTest->return = 'test.return@example.com';
  983. $this->Controller->EmailTest->cc = array('cc1@example.com', 'cc2@example.com');
  984. $this->Controller->EmailTest->bcc = array('bcc1@example.com', 'bcc2@example.com');
  985. $this->Controller->EmailTest->date = 'Today!';
  986. $this->Controller->EmailTest->subject = 'Test subject';
  987. $this->Controller->EmailTest->additionalParams = 'X-additional-header';
  988. $this->Controller->EmailTest->delivery = 'smtp';
  989. $this->Controller->EmailTest->smtpOptions['host'] = 'blah';
  990. $this->Controller->EmailTest->smtpOptions['timeout'] = 0.5;
  991. $this->Controller->EmailTest->attachments = array('attachment1', 'attachment2');
  992. $this->Controller->EmailTest->textMessage = 'This is the body of the message';
  993. $this->Controller->EmailTest->htmlMessage = 'This is the body of the message';
  994. $this->Controller->EmailTest->messageId = false;
  995. $this->assertFalse($this->Controller->EmailTest->send('Should not work'));
  996. $this->Controller->EmailTest->reset();
  997. $this->assertNull($this->Controller->EmailTest->template);
  998. $this->assertIdentical($this->Controller->EmailTest->to, array());
  999. $this->assertNull($this->Controller->EmailTest->from);
  1000. $this->assertNull($this->Controller->EmailTest->replyTo);
  1001. $this->assertNull($this->Controller->EmailTest->return);
  1002. $this->assertIdentical($this->Controller->EmailTest->cc, array());
  1003. $this->assertIdentical($this->Controller->EmailTest->bcc, array());
  1004. $this->assertNull($this->Controller->EmailTest->date);
  1005. $this->assertNull($this->Controller->EmailTest->subject);
  1006. $this->assertNull($this->Controller->EmailTest->additionalParams);
  1007. $this->assertIdentical($this->Controller->EmailTest->getHeaders(), array());
  1008. $this->assertNull($this->Controller->EmailTest->getBoundary());
  1009. $this->assertIdentical($this->Controller->EmailTest->getMessage(), array());
  1010. $this->assertNull($this->Controller->EmailTest->smtpError);
  1011. $this->assertIdentical($this->Controller->EmailTest->attachments, array());
  1012. $this->assertNull($this->Controller->EmailTest->textMessage);
  1013. $this->assertTrue($this->Controller->EmailTest->messageId);
  1014. $this->assertEqual('mail', $this->Controller->EmailTest->delivery);
  1015. }
  1016. function testPluginCustomViewClass() {
  1017. App::build(array(
  1018. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  1019. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  1020. ));
  1021. $this->Controller->view = 'TestPlugin.Email';
  1022. $this->Controller->EmailTest->to = 'postmaster@localhost';
  1023. $this->Controller->EmailTest->from = 'noreply@example.com';
  1024. $this->Controller->EmailTest->subject = 'CustomViewClass test';
  1025. $this->Controller->EmailTest->delivery = 'debug';
  1026. $body = 'Body of message';
  1027. $this->assertTrue($this->Controller->EmailTest->send($body));
  1028. $result = $this->Controller->Session->read('Message.email.message');
  1029. $this->assertPattern('/Body of message/', $result);
  1030. }
  1031. /**
  1032. * testStartup method
  1033. *
  1034. * @access public
  1035. * @return void
  1036. */
  1037. function testStartup() {
  1038. $this->assertNull($this->Controller->EmailTest->startup($this->Controller));
  1039. }
  1040. /**
  1041. * testMessageId method
  1042. *
  1043. * @access public
  1044. * @return void
  1045. */
  1046. function testMessageId() {
  1047. $this->Controller->EmailTest->to = 'postmaster@localhost';
  1048. $this->Controller->EmailTest->from = 'noreply@example.com';
  1049. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  1050. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  1051. $this->Controller->EmailTest->template = null;
  1052. $this->Controller->EmailTest->delivery = 'debug';
  1053. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  1054. $result = $this->Controller->Session->read('Message.email.message');
  1055. $this->assertPattern('/Message-ID: \<[a-f0-9]{32}@' . env('HTTP_HOST') . '\>\n/', $result);
  1056. $this->Controller->EmailTest->messageId = '<22091985.998877@localhost>';
  1057. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  1058. $result = $this->Controller->Session->read('Message.email.message');
  1059. $this->assertPattern('/Message-ID: <22091985.998877@localhost>\n/', $result);
  1060. $this->Controller->EmailTest->messageId = false;
  1061. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  1062. $result = $this->Controller->Session->read('Message.email.message');
  1063. $this->assertNoPattern('/Message-ID:/', $result);
  1064. }
  1065. /**
  1066. * testSendMessage method
  1067. *
  1068. * @access public
  1069. * @return void
  1070. */
  1071. function testSendMessage() {
  1072. $this->Controller->EmailTest->delivery = 'getMessage';
  1073. $this->Controller->EmailTest->lineLength = 70;
  1074. $text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
  1075. $this->Controller->EmailTest->sendAs = 'text';
  1076. $result = $this->Controller->EmailTest->send($text);
  1077. $expected = array(
  1078. 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do',
  1079. 'eiusmod tempor incididunt ut labore et dolore magna aliqua.',
  1080. '',
  1081. ''
  1082. );
  1083. $this->assertEqual($expected, $result);
  1084. $text = 'Lorem ipsum dolor sit amet, <b>consectetur</b> adipisicing elit, sed do <span>eiusmod tempor</span> incididunt ut labore et dolore magna aliqua.';
  1085. $this->Controller->EmailTest->sendAs = 'html';
  1086. $result = $this->Controller->EmailTest->send($text);
  1087. $expected = array(
  1088. $text,
  1089. '',
  1090. ''
  1091. );
  1092. $this->assertEqual($expected, $result);
  1093. }
  1094. /**
  1095. * Test that _formatName doesn't jack up email addresses with alias parts.
  1096. *
  1097. * @return void
  1098. */
  1099. function testFormatAddressAliases() {
  1100. $result = $this->Controller->EmailTest->formatAddress('email@example.com');
  1101. $this->assertEqual($result, 'email@example.com');
  1102. $result = $this->Controller->EmailTest->formatAddress('alias <email@example.com>');
  1103. $this->assertEqual($result, 'alias <email@example.com>');
  1104. $result = $this->Controller->EmailTest->formatAddress('alias<email@example.com>');
  1105. $this->assertEqual($result, 'alias <email@example.com>');
  1106. $result = $this->Controller->EmailTest->formatAddress('email@example.com');
  1107. $this->assertEqual($result, 'email@example.com');
  1108. $result = $this->Controller->EmailTest->formatAddress('<email@example.com>');
  1109. $this->assertEqual($result, '<email@example.com>');
  1110. $result = $this->Controller->EmailTest->formatAddress('email@example.com', true);
  1111. $this->assertEqual($result, '<email@example.com>');
  1112. $result = $this->Controller->EmailTest->formatAddress('<email@example.com>', true);
  1113. $this->assertEqual($result, '<email@example.com>');
  1114. $result = $this->Controller->EmailTest->formatAddress('alias name <email@example.com>', true);
  1115. $this->assertEqual($result, '<email@example.com>');
  1116. }
  1117. /**
  1118. * test formatting addresses with multibyte chars
  1119. *
  1120. * @return void
  1121. */
  1122. function testFormatAddressMultibyte() {
  1123. $this->Controller->EmailTest->charset = 'UTF-8';
  1124. $result = $this->Controller->EmailTest->formatAddress('ÄÖÜTest <email@domain.de>');
  1125. $this->assertEqual($result, '=?UTF-8?B?w4TDlsOcVGVzdA==?= <email@domain.de>');
  1126. $result = $this->Controller->EmailTest->formatAddress('ÄÖÜTest<email@domain.de>');
  1127. $this->assertEqual($result, '=?UTF-8?B?w4TDlsOcVGVzdA==?= <email@domain.de>');
  1128. $result = $this->Controller->EmailTest->formatAddress('ÄÖÜTest <email@domain.de>', true);
  1129. $this->assertEqual($result, '<email@domain.de>');
  1130. }
  1131. }