PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/cgajardo/repositorium
PHP | 1310 lines | 778 code | 195 blank | 337 comment | 10 complexity | 93c15158766526be418c87509d110835 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-2010, 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-2010, 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. $expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"', $message);
  503. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  504. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  505. }
  506. /**
  507. * testTemplates method
  508. *
  509. * @access public
  510. * @return void
  511. */
  512. function testTemplates() {
  513. ClassRegistry::flush();
  514. $this->Controller->EmailTest->to = 'postmaster@localhost';
  515. $this->Controller->EmailTest->from = 'noreply@example.com';
  516. $this->Controller->EmailTest->subject = 'Cake SMTP test';
  517. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  518. $this->Controller->EmailTest->delivery = 'debug';
  519. $this->Controller->EmailTest->messageId = false;
  520. $date = date(DATE_RFC2822);
  521. $header = <<<HEADBLOC
  522. To: postmaster@localhost
  523. From: noreply@example.com
  524. Subject: Cake SMTP test
  525. Header:
  526. From: noreply@example.com
  527. Reply-To: noreply@example.com
  528. Date: $date
  529. X-Mailer: CakePHP Email Component
  530. Content-Type: {CONTENTTYPE}
  531. Content-Transfer-Encoding: 7bitParameters:
  532. Message:
  533. HEADBLOC;
  534. $this->Controller->EmailTest->layout = 'default';
  535. $this->Controller->EmailTest->template = 'default';
  536. $text = <<<TEXTBLOC
  537. This is the body of the message
  538. This email was sent using the CakePHP Framework, http://cakephp.org.
  539. TEXTBLOC;
  540. $html = <<<HTMLBLOC
  541. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  542. <html>
  543. <head>
  544. <title>Email Test</title>
  545. </head>
  546. <body>
  547. <p> This is the body of the message</p><p> </p>
  548. <p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
  549. </body>
  550. </html>
  551. HTMLBLOC;
  552. $this->Controller->EmailTest->sendAs = 'text';
  553. $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $header) . $text . "\n" . '</pre>';
  554. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  555. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  556. $this->Controller->EmailTest->sendAs = 'html';
  557. $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
  558. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  559. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  560. $this->Controller->EmailTest->sendAs = 'both';
  561. $expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"', $header);
  562. $expect .= '--alt-' . "\n" . 'Content-Type: text/plain; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $text . "\n\n";
  563. $expect .= '--alt-' . "\n" . 'Content-Type: text/html; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $html . "\n\n";
  564. $expect = '<pre>' . $expect . '--alt---' . "\n\n" . '</pre>';
  565. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  566. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  567. $html = <<<HTMLBLOC
  568. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  569. <html>
  570. <head>
  571. <title>Email Test</title>
  572. </head>
  573. <body>
  574. <p> This is the body of the message</p><p> </p>
  575. <p>This email was sent using the CakePHP Framework</p>
  576. </body>
  577. </html>
  578. HTMLBLOC;
  579. $this->Controller->EmailTest->sendAs = 'html';
  580. $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . '</pre>';
  581. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message', 'default', 'thin'));
  582. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  583. $result = ClassRegistry::getObject('view');
  584. $this->assertFalse($result);
  585. }
  586. /**
  587. * test that elements used in email templates get helpers.
  588. *
  589. * @return void
  590. */
  591. function testTemplateNestedElements() {
  592. $this->Controller->EmailTest->to = 'postmaster@localhost';
  593. $this->Controller->EmailTest->from = 'noreply@example.com';
  594. $this->Controller->EmailTest->subject = 'Cake SMTP test';
  595. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  596. $this->Controller->EmailTest->delivery = 'debug';
  597. $this->Controller->EmailTest->messageId = false;
  598. $this->Controller->EmailTest->layout = 'default';
  599. $this->Controller->EmailTest->template = 'nested_element';
  600. $this->Controller->EmailTest->sendAs = 'html';
  601. $this->Controller->helpers = array('Html');
  602. $this->Controller->EmailTest->send();
  603. $result = $this->Controller->Session->read('Message.email.message');
  604. $this->assertPattern('/Test/', $result);
  605. $this->assertPattern('/http\:\/\/example\.com/', $result);
  606. }
  607. /**
  608. * testSmtpSendSocket method
  609. *
  610. * @access public
  611. * @return void
  612. */
  613. function testSmtpSendSocket() {
  614. if ($this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) {
  615. return;
  616. }
  617. $this->Controller->EmailTest->smtpOptions['timeout'] = 10;
  618. $socket =& new CakeSocket(array_merge(array('protocol'=>'smtp'), $this->Controller->EmailTest->smtpOptions));
  619. $this->Controller->EmailTest->setConnectionSocket($socket);
  620. $this->assertTrue($this->Controller->EmailTest->getConnectionSocket());
  621. $response = $this->Controller->EmailTest->smtpSend('HELO', '250');
  622. $this->assertPattern('/501 Syntax: HELO hostname/', $this->Controller->EmailTest->smtpError);
  623. $this->Controller->EmailTest->reset();
  624. $response = $this->Controller->EmailTest->smtpSend('HELO somehostname', '250');
  625. $this->assertNoPattern('/501 Syntax: HELO hostname/', $this->Controller->EmailTest->smtpError);
  626. }
  627. /**
  628. * testSendDebug method
  629. *
  630. * @access public
  631. * @return void
  632. */
  633. function testSendDebug() {
  634. $this->Controller->EmailTest->to = 'postmaster@localhost';
  635. $this->Controller->EmailTest->from = 'noreply@example.com';
  636. $this->Controller->EmailTest->cc = 'cc@example.com';
  637. $this->Controller->EmailTest->bcc = 'bcc@example.com';
  638. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  639. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  640. $this->Controller->EmailTest->template = null;
  641. $this->Controller->EmailTest->delivery = 'debug';
  642. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  643. $result = $this->Controller->Session->read('Message.email.message');
  644. $this->assertPattern('/To: postmaster@localhost\n/', $result);
  645. $this->assertPattern('/Subject: Cake Debug Test\n/', $result);
  646. $this->assertPattern('/Reply-To: noreply@example.com\n/', $result);
  647. $this->assertPattern('/From: noreply@example.com\n/', $result);
  648. $this->assertPattern('/Cc: cc@example.com\n/', $result);
  649. $this->assertPattern('/Bcc: bcc@example.com\n/', $result);
  650. $this->assertPattern('/Date: ' . preg_quote(date(DATE_RFC2822)) . '\n/', $result);
  651. $this->assertPattern('/X-Mailer: CakePHP Email Component\n/', $result);
  652. $this->assertPattern('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
  653. $this->assertPattern('/Content-Transfer-Encoding: 7bitParameters:\n/', $result);
  654. $this->assertPattern('/This is the body of the message/', $result);
  655. }
  656. /**
  657. * test send with delivery = debug and not using sessions.
  658. *
  659. * @return void
  660. */
  661. function testSendDebugWithNoSessions() {
  662. $session =& $this->Controller->Session;
  663. unset($this->Controller->Session);
  664. $this->Controller->EmailTest->to = 'postmaster@localhost';
  665. $this->Controller->EmailTest->from = 'noreply@example.com';
  666. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  667. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  668. $this->Controller->EmailTest->template = null;
  669. $this->Controller->EmailTest->delivery = 'debug';
  670. $result = $this->Controller->EmailTest->send('This is the body of the message');
  671. $this->assertPattern('/To: postmaster@localhost\n/', $result);
  672. $this->assertPattern('/Subject: Cake Debug Test\n/', $result);
  673. $this->assertPattern('/Reply-To: noreply@example.com\n/', $result);
  674. $this->assertPattern('/From: noreply@example.com\n/', $result);
  675. $this->assertPattern('/Date: ' . preg_quote(date(DATE_RFC2822)) . '\n/', $result);
  676. $this->assertPattern('/X-Mailer: CakePHP Email Component\n/', $result);
  677. $this->assertPattern('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
  678. $this->assertPattern('/Content-Transfer-Encoding: 7bitParameters:\n/', $result);
  679. $this->assertPattern('/This is the body of the message/', $result);
  680. $this->Controller->Session = $session;
  681. }
  682. /**
  683. * testMessageRetrievalWithoutTemplate method
  684. *
  685. * @access public
  686. * @return void
  687. */
  688. function testMessageRetrievalWithoutTemplate() {
  689. App::build(array(
  690. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  691. ));
  692. $this->Controller->EmailTest->to = 'postmaster@localhost';
  693. $this->Controller->EmailTest->from = 'noreply@example.com';
  694. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  695. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  696. $this->Controller->EmailTest->layout = 'default';
  697. $this->Controller->EmailTest->template = null;
  698. $this->Controller->EmailTest->delivery = 'debug';
  699. $text = $html = 'This is the body of the message';
  700. $this->Controller->EmailTest->sendAs = 'both';
  701. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  702. $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
  703. $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
  704. $this->Controller->EmailTest->sendAs = 'text';
  705. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  706. $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
  707. $this->assertNull($this->Controller->EmailTest->htmlMessage);
  708. $this->Controller->EmailTest->sendAs = 'html';
  709. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  710. $this->assertNull($this->Controller->EmailTest->textMessage);
  711. $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
  712. }
  713. /**
  714. * testMessageRetrievalWithTemplate method
  715. *
  716. * @access public
  717. * @return void
  718. */
  719. function testMessageRetrievalWithTemplate() {
  720. App::build(array(
  721. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  722. ));
  723. $this->Controller->set('value', 22091985);
  724. $this->Controller->set('title_for_layout', 'EmailTest');
  725. $this->Controller->EmailTest->to = 'postmaster@localhost';
  726. $this->Controller->EmailTest->from = 'noreply@example.com';
  727. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  728. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  729. $this->Controller->EmailTest->layout = 'default';
  730. $this->Controller->EmailTest->template = 'custom';
  731. $this->Controller->EmailTest->delivery = 'debug';
  732. $text = <<<TEXTBLOC
  733. Here is your value: 22091985
  734. This email was sent using the CakePHP Framework, http://cakephp.org.
  735. TEXTBLOC;
  736. $html = <<<HTMLBLOC
  737. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  738. <html>
  739. <head>
  740. <title>EmailTest</title>
  741. </head>
  742. <body>
  743. <p>Here is your value: <b>22091985</b></p>
  744. <p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
  745. </body>
  746. </html>
  747. HTMLBLOC;
  748. $this->Controller->EmailTest->sendAs = 'both';
  749. $this->assertTrue($this->Controller->EmailTest->send());
  750. $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
  751. $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
  752. $this->Controller->EmailTest->sendAs = 'text';
  753. $this->assertTrue($this->Controller->EmailTest->send());
  754. $this->assertEqual($this->Controller->EmailTest->textMessage, $this->__osFix($text));
  755. $this->assertNull($this->Controller->EmailTest->htmlMessage);
  756. $this->Controller->EmailTest->sendAs = 'html';
  757. $this->assertTrue($this->Controller->EmailTest->send());
  758. $this->assertNull($this->Controller->EmailTest->textMessage);
  759. $this->assertEqual($this->Controller->EmailTest->htmlMessage, $this->__osFix($html));
  760. }
  761. /**
  762. * testContentArray method
  763. *
  764. * @access public
  765. * @return void
  766. */
  767. function testSendContentArray() {
  768. $this->Controller->EmailTest->to = 'postmaster@localhost';
  769. $this->Controller->EmailTest->from = 'noreply@example.com';
  770. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  771. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  772. $this->Controller->EmailTest->template = null;
  773. $this->Controller->EmailTest->delivery = 'debug';
  774. $content = array('First line', 'Second line', 'Third line');
  775. $this->assertTrue($this->Controller->EmailTest->send($content));
  776. $result = $this->Controller->Session->read('Message.email.message');
  777. $this->assertPattern('/To: postmaster@localhost\n/', $result);
  778. $this->assertPattern('/Subject: Cake Debug Test\n/', $result);
  779. $this->assertPattern('/Reply-To: noreply@example.com\n/', $result);
  780. $this->assertPattern('/From: noreply@example.com\n/', $result);
  781. $this->assertPattern('/X-Mailer: CakePHP Email Component\n/', $result);
  782. $this->assertPattern('/Content-Type: text\/plain; charset=UTF-8\n/', $result);
  783. $this->assertPattern('/Content-Transfer-Encoding: 7bitParameters:\n/', $result);
  784. $this->assertPattern('/First line\n/', $result);
  785. $this->assertPattern('/Second line\n/', $result);
  786. $this->assertPattern('/Third line\n/', $result);
  787. }
  788. /**
  789. * test setting a custom date.
  790. *
  791. * @return void
  792. */
  793. function testDateProperty() {
  794. $this->Controller->EmailTest->to = 'postmaster@localhost';
  795. $this->Controller->EmailTest->from = 'noreply@example.com';
  796. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  797. $this->Controller->EmailTest->date = 'Today!';
  798. $this->Controller->EmailTest->template = null;
  799. $this->Controller->EmailTest->delivery = 'debug';
  800. $this->assertTrue($this->Controller->EmailTest->send('test message'));
  801. $result = $this->Controller->Session->read('Message.email.message');
  802. $this->assertPattern('/Date: Today!\n/', $result);
  803. }
  804. /**
  805. * testContentStripping method
  806. *
  807. * @access public
  808. * @return void
  809. */
  810. function testContentStripping() {
  811. $content = "Previous content\n--alt-\nContent-TypeContent-Type:: text/html; charsetcharset==utf-8\nContent-Transfer-Encoding: 7bit";
  812. $content .= "\n\n<p>My own html content</p>";
  813. $result = $this->Controller->EmailTest->strip($content, true);
  814. $expected = "Previous content\n--alt-\n text/html; utf-8\n 7bit\n\n<p>My own html content</p>";
  815. $this->assertEqual($result, $expected);
  816. $content = '<p>Some HTML content with an <a href="mailto:test@example.com">email link</a>';
  817. $result = $this->Controller->EmailTest->strip($content, true);
  818. $expected = $content;
  819. $this->assertEqual($result, $expected);
  820. $content = '<p>Some HTML content with an ';
  821. $content .= '<a href="mailto:test@example.com,test2@example.com">email link</a>';
  822. $result = $this->Controller->EmailTest->strip($content, true);
  823. $expected = $content;
  824. $this->assertEqual($result, $expected);
  825. }
  826. /**
  827. * test that the _encode() will set mb_internal_encoding.
  828. *
  829. * @return void
  830. */
  831. function test_encodeSettingInternalCharset() {
  832. $skip = !function_exists('mb_internal_encoding');
  833. if ($this->skipIf($skip, 'Missing mb_* functions, cannot run test.')) {
  834. return;
  835. }
  836. mb_internal_encoding('ISO-8859-1');
  837. $this->Controller->charset = 'UTF-8';
  838. $this->Controller->EmailTest->to = 'postmaster@localhost';
  839. $this->Controller->EmailTest->from = 'noreply@example.com';
  840. $this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
  841. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  842. $this->Controller->EmailTest->template = null;
  843. $this->Controller->EmailTest->delivery = 'debug';
  844. $this->Controller->EmailTest->sendAs = 'text';
  845. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  846. $subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
  847. preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
  848. $this->assertEqual(trim($matches[1]), $subject);
  849. $result = mb_internal_encoding();
  850. $this->assertEqual($result, 'ISO-8859-1');
  851. }
  852. /**
  853. * testMultibyte method
  854. *
  855. * @access public
  856. * @return void
  857. */
  858. function testMultibyte() {
  859. $this->Controller->charset = 'UTF-8';
  860. $this->Controller->EmailTest->to = 'postmaster@localhost';
  861. $this->Controller->EmailTest->from = 'noreply@example.com';
  862. $this->Controller->EmailTest->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
  863. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  864. $this->Controller->EmailTest->template = null;
  865. $this->Controller->EmailTest->delivery = 'debug';
  866. $subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
  867. $this->Controller->EmailTest->sendAs = 'text';
  868. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  869. preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
  870. $this->assertEqual(trim($matches[1]), $subject);
  871. $this->Controller->EmailTest->sendAs = 'html';
  872. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  873. preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
  874. $this->assertEqual(trim($matches[1]), $subject);
  875. $this->Controller->EmailTest->sendAs = 'both';
  876. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  877. preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
  878. $this->assertEqual(trim($matches[1]), $subject);
  879. }
  880. /**
  881. * undocumented function
  882. *
  883. * @return void
  884. * @access public
  885. */
  886. function testSendWithAttachments() {
  887. $this->Controller->EmailTest->to = 'postmaster@localhost';
  888. $this->Controller->EmailTest->from = 'noreply@example.com';
  889. $this->Controller->EmailTest->subject = 'Attachment Test';
  890. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  891. $this->Controller->EmailTest->template = null;
  892. $this->Controller->EmailTest->delivery = 'debug';
  893. $this->Controller->EmailTest->attachments = array(
  894. __FILE__,
  895. 'some-name.php' => __FILE__
  896. );
  897. $body = '<p>This is the body of the message</p>';
  898. $this->Controller->EmailTest->sendAs = 'text';
  899. $this->assertTrue($this->Controller->EmailTest->send($body));
  900. $msg = $this->Controller->Session->read('Message.email.message');
  901. $this->assertPattern('/' . preg_quote('Content-Disposition: attachment; filename="email.test.php"') . '/', $msg);
  902. $this->assertPattern('/' . preg_quote('Content-Disposition: attachment; filename="some-name.php"') . '/', $msg);
  903. }
  904. /**
  905. * testSendAsIsNotIgnoredIfAttachmentsPresent method
  906. *
  907. * @return void
  908. * @access public
  909. */
  910. function testSendAsIsNotIgnoredIfAttachmentsPresent() {
  911. $this->Controller->EmailTest->to = 'postmaster@localhost';
  912. $this->Controller->EmailTest->from = 'noreply@example.com';
  913. $this->Controller->EmailTest->subject = 'Attachment Test';
  914. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  915. $this->Controller->EmailTest->template = null;
  916. $this->Controller->EmailTest->delivery = 'debug';
  917. $this->Controller->EmailTest->attachments = array(__FILE__);
  918. $body = '<p>This is the body of the message</p>';
  919. $this->Controller->EmailTest->sendAs = 'html';
  920. $this->assertTrue($this->Controller->EmailTest->send($body));
  921. $msg = $this->Controller->Session->read('Message.email.message');
  922. $this->assertNoPattern('/text\/plain/', $msg);
  923. $this->assertPattern('/text\/html/', $msg);
  924. $this->Controller->EmailTest->sendAs = 'text';
  925. $this->assertTrue($this->Controller->EmailTest->send($body));
  926. $msg = $this->Controller->Session->read('Message.email.message');
  927. $this->assertPattern('/text\/plain/', $msg);
  928. $this->assertNoPattern('/text\/html/', $msg);
  929. $this->Controller->EmailTest->sendAs = 'both';
  930. $this->assertTrue($this->Controller->EmailTest->send($body));
  931. $msg = $this->Controller->Session->read('Message.email.message');
  932. $this->assertNoPattern('/text\/plain/', $msg);
  933. $this->assertNoPattern('/text\/html/', $msg);
  934. $this->assertPattern('/multipart\/alternative/', $msg);
  935. }
  936. /**
  937. * testNoDoubleNewlinesInHeaders function
  938. *
  939. * @return void
  940. * @access public
  941. */
  942. function testNoDoubleNewlinesInHeaders() {
  943. $this->Controller->EmailTest->to = 'postmaster@localhost';
  944. $this->Controller->EmailTest->from = 'noreply@example.com';
  945. $this->Controller->EmailTest->subject = 'Attachment Test';
  946. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  947. $this->Controller->EmailTest->template = null;
  948. $this->Controller->EmailTest->delivery = 'debug';
  949. $body = '<p>This is the body of the message</p>';
  950. $this->Controller->EmailTest->sendAs = 'both';
  951. $this->assertTrue($this->Controller->EmailTest->send($body));
  952. $msg = $this->Controller->Session->read('Message.email.message');
  953. $this->assertNoPattern('/\n\nContent-Transfer-Encoding/', $msg);
  954. $this->assertPattern('/\nContent-Transfer-Encoding/', $msg);
  955. }
  956. /**
  957. * testReset method
  958. *
  959. * @access public
  960. * @return void
  961. */
  962. function testReset() {
  963. $this->Controller->EmailTest->template = 'test_template';
  964. $this->Controller->EmailTest->to = 'test.recipient@example.com';
  965. $this->Controller->EmailTest->from = 'test.sender@example.com';
  966. $this->Controller->EmailTest->replyTo = 'test.replyto@example.com';
  967. $this->Controller->EmailTest->return = 'test.return@example.com';
  968. $this->Controller->EmailTest->cc = array('cc1@example.com', 'cc2@example.com');
  969. $this->Controller->EmailTest->bcc = array('bcc1@example.com', 'bcc2@example.com');
  970. $this->Controller->EmailTest->date = 'Today!';
  971. $this->Controller->EmailTest->subject = 'Test subject';
  972. $this->Controller->EmailTest->additionalParams = 'X-additional-header';
  973. $this->Controller->EmailTest->delivery = 'smtp';
  974. $this->Controller->EmailTest->smtpOptions['host'] = 'blah';
  975. $this->Controller->EmailTest->smtpOptions['timeout'] = 0.5;
  976. $this->Controller->EmailTest->attachments = array('attachment1', 'attachment2');
  977. $this->Controller->EmailTest->textMessage = 'This is the body of the message';
  978. $this->Controller->EmailTest->htmlMessage = 'This is the body of the message';
  979. $this->Controller->EmailTest->messageId = false;
  980. $this->assertFalse($this->Controller->EmailTest->send('Should not work'));
  981. $this->Controller->EmailTest->reset();
  982. $this->assertNull($this->Controller->EmailTest->template);
  983. $this->assertIdentical($this->Controller->EmailTest->to, array());
  984. $this->assertNull($this->Controller->EmailTest->from);
  985. $this->assertNull($this->Controller->EmailTest->replyTo);
  986. $this->assertNull($this->Controller->EmailTest->return);
  987. $this->assertIdentical($this->Controller->EmailTest->cc, array());
  988. $this->assertIdentical($this->Controller->EmailTest->bcc, array());
  989. $this->assertNull($this->Controller->EmailTest->date);
  990. $this->assertNull($this->Controller->EmailTest->subject);
  991. $this->assertNull($this->Controller->EmailTest->additionalParams);
  992. $this->assertIdentical($this->Controller->EmailTest->getHeaders(), array());
  993. $this->assertNull($this->Controller->EmailTest->getBoundary());
  994. $this->assertIdentical($this->Controller->EmailTest->getMessage(), array());
  995. $this->assertNull($this->Controller->EmailTest->smtpError);
  996. $this->assertIdentical($this->Controller->EmailTest->attachments, array());
  997. $this->assertNull($this->Controller->EmailTest->textMessage);
  998. $this->assertTrue($this->Controller->EmailTest->messageId);
  999. }
  1000. function testPluginCustomViewClass() {
  1001. App::build(array(
  1002. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
  1003. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  1004. ));
  1005. $this->Controller->view = 'TestPlugin.Email';
  1006. $this->Controller->EmailTest->to = 'postmaster@localhost';
  1007. $this->Controller->EmailTest->from = 'noreply@example.com';
  1008. $this->Controller->EmailTest->subject = 'CustomViewClass test';
  1009. $this->Controller->EmailTest->delivery = 'debug';
  1010. $body = 'Body of message';
  1011. $this->assertTrue($this->Controller->EmailTest->send($body));
  1012. $result = $this->Controller->Session->read('Message.email.message');
  1013. $this->assertPattern('/Body of message/', $result);
  1014. }
  1015. /**
  1016. * testStartup method
  1017. *
  1018. * @access public
  1019. * @return void
  1020. */
  1021. function testStartup() {
  1022. $this->assertNull($this->Controller->EmailTest->startup($this->Controller));
  1023. }
  1024. /**
  1025. * testMessageId method
  1026. *
  1027. * @access public
  1028. * @return void
  1029. */
  1030. function testMessageId() {
  1031. $this->Controller->EmailTest->to = 'postmaster@localhost';
  1032. $this->Controller->EmailTest->from = 'noreply@example.com';
  1033. $this->Controller->EmailTest->subject = 'Cake Debug Test';
  1034. $this->Controller->EmailTest->replyTo = 'noreply@example.com';
  1035. $this->Controller->EmailTest->template = null;
  1036. $this->Controller->EmailTest->delivery = 'debug';
  1037. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  1038. $result = $this->Controller->Session->read('Message.email.message');
  1039. $this->assertPattern('/Message-ID: \<[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}@' . env('HTTP_HOST') . '\>\n/', $result);
  1040. $this->Controller->EmailTest->messageId = '<22091985.998877@localhost>';
  1041. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  1042. $result = $this->Controller->Session->read('Message.email.message');
  1043. $this->assertPattern('/Message-ID: <22091985.998877@localhost>\n/', $result);
  1044. $this->Controller->EmailTest->messageId = false;
  1045. $this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
  1046. $result = $this->Controller->Session->read('Message.email.message');
  1047. $this->assertNoPattern('/Message-ID:/', $result);
  1048. }
  1049. /**
  1050. * testSendMessage method
  1051. *
  1052. * @access public
  1053. * @return void
  1054. */
  1055. function testSendMessage() {
  1056. $this->Controller->EmailTest->delivery = 'getMessage';
  1057. $this->Controller->EmailTest->lineLength = 70;
  1058. $text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
  1059. $this->Controller->EmailTest->sendAs = 'text';
  1060. $result = $this->Controller->EmailTest->send($text);
  1061. $expected = array(
  1062. 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do',
  1063. 'eiusmod tempor incididunt ut labore et dolore magna aliqua.',
  1064. '',
  1065. ''
  1066. );
  1067. $this->assertEqual($expected, $result);
  1068. $text = 'Lorem ipsum dolor sit amet, <b>consectetur</b> adipisicing elit, sed do <span>eiusmod tempor</span> incididunt ut labore et dolore magna aliqua.';
  1069. $this->Controller->EmailTest->sendAs = 'html';
  1070. $result = $this->Controller->EmailTest->send($text);
  1071. $expected = array(
  1072. $text,
  1073. '',
  1074. ''
  1075. );
  1076. $this->assertEqual($expected, $result);
  1077. }
  1078. /**
  1079. * Test that _formatName doesn't jack up email addresses with alias parts.
  1080. *
  1081. * @return void
  1082. */
  1083. function testFormatAddressAliases() {
  1084. $result = $this->Controller->EmailTest->formatAddress('email@example.com');
  1085. $this->assertEqual($result, 'email@example.com');
  1086. $result = $this->Controller->EmailTest->formatAddress('alias <email@example.com>');
  1087. $this->assertEqual($result, 'alias <email@example.com>');
  1088. $result = $this->Controller->EmailTest->formatAddress('alias<email@example.com>');
  1089. $this->assertEqual($result, 'alias <email@example.com>');
  1090. $result = $this->Controller->EmailTest->formatAddress('email@example.com');
  1091. $this->assertEqual($result, 'email@example.com');
  1092. $result = $this->Controller->EmailTest->formatAddress('<email@example.com>');
  1093. $this->assertEqual($result, '<email@example.com>');
  1094. $result = $this->Controller->EmailTest->formatAddress('email@example.com', true);
  1095. $this->assertEqual($result, '<email@example.com>');
  1096. $result = $this->Controller->EmailTest->formatAddress('<email@example.com>', true);
  1097. $this->assertEqual($result, '<email@example.com>');
  1098. $result = $this->Controller->EmailTest->formatAddress('alias name <email@example.com>', true);
  1099. $this->assertEqual($result, '<email@example.com>');
  1100. }
  1101. /**
  1102. * test formatting addresses with multibyte chars
  1103. *
  1104. * @return void
  1105. */
  1106. function testFormatAddressMultibyte() {
  1107. $this->Controller->EmailTest->charset = 'UTF-8';
  1108. $result = $this->Controller->EmailTest->formatAddress('ÄÖÜTest <email@domain.de>');
  1109. $this->assertEqual($result, '=?UTF-8?B?w4TDlsOcVGVzdCA=?= <email@domain.de>');
  1110. $result = $this->Controller->EmailTest->formatAddress('ÄÖÜTest<email@domain.de>');
  1111. $this->assertEqual($result, '=?UTF-8?B?w4TDlsOcVGVzdA==?= <email@domain.de>');
  1112. $result = $this->Controller->EmailTest->formatAddress('ÄÖÜTest <email@domain.de>', true);
  1113. $this->assertEqual($result, '<email@domain.de>');
  1114. }
  1115. }