PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/hardsshah/bookmarks
PHP | 427 lines | 237 code | 55 blank | 135 comment | 3 complexity | eba310cd515129b9cab20fa2769473ec MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * EmailComponentTest file
  5. *
  6. * Series of tests for email component.
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.cake.tests.cases.libs.controller.components
  21. * @since CakePHP(tm) v 1.2.0.5347
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. Configure::write('App.encoding', 'UTF-8');
  28. App::import('Component', 'Email');
  29. /**
  30. * EmailTestController class
  31. *
  32. * @package cake
  33. * @subpackage cake.tests.cases.libs.controller.components
  34. */
  35. class EmailTestController extends Controller {
  36. /**
  37. * name property
  38. *
  39. * @var string 'EmailTest'
  40. * @access public
  41. */
  42. var $name = 'EmailTest';
  43. /**
  44. * uses property
  45. *
  46. * @var mixed null
  47. * @access public
  48. */
  49. var $uses = null;
  50. /**
  51. * components property
  52. *
  53. * @var array
  54. * @access public
  55. */
  56. var $components = array('Email');
  57. /**
  58. * pageTitle property
  59. *
  60. * @var string
  61. * @access public
  62. */
  63. var $pageTitle = 'EmailTest';
  64. }
  65. /**
  66. * EmailTest class
  67. *
  68. * @package cake
  69. * @subpackage cake.tests.cases.libs.controller.components
  70. */
  71. class EmailComponentTest extends CakeTestCase {
  72. /**
  73. * Controller property
  74. *
  75. * @var EmailTestController
  76. * @access public
  77. */
  78. var $Controller;
  79. /**
  80. * name property
  81. *
  82. * @var string 'Email'
  83. * @access public
  84. */
  85. var $name = 'Email';
  86. /**
  87. * setUp method
  88. *
  89. * @access public
  90. * @return void
  91. */
  92. function setUp() {
  93. $this->Controller =& new EmailTestController();
  94. restore_error_handler();
  95. @$this->Controller->Component->init($this->Controller);
  96. set_error_handler('simpleTestErrorHandler');
  97. $this->Controller->Email->initialize($this->Controller, array());
  98. ClassRegistry::addObject('view', new View($this->Controller));
  99. Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS));
  100. }
  101. /**
  102. * testBadSmtpSend method
  103. *
  104. * @access public
  105. * @return void
  106. */
  107. function testBadSmtpSend() {
  108. $this->Controller->Email->smtpOptions['host'] = 'blah';
  109. $this->Controller->Email->delivery = 'smtp';
  110. $this->assertFalse($this->Controller->Email->send('Should not work'));
  111. }
  112. /**
  113. * testSmtpSend method
  114. *
  115. * @access public
  116. * @return void
  117. */
  118. function testSmtpSend() {
  119. if (!$this->skipIf(!@fsockopen('localhost', 25), 'No SMTP server running on localhost')) {
  120. return;
  121. }
  122. $this->Controller->Email->reset();
  123. $this->Controller->Email->to = 'postmaster@localhost';
  124. $this->Controller->Email->from = 'noreply@example.com';
  125. $this->Controller->Email->subject = 'Cake SMTP test';
  126. $this->Controller->Email->replyTo = 'noreply@example.com';
  127. $this->Controller->Email->template = null;
  128. $this->Controller->Email->delivery = 'smtp';
  129. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  130. $this->Controller->Email->_debug = true;
  131. $this->Controller->Email->sendAs = 'text';
  132. $expect = <<<TEMPDOC
  133. <pre>Host: localhost
  134. Port: 25
  135. Timeout: 30
  136. To: postmaster@localhost
  137. From: noreply@example.com
  138. Subject: Cake SMTP test
  139. Header:
  140. To: postmaster@localhost
  141. From: noreply@example.com
  142. Reply-To: noreply@example.com
  143. Subject: Cake SMTP test
  144. X-Mailer: CakePHP Email Component
  145. Content-Type: text/plain; charset=UTF-8
  146. Content-Transfer-Encoding: 7bitParameters:
  147. Message:
  148. This is the body of the message
  149. </pre>
  150. TEMPDOC;
  151. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  152. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  153. }
  154. /**
  155. * testAuthenticatedSmtpSend method
  156. *
  157. * @access public
  158. * @return void
  159. */
  160. function testAuthenticatedSmtpSend() {
  161. $this->skipIf(!@fsockopen('localhost', 25), 'No SMTP server running on localhost');
  162. $this->Controller->Email->reset();
  163. $this->Controller->Email->to = 'postmaster@localhost';
  164. $this->Controller->Email->from = 'noreply@example.com';
  165. $this->Controller->Email->subject = 'Cake SMTP test';
  166. $this->Controller->Email->replyTo = 'noreply@example.com';
  167. $this->Controller->Email->template = null;
  168. $this->Controller->Email->smtpOptions['username'] = 'test';
  169. $this->Controller->Email->smtpOptions['password'] = 'testing';
  170. $this->Controller->Email->delivery = 'smtp';
  171. $result = $this->Controller->Email->send('This is the body of the message');
  172. $code = substr($this->Controller->Email->smtpError, 0, 3);
  173. $this->skipIf(!$code, 'Authentication not enabled on server');
  174. $this->assertTrue(!$result && $code == '535');
  175. }
  176. /**
  177. * testSendFormats method
  178. *
  179. * @access public
  180. * @return void
  181. */
  182. function testSendFormats() {
  183. $this->Controller->Email->reset();
  184. $this->Controller->Email->to = 'postmaster@localhost';
  185. $this->Controller->Email->from = 'noreply@example.com';
  186. $this->Controller->Email->subject = 'Cake SMTP test';
  187. $this->Controller->Email->replyTo = 'noreply@example.com';
  188. $this->Controller->Email->template = null;
  189. $this->Controller->Email->delivery = 'debug';
  190. $message = <<<MSGBLOC
  191. <pre>To: postmaster@localhost
  192. From: noreply@example.com
  193. Subject: Cake SMTP test
  194. Header:
  195. From: noreply@example.com
  196. Reply-To: noreply@example.com
  197. X-Mailer: CakePHP Email Component
  198. Content-Type: {CONTENTTYPE}
  199. Content-Transfer-Encoding: 7bitParameters:
  200. Message:
  201. This is the body of the message
  202. </pre>
  203. MSGBLOC;
  204. $this->Controller->Email->sendAs = 'text';
  205. $expect = str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $message);
  206. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  207. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  208. $this->Controller->Email->sendAs = 'html';
  209. $expect = str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $message);
  210. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  211. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  212. // TODO: better test for format of message sent?
  213. $this->Controller->Email->sendAs = 'both';
  214. $expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"' . "\n", $message);
  215. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  216. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  217. }
  218. /**
  219. * testTemplates method
  220. *
  221. * @access public
  222. * @return void
  223. */
  224. function testTemplates() {
  225. $this->Controller->Email->reset();
  226. $this->Controller->Email->to = 'postmaster@localhost';
  227. $this->Controller->Email->from = 'noreply@example.com';
  228. $this->Controller->Email->subject = 'Cake SMTP test';
  229. $this->Controller->Email->replyTo = 'noreply@example.com';
  230. $this->Controller->Email->delivery = 'debug';
  231. $header = <<<HEADBLOC
  232. To: postmaster@localhost
  233. From: noreply@example.com
  234. Subject: Cake SMTP test
  235. Header:
  236. From: noreply@example.com
  237. Reply-To: noreply@example.com
  238. X-Mailer: CakePHP Email Component
  239. Content-Type: {CONTENTTYPE}
  240. Content-Transfer-Encoding: 7bitParameters:
  241. Message:
  242. HEADBLOC;
  243. $this->Controller->Email->layout = 'default';
  244. $this->Controller->Email->template = 'default';
  245. $text = <<<TEXTBLOC
  246. This is the body of the message
  247. This email was sent using the CakePHP Framework, http://cakephp.org.
  248. TEXTBLOC;
  249. $html = <<<HTMLBLOC
  250. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  251. <html>
  252. <head>
  253. <title>EmailTest</title>
  254. </head>
  255. <body>
  256. <p> This is the body of the message</p><p> </p>
  257. <p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>
  258. </body>
  259. </html>
  260. HTMLBLOC;
  261. $this->Controller->Email->sendAs = 'text';
  262. $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $header) . $text . "\n" . '</pre>';
  263. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  264. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  265. $this->Controller->Email->sendAs = 'html';
  266. $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
  267. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  268. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  269. $this->Controller->Email->sendAs = 'both';
  270. $expect = str_replace('{CONTENTTYPE}', 'multipart/alternative; boundary="alt-"' . "\n", $header);
  271. $expect .= '--alt-' . "\n" . 'Content-Type: text/plain; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $text . "\n\n";
  272. $expect .= '--alt-' . "\n" . 'Content-Type: text/html; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: 7bit' . "\n\n" . $html . "\n\n";
  273. $expect = '<pre>' . $expect . '--alt---' . "\n\n" . '</pre>';
  274. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  275. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  276. $html = <<<HTMLBLOC
  277. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  278. <html>
  279. <head>
  280. <title>EmailTest</title>
  281. </head>
  282. <body>
  283. <p> This is the body of the message</p><p> </p>
  284. <p>This email was sent using the CakePHP Framework</p>
  285. </body>
  286. </html>
  287. HTMLBLOC;
  288. $this->Controller->Email->sendAs = 'html';
  289. $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/html; charset=UTF-8', $header) . $html . "\n" . '</pre>';
  290. $this->assertTrue($this->Controller->Email->send('This is the body of the message', 'default', 'thin'));
  291. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  292. return;
  293. $text = <<<TEXTBLOC
  294. This element has some text that is just too wide to comply with email
  295. standards.
  296. This is the body of the message
  297. This email was sent using the CakePHP Framework, http://cakephp.org.
  298. TEXTBLOC;
  299. $this->Controller->Email->sendAs = 'text';
  300. $expect = '<pre>' . str_replace('{CONTENTTYPE}', 'text/plain; charset=UTF-8', $header) . $text . "\n" . '</pre>';
  301. $this->assertTrue($this->Controller->Email->send('This is the body of the message', 'wide', 'default'));
  302. $this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  303. }
  304. /**
  305. * testSendDebug method
  306. *
  307. * @access public
  308. * @return void
  309. */
  310. function testSendDebug() {
  311. $this->Controller->Email->reset();
  312. $this->Controller->Email->to = 'postmaster@localhost';
  313. $this->Controller->Email->from = 'noreply@example.com';
  314. $this->Controller->Email->subject = 'Cake SMTP test';
  315. $this->Controller->Email->replyTo = 'noreply@example.com';
  316. $this->Controller->Email->template = null;
  317. $this->Controller->Email->delivery = 'debug';
  318. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  319. }
  320. /**
  321. * testContentStripping method
  322. *
  323. * @access public
  324. * @return void
  325. */
  326. function testContentStripping() {
  327. $content = "Previous content\n--alt-\nContent-TypeContent-Type:: text/html; charsetcharset==utf-8\nContent-Transfer-Encoding: 7bit";
  328. $content .= "\n\n<p>My own html content</p>";
  329. $result = $this->Controller->Email->__strip($content, true);
  330. $expected = "Previous content\n--alt-\n text/html; utf-8\n 7bit\n\n<p>My own html content</p>";
  331. $this->assertEqual($result, $expected);
  332. }
  333. /**
  334. * testMultibyte method
  335. *
  336. * @access public
  337. * @return void
  338. */
  339. function testMultibyte() {
  340. $this->Controller->Email->reset();
  341. $this->Controller->Email->to = 'postmaster@localhost';
  342. $this->Controller->Email->from = 'noreply@example.com';
  343. $this->Controller->Email->subject = 'هذه رسالة بعنوان طويل مرسل للمستلم';
  344. $this->Controller->Email->replyTo = 'noreply@example.com';
  345. $this->Controller->Email->template = null;
  346. $this->Controller->Email->delivery = 'debug';
  347. $subject = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
  348. $this->Controller->Email->sendAs = 'text';
  349. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  350. preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
  351. $this->assertEqual(trim($matches[1]), $subject);
  352. $this->Controller->Email->sendAs = 'html';
  353. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  354. preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
  355. $this->assertEqual(trim($matches[1]), $subject);
  356. $this->Controller->Email->sendAs = 'both';
  357. $this->assertTrue($this->Controller->Email->send('This is the body of the message'));
  358. preg_match('/Subject: (.*)Header:/s', $this->Controller->Session->read('Message.email.message'), $matches);
  359. $this->assertEqual(trim($matches[1]), $subject);
  360. }
  361. /**
  362. * osFix method
  363. *
  364. * @param string $string
  365. * @access private
  366. * @return string
  367. */
  368. function __osFix($string) {
  369. return str_replace(array("\r\n", "\r"), "\n", $string);
  370. }
  371. }
  372. ?>