PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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

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