PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/vgarcias/expediamedicus
PHP | 1801 lines | 1156 code | 246 blank | 399 comment | 16 complexity | 4514b80547e88383d59ddf1103976150 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. $this->CakeEmail->domain('example.org');
  360. $result = $this->CakeEmail->getHeaders();
  361. $expected = '@example.org>';
  362. $this->assertTextContains($expected, $result['Message-ID']);
  363. $_SERVER['HTTP_HOST'] = 'example.org';
  364. $result = $this->CakeEmail->getHeaders();
  365. $this->assertTextContains('example.org', $result['Message-ID']);
  366. $_SERVER['HTTP_HOST'] = 'example.org:81';
  367. $result = $this->CakeEmail->getHeaders();
  368. $this->assertTextNotContains(':81', $result['Message-ID']);
  369. }
  370. /**
  371. * testSubject method
  372. *
  373. * @return void
  374. */
  375. public function testSubject() {
  376. $this->CakeEmail->subject('You have a new message.');
  377. $this->assertSame($this->CakeEmail->subject(), 'You have a new message.');
  378. $this->CakeEmail->subject('You have a new message, I think.');
  379. $this->assertSame($this->CakeEmail->subject(), 'You have a new message, I think.');
  380. $this->CakeEmail->subject(1);
  381. $this->assertSame($this->CakeEmail->subject(), '1');
  382. $this->CakeEmail->subject('هذه رسالة بعنوان طويل مرسل للمستلم');
  383. $expected = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
  384. $this->assertSame($this->CakeEmail->subject(), $expected);
  385. }
  386. /**
  387. * testSubjectJapanese
  388. *
  389. * @return void
  390. */
  391. public function testSubjectJapanese() {
  392. $this->skipIf(!function_exists('mb_convert_encoding'));
  393. mb_internal_encoding('UTF-8');
  394. $this->CakeEmail->headerCharset = 'ISO-2022-JP';
  395. $this->CakeEmail->subject('日本語のSubjectにも対応するよ');
  396. $expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsJE4bKEJTdWJqZWN0GyRCJEskYkJQMX4kOSRrJGgbKEI=?=';
  397. $this->assertSame($this->CakeEmail->subject(), $expected);
  398. $this->CakeEmail->subject('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?');
  399. $expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
  400. " =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
  401. " =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
  402. $this->assertSame($this->CakeEmail->subject(), $expected);
  403. }
  404. /**
  405. * testHeaders method
  406. *
  407. * @return void
  408. */
  409. public function testHeaders() {
  410. $this->CakeEmail->messageId(false);
  411. $this->CakeEmail->setHeaders(array('X-Something' => 'nice'));
  412. $expected = array(
  413. 'X-Something' => 'nice',
  414. 'X-Mailer' => 'CakePHP Email',
  415. 'Date' => date(DATE_RFC2822),
  416. 'MIME-Version' => '1.0',
  417. 'Content-Type' => 'text/plain; charset=UTF-8',
  418. 'Content-Transfer-Encoding' => '8bit'
  419. );
  420. $this->assertSame($this->CakeEmail->getHeaders(), $expected);
  421. $this->CakeEmail->addHeaders(array('X-Something' => 'very nice', 'X-Other' => 'cool'));
  422. $expected = array(
  423. 'X-Something' => 'very nice',
  424. 'X-Other' => 'cool',
  425. 'X-Mailer' => 'CakePHP Email',
  426. 'Date' => date(DATE_RFC2822),
  427. 'MIME-Version' => '1.0',
  428. 'Content-Type' => 'text/plain; charset=UTF-8',
  429. 'Content-Transfer-Encoding' => '8bit'
  430. );
  431. $this->assertSame($this->CakeEmail->getHeaders(), $expected);
  432. $this->CakeEmail->from('cake@cakephp.org');
  433. $this->assertSame($this->CakeEmail->getHeaders(), $expected);
  434. $expected = array(
  435. 'From' => 'cake@cakephp.org',
  436. 'X-Something' => 'very nice',
  437. 'X-Other' => 'cool',
  438. 'X-Mailer' => 'CakePHP Email',
  439. 'Date' => date(DATE_RFC2822),
  440. 'MIME-Version' => '1.0',
  441. 'Content-Type' => 'text/plain; charset=UTF-8',
  442. 'Content-Transfer-Encoding' => '8bit'
  443. );
  444. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true)), $expected);
  445. $this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
  446. $expected['From'] = 'CakePHP <cake@cakephp.org>';
  447. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true)), $expected);
  448. $this->CakeEmail->to(array('cake@cakephp.org', 'php@cakephp.org' => 'CakePHP'));
  449. $expected = array(
  450. 'From' => 'CakePHP <cake@cakephp.org>',
  451. 'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
  452. 'X-Something' => 'very nice',
  453. 'X-Other' => 'cool',
  454. 'X-Mailer' => 'CakePHP Email',
  455. 'Date' => date(DATE_RFC2822),
  456. 'MIME-Version' => '1.0',
  457. 'Content-Type' => 'text/plain; charset=UTF-8',
  458. 'Content-Transfer-Encoding' => '8bit'
  459. );
  460. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
  461. $this->CakeEmail->charset = 'ISO-2022-JP';
  462. $expected = array(
  463. 'From' => 'CakePHP <cake@cakephp.org>',
  464. 'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
  465. 'X-Something' => 'very nice',
  466. 'X-Other' => 'cool',
  467. 'X-Mailer' => 'CakePHP Email',
  468. 'Date' => date(DATE_RFC2822),
  469. 'MIME-Version' => '1.0',
  470. 'Content-Type' => 'text/plain; charset=ISO-2022-JP',
  471. 'Content-Transfer-Encoding' => '7bit'
  472. );
  473. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
  474. $result = $this->CakeEmail->setHeaders(array());
  475. $this->assertInstanceOf('CakeEmail', $result);
  476. }
  477. /**
  478. * Data provider function for testInvalidHeaders
  479. *
  480. * @return array
  481. */
  482. public static function invalidHeaders() {
  483. return array(
  484. array(10),
  485. array(''),
  486. array('string'),
  487. array(false),
  488. array(null)
  489. );
  490. }
  491. /**
  492. * testInvalidHeaders
  493. *
  494. * @dataProvider invalidHeaders
  495. * @expectedException SocketException
  496. * @return void
  497. */
  498. public function testInvalidHeaders($value) {
  499. $this->CakeEmail->setHeaders($value);
  500. }
  501. /**
  502. * testInvalidAddHeaders
  503. *
  504. * @dataProvider invalidHeaders
  505. * @expectedException SocketException
  506. * @return void
  507. */
  508. public function testInvalidAddHeaders($value) {
  509. $this->CakeEmail->addHeaders($value);
  510. }
  511. /**
  512. * testTemplate method
  513. *
  514. * @return void
  515. */
  516. public function testTemplate() {
  517. $this->CakeEmail->template('template', 'layout');
  518. $expected = array('template' => 'template', 'layout' => 'layout');
  519. $this->assertSame($this->CakeEmail->template(), $expected);
  520. $this->CakeEmail->template('new_template');
  521. $expected = array('template' => 'new_template', 'layout' => 'layout');
  522. $this->assertSame($this->CakeEmail->template(), $expected);
  523. $this->CakeEmail->template('template', null);
  524. $expected = array('template' => 'template', 'layout' => null);
  525. $this->assertSame($this->CakeEmail->template(), $expected);
  526. $this->CakeEmail->template(null, null);
  527. $expected = array('template' => null, 'layout' => null);
  528. $this->assertSame($this->CakeEmail->template(), $expected);
  529. }
  530. /**
  531. * testTheme method
  532. *
  533. * @return void
  534. */
  535. public function testTheme() {
  536. $this->assertSame(null, $this->CakeEmail->theme());
  537. $this->CakeEmail->theme('default');
  538. $expected = 'default';
  539. $this->assertSame($expected, $this->CakeEmail->theme());
  540. }
  541. /**
  542. * testViewVars method
  543. *
  544. * @return void
  545. */
  546. public function testViewVars() {
  547. $this->assertSame($this->CakeEmail->viewVars(), array());
  548. $this->CakeEmail->viewVars(array('value' => 12345));
  549. $this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345));
  550. $this->CakeEmail->viewVars(array('name' => 'CakePHP'));
  551. $this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345, 'name' => 'CakePHP'));
  552. $this->CakeEmail->viewVars(array('value' => 4567));
  553. $this->assertSame($this->CakeEmail->viewVars(), array('value' => 4567, 'name' => 'CakePHP'));
  554. }
  555. /**
  556. * testAttachments method
  557. *
  558. * @return void
  559. */
  560. public function testAttachments() {
  561. $this->CakeEmail->attachments(CAKE . 'basics.php');
  562. $expected = array('basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'application/octet-stream'));
  563. $this->assertSame($this->CakeEmail->attachments(), $expected);
  564. $this->CakeEmail->attachments(array());
  565. $this->assertSame($this->CakeEmail->attachments(), array());
  566. $this->CakeEmail->attachments(array(array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
  567. $this->CakeEmail->addAttachments(CAKE . 'bootstrap.php');
  568. $this->CakeEmail->addAttachments(array(CAKE . 'bootstrap.php'));
  569. $this->CakeEmail->addAttachments(array('other.txt' => CAKE . 'bootstrap.php', 'license' => CAKE . 'LICENSE.txt'));
  570. $expected = array(
  571. 'basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'),
  572. 'bootstrap.php' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
  573. 'other.txt' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
  574. 'license' => array('file' => CAKE . 'LICENSE.txt', 'mimetype' => 'application/octet-stream')
  575. );
  576. $this->assertSame($this->CakeEmail->attachments(), $expected);
  577. $this->setExpectedException('SocketException');
  578. $this->CakeEmail->attachments(array(array('nofile' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
  579. }
  580. /**
  581. * testTransport method
  582. *
  583. * @return void
  584. */
  585. public function testTransport() {
  586. $result = $this->CakeEmail->transport('Debug');
  587. $this->assertSame($this->CakeEmail, $result);
  588. $this->assertSame($this->CakeEmail->transport(), 'Debug');
  589. $result = $this->CakeEmail->transportClass();
  590. $this->assertInstanceOf('DebugTransport', $result);
  591. $this->setExpectedException('SocketException');
  592. $this->CakeEmail->transport('Invalid');
  593. $result = $this->CakeEmail->transportClass();
  594. }
  595. /**
  596. * testExtendTransport method
  597. *
  598. * @return void
  599. */
  600. public function testExtendTransport() {
  601. $this->setExpectedException('SocketException');
  602. $this->CakeEmail->transport('Extend');
  603. $result = $this->CakeEmail->transportClass();
  604. }
  605. /**
  606. * testConfig method
  607. *
  608. * @return void
  609. */
  610. public function testConfig() {
  611. $transportClass = $this->CakeEmail->transport('debug')->transportClass();
  612. $config = array('test' => 'ok', 'test2' => true);
  613. $this->CakeEmail->config($config);
  614. $this->assertSame($transportClass->config(), $config);
  615. $this->assertSame($this->CakeEmail->config(), $config);
  616. $this->CakeEmail->config(array());
  617. $this->assertSame($transportClass->config(), array());
  618. }
  619. /**
  620. * testConfigString method
  621. *
  622. * @return void
  623. */
  624. public function testConfigString() {
  625. $configs = new EmailConfig();
  626. $this->CakeEmail->config('test');
  627. $result = $this->CakeEmail->to();
  628. $this->assertEquals($configs->test['to'], $result);
  629. $result = $this->CakeEmail->from();
  630. $this->assertEquals($configs->test['from'], $result);
  631. $result = $this->CakeEmail->subject();
  632. $this->assertEquals($configs->test['subject'], $result);
  633. $result = $this->CakeEmail->theme();
  634. $this->assertEquals($configs->test['theme'], $result);
  635. $result = $this->CakeEmail->transport();
  636. $this->assertEquals($configs->test['transport'], $result);
  637. $result = $this->CakeEmail->transportClass();
  638. $this->assertInstanceOf('DebugTransport', $result);
  639. $result = $this->CakeEmail->helpers();
  640. $this->assertEquals($configs->test['helpers'], $result);
  641. }
  642. /**
  643. * testSendWithContent method
  644. *
  645. * @return void
  646. */
  647. public function testSendWithContent() {
  648. $this->CakeEmail->reset();
  649. $this->CakeEmail->transport('Debug');
  650. $this->CakeEmail->from('cake@cakephp.org');
  651. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  652. $this->CakeEmail->subject('My title');
  653. $this->CakeEmail->config(array('empty'));
  654. $result = $this->CakeEmail->send("Here is my body, with multi lines.\nThis is the second line.\r\n\r\nAnd the last.");
  655. $expected = array('headers', 'message');
  656. $this->assertEquals($expected, array_keys($result));
  657. $expected = "Here is my body, with multi lines.\r\nThis is the second line.\r\n\r\nAnd the last.\r\n\r\n";
  658. $this->assertEquals($expected, $result['message']);
  659. $this->assertTrue((bool)strpos($result['headers'], 'Date: '));
  660. $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
  661. $this->assertTrue((bool)strpos($result['headers'], 'To: '));
  662. $result = $this->CakeEmail->send("Other body");
  663. $expected = "Other body\r\n\r\n";
  664. $this->assertSame($result['message'], $expected);
  665. $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
  666. $this->assertTrue((bool)strpos($result['headers'], 'To: '));
  667. $this->CakeEmail->reset();
  668. $this->CakeEmail->transport('Debug');
  669. $this->CakeEmail->from('cake@cakephp.org');
  670. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  671. $this->CakeEmail->subject('My title');
  672. $this->CakeEmail->config(array('empty'));
  673. $result = $this->CakeEmail->send(array('Sending content', 'As array'));
  674. $expected = "Sending content\r\nAs array\r\n\r\n\r\n";
  675. $this->assertSame($result['message'], $expected);
  676. }
  677. /**
  678. * testSendWithoutFrom method
  679. *
  680. * @return void
  681. */
  682. public function testSendWithoutFrom() {
  683. $this->CakeEmail->transport('Debug');
  684. $this->CakeEmail->to('cake@cakephp.org');
  685. $this->CakeEmail->subject('My title');
  686. $this->CakeEmail->config(array('empty'));
  687. $this->setExpectedException('SocketException');
  688. $this->CakeEmail->send("Forgot to set From");
  689. }
  690. /**
  691. * testSendWithoutTo method
  692. *
  693. * @return void
  694. */
  695. public function testSendWithoutTo() {
  696. $this->CakeEmail->transport('Debug');
  697. $this->CakeEmail->from('cake@cakephp.org');
  698. $this->CakeEmail->subject('My title');
  699. $this->CakeEmail->config(array('empty'));
  700. $this->setExpectedException('SocketException');
  701. $this->CakeEmail->send("Forgot to set To");
  702. }
  703. /**
  704. * Test send() with no template.
  705. *
  706. * @return void
  707. */
  708. public function testSendNoTemplateWithAttachments() {
  709. $this->CakeEmail->transport('debug');
  710. $this->CakeEmail->from('cake@cakephp.org');
  711. $this->CakeEmail->to('cake@cakephp.org');
  712. $this->CakeEmail->subject('My title');
  713. $this->CakeEmail->emailFormat('text');
  714. $this->CakeEmail->attachments(array(CAKE . 'basics.php'));
  715. $result = $this->CakeEmail->send('Hello');
  716. $boundary = $this->CakeEmail->getBoundary();
  717. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  718. $expected = "--$boundary\r\n" .
  719. "Content-Type: text/plain; charset=UTF-8\r\n" .
  720. "Content-Transfer-Encoding: 8bit\r\n" .
  721. "\r\n" .
  722. "Hello" .
  723. "\r\n" .
  724. "\r\n" .
  725. "\r\n" .
  726. "--$boundary\r\n" .
  727. "Content-Type: application/octet-stream\r\n" .
  728. "Content-Transfer-Encoding: base64\r\n" .
  729. "Content-Disposition: attachment; filename=\"basics.php\"\r\n\r\n";
  730. $this->assertContains($expected, $result['message']);
  731. }
  732. /**
  733. * Test send() with no template as both
  734. *
  735. * @return void
  736. */
  737. public function testSendNoTemplateWithAttachmentsAsBoth() {
  738. $this->CakeEmail->transport('debug');
  739. $this->CakeEmail->from('cake@cakephp.org');
  740. $this->CakeEmail->to('cake@cakephp.org');
  741. $this->CakeEmail->subject('My title');
  742. $this->CakeEmail->emailFormat('both');
  743. $this->CakeEmail->attachments(array(CAKE . 'VERSION.txt'));
  744. $result = $this->CakeEmail->send('Hello');
  745. $boundary = $this->CakeEmail->getBoundary();
  746. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  747. $expected = "--$boundary\r\n" .
  748. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  749. "\r\n" .
  750. "--alt-$boundary\r\n" .
  751. "Content-Type: text/plain; charset=UTF-8\r\n" .
  752. "Content-Transfer-Encoding: 8bit\r\n" .
  753. "\r\n" .
  754. "Hello" .
  755. "\r\n" .
  756. "\r\n" .
  757. "\r\n" .
  758. "--alt-$boundary\r\n" .
  759. "Content-Type: text/html; charset=UTF-8\r\n" .
  760. "Content-Transfer-Encoding: 8bit\r\n" .
  761. "\r\n" .
  762. "Hello" .
  763. "\r\n" .
  764. "\r\n" .
  765. "\r\n" .
  766. "--alt-{$boundary}--\r\n" .
  767. "\r\n" .
  768. "--$boundary\r\n" .
  769. "Content-Type: application/octet-stream\r\n" .
  770. "Content-Transfer-Encoding: base64\r\n" .
  771. "Content-Disposition: attachment; filename=\"VERSION.txt\"\r\n\r\n";
  772. $this->assertContains($expected, $result['message']);
  773. }
  774. /**
  775. * Test setting inline attachments and messages.
  776. *
  777. * @return void
  778. */
  779. public function testSendWithInlineAttachments() {
  780. $this->CakeEmail->transport('debug');
  781. $this->CakeEmail->from('cake@cakephp.org');
  782. $this->CakeEmail->to('cake@cakephp.org');
  783. $this->CakeEmail->subject('My title');
  784. $this->CakeEmail->emailFormat('both');
  785. $this->CakeEmail->attachments(array(
  786. 'cake.png' => array(
  787. 'file' => CAKE . 'VERSION.txt',
  788. 'contentId' => 'abc123'
  789. )
  790. ));
  791. $result = $this->CakeEmail->send('Hello');
  792. $boundary = $this->CakeEmail->getBoundary();
  793. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  794. $expected = "--$boundary\r\n" .
  795. "Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
  796. "\r\n" .
  797. "--rel-$boundary\r\n" .
  798. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  799. "\r\n" .
  800. "--alt-$boundary\r\n" .
  801. "Content-Type: text/plain; charset=UTF-8\r\n" .
  802. "Content-Transfer-Encoding: 8bit\r\n" .
  803. "\r\n" .
  804. "Hello" .
  805. "\r\n" .
  806. "\r\n" .
  807. "\r\n" .
  808. "--alt-$boundary\r\n" .
  809. "Content-Type: text/html; charset=UTF-8\r\n" .
  810. "Content-Transfer-Encoding: 8bit\r\n" .
  811. "\r\n" .
  812. "Hello" .
  813. "\r\n" .
  814. "\r\n" .
  815. "\r\n" .
  816. "--alt-{$boundary}--\r\n" .
  817. "\r\n" .
  818. "--rel-$boundary\r\n" .
  819. "Content-Type: application/octet-stream\r\n" .
  820. "Content-Transfer-Encoding: base64\r\n" .
  821. "Content-ID: <abc123>\r\n" .
  822. "Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
  823. $this->assertContains($expected, $result['message']);
  824. $this->assertContains('--rel-' . $boundary . '--', $result['message']);
  825. $this->assertContains('--' . $boundary . '--', $result['message']);
  826. }
  827. /**
  828. * testSendWithLog method
  829. *
  830. * @return void
  831. */
  832. public function testSendWithLog() {
  833. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'tmp' . DS;
  834. CakeLog::config('email', array(
  835. 'engine' => 'FileLog',
  836. 'path' => TMP
  837. ));
  838. CakeLog::drop('default');
  839. $this->CakeEmail->transport('Debug');
  840. $this->CakeEmail->to('me@cakephp.org');
  841. $this->CakeEmail->from('cake@cakephp.org');
  842. $this->CakeEmail->subject('My title');
  843. $this->CakeEmail->config(array('log' => 'cake_test_emails'));
  844. $result = $this->CakeEmail->send("Logging This");
  845. App::uses('File', 'Utility');
  846. $File = new File(TMP . 'cake_test_emails.log');
  847. $log = $File->read();
  848. $this->assertTrue(strpos($log, $result['headers']) !== false);
  849. $this->assertTrue(strpos($log, $result['message']) !== false);
  850. $File->delete();
  851. CakeLog::drop('email');
  852. }
  853. /**
  854. * testSendRender method
  855. *
  856. * @return void
  857. */
  858. public function testSendRender() {
  859. $this->CakeEmail->reset();
  860. $this->CakeEmail->transport('debug');
  861. $this->CakeEmail->from('cake@cakephp.org');
  862. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  863. $this->CakeEmail->subject('My title');
  864. $this->CakeEmail->config(array('empty'));
  865. $this->CakeEmail->template('default', 'default');
  866. $result = $this->CakeEmail->send();
  867. $this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
  868. $this->assertContains('Message-ID: ', $result['headers']);
  869. $this->assertContains('To: ', $result['headers']);
  870. }
  871. /**
  872. * testSendRender method for ISO-2022-JP
  873. *
  874. * @return void
  875. */
  876. public function testSendRenderJapanese() {
  877. $this->skipIf(!function_exists('mb_convert_encoding'));
  878. $this->CakeEmail->reset();
  879. $this->CakeEmail->transport('debug');
  880. $this->CakeEmail->from('cake@cakephp.org');
  881. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  882. $this->CakeEmail->subject('My title');
  883. $this->CakeEmail->config(array('empty'));
  884. $this->CakeEmail->template('default', 'japanese');
  885. $this->CakeEmail->charset = 'ISO-2022-JP';
  886. $result = $this->CakeEmail->send();
  887. $expected = mb_convert_encoding('CakePHP Framework を使って送信したメールです。 http://cakephp.org.', 'ISO-2022-JP');
  888. $this->assertContains($expected, $result['message']);
  889. $this->assertContains('Message-ID: ', $result['headers']);
  890. $this->assertContains('To: ', $result['headers']);
  891. }
  892. /**
  893. * testSendRenderThemed method
  894. *
  895. * @return void
  896. */
  897. public function testSendRenderThemed() {
  898. $this->CakeEmail->reset();
  899. $this->CakeEmail->transport('debug');
  900. $this->CakeEmail->from('cake@cakephp.org');
  901. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  902. $this->CakeEmail->subject('My title');
  903. $this->CakeEmail->config(array('empty'));
  904. $this->CakeEmail->theme('TestTheme');
  905. $this->CakeEmail->template('themed', 'default');
  906. $result = $this->CakeEmail->send();
  907. $this->assertContains('In TestTheme', $result['message']);
  908. $this->assertContains('Message-ID: ', $result['headers']);
  909. $this->assertContains('To: ', $result['headers']);
  910. }
  911. /**
  912. * testSendRenderWithVars method
  913. *
  914. * @return void
  915. */
  916. public function testSendRenderWithVars() {
  917. $this->CakeEmail->reset();
  918. $this->CakeEmail->transport('debug');
  919. $this->CakeEmail->from('cake@cakephp.org');
  920. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  921. $this->CakeEmail->subject('My title');
  922. $this->CakeEmail->config(array('empty'));
  923. $this->CakeEmail->template('custom', 'default');
  924. $this->CakeEmail->viewVars(array('value' => 12345));
  925. $result = $this->CakeEmail->send();
  926. $this->assertContains('Here is your value: 12345', $result['message']);
  927. }
  928. /**
  929. * testSendRenderWithVars method for ISO-2022-JP
  930. *
  931. * @return void
  932. */
  933. public function testSendRenderWithVarsJapanese() {
  934. $this->skipIf(!function_exists('mb_convert_encoding'));
  935. $this->CakeEmail->reset();
  936. $this->CakeEmail->transport('debug');
  937. $this->CakeEmail->from('cake@cakephp.org');
  938. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  939. $this->CakeEmail->subject('My title');
  940. $this->CakeEmail->config(array('empty'));
  941. $this->CakeEmail->template('japanese', 'default');
  942. $this->CakeEmail->viewVars(array('value' => '日本語の差し込み123'));
  943. $this->CakeEmail->charset = 'ISO-2022-JP';
  944. $result = $this->CakeEmail->send();
  945. $expected = mb_convert_encoding('ここにあなたの設定した値が入ります: 日本語の差し込み123', 'ISO-2022-JP');
  946. $this->assertTrue((bool)strpos($result['message'], $expected));
  947. }
  948. /**
  949. * testSendRenderWithHelpers method
  950. *
  951. * @return void
  952. */
  953. public function testSendRenderWithHelpers() {
  954. $this->CakeEmail->reset();
  955. $this->CakeEmail->transport('debug');
  956. $timestamp = time();
  957. $this->CakeEmail->from('cake@cakephp.org');
  958. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  959. $this->CakeEmail->subject('My title');
  960. $this->CakeEmail->config(array('empty'));
  961. $this->CakeEmail->template('custom_helper', 'default');
  962. $this->CakeEmail->viewVars(array('time' => $timestamp));
  963. $result = $this->CakeEmail->helpers(array('Time'));
  964. $this->assertInstanceOf('CakeEmail', $result);
  965. $result = $this->CakeEmail->send();
  966. $this->assertTrue((bool)strpos($result['message'], 'Right now: ' . date('Y-m-d\TH:i:s\Z', $timestamp)));
  967. $result = $this->CakeEmail->helpers();
  968. $this->assertEquals(array('Time'), $result);
  969. }
  970. /**
  971. * testSendRenderWithImage method
  972. *
  973. * @return void
  974. */
  975. public function testSendRenderWithImage() {
  976. $this->CakeEmail->reset();
  977. $this->CakeEmail->transport('Debug');
  978. $this->CakeEmail->from('cake@cakephp.org');
  979. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  980. $this->CakeEmail->subject('My title');
  981. $this->CakeEmail->config(array('empty'));
  982. $this->CakeEmail->template('image');
  983. $this->CakeEmail->emailFormat('html');
  984. $server = env('SERVER_NAME') ? env('SERVER_NAME') : 'localhost';
  985. if (env('SERVER_PORT') != null && env('SERVER_PORT') != 80) {
  986. $server .= ':' . env('SERVER_PORT');
  987. }
  988. $expected = '<img src="http://' . $server . '/img/image.gif" alt="cool image" width="100" height="100" />';
  989. $result = $this->CakeEmail->send();
  990. $this->assertContains($expected, $result['message']);
  991. }
  992. /**
  993. * testSendRenderPlugin method
  994. *
  995. * @return void
  996. */
  997. public function testSendRenderPlugin() {
  998. App::build(array(
  999. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  1000. ));
  1001. CakePlugin::load('TestPlugin');
  1002. $this->CakeEmail->reset();
  1003. $this->CakeEmail->transport('debug');
  1004. $this->CakeEmail->from('cake@cakephp.org');
  1005. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1006. $this->CakeEmail->subject('My title');
  1007. $this->CakeEmail->config(array('empty'));
  1008. $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'default')->send();
  1009. $this->assertContains('Into TestPlugin.', $result['message']);
  1010. $this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
  1011. $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'TestPlugin.plug_default')->send();
  1012. $this->assertContains('Into TestPlugin.', $result['message']);
  1013. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1014. $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'plug_default')->send();
  1015. $this->assertContains('Into TestPlugin.', $result['message']);
  1016. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1017. // test plugin template overridden by theme
  1018. $this->CakeEmail->theme('TestTheme');
  1019. $result = $this->CakeEmail->send();
  1020. $this->assertContains('Into TestPlugin. (themed)', $result['message']);
  1021. $this->CakeEmail->viewVars(array('value' => 12345));
  1022. $result = $this->CakeEmail->template('custom', 'TestPlugin.plug_default')->send();
  1023. $this->assertContains('Here is your value: 12345', $result['message']);
  1024. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1025. $this->setExpectedException('MissingViewException');
  1026. $this->CakeEmail->template('test_plugin_tpl', 'plug_default')->send();
  1027. }
  1028. /**
  1029. * testSendMultipleMIME method
  1030. *
  1031. * @return void
  1032. */
  1033. public function testSendMultipleMIME() {
  1034. $this->CakeEmail->reset();
  1035. $this->CakeEmail->transport('debug');
  1036. $this->CakeEmail->from('cake@cakephp.org');
  1037. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1038. $this->CakeEmail->subject('My title');
  1039. $this->CakeEmail->template('custom', 'default');
  1040. $this->CakeEmail->config(array());
  1041. $this->CakeEmail->viewVars(array('value' => 12345));
  1042. $this->CakeEmail->emailFormat('both');
  1043. $result = $this->CakeEmail->send();
  1044. $message = $this->CakeEmail->message();
  1045. $boundary = $this->CakeEmail->getBoundary();
  1046. $this->assertFalse(empty($boundary));
  1047. $this->assertContains('--' . $boundary, $message);
  1048. $this->assertContains('--' . $boundary . '--', $message);
  1049. $this->assertContains('--alt-' . $boundary, $message);
  1050. $this->assertContains('--alt-' . $boundary . '--', $message);
  1051. $this->CakeEmail->attachments(array('fake.php' => __FILE__));
  1052. $this->CakeEmail->send();
  1053. $message = $this->CakeEmail->message();
  1054. $boundary = $this->CakeEmail->getBoundary();
  1055. $this->assertFalse(empty($boundary));
  1056. $this->assertContains('--' . $boundary, $message);
  1057. $this->assertContains('--' . $boundary . '--', $message);
  1058. $this->assertContains('--alt-' . $boundary, $message);
  1059. $this->assertContains('--alt-' . $boundary . '--', $message);
  1060. }
  1061. /**
  1062. * testSendAttachment method
  1063. *
  1064. * @return void
  1065. */
  1066. public function testSendAttachment() {
  1067. $this->CakeEmail->reset();
  1068. $this->CakeEmail->transport('debug');
  1069. $this->CakeEmail->from('cake@cakephp.org');
  1070. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1071. $this->CakeEmail->subject('My title');
  1072. $this->CakeEmail->config(array());
  1073. $this->CakeEmail->attachments(array(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=\"basics.php\"", $result['message']);
  1076. $this->CakeEmail->attachments(array('my.file.txt' => CAKE . 'basics.php'));
  1077. $result = $this->CakeEmail->send('body');
  1078. $this->assertContains("Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"my.file.txt\"", $result['message']);
  1079. $this->CakeEmail->attachments(array('file.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
  1080. $result = $this->CakeEmail->send('body');
  1081. $this->assertContains("Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"file.txt\"", $result['message']);
  1082. $this->CakeEmail->attachments(array('file2.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain', 'contentId' => 'a1b1c1')));
  1083. $result = $this->CakeEmail->send('body');
  1084. $this->assertContains("Content-Type: text/plain\r\nContent-Transfer-Encoding: base64\r\nContent-ID: <a1b1c1>\r\nContent-Disposition: inline; filename=\"file2.txt\"", $result['message']);
  1085. }
  1086. /**
  1087. * testDeliver method
  1088. *
  1089. * @return void
  1090. */
  1091. public function testDeliver() {
  1092. $instance = CakeEmail::deliver('all@cakephp.org', 'About', 'Everything ok', array('from' => 'root@cakephp.org'), false);
  1093. $this->assertInstanceOf('CakeEmail', $instance);
  1094. $this->assertSame($instance->to(), array('all@cakephp.org' => 'all@cakephp.org'));
  1095. $this->assertSame($instance->subject(), 'About');
  1096. $this->assertSame($instance->from(), array('root@cakephp.org' => 'root@cakephp.org'));
  1097. $config = array(
  1098. 'from' => 'cake@cakephp.org',
  1099. 'to' => 'debug@cakephp.org',
  1100. 'subject' => 'Update ok',
  1101. 'template' => 'custom',
  1102. 'layout' => 'custom_layout',
  1103. 'viewVars' => array('value' => 123),
  1104. 'cc' => array('cake@cakephp.org' => 'Myself')
  1105. );
  1106. $instance = CakeEmail::deliver(null, null, array('name' => 'CakePHP'), $config, false);
  1107. $this->assertSame($instance->from(), array('cake@cakephp.org' => 'cake@cakephp.org'));
  1108. $this->assertSame($instance->to(), array('debug@cakephp.org' => 'debug@cakephp.org'));
  1109. $this->assertSame($instance->subject(), 'Update ok');
  1110. $this->assertSame($instance->template(), array('template' => 'custom', 'layout' => 'custom_layout'));
  1111. $this->assertSame($instance->viewVars(), array('value' => 123, 'name' => 'CakePHP'));
  1112. $this->assertSame($instance->cc(), array('cake@cakephp.org' => 'Myself'));
  1113. $configs = array('from' => 'root@cakephp.org', 'message' => 'Message from configs', 'transport' => 'Debug');
  1114. $instance = CakeEmail::deliver('all@cakephp.org', 'About', null, $configs, true);
  1115. $message = $instance->message();
  1116. $this->assertEquals($configs['message'], $message[0]);
  1117. }
  1118. /**
  1119. * testMessage method
  1120. *
  1121. * @return void
  1122. */
  1123. public function testMessage() {
  1124. $this->CakeEmail->reset();
  1125. $this->CakeEmail->transport('debug');
  1126. $this->CakeEmail->from('cake@cakephp.org');
  1127. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1128. $this->CakeEmail->subject('My title');
  1129. $this->CakeEmail->config(array('empty'));
  1130. $this->CakeEmail->template('default', 'default');
  1131. $this->CakeEmail->emailFormat('both');
  1132. $result = $this->CakeEmail->send();
  1133. $expected = '<p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>';
  1134. $this->assertContains($expected, $this->CakeEmail->message(CakeEmail::MESSAGE_HTML));
  1135. $expected = 'This email was sent using the CakePHP Framework, http://cakephp.org.';
  1136. $this->assertContains($expected, $this->CakeEmail->message(CakeEmail::MESSAGE_TEXT));
  1137. $message = $this->CakeEmail->message();
  1138. $this->assertContains('Content-Type: text/plain; charset=UTF-8', $message);
  1139. $this->assertContains('Content-Type: text/html; charset=UTF-8', $message);
  1140. // UTF-8 is 8bit
  1141. $this->assertTrue($this->_checkContentTransferEncoding($message, '8bit'));
  1142. $this->CakeEmail->charset = 'ISO-2022-JP';
  1143. $this->CakeEmail->send();
  1144. $message = $this->CakeEmail->message();
  1145. $this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $message);
  1146. $this->assertContains('Content-Type: text/html; charset=ISO-2022-JP', $message);
  1147. // ISO-2022-JP is 7bit
  1148. $this->assertTrue($this->_checkContentTransferEncoding($message, '7bit'));
  1149. }
  1150. /**
  1151. * testReset method
  1152. *
  1153. * @return void
  1154. */
  1155. public function testReset() {
  1156. $this->CakeEmail->to('cake@cakephp.org');
  1157. $this->CakeEmail->theme('TestTheme');
  1158. $this->assertSame($this->CakeEmail->to(), array('cake@cakephp.org' => 'cake@cakephp.org'));
  1159. $this->CakeEmail->reset();
  1160. $this->assertSame($this->CakeEmail->to(), array());
  1161. $this->assertSame(null, $this->CakeEmail->theme());
  1162. }
  1163. /**
  1164. * testReset with charset
  1165. *
  1166. * @return void
  1167. */
  1168. public function testResetWithCharset() {
  1169. $this->CakeEmail->charset = 'ISO-2022-JP';
  1170. $this->CakeEmail->reset();
  1171. $this->assertSame($this->CakeEmail->charset, 'utf-8', $this->CakeEmail->charset);
  1172. $this->assertSame($this->CakeEmail->headerCharset, null, $this->CakeEmail->headerCharset);
  1173. }
  1174. /**
  1175. * testWrap method
  1176. *
  1177. * @return void
  1178. */
  1179. public function testWrap() {
  1180. $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
  1181. $result = $this->CakeEmail->wrap($text);
  1182. $expected = array(
  1183. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
  1184. 'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
  1185. ''
  1186. );
  1187. $this->assertSame($expected, $result);
  1188. $text = 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan amet.';
  1189. $result = $this->CakeEmail->wrap($text);
  1190. $expected = array(
  1191. 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
  1192. 'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
  1193. 'amet.',
  1194. ''
  1195. );
  1196. $this->assertSame($expected, $result);
  1197. $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>';
  1198. $result = $this->CakeEmail->wrap($text);
  1199. $expected = array(
  1200. '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac',
  1201. 'turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula',
  1202. 'pellentesque accumsan amet.<hr></p>',
  1203. ''
  1204. );
  1205. $this->assertSame($expected, $result);
  1206. $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.';
  1207. $result = $this->CakeEmail->wrap($text);
  1208. $expected = array(
  1209. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac',
  1210. '<a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh',
  1211. 'nisi, vehicula pellentesque accumsan amet.',
  1212. ''
  1213. );
  1214. $this->assertSame($expected, $result);
  1215. $text = 'Lorem ipsum <a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">ok</a>';
  1216. $result = $this->CakeEmail->wrap($text);
  1217. $expected = array(
  1218. 'Lorem ipsum',
  1219. '<a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">',
  1220. 'ok</a>',
  1221. ''
  1222. );
  1223. $this->assertSame($expected, $result);
  1224. $text = 'Lorem ipsum withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite ok.';
  1225. $result = $this->CakeEmail->wrap($text);
  1226. $expected = array(
  1227. 'Lorem ipsum',
  1228. 'withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite',
  1229. 'ok.',
  1230. ''
  1231. );
  1232. $this->assertSame($expected, $result);
  1233. }
  1234. /**
  1235. * testConstructWithConfigArray method
  1236. *
  1237. * @return void
  1238. */
  1239. public function testConstructWithConfigArray() {
  1240. $configs = array(
  1241. 'from' => array('some@example.com' => 'My website'),
  1242. 'to' => 'test@example.com',
  1243. 'subject' => 'Test mail subject',
  1244. 'transport' => 'Debug',
  1245. );
  1246. $this->CakeEmail = new CakeEmail($configs);
  1247. $result = $this->CakeEmail->to();
  1248. $this->assertEquals(array($configs['to'] => $configs['to']), $result);
  1249. $result = $this->CakeEmail->from();
  1250. $this->assertEquals($configs['from'], $result);
  1251. $result = $this->CakeEmail->subject();
  1252. $this->assertEquals($c

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