PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Network/Email/CakeEmailTest.php

https://bitbucket.org/ttalov/fgcu_pci
PHP | 1797 lines | 1153 code | 245 blank | 399 comment | 16 complexity | cde71d60ba21e5610895db22bee2aeb5 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * CakeEmailTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Network.Email
  16. * @since CakePHP(tm) v 2.0.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeEmail', 'Network/Email');
  20. /**
  21. * Help to test CakeEmail
  22. *
  23. */
  24. class TestCakeEmail extends CakeEmail {
  25. /**
  26. * Config
  27. *
  28. */
  29. protected $_config = array();
  30. /**
  31. * Wrap to protected method
  32. *
  33. */
  34. public function formatAddress($address) {
  35. return parent::_formatAddress($address);
  36. }
  37. /**
  38. * Wrap to protected method
  39. *
  40. */
  41. public function wrap($text) {
  42. return parent::_wrap($text);
  43. }
  44. /**
  45. * Get the boundary attribute
  46. *
  47. * @return string
  48. */
  49. public function getBoundary() {
  50. return $this->_boundary;
  51. }
  52. /**
  53. * Encode to protected method
  54. *
  55. */
  56. public function encode($text) {
  57. return $this->_encode($text);
  58. }
  59. }
  60. /*
  61. * EmailConfig class
  62. *
  63. */
  64. class EmailConfig {
  65. /**
  66. * test config
  67. *
  68. * @var string
  69. */
  70. public $test = array(
  71. 'from' => array('some@example.com' => 'My website'),
  72. 'to' => array('test@example.com' => 'Testname'),
  73. 'subject' => 'Test mail subject',
  74. 'transport' => 'Debug',
  75. 'theme' => 'TestTheme',
  76. 'helpers' => array('Html', 'Form'),
  77. );
  78. }
  79. /*
  80. * ExtendTransport class
  81. * test class to ensure the class has send() method
  82. *
  83. */
  84. class ExtendTransport {
  85. }
  86. /**
  87. * CakeEmailTest class
  88. *
  89. * @package Cake.Test.Case.Network.Email
  90. */
  91. class CakeEmailTest extends CakeTestCase {
  92. /**
  93. * setUp
  94. *
  95. * @return void
  96. */
  97. public function setUp() {
  98. parent::setUp();
  99. $this->CakeEmail = new TestCakeEmail();
  100. App::build(array(
  101. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  102. ));
  103. }
  104. /**
  105. * tearDown method
  106. *
  107. * @return void
  108. */
  109. public function tearDown() {
  110. parent::tearDown();
  111. App::build();
  112. }
  113. /**
  114. * testFrom method
  115. *
  116. * @return void
  117. */
  118. public function testFrom() {
  119. $this->assertSame($this->CakeEmail->from(), array());
  120. $this->CakeEmail->from('cake@cakephp.org');
  121. $expected = array('cake@cakephp.org' => 'cake@cakephp.org');
  122. $this->assertSame($this->CakeEmail->from(), $expected);
  123. $this->CakeEmail->from(array('cake@cakephp.org'));
  124. $this->assertSame($this->CakeEmail->from(), $expected);
  125. $this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
  126. $expected = array('cake@cakephp.org' => 'CakePHP');
  127. $this->assertSame($this->CakeEmail->from(), $expected);
  128. $result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP'));
  129. $this->assertSame($this->CakeEmail->from(), $expected);
  130. $this->assertSame($this->CakeEmail, $result);
  131. $this->setExpectedException('SocketException');
  132. $result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address'));
  133. }
  134. /**
  135. * testSender method
  136. *
  137. * @return void
  138. */
  139. public function testSender() {
  140. $this->CakeEmail->reset();
  141. $this->assertSame($this->CakeEmail->sender(), array());
  142. $this->CakeEmail->sender('cake@cakephp.org', 'Name');
  143. $expected = array('cake@cakephp.org' => 'Name');
  144. $this->assertSame($this->CakeEmail->sender(), $expected);
  145. $headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
  146. $this->assertSame($headers['From'], false);
  147. $this->assertSame($headers['Sender'], 'Name <cake@cakephp.org>');
  148. $this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
  149. $headers = $this->CakeEmail->getHeaders(array('from' => true, 'sender' => true));
  150. $this->assertSame($headers['From'], 'CakePHP <cake@cakephp.org>');
  151. $this->assertSame($headers['Sender'], '');
  152. }
  153. /**
  154. * testTo method
  155. *
  156. * @return void
  157. */
  158. public function testTo() {
  159. $this->assertSame($this->CakeEmail->to(), array());
  160. $result = $this->CakeEmail->to('cake@cakephp.org');
  161. $expected = array('cake@cakephp.org' => 'cake@cakephp.org');
  162. $this->assertSame($this->CakeEmail->to(), $expected);
  163. $this->assertSame($this->CakeEmail, $result);
  164. $this->CakeEmail->to('cake@cakephp.org', 'CakePHP');
  165. $expected = array('cake@cakephp.org' => 'CakePHP');
  166. $this->assertSame($this->CakeEmail->to(), $expected);
  167. $list = array(
  168. 'cake@cakephp.org' => 'Cake PHP',
  169. 'cake-php@googlegroups.com' => 'Cake Groups',
  170. 'root@cakephp.org'
  171. );
  172. $this->CakeEmail->to($list);
  173. $expected = array(
  174. 'cake@cakephp.org' => 'Cake PHP',
  175. 'cake-php@googlegroups.com' => 'Cake Groups',
  176. 'root@cakephp.org' => 'root@cakephp.org'
  177. );
  178. $this->assertSame($this->CakeEmail->to(), $expected);
  179. $this->CakeEmail->addTo('jrbasso@cakephp.org');
  180. $this->CakeEmail->addTo('mark_story@cakephp.org', 'Mark Story');
  181. $result = $this->CakeEmail->addTo(array('phpnut@cakephp.org' => 'PhpNut', 'jose_zap@cakephp.org'));
  182. $expected = array(
  183. 'cake@cakephp.org' => 'Cake PHP',
  184. 'cake-php@googlegroups.com' => 'Cake Groups',
  185. 'root@cakephp.org' => 'root@cakephp.org',
  186. 'jrbasso@cakephp.org' => 'jrbasso@cakephp.org',
  187. 'mark_story@cakephp.org' => 'Mark Story',
  188. 'phpnut@cakephp.org' => 'PhpNut',
  189. 'jose_zap@cakephp.org' => 'jose_zap@cakephp.org'
  190. );
  191. $this->assertSame($this->CakeEmail->to(), $expected);
  192. $this->assertSame($this->CakeEmail, $result);
  193. }
  194. /**
  195. * Data provider function for testBuildInvalidData
  196. *
  197. * @return array
  198. */
  199. public static function invalidEmails() {
  200. return array(
  201. array(1.0),
  202. array(''),
  203. array('string'),
  204. array('<tag>'),
  205. array('some@one.whereis'),
  206. array('wrong@key' => 'Name'),
  207. array(array('ok@cakephp.org', 1.0, '', 'string'))
  208. );
  209. }
  210. /**
  211. * testBuildInvalidData
  212. *
  213. * @dataProvider invalidEmails
  214. * @expectedException SocketException
  215. * @return void
  216. */
  217. public function testInvalidEmail($value) {
  218. $this->CakeEmail->to($value);
  219. }
  220. /**
  221. * testBuildInvalidData
  222. *
  223. * @dataProvider invalidEmails
  224. * @expectedException SocketException
  225. * @return void
  226. */
  227. public function testInvalidEmailAdd($value) {
  228. $this->CakeEmail->addTo($value);
  229. }
  230. /**
  231. * testFormatAddress method
  232. *
  233. * @return void
  234. */
  235. public function testFormatAddress() {
  236. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org'));
  237. $expected = array('cake@cakephp.org');
  238. $this->assertSame($expected, $result);
  239. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org', 'php@cakephp.org' => 'php@cakephp.org'));
  240. $expected = array('cake@cakephp.org', 'php@cakephp.org');
  241. $this->assertSame($expected, $result);
  242. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'CakePHP', 'php@cakephp.org' => 'Cake'));
  243. $expected = array('CakePHP <cake@cakephp.org>', 'Cake <php@cakephp.org>');
  244. $this->assertSame($expected, $result);
  245. $result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last, First'));
  246. $expected = array('"Last, First" <me@example.com>');
  247. $this->assertSame($expected, $result);
  248. $result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last First'));
  249. $expected = array('Last First <me@example.com>');
  250. $this->assertSame($expected, $result);
  251. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'ÄÖÜTest'));
  252. $expected = array('=?UTF-8?B?w4TDlsOcVGVzdA==?= <cake@cakephp.org>');
  253. $this->assertSame($expected, $result);
  254. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '日本語Test'));
  255. $expected = array('=?UTF-8?B?5pel5pys6KqeVGVzdA==?= <cake@cakephp.org>');
  256. $this->assertSame($expected, $result);
  257. }
  258. /**
  259. * testFormatAddressJapanese
  260. *
  261. * @return void
  262. */
  263. public function testFormatAddressJapanese() {
  264. $this->skipIf(!function_exists('mb_convert_encoding'));
  265. $this->CakeEmail->headerCharset = 'ISO-2022-JP';
  266. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '日本語Test'));
  267. $expected = array('=?ISO-2022-JP?B?GyRCRnxLXDhsGyhCVGVzdA==?= <cake@cakephp.org>');
  268. $this->assertSame($expected, $result);
  269. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '寿限無寿限無五劫の擦り切れ海砂利水魚の水行末雲来末風来末食う寝る処に住む処やぶら小路の藪柑子パイポパイポパイポのシューリンガンシューリンガンのグーリンダイグーリンダイのポンポコピーのポンポコナーの長久命の長助'));
  270. $expected = array("=?ISO-2022-JP?B?GyRCPHc4Qkw1PHc4Qkw1OF45ZSROOyQkakBaJGwzJDo9TXg/ZTV7GyhC?=\r\n" .
  271. " =?ISO-2022-JP?B?GyRCJE4/ZTlUS3YxQE1oS3ZJd01oS3Y/KSQmPzIkaz1oJEs9OyRgGyhC?=\r\n" .
  272. " =?ISO-2022-JP?B?GyRCPWgkZCRWJGk+Lk8pJE5pLjQ7O1IlUSUkJV0lUSUkJV0lUSUkGyhC?=\r\n" .
  273. " =?ISO-2022-JP?B?GyRCJV0kTiU3JWUhPCVqJXMlLCVzJTclZSE8JWolcyUsJXMkTiUwGyhC?=\r\n" .
  274. " =?ISO-2022-JP?B?GyRCITwlaiVzJUAlJCUwITwlaiVzJUAlJCROJV0lcyVdJTMlVCE8GyhC?=\r\n" .
  275. " =?ISO-2022-JP?B?GyRCJE4lXSVzJV0lMyVKITwkTkQ5NVdMPyRORDk9dRsoQg==?= <cake@cakephp.org>");
  276. $this->assertSame($expected, $result);
  277. }
  278. /**
  279. * testAddresses method
  280. *
  281. * @return void
  282. */
  283. public function testAddresses() {
  284. $this->CakeEmail->reset();
  285. $this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
  286. $this->CakeEmail->replyTo('replyto@cakephp.org', 'ReplyTo CakePHP');
  287. $this->CakeEmail->readReceipt('readreceipt@cakephp.org', 'ReadReceipt CakePHP');
  288. $this->CakeEmail->returnPath('returnpath@cakephp.org', 'ReturnPath CakePHP');
  289. $this->CakeEmail->to('to@cakephp.org', 'To CakePHP');
  290. $this->CakeEmail->cc('cc@cakephp.org', 'Cc CakePHP');
  291. $this->CakeEmail->bcc('bcc@cakephp.org', 'Bcc CakePHP');
  292. $this->CakeEmail->addTo('to2@cakephp.org', 'To2 CakePHP');
  293. $this->CakeEmail->addCc('cc2@cakephp.org', 'Cc2 CakePHP');
  294. $this->CakeEmail->addBcc('bcc2@cakephp.org', 'Bcc2 CakePHP');
  295. $this->assertSame($this->CakeEmail->from(), array('cake@cakephp.org' => 'CakePHP'));
  296. $this->assertSame($this->CakeEmail->replyTo(), array('replyto@cakephp.org' => 'ReplyTo CakePHP'));
  297. $this->assertSame($this->CakeEmail->readReceipt(), array('readreceipt@cakephp.org' => 'ReadReceipt CakePHP'));
  298. $this->assertSame($this->CakeEmail->returnPath(), array('returnpath@cakephp.org' => 'ReturnPath CakePHP'));
  299. $this->assertSame($this->CakeEmail->to(), array('to@cakephp.org' => 'To CakePHP', 'to2@cakephp.org' => 'To2 CakePHP'));
  300. $this->assertSame($this->CakeEmail->cc(), array('cc@cakephp.org' => 'Cc CakePHP', 'cc2@cakephp.org' => 'Cc2 CakePHP'));
  301. $this->assertSame($this->CakeEmail->bcc(), array('bcc@cakephp.org' => 'Bcc CakePHP', 'bcc2@cakephp.org' => 'Bcc2 CakePHP'));
  302. $headers = $this->CakeEmail->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true));
  303. $this->assertSame($headers['From'], 'CakePHP <cake@cakephp.org>');
  304. $this->assertSame($headers['Reply-To'], 'ReplyTo CakePHP <replyto@cakephp.org>');
  305. $this->assertSame($headers['Disposition-Notification-To'], 'ReadReceipt CakePHP <readreceipt@cakephp.org>');
  306. $this->assertSame($headers['Return-Path'], 'ReturnPath CakePHP <returnpath@cakephp.org>');
  307. $this->assertSame($headers['To'], 'To CakePHP <to@cakephp.org>, To2 CakePHP <to2@cakephp.org>');
  308. $this->assertSame($headers['Cc'], 'Cc CakePHP <cc@cakephp.org>, Cc2 CakePHP <cc2@cakephp.org>');
  309. $this->assertSame($headers['Bcc'], 'Bcc CakePHP <bcc@cakephp.org>, Bcc2 CakePHP <bcc2@cakephp.org>');
  310. }
  311. /**
  312. * testMessageId method
  313. *
  314. * @return void
  315. */
  316. public function testMessageId() {
  317. $this->CakeEmail->messageId(true);
  318. $result = $this->CakeEmail->getHeaders();
  319. $this->assertTrue(isset($result['Message-ID']));
  320. $this->CakeEmail->messageId(false);
  321. $result = $this->CakeEmail->getHeaders();
  322. $this->assertFalse(isset($result['Message-ID']));
  323. $result = $this->CakeEmail->messageId('<my-email@localhost>');
  324. $this->assertSame($this->CakeEmail, $result);
  325. $result = $this->CakeEmail->getHeaders();
  326. $this->assertSame($result['Message-ID'], '<my-email@localhost>');
  327. $result = $this->CakeEmail->messageId();
  328. $this->assertSame($result, '<my-email@localhost>');
  329. }
  330. /**
  331. * testMessageIdInvalid method
  332. *
  333. * @return void
  334. * @expectedException SocketException
  335. */
  336. public function testMessageIdInvalid() {
  337. $this->CakeEmail->messageId('my-email@localhost');
  338. }
  339. /**
  340. * testDomain method
  341. *
  342. * @return void
  343. */
  344. public function testDomain() {
  345. $result = $this->CakeEmail->domain();
  346. $expected = env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n');
  347. $this->assertSame($expected, $result);
  348. $this->CakeEmail->domain('example.org');
  349. $result = $this->CakeEmail->domain();
  350. $expected = 'example.org';
  351. $this->assertSame($expected, $result);
  352. }
  353. /**
  354. * testMessageIdWithDomain method
  355. *
  356. * @return void
  357. */
  358. public function testMessageIdWithDomain() {
  359. $result = $this->CakeEmail->getHeaders();
  360. $expected = '@' . (env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n')) . '>';
  361. $this->assertTextContains($expected, $result['Message-ID']);
  362. $this->CakeEmail->domain('example.org');
  363. $result = $this->CakeEmail->getHeaders();
  364. $expected = '@example.org>';
  365. $this->assertTextContains($expected, $result['Message-ID']);
  366. }
  367. /**
  368. * testSubject method
  369. *
  370. * @return void
  371. */
  372. public function testSubject() {
  373. $this->CakeEmail->subject('You have a new message.');
  374. $this->assertSame($this->CakeEmail->subject(), 'You have a new message.');
  375. $this->CakeEmail->subject('You have a new message, I think.');
  376. $this->assertSame($this->CakeEmail->subject(), 'You have a new message, I think.');
  377. $this->CakeEmail->subject(1);
  378. $this->assertSame($this->CakeEmail->subject(), '1');
  379. $this->CakeEmail->subject('هذه رسالة بعنوان طويل مرسل للمستلم');
  380. $expected = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
  381. $this->assertSame($this->CakeEmail->subject(), $expected);
  382. }
  383. /**
  384. * testSubjectJapanese
  385. *
  386. * @return void
  387. */
  388. public function testSubjectJapanese() {
  389. $this->skipIf(!function_exists('mb_convert_encoding'));
  390. mb_internal_encoding('UTF-8');
  391. $this->CakeEmail->headerCharset = 'ISO-2022-JP';
  392. $this->CakeEmail->subject('日本語のSubjectにも対応するよ');
  393. $expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsJE4bKEJTdWJqZWN0GyRCJEskYkJQMX4kOSRrJGgbKEI=?=';
  394. $this->assertSame($this->CakeEmail->subject(), $expected);
  395. $this->CakeEmail->subject('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?');
  396. $expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
  397. " =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
  398. " =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
  399. $this->assertSame($this->CakeEmail->subject(), $expected);
  400. }
  401. /**
  402. * testHeaders method
  403. *
  404. * @return void
  405. */
  406. public function testHeaders() {
  407. $this->CakeEmail->messageId(false);
  408. $this->CakeEmail->setHeaders(array('X-Something' => 'nice'));
  409. $expected = array(
  410. 'X-Something' => 'nice',
  411. 'X-Mailer' => 'CakePHP Email',
  412. 'Date' => date(DATE_RFC2822),
  413. 'MIME-Version' => '1.0',
  414. 'Content-Type' => 'text/plain; charset=UTF-8',
  415. 'Content-Transfer-Encoding' => '8bit'
  416. );
  417. $this->assertSame($this->CakeEmail->getHeaders(), $expected);
  418. $this->CakeEmail->addHeaders(array('X-Something' => 'very nice', 'X-Other' => 'cool'));
  419. $expected = array(
  420. 'X-Something' => 'very nice',
  421. 'X-Other' => 'cool',
  422. 'X-Mailer' => 'CakePHP Email',
  423. 'Date' => date(DATE_RFC2822),
  424. 'MIME-Version' => '1.0',
  425. 'Content-Type' => 'text/plain; charset=UTF-8',
  426. 'Content-Transfer-Encoding' => '8bit'
  427. );
  428. $this->assertSame($this->CakeEmail->getHeaders(), $expected);
  429. $this->CakeEmail->from('cake@cakephp.org');
  430. $this->assertSame($this->CakeEmail->getHeaders(), $expected);
  431. $expected = array(
  432. 'From' => 'cake@cakephp.org',
  433. 'X-Something' => 'very nice',
  434. 'X-Other' => 'cool',
  435. 'X-Mailer' => 'CakePHP Email',
  436. 'Date' => date(DATE_RFC2822),
  437. 'MIME-Version' => '1.0',
  438. 'Content-Type' => 'text/plain; charset=UTF-8',
  439. 'Content-Transfer-Encoding' => '8bit'
  440. );
  441. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true)), $expected);
  442. $this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
  443. $expected['From'] = 'CakePHP <cake@cakephp.org>';
  444. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true)), $expected);
  445. $this->CakeEmail->to(array('cake@cakephp.org', 'php@cakephp.org' => 'CakePHP'));
  446. $expected = array(
  447. 'From' => 'CakePHP <cake@cakephp.org>',
  448. 'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
  449. 'X-Something' => 'very nice',
  450. 'X-Other' => 'cool',
  451. 'X-Mailer' => 'CakePHP Email',
  452. 'Date' => date(DATE_RFC2822),
  453. 'MIME-Version' => '1.0',
  454. 'Content-Type' => 'text/plain; charset=UTF-8',
  455. 'Content-Transfer-Encoding' => '8bit'
  456. );
  457. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
  458. $this->CakeEmail->charset = 'ISO-2022-JP';
  459. $expected = array(
  460. 'From' => 'CakePHP <cake@cakephp.org>',
  461. 'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
  462. 'X-Something' => 'very nice',
  463. 'X-Other' => 'cool',
  464. 'X-Mailer' => 'CakePHP Email',
  465. 'Date' => date(DATE_RFC2822),
  466. 'MIME-Version' => '1.0',
  467. 'Content-Type' => 'text/plain; charset=ISO-2022-JP',
  468. 'Content-Transfer-Encoding' => '7bit'
  469. );
  470. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
  471. $result = $this->CakeEmail->setHeaders(array());
  472. $this->assertInstanceOf('CakeEmail', $result);
  473. }
  474. /**
  475. * Data provider function for testInvalidHeaders
  476. *
  477. * @return array
  478. */
  479. public static function invalidHeaders() {
  480. return array(
  481. array(10),
  482. array(''),
  483. array('string'),
  484. array(false),
  485. array(null)
  486. );
  487. }
  488. /**
  489. * testInvalidHeaders
  490. *
  491. * @dataProvider invalidHeaders
  492. * @expectedException SocketException
  493. * @return void
  494. */
  495. public function testInvalidHeaders($value) {
  496. $this->CakeEmail->setHeaders($value);
  497. }
  498. /**
  499. * testInvalidAddHeaders
  500. *
  501. * @dataProvider invalidHeaders
  502. * @expectedException SocketException
  503. * @return void
  504. */
  505. public function testInvalidAddHeaders($value) {
  506. $this->CakeEmail->addHeaders($value);
  507. }
  508. /**
  509. * testTemplate method
  510. *
  511. * @return void
  512. */
  513. public function testTemplate() {
  514. $this->CakeEmail->template('template', 'layout');
  515. $expected = array('template' => 'template', 'layout' => 'layout');
  516. $this->assertSame($this->CakeEmail->template(), $expected);
  517. $this->CakeEmail->template('new_template');
  518. $expected = array('template' => 'new_template', 'layout' => 'layout');
  519. $this->assertSame($this->CakeEmail->template(), $expected);
  520. $this->CakeEmail->template('template', null);
  521. $expected = array('template' => 'template', 'layout' => null);
  522. $this->assertSame($this->CakeEmail->template(), $expected);
  523. $this->CakeEmail->template(null, null);
  524. $expected = array('template' => null, 'layout' => null);
  525. $this->assertSame($this->CakeEmail->template(), $expected);
  526. }
  527. /**
  528. * testTheme method
  529. *
  530. * @return void
  531. */
  532. public function testTheme() {
  533. $this->assertSame(null, $this->CakeEmail->theme());
  534. $this->CakeEmail->theme('default');
  535. $expected = 'default';
  536. $this->assertSame($expected, $this->CakeEmail->theme());
  537. }
  538. /**
  539. * testViewVars method
  540. *
  541. * @return void
  542. */
  543. public function testViewVars() {
  544. $this->assertSame($this->CakeEmail->viewVars(), array());
  545. $this->CakeEmail->viewVars(array('value' => 12345));
  546. $this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345));
  547. $this->CakeEmail->viewVars(array('name' => 'CakePHP'));
  548. $this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345, 'name' => 'CakePHP'));
  549. $this->CakeEmail->viewVars(array('value' => 4567));
  550. $this->assertSame($this->CakeEmail->viewVars(), array('value' => 4567, 'name' => 'CakePHP'));
  551. }
  552. /**
  553. * testAttachments method
  554. *
  555. * @return void
  556. */
  557. public function testAttachments() {
  558. $this->CakeEmail->attachments(CAKE . 'basics.php');
  559. $expected = array('basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'application/octet-stream'));
  560. $this->assertSame($this->CakeEmail->attachments(), $expected);
  561. $this->CakeEmail->attachments(array());
  562. $this->assertSame($this->CakeEmail->attachments(), array());
  563. $this->CakeEmail->attachments(array(array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
  564. $this->CakeEmail->addAttachments(CAKE . 'bootstrap.php');
  565. $this->CakeEmail->addAttachments(array(CAKE . 'bootstrap.php'));
  566. $this->CakeEmail->addAttachments(array('other.txt' => CAKE . 'bootstrap.php', 'license' => CAKE . 'LICENSE.txt'));
  567. $expected = array(
  568. 'basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'),
  569. 'bootstrap.php' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
  570. 'other.txt' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
  571. 'license' => array('file' => CAKE . 'LICENSE.txt', 'mimetype' => 'application/octet-stream')
  572. );
  573. $this->assertSame($this->CakeEmail->attachments(), $expected);
  574. $this->setExpectedException('SocketException');
  575. $this->CakeEmail->attachments(array(array('nofile' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
  576. }
  577. /**
  578. * testTransport method
  579. *
  580. * @return void
  581. */
  582. public function testTransport() {
  583. $result = $this->CakeEmail->transport('Debug');
  584. $this->assertSame($this->CakeEmail, $result);
  585. $this->assertSame($this->CakeEmail->transport(), 'Debug');
  586. $result = $this->CakeEmail->transportClass();
  587. $this->assertInstanceOf('DebugTransport', $result);
  588. $this->setExpectedException('SocketException');
  589. $this->CakeEmail->transport('Invalid');
  590. $result = $this->CakeEmail->transportClass();
  591. }
  592. /**
  593. * testExtendTransport method
  594. *
  595. * @return void
  596. */
  597. public function testExtendTransport() {
  598. $this->setExpectedException('SocketException');
  599. $this->CakeEmail->transport('Extend');
  600. $result = $this->CakeEmail->transportClass();
  601. }
  602. /**
  603. * testConfig method
  604. *
  605. * @return void
  606. */
  607. public function testConfig() {
  608. $transportClass = $this->CakeEmail->transport('debug')->transportClass();
  609. $config = array('test' => 'ok', 'test2' => true);
  610. $this->CakeEmail->config($config);
  611. $this->assertSame($transportClass->config(), $config);
  612. $this->assertSame($this->CakeEmail->config(), $config);
  613. $this->CakeEmail->config(array());
  614. $this->assertSame($transportClass->config(), array());
  615. }
  616. /**
  617. * testConfigString method
  618. *
  619. * @return void
  620. */
  621. public function testConfigString() {
  622. $configs = new EmailConfig();
  623. $this->CakeEmail->config('test');
  624. $result = $this->CakeEmail->to();
  625. $this->assertEquals($configs->test['to'], $result);
  626. $result = $this->CakeEmail->from();
  627. $this->assertEquals($configs->test['from'], $result);
  628. $result = $this->CakeEmail->subject();
  629. $this->assertEquals($configs->test['subject'], $result);
  630. $result = $this->CakeEmail->theme();
  631. $this->assertEquals($configs->test['theme'], $result);
  632. $result = $this->CakeEmail->transport();
  633. $this->assertEquals($configs->test['transport'], $result);
  634. $result = $this->CakeEmail->transportClass();
  635. $this->assertInstanceOf('DebugTransport', $result);
  636. $result = $this->CakeEmail->helpers();
  637. $this->assertEquals($configs->test['helpers'], $result);
  638. }
  639. /**
  640. * testSendWithContent method
  641. *
  642. * @return void
  643. */
  644. public function testSendWithContent() {
  645. $this->CakeEmail->reset();
  646. $this->CakeEmail->transport('Debug');
  647. $this->CakeEmail->from('cake@cakephp.org');
  648. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  649. $this->CakeEmail->subject('My title');
  650. $this->CakeEmail->config(array('empty'));
  651. $result = $this->CakeEmail->send("Here is my body, with multi lines.\nThis is the second line.\r\n\r\nAnd the last.");
  652. $expected = array('headers', 'message');
  653. $this->assertEquals($expected, array_keys($result));
  654. $expected = "Here is my body, with multi lines.\r\nThis is the second line.\r\n\r\nAnd the last.\r\n\r\n";
  655. $this->assertEquals($expected, $result['message']);
  656. $this->assertTrue((bool)strpos($result['headers'], 'Date: '));
  657. $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
  658. $this->assertTrue((bool)strpos($result['headers'], 'To: '));
  659. $result = $this->CakeEmail->send("Other body");
  660. $expected = "Other body\r\n\r\n";
  661. $this->assertSame($result['message'], $expected);
  662. $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
  663. $this->assertTrue((bool)strpos($result['headers'], 'To: '));
  664. $this->CakeEmail->reset();
  665. $this->CakeEmail->transport('Debug');
  666. $this->CakeEmail->from('cake@cakephp.org');
  667. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  668. $this->CakeEmail->subject('My title');
  669. $this->CakeEmail->config(array('empty'));
  670. $result = $this->CakeEmail->send(array('Sending content', 'As array'));
  671. $expected = "Sending content\r\nAs array\r\n\r\n\r\n";
  672. $this->assertSame($result['message'], $expected);
  673. }
  674. /**
  675. * testSendWithoutFrom method
  676. *
  677. * @return void
  678. */
  679. public function testSendWithoutFrom() {
  680. $this->CakeEmail->transport('Debug');
  681. $this->CakeEmail->to('cake@cakephp.org');
  682. $this->CakeEmail->subject('My title');
  683. $this->CakeEmail->config(array('empty'));
  684. $this->setExpectedException('SocketException');
  685. $this->CakeEmail->send("Forgot to set From");
  686. }
  687. /**
  688. * testSendWithoutTo method
  689. *
  690. * @return void
  691. */
  692. public function testSendWithoutTo() {
  693. $this->CakeEmail->transport('Debug');
  694. $this->CakeEmail->from('cake@cakephp.org');
  695. $this->CakeEmail->subject('My title');
  696. $this->CakeEmail->config(array('empty'));
  697. $this->setExpectedException('SocketException');
  698. $this->CakeEmail->send("Forgot to set To");
  699. }
  700. /**
  701. * Test send() with no template.
  702. *
  703. * @return void
  704. */
  705. public function testSendNoTemplateWithAttachments() {
  706. $this->CakeEmail->transport('debug');
  707. $this->CakeEmail->from('cake@cakephp.org');
  708. $this->CakeEmail->to('cake@cakephp.org');
  709. $this->CakeEmail->subject('My title');
  710. $this->CakeEmail->emailFormat('text');
  711. $this->CakeEmail->attachments(array(CAKE . 'basics.php'));
  712. $result = $this->CakeEmail->send('Hello');
  713. $boundary = $this->CakeEmail->getBoundary();
  714. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  715. $expected = "--$boundary\r\n" .
  716. "Content-Type: text/plain; charset=UTF-8\r\n" .
  717. "Content-Transfer-Encoding: 8bit\r\n" .
  718. "\r\n" .
  719. "Hello" .
  720. "\r\n" .
  721. "\r\n" .
  722. "\r\n" .
  723. "--$boundary\r\n" .
  724. "Content-Type: application/octet-stream\r\n" .
  725. "Content-Transfer-Encoding: base64\r\n" .
  726. "Content-Disposition: attachment; filename=\"basics.php\"\r\n\r\n";
  727. $this->assertContains($expected, $result['message']);
  728. }
  729. /**
  730. * Test send() with no template as both
  731. *
  732. * @return void
  733. */
  734. public function testSendNoTemplateWithAttachmentsAsBoth() {
  735. $this->CakeEmail->transport('debug');
  736. $this->CakeEmail->from('cake@cakephp.org');
  737. $this->CakeEmail->to('cake@cakephp.org');
  738. $this->CakeEmail->subject('My title');
  739. $this->CakeEmail->emailFormat('both');
  740. $this->CakeEmail->attachments(array(CAKE . 'VERSION.txt'));
  741. $result = $this->CakeEmail->send('Hello');
  742. $boundary = $this->CakeEmail->getBoundary();
  743. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  744. $expected = "--$boundary\r\n" .
  745. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  746. "\r\n" .
  747. "--alt-$boundary\r\n" .
  748. "Content-Type: text/plain; charset=UTF-8\r\n" .
  749. "Content-Transfer-Encoding: 8bit\r\n" .
  750. "\r\n" .
  751. "Hello" .
  752. "\r\n" .
  753. "\r\n" .
  754. "\r\n" .
  755. "--alt-$boundary\r\n" .
  756. "Content-Type: text/html; charset=UTF-8\r\n" .
  757. "Content-Transfer-Encoding: 8bit\r\n" .
  758. "\r\n" .
  759. "Hello" .
  760. "\r\n" .
  761. "\r\n" .
  762. "\r\n" .
  763. "--alt-{$boundary}--\r\n" .
  764. "\r\n" .
  765. "--$boundary\r\n" .
  766. "Content-Type: application/octet-stream\r\n" .
  767. "Content-Transfer-Encoding: base64\r\n" .
  768. "Content-Disposition: attachment; filename=\"VERSION.txt\"\r\n\r\n";
  769. $this->assertContains($expected, $result['message']);
  770. }
  771. /**
  772. * Test setting inline attachments and messages.
  773. *
  774. * @return void
  775. */
  776. public function testSendWithInlineAttachments() {
  777. $this->CakeEmail->transport('debug');
  778. $this->CakeEmail->from('cake@cakephp.org');
  779. $this->CakeEmail->to('cake@cakephp.org');
  780. $this->CakeEmail->subject('My title');
  781. $this->CakeEmail->emailFormat('both');
  782. $this->CakeEmail->attachments(array(
  783. 'cake.png' => array(
  784. 'file' => CAKE . 'VERSION.txt',
  785. 'contentId' => 'abc123'
  786. )
  787. ));
  788. $result = $this->CakeEmail->send('Hello');
  789. $boundary = $this->CakeEmail->getBoundary();
  790. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  791. $expected = "--$boundary\r\n" .
  792. "Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
  793. "\r\n" .
  794. "--rel-$boundary\r\n" .
  795. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  796. "\r\n" .
  797. "--alt-$boundary\r\n" .
  798. "Content-Type: text/plain; charset=UTF-8\r\n" .
  799. "Content-Transfer-Encoding: 8bit\r\n" .
  800. "\r\n" .
  801. "Hello" .
  802. "\r\n" .
  803. "\r\n" .
  804. "\r\n" .
  805. "--alt-$boundary\r\n" .
  806. "Content-Type: text/html; charset=UTF-8\r\n" .
  807. "Content-Transfer-Encoding: 8bit\r\n" .
  808. "\r\n" .
  809. "Hello" .
  810. "\r\n" .
  811. "\r\n" .
  812. "\r\n" .
  813. "--alt-{$boundary}--\r\n" .
  814. "\r\n" .
  815. "--rel-$boundary\r\n" .
  816. "Content-Type: application/octet-stream\r\n" .
  817. "Content-Transfer-Encoding: base64\r\n" .
  818. "Content-ID: <abc123>\r\n" .
  819. "Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
  820. $this->assertContains($expected, $result['message']);
  821. $this->assertContains('--rel-' . $boundary . '--', $result['message']);
  822. $this->assertContains('--' . $boundary . '--', $result['message']);
  823. }
  824. /**
  825. * testSendWithLog method
  826. *
  827. * @return void
  828. */
  829. public function testSendWithLog() {
  830. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'tmp' . DS;
  831. CakeLog::config('email', array(
  832. 'engine' => 'FileLog',
  833. 'path' => TMP
  834. ));
  835. CakeLog::drop('default');
  836. $this->CakeEmail->transport('Debug');
  837. $this->CakeEmail->to('me@cakephp.org');
  838. $this->CakeEmail->from('cake@cakephp.org');
  839. $this->CakeEmail->subject('My title');
  840. $this->CakeEmail->config(array('log' => 'cake_test_emails'));
  841. $result = $this->CakeEmail->send("Logging This");
  842. App::uses('File', 'Utility');
  843. $File = new File(TMP . 'cake_test_emails.log');
  844. $log = $File->read();
  845. $this->assertTrue(strpos($log, $result['headers']) !== false);
  846. $this->assertTrue(strpos($log, $result['message']) !== false);
  847. $File->delete();
  848. CakeLog::drop('email');
  849. }
  850. /**
  851. * testSendRender method
  852. *
  853. * @return void
  854. */
  855. public function testSendRender() {
  856. $this->CakeEmail->reset();
  857. $this->CakeEmail->transport('debug');
  858. $this->CakeEmail->from('cake@cakephp.org');
  859. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  860. $this->CakeEmail->subject('My title');
  861. $this->CakeEmail->config(array('empty'));
  862. $this->CakeEmail->template('default', 'default');
  863. $result = $this->CakeEmail->send();
  864. $this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
  865. $this->assertContains('Message-ID: ', $result['headers']);
  866. $this->assertContains('To: ', $result['headers']);
  867. }
  868. /**
  869. * testSendRender method for ISO-2022-JP
  870. *
  871. * @return void
  872. */
  873. public function testSendRenderJapanese() {
  874. $this->skipIf(!function_exists('mb_convert_encoding'));
  875. $this->CakeEmail->reset();
  876. $this->CakeEmail->transport('debug');
  877. $this->CakeEmail->from('cake@cakephp.org');
  878. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  879. $this->CakeEmail->subject('My title');
  880. $this->CakeEmail->config(array('empty'));
  881. $this->CakeEmail->template('default', 'japanese');
  882. $this->CakeEmail->charset = 'ISO-2022-JP';
  883. $result = $this->CakeEmail->send();
  884. $expected = mb_convert_encoding('CakePHP Framework を使って送信したメールです。 http://cakephp.org.', 'ISO-2022-JP');
  885. $this->assertContains($expected, $result['message']);
  886. $this->assertContains('Message-ID: ', $result['headers']);
  887. $this->assertContains('To: ', $result['headers']);
  888. }
  889. /**
  890. * testSendRenderThemed method
  891. *
  892. * @return void
  893. */
  894. public function testSendRenderThemed() {
  895. $this->CakeEmail->reset();
  896. $this->CakeEmail->transport('debug');
  897. $this->CakeEmail->from('cake@cakephp.org');
  898. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  899. $this->CakeEmail->subject('My title');
  900. $this->CakeEmail->config(array('empty'));
  901. $this->CakeEmail->theme('TestTheme');
  902. $this->CakeEmail->template('themed', 'default');
  903. $result = $this->CakeEmail->send();
  904. $this->assertContains('In TestTheme', $result['message']);
  905. $this->assertContains('Message-ID: ', $result['headers']);
  906. $this->assertContains('To: ', $result['headers']);
  907. }
  908. /**
  909. * testSendRenderWithVars method
  910. *
  911. * @return void
  912. */
  913. public function testSendRenderWithVars() {
  914. $this->CakeEmail->reset();
  915. $this->CakeEmail->transport('debug');
  916. $this->CakeEmail->from('cake@cakephp.org');
  917. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  918. $this->CakeEmail->subject('My title');
  919. $this->CakeEmail->config(array('empty'));
  920. $this->CakeEmail->template('custom', 'default');
  921. $this->CakeEmail->viewVars(array('value' => 12345));
  922. $result = $this->CakeEmail->send();
  923. $this->assertContains('Here is your value: 12345', $result['message']);
  924. }
  925. /**
  926. * testSendRenderWithVars method for ISO-2022-JP
  927. *
  928. * @return void
  929. */
  930. public function testSendRenderWithVarsJapanese() {
  931. $this->skipIf(!function_exists('mb_convert_encoding'));
  932. $this->CakeEmail->reset();
  933. $this->CakeEmail->transport('debug');
  934. $this->CakeEmail->from('cake@cakephp.org');
  935. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  936. $this->CakeEmail->subject('My title');
  937. $this->CakeEmail->config(array('empty'));
  938. $this->CakeEmail->template('japanese', 'default');
  939. $this->CakeEmail->viewVars(array('value' => '日本語の差し込み123'));
  940. $this->CakeEmail->charset = 'ISO-2022-JP';
  941. $result = $this->CakeEmail->send();
  942. $expected = mb_convert_encoding('ここにあなたの設定した値が入ります: 日本語の差し込み123', 'ISO-2022-JP');
  943. $this->assertTrue((bool)strpos($result['message'], $expected));
  944. }
  945. /**
  946. * testSendRenderWithHelpers method
  947. *
  948. * @return void
  949. */
  950. public function testSendRenderWithHelpers() {
  951. $this->CakeEmail->reset();
  952. $this->CakeEmail->transport('debug');
  953. $timestamp = time();
  954. $this->CakeEmail->from('cake@cakephp.org');
  955. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  956. $this->CakeEmail->subject('My title');
  957. $this->CakeEmail->config(array('empty'));
  958. $this->CakeEmail->template('custom_helper', 'default');
  959. $this->CakeEmail->viewVars(array('time' => $timestamp));
  960. $result = $this->CakeEmail->helpers(array('Time'));
  961. $this->assertInstanceOf('CakeEmail', $result);
  962. $result = $this->CakeEmail->send();
  963. $this->assertTrue((bool)strpos($result['message'], 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
  964. $result = $this->CakeEmail->helpers();
  965. $this->assertEquals(array('Time'), $result);
  966. }
  967. /**
  968. * testSendRenderWithImage method
  969. *
  970. * @return void
  971. */
  972. public function testSendRenderWithImage() {
  973. $this->CakeEmail->reset();
  974. $this->CakeEmail->transport('Debug');
  975. $this->CakeEmail->from('cake@cakephp.org');
  976. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  977. $this->CakeEmail->subject('My title');
  978. $this->CakeEmail->config(array('empty'));
  979. $this->CakeEmail->template('image');
  980. $this->CakeEmail->emailFormat('html');
  981. $server = env('SERVER_NAME') ? env('SERVER_NAME') : 'localhost';
  982. if (env('SERVER_PORT') != null && env('SERVER_PORT') != 80) {
  983. $server .= ':' . env('SERVER_PORT');
  984. }
  985. $expected = '<img src="http://' . $server . '/img/image.gif" alt="cool image" width="100" height="100" />';
  986. $result = $this->CakeEmail->send();
  987. $this->assertContains($expected, $result['message']);
  988. }
  989. /**
  990. * testSendRenderPlugin method
  991. *
  992. * @return void
  993. */
  994. public function testSendRenderPlugin() {
  995. App::build(array(
  996. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  997. ));
  998. CakePlugin::load('TestPlugin');
  999. $this->CakeEmail->reset();
  1000. $this->CakeEmail->transport('debug');
  1001. $this->CakeEmail->from('cake@cakephp.org');
  1002. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1003. $this->CakeEmail->subject('My title');
  1004. $this->CakeEmail->config(array('empty'));
  1005. $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'default')->send();
  1006. $this->assertContains('Into TestPlugin.', $result['message']);
  1007. $this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
  1008. $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'TestPlugin.plug_default')->send();
  1009. $this->assertContains('Into TestPlugin.', $result['message']);
  1010. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1011. $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'plug_default')->send();
  1012. $this->assertContains('Into TestPlugin.', $result['message']);
  1013. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1014. // test plugin template overridden by theme
  1015. $this->CakeEmail->theme('TestTheme');
  1016. $result = $this->CakeEmail->send();
  1017. $this->assertContains('Into TestPlugin. (themed)', $result['message']);
  1018. $this->CakeEmail->viewVars(array('value' => 12345));
  1019. $result = $this->CakeEmail->template('custom', 'TestPlugin.plug_default')->send();
  1020. $this->assertContains('Here is your value: 12345', $result['message']);
  1021. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1022. $this->setExpectedException('MissingViewException');
  1023. $this->CakeEmail->template('test_plugin_tpl', 'plug_default')->send();
  1024. }
  1025. /**
  1026. * testSendMultipleMIME method
  1027. *
  1028. * @return void
  1029. */
  1030. public function testSendMultipleMIME() {
  1031. $this->CakeEmail->reset();
  1032. $this->CakeEmail->transport('debug');
  1033. $this->CakeEmail->from('cake@cakephp.org');
  1034. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1035. $this->CakeEmail->subject('My title');
  1036. $this->CakeEmail->template('custom', 'default');
  1037. $this->CakeEmail->config(array());
  1038. $this->CakeEmail->viewVars(array('value' => 12345));
  1039. $this->CakeEmail->emailFormat('both');
  1040. $result = $this->CakeEmail->send();
  1041. $message = $this->CakeEmail->message();
  1042. $boundary = $this->CakeEmail->getBoundary();
  1043. $this->assertFalse(empty($boundary));
  1044. $this->assertContains('--' . $boundary, $message);
  1045. $this->assertContains('--' . $boundary . '--', $message);
  1046. $this->assertContains('--alt-' . $boundary, $message);
  1047. $this->assertContains('--alt-' . $boundary . '--', $message);
  1048. $this->CakeEmail->attachments(array('fake.php' => __FILE__));
  1049. $this->CakeEmail->send();
  1050. $message = $this->CakeEmail->message();
  1051. $boundary = $this->CakeEmail->getBoundary();
  1052. $this->assertFalse(empty($boundary));
  1053. $this->assertContains('--' . $boundary, $message);
  1054. $this->assertContains('--' . $boundary . '--', $message);
  1055. $this->assertContains('--alt-' . $boundary, $message);
  1056. $this->assertContains('--alt-' . $boundary . '--', $message);
  1057. }
  1058. /**
  1059. * testSendAttachment method
  1060. *
  1061. * @return void
  1062. */
  1063. public function testSendAttachment() {
  1064. $this->CakeEmail->reset();
  1065. $this->CakeEmail->transport('debug');
  1066. $this->CakeEmail->from('cake@cakephp.org');
  1067. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1068. $this->CakeEmail->subject('My title');
  1069. $this->CakeEmail->config(array());
  1070. $this->CakeEmail->attachments(array(CAKE . 'basics.php'));
  1071. $result = $this->CakeEmail->send('body');
  1072. $this->assertContains("Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"basics.php\"", $result['message']);
  1073. $this->CakeEmail->attachments(array('my.file.txt' => CAKE . 'basics.php'));
  1074. $result = $this->CakeEmail->send('body');
  1075. $this->assertContains("Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"my.file.txt\"", $result['message']);
  1076. $this->CakeEmail->attachments(array('file.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
  1077. $result = $this->CakeEmail->send('body');
  1078. $this->assertContains("Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"file.txt\"", $result['message']);
  1079. $this->CakeEmail->attachments(array('file2.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain', 'contentId' => 'a1b1c1')));
  1080. $result = $this->CakeEmail->send('body');
  1081. $this->assertContains("Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-ID: <a1b1c1>\r\nContent-Disposition: inline; filename=\"file2.txt\"", $result['message']);
  1082. }
  1083. /**
  1084. * testDeliver method
  1085. *
  1086. * @return void
  1087. */
  1088. public function testDeliver() {
  1089. $instance = CakeEmail::deliver('all@cakephp.org', 'About', 'Everything ok', array('from' => 'root@cakephp.org'), false);
  1090. $this->assertInstanceOf('CakeEmail', $instance);
  1091. $this->assertSame($instance->to(), array('all@cakephp.org' => 'all@cakephp.org'));
  1092. $this->assertSame($instance->subject(), 'About');
  1093. $this->assertSame($instance->from(), array('root@cakephp.org' => 'root@cakephp.org'));
  1094. $config = array(
  1095. 'from' => 'cake@cakephp.org',
  1096. 'to' => 'debug@cakephp.org',
  1097. 'subject' => 'Update ok',
  1098. 'template' => 'custom',
  1099. 'layout' => 'custom_layout',
  1100. 'viewVars' => array('value' => 123),
  1101. 'cc' => array('cake@cakephp.org' => 'Myself')
  1102. );
  1103. $instance = CakeEmail::deliver(null, null, array('name' => 'CakePHP'), $config, false);
  1104. $this->assertSame($instance->from(), array('cake@cakephp.org' => 'cake@cakephp.org'));
  1105. $this->assertSame($instance->to(), array('debug@cakephp.org' => 'debug@cakephp.org'));
  1106. $this->assertSame($instance->subject(), 'Update ok');
  1107. $this->assertSame($instance->template(), array('template' => 'custom', 'layout' => 'custom_layout'));
  1108. $this->assertSame($instance->viewVars(), array('value' => 123, 'name' => 'CakePHP'));
  1109. $this->assertSame($instance->cc(), array('cake@cakephp.org' => 'Myself'));
  1110. $configs = array('from' => 'root@cakephp.org', 'message' => 'Message from configs', 'transport' => 'Debug');
  1111. $instance = CakeEmail::deliver('all@cakephp.org', 'About', null, $configs, true);
  1112. $message = $instance->message();
  1113. $this->assertEquals($configs['message'], $message[0]);
  1114. }
  1115. /**
  1116. * testMessage method
  1117. *
  1118. * @return void
  1119. */
  1120. public function testMessage() {
  1121. $this->CakeEmail->reset();
  1122. $this->CakeEmail->transport('debug');
  1123. $this->CakeEmail->from('cake@cakephp.org');
  1124. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1125. $this->CakeEmail->subject('My title');
  1126. $this->CakeEmail->config(array('empty'));
  1127. $this->CakeEmail->template('default', 'default');
  1128. $this->CakeEmail->emailFormat('both');
  1129. $result = $this->CakeEmail->send();
  1130. $expected = '<p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>';
  1131. $this->assertContains($expected, $this->CakeEmail->message(CakeEmail::MESSAGE_HTML));
  1132. $expected = 'This email was sent using the CakePHP Framework, http://cakephp.org.';
  1133. $this->assertContains($expected, $this->CakeEmail->message(CakeEmail::MESSAGE_TEXT));
  1134. $message = $this->CakeEmail->message();
  1135. $this->assertContains('Content-Type: text/plain; charset=UTF-8', $message);
  1136. $this->assertContains('Content-Type: text/html; charset=UTF-8', $message);
  1137. // UTF-8 is 8bit
  1138. $this->assertTrue($this->_checkContentTransferEncoding($message, '8bit'));
  1139. $this->CakeEmail->charset = 'ISO-2022-JP';
  1140. $this->CakeEmail->send();
  1141. $message = $this->CakeEmail->message();
  1142. $this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $message);
  1143. $this->assertContains('Content-Type: text/html; charset=ISO-2022-JP', $message);
  1144. // ISO-2022-JP is 7bit
  1145. $this->assertTrue($this->_checkContentTransferEncoding($message, '7bit'));
  1146. }
  1147. /**
  1148. * testReset method
  1149. *
  1150. * @return void
  1151. */
  1152. public function testReset() {
  1153. $this->CakeEmail->to('cake@cakephp.org');
  1154. $this->CakeEmail->theme('TestTheme');
  1155. $this->assertSame($this->CakeEmail->to(), array('cake@cakephp.org' => 'cake@cakephp.org'));
  1156. $this->CakeEmail->reset();
  1157. $this->assertSame($this->CakeEmail->to(), array());
  1158. $this->assertSame(null, $this->CakeEmail->theme());
  1159. }
  1160. /**
  1161. * testReset with charset
  1162. *
  1163. * @return void
  1164. */
  1165. public function testResetWithCharset() {
  1166. $this->CakeEmail->charset = 'ISO-2022-JP';
  1167. $this->CakeEmail->reset();
  1168. $this->assertSame($this->CakeEmail->charset, 'utf-8', $this->CakeEmail->charset);
  1169. $this->assertSame($this->CakeEmail->headerCharset, null, $this->CakeEmail->headerCharset);
  1170. }
  1171. /**
  1172. * testWrap method
  1173. *
  1174. * @return void
  1175. */
  1176. public function testWrap() {
  1177. $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
  1178. $result = $this->CakeEmail->wrap($text);
  1179. $expected = array(
  1180. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
  1181. 'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
  1182. ''
  1183. );
  1184. $this->assertSame($expected, $result);
  1185. $text = 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan amet.';
  1186. $result = $this->CakeEmail->wrap($text);
  1187. $expected = array(
  1188. 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
  1189. 'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
  1190. 'amet.',
  1191. ''
  1192. );
  1193. $this->assertSame($expected, $result);
  1194. $text = '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula pellentesque accumsan amet.<hr></p>';
  1195. $result = $this->CakeEmail->wrap($text);
  1196. $expected = array(
  1197. '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac',
  1198. 'turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula',
  1199. 'pellentesque accumsan amet.<hr></p>',
  1200. ''
  1201. );
  1202. $this->assertSame($expected, $result);
  1203. $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac <a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
  1204. $result = $this->CakeEmail->wrap($text);
  1205. $expected = array(
  1206. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac',
  1207. '<a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh',
  1208. 'nisi, vehicula pellentesque accumsan amet.',
  1209. ''
  1210. );
  1211. $this->assertSame($expected, $result);
  1212. $text = 'Lorem ipsum <a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">ok</a>';
  1213. $result = $this->CakeEmail->wrap($text);
  1214. $expected = array(
  1215. 'Lorem ipsum',
  1216. '<a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">',
  1217. 'ok</a>',
  1218. ''
  1219. );
  1220. $this->assertSame($expected, $result);
  1221. $text = 'Lorem ipsum withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite ok.';
  1222. $result = $this->CakeEmail->wrap($text);
  1223. $expected = array(
  1224. 'Lorem ipsum',
  1225. 'withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite',
  1226. 'ok.',
  1227. ''
  1228. );
  1229. $this->assertSame($expected, $result);
  1230. }
  1231. /**
  1232. * testConstructWithConfigArray method
  1233. *
  1234. * @return void
  1235. */
  1236. public function testConstructWithConfigArray() {
  1237. $configs = array(
  1238. 'from' => array('some@example.com' => 'My website'),
  1239. 'to' => 'test@example.com',
  1240. 'subject' => 'Test mail subject',
  1241. 'transport' => 'Debug',
  1242. );
  1243. $this->CakeEmail = new CakeEmail($configs);
  1244. $result = $this->CakeEmail->to();
  1245. $this->assertEquals(array($configs['to'] => $configs['to']), $result);
  1246. $result = $this->CakeEmail->from();
  1247. $this->assertEquals($configs['from'], $result);
  1248. $result = $this->CakeEmail->subject();
  1249. $this->assertEquals($configs['subject'], $result);
  1250. $result = $this->CakeEmail->transport();
  1251. $this->assertEquals($configs['transport'], $result);
  1252. $result = $this->CakeEmail->transportClass();
  1253. $this->assertTrue($result instanceof DebugTransport);
  1254. $result = $this->CakeEmail->send('This is the message');
  1255. $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
  1256. $this->assertTrue((bool)strpos($result['headers'], 'To: '));
  1257. }
  1258. /**
  1259. * testConstructWithConfigString method
  1260. *
  1261. * @return void
  1262. */
  1263. public function testConstructWithConfigString() {
  1264. $configs = new EmailConfig();
  1265. $this->CakeEmail = new CakeEmail('test');
  1266. $result = $this->CakeEmail->to();
  1267. $this->assertEquals($configs->test['to'], $result);
  1268. $result = $this->CakeEmail->from();
  1269. $this->assertEquals($configs->test['from'], $result);
  1270. $result = $this->CakeEmail->subject();
  1271. $this->assertEquals($configs->test['subject'], $result);
  1272. $result = $this->CakeEmail->transport();
  1273. $this->assertEquals($configs->test['transport'], $result);
  1274. $result = $this->CakeEmail->transportClass();
  1275. $this->assertTrue($result instanceof DebugTransport);
  1276. $result = $this->CakeEmail->send('This is the message');
  1277. $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
  1278. $this->assertTrue((bool)strpos($result['headers'], 'To: '));
  1279. }
  1280. /**
  1281. * testViewRender method
  1282. *
  1283. * @return void
  1284. */
  1285. public function testViewRender() {
  1286. $result = $this->CakeEmail->viewRender();
  1287. $this->assertEquals('View', $result);
  1288. $result = $this->CakeEmail->viewRender('Theme');
  1289. $t

Large files files are truncated, but you can click here to view the full file