EmailTest.php 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  1. <?php
  2. /**
  3. * CakeEmailTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 2.0.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Network\Email;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\Log\Log;
  22. use Cake\Network\Email\DebugTransport;
  23. use Cake\Network\Email\Email;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\File;
  26. /**
  27. * Help to test CakeEmail
  28. *
  29. */
  30. class TestEmail extends Email {
  31. /**
  32. * Wrap to protected method
  33. *
  34. */
  35. public function formatAddress($address) {
  36. return parent::_formatAddress($address);
  37. }
  38. /**
  39. * Wrap to protected method
  40. *
  41. */
  42. public function wrap($text, $length = Email::LINE_LENGTH_MUST) {
  43. return parent::_wrap($text, $length);
  44. }
  45. /**
  46. * Get the boundary attribute
  47. *
  48. * @return string
  49. */
  50. public function getBoundary() {
  51. return $this->_boundary;
  52. }
  53. /**
  54. * Encode to protected method
  55. *
  56. */
  57. public function encode($text) {
  58. return $this->_encode($text);
  59. }
  60. /**
  61. * Render to protected method
  62. *
  63. */
  64. public function render($content) {
  65. return $this->_render($content);
  66. }
  67. }
  68. /**
  69. * EmailTest class
  70. */
  71. class EmailTest extends TestCase {
  72. /**
  73. * setUp
  74. *
  75. * @return void
  76. */
  77. public function setUp() {
  78. parent::setUp();
  79. $this->CakeEmail = new TestEmail();
  80. Email::configTransport('debug', [
  81. 'className' => 'Debug'
  82. ]);
  83. }
  84. /**
  85. * tearDown method
  86. *
  87. * @return void
  88. */
  89. public function tearDown() {
  90. parent::tearDown();
  91. Log::drop('email');
  92. Email::drop('test');
  93. Email::dropTransport('debug');
  94. Email::dropTransport('test_smtp');
  95. }
  96. /**
  97. * testFrom method
  98. *
  99. * @return void
  100. */
  101. public function testFrom() {
  102. $this->assertSame($this->CakeEmail->from(), array());
  103. $this->CakeEmail->from('cake@cakephp.org');
  104. $expected = array('cake@cakephp.org' => 'cake@cakephp.org');
  105. $this->assertSame($this->CakeEmail->from(), $expected);
  106. $this->CakeEmail->from(array('cake@cakephp.org'));
  107. $this->assertSame($this->CakeEmail->from(), $expected);
  108. $this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
  109. $expected = array('cake@cakephp.org' => 'CakePHP');
  110. $this->assertSame($this->CakeEmail->from(), $expected);
  111. $result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP'));
  112. $this->assertSame($this->CakeEmail->from(), $expected);
  113. $this->assertSame($this->CakeEmail, $result);
  114. $this->setExpectedException('Cake\Error\SocketException');
  115. $result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address'));
  116. }
  117. /**
  118. * Test that from addresses using colons work.
  119. *
  120. * @return void
  121. */
  122. public function testFromWithColonsAndQuotes() {
  123. $address = array(
  124. 'info@example.com' => '70:20:00 " Forum'
  125. );
  126. $this->CakeEmail->from($address);
  127. $this->assertEquals($address, $this->CakeEmail->from());
  128. $this->CakeEmail->to('info@example.com')
  129. ->subject('Test email')
  130. ->transport('debug');
  131. $result = $this->CakeEmail->send();
  132. $this->assertContains('From: "70:20:00 \" Forum" <info@example.com>', $result['headers']);
  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->assertFalse($headers['From']);
  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. $this->setExpectedException('Cake\Error\SocketException');
  194. $this->CakeEmail->to(array('cake@localhost', 'CakePHP'));
  195. }
  196. /**
  197. * Data provider function for testBuildInvalidData
  198. *
  199. * @return array
  200. */
  201. public static function invalidEmails() {
  202. return array(
  203. array(1.0),
  204. array(''),
  205. array('string'),
  206. array('<tag>'),
  207. array('some@one-whereis'),
  208. array('wrong@key' => 'Name'),
  209. array(array('ok@cakephp.org', 1.0, '', 'string'))
  210. );
  211. }
  212. /**
  213. * testBuildInvalidData
  214. *
  215. * @dataProvider invalidEmails
  216. * @expectedException \Cake\Error\SocketException
  217. * @return void
  218. */
  219. public function testInvalidEmail($value) {
  220. $this->CakeEmail->to($value);
  221. }
  222. /**
  223. * testBuildInvalidData
  224. *
  225. * @dataProvider invalidEmails
  226. * @expectedException \Cake\Error\SocketException
  227. * @return void
  228. */
  229. public function testInvalidEmailAdd($value) {
  230. $this->CakeEmail->addTo($value);
  231. }
  232. /**
  233. * test emailPattern method
  234. *
  235. * @return void
  236. */
  237. public function testEmailPattern() {
  238. $regex = '/.+@.+\..+/i';
  239. $this->assertNull($this->CakeEmail->emailPattern());
  240. $this->assertSame($regex, $this->CakeEmail->emailPattern($regex)->emailPattern());
  241. }
  242. /**
  243. * Tests that it is possible to set email regex configuration to a CakeEmail object
  244. *
  245. * @return void
  246. */
  247. public function testConfigEmailPattern() {
  248. $regex = '/.+@.+\..+/i';
  249. $email = new Email(array('emailPattern' => $regex));
  250. $this->assertSame($regex, $email->emailPattern());
  251. }
  252. /**
  253. * Tests that it is possible set custom email validation
  254. */
  255. public function testCustomEmailValidation() {
  256. $regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}$/i';
  257. $this->CakeEmail->emailPattern($regex)->to('pass.@example.com');
  258. $this->assertSame(array(
  259. 'pass.@example.com' => 'pass.@example.com',
  260. ), $this->CakeEmail->to());
  261. $this->CakeEmail->addTo('pass..old.docomo@example.com');
  262. $this->assertSame(array(
  263. 'pass.@example.com' => 'pass.@example.com',
  264. 'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
  265. ), $this->CakeEmail->to());
  266. $this->CakeEmail->reset();
  267. $emails = array(
  268. 'pass.@example.com',
  269. 'pass..old.docomo@example.com'
  270. );
  271. $additionalEmails = array(
  272. '.extend.@example.com',
  273. '.docomo@example.com'
  274. );
  275. $this->CakeEmail->emailPattern($regex)->to($emails);
  276. $this->assertSame(array(
  277. 'pass.@example.com' => 'pass.@example.com',
  278. 'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
  279. ), $this->CakeEmail->to());
  280. $this->CakeEmail->addTo($additionalEmails);
  281. $this->assertSame(array(
  282. 'pass.@example.com' => 'pass.@example.com',
  283. 'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
  284. '.extend.@example.com' => '.extend.@example.com',
  285. '.docomo@example.com' => '.docomo@example.com',
  286. ), $this->CakeEmail->to());
  287. }
  288. /**
  289. * testFormatAddress method
  290. *
  291. * @return void
  292. */
  293. public function testFormatAddress() {
  294. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org'));
  295. $expected = array('cake@cakephp.org');
  296. $this->assertSame($expected, $result);
  297. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'cake@cakephp.org', 'php@cakephp.org' => 'php@cakephp.org'));
  298. $expected = array('cake@cakephp.org', 'php@cakephp.org');
  299. $this->assertSame($expected, $result);
  300. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'CakePHP', 'php@cakephp.org' => 'Cake'));
  301. $expected = array('CakePHP <cake@cakephp.org>', 'Cake <php@cakephp.org>');
  302. $this->assertSame($expected, $result);
  303. $result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last, First'));
  304. $expected = array('"Last, First" <me@example.com>');
  305. $this->assertSame($expected, $result);
  306. $result = $this->CakeEmail->formatAddress(array('me@example.com' => '"Last" First'));
  307. $expected = array('"\"Last\" First" <me@example.com>');
  308. $this->assertSame($expected, $result);
  309. $result = $this->CakeEmail->formatAddress(array('me@example.com' => 'Last First'));
  310. $expected = array('Last First <me@example.com>');
  311. $this->assertSame($expected, $result);
  312. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => 'ÄÖÜTest'));
  313. $expected = array('=?UTF-8?B?w4TDlsOcVGVzdA==?= <cake@cakephp.org>');
  314. $this->assertSame($expected, $result);
  315. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '日本語Test'));
  316. $expected = array('=?UTF-8?B?5pel5pys6KqeVGVzdA==?= <cake@cakephp.org>');
  317. $this->assertSame($expected, $result);
  318. }
  319. /**
  320. * testFormatAddressJapanese
  321. *
  322. * @return void
  323. */
  324. public function testFormatAddressJapanese() {
  325. $this->CakeEmail->headerCharset = 'ISO-2022-JP';
  326. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '日本語Test'));
  327. $expected = array('=?ISO-2022-JP?B?GyRCRnxLXDhsGyhCVGVzdA==?= <cake@cakephp.org>');
  328. $this->assertSame($expected, $result);
  329. $result = $this->CakeEmail->formatAddress(array('cake@cakephp.org' => '寿限無寿限無五劫の擦り切れ海砂利水魚の水行末雲来末風来末食う寝る処に住む処やぶら小路の藪柑子パイポパイポパイポのシューリンガンシューリンガンのグーリンダイグーリンダイのポンポコピーのポンポコナーの長久命の長助'));
  330. $expected = array("=?ISO-2022-JP?B?GyRCPHc4Qkw1PHc4Qkw1OF45ZSROOyQkakBaJGwzJDo9TXg/ZTV7GyhC?=\r\n" .
  331. " =?ISO-2022-JP?B?GyRCJE4/ZTlUS3YxQE1oS3ZJd01oS3Y/KSQmPzIkaz1oJEs9OyRgGyhC?=\r\n" .
  332. " =?ISO-2022-JP?B?GyRCPWgkZCRWJGk+Lk8pJE5pLjQ7O1IlUSUkJV0lUSUkJV0lUSUkGyhC?=\r\n" .
  333. " =?ISO-2022-JP?B?GyRCJV0kTiU3JWUhPCVqJXMlLCVzJTclZSE8JWolcyUsJXMkTiUwGyhC?=\r\n" .
  334. " =?ISO-2022-JP?B?GyRCITwlaiVzJUAlJCUwITwlaiVzJUAlJCROJV0lcyVdJTMlVCE8GyhC?=\r\n" .
  335. " =?ISO-2022-JP?B?GyRCJE4lXSVzJV0lMyVKITwkTkQ5NVdMPyRORDk9dRsoQg==?= <cake@cakephp.org>");
  336. $this->assertSame($expected, $result);
  337. }
  338. /**
  339. * testAddresses method
  340. *
  341. * @return void
  342. */
  343. public function testAddresses() {
  344. $this->CakeEmail->reset();
  345. $this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
  346. $this->CakeEmail->replyTo('replyto@cakephp.org', 'ReplyTo CakePHP');
  347. $this->CakeEmail->readReceipt('readreceipt@cakephp.org', 'ReadReceipt CakePHP');
  348. $this->CakeEmail->returnPath('returnpath@cakephp.org', 'ReturnPath CakePHP');
  349. $this->CakeEmail->to('to@cakephp.org', 'To, CakePHP');
  350. $this->CakeEmail->cc('cc@cakephp.org', 'Cc CakePHP');
  351. $this->CakeEmail->bcc('bcc@cakephp.org', 'Bcc CakePHP');
  352. $this->CakeEmail->addTo('to2@cakephp.org', 'To2 CakePHP');
  353. $this->CakeEmail->addCc('cc2@cakephp.org', 'Cc2 CakePHP');
  354. $this->CakeEmail->addBcc('bcc2@cakephp.org', 'Bcc2 CakePHP');
  355. $this->assertSame($this->CakeEmail->from(), array('cake@cakephp.org' => 'CakePHP'));
  356. $this->assertSame($this->CakeEmail->replyTo(), array('replyto@cakephp.org' => 'ReplyTo CakePHP'));
  357. $this->assertSame($this->CakeEmail->readReceipt(), array('readreceipt@cakephp.org' => 'ReadReceipt CakePHP'));
  358. $this->assertSame($this->CakeEmail->returnPath(), array('returnpath@cakephp.org' => 'ReturnPath CakePHP'));
  359. $this->assertSame($this->CakeEmail->to(), array('to@cakephp.org' => 'To, CakePHP', 'to2@cakephp.org' => 'To2 CakePHP'));
  360. $this->assertSame($this->CakeEmail->cc(), array('cc@cakephp.org' => 'Cc CakePHP', 'cc2@cakephp.org' => 'Cc2 CakePHP'));
  361. $this->assertSame($this->CakeEmail->bcc(), array('bcc@cakephp.org' => 'Bcc CakePHP', 'bcc2@cakephp.org' => 'Bcc2 CakePHP'));
  362. $headers = $this->CakeEmail->getHeaders(array_fill_keys(array('from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'), true));
  363. $this->assertSame($headers['From'], 'CakePHP <cake@cakephp.org>');
  364. $this->assertSame($headers['Reply-To'], 'ReplyTo CakePHP <replyto@cakephp.org>');
  365. $this->assertSame($headers['Disposition-Notification-To'], 'ReadReceipt CakePHP <readreceipt@cakephp.org>');
  366. $this->assertSame($headers['Return-Path'], 'ReturnPath CakePHP <returnpath@cakephp.org>');
  367. $this->assertSame($headers['To'], '"To, CakePHP" <to@cakephp.org>, To2 CakePHP <to2@cakephp.org>');
  368. $this->assertSame($headers['Cc'], 'Cc CakePHP <cc@cakephp.org>, Cc2 CakePHP <cc2@cakephp.org>');
  369. $this->assertSame($headers['Bcc'], 'Bcc CakePHP <bcc@cakephp.org>, Bcc2 CakePHP <bcc2@cakephp.org>');
  370. }
  371. /**
  372. * testMessageId method
  373. *
  374. * @return void
  375. */
  376. public function testMessageId() {
  377. $this->CakeEmail->messageId(true);
  378. $result = $this->CakeEmail->getHeaders();
  379. $this->assertTrue(isset($result['Message-ID']));
  380. $this->CakeEmail->messageId(false);
  381. $result = $this->CakeEmail->getHeaders();
  382. $this->assertFalse(isset($result['Message-ID']));
  383. $result = $this->CakeEmail->messageId('<my-email@localhost>');
  384. $this->assertSame($this->CakeEmail, $result);
  385. $result = $this->CakeEmail->getHeaders();
  386. $this->assertSame($result['Message-ID'], '<my-email@localhost>');
  387. $result = $this->CakeEmail->messageId();
  388. $this->assertSame($result, '<my-email@localhost>');
  389. }
  390. /**
  391. * testMessageIdInvalid method
  392. *
  393. * @return void
  394. * @expectedException \Cake\Error\SocketException
  395. */
  396. public function testMessageIdInvalid() {
  397. $this->CakeEmail->messageId('my-email@localhost');
  398. }
  399. /**
  400. * testDomain method
  401. *
  402. * @return void
  403. */
  404. public function testDomain() {
  405. $result = $this->CakeEmail->domain();
  406. $expected = env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n');
  407. $this->assertSame($expected, $result);
  408. $this->CakeEmail->domain('example.org');
  409. $result = $this->CakeEmail->domain();
  410. $expected = 'example.org';
  411. $this->assertSame($expected, $result);
  412. }
  413. /**
  414. * testMessageIdWithDomain method
  415. *
  416. * @return void
  417. */
  418. public function testMessageIdWithDomain() {
  419. $this->CakeEmail->domain('example.org');
  420. $result = $this->CakeEmail->getHeaders();
  421. $expected = '@example.org>';
  422. $this->assertTextContains($expected, $result['Message-ID']);
  423. $_SERVER['HTTP_HOST'] = 'example.org';
  424. $result = $this->CakeEmail->getHeaders();
  425. $this->assertTextContains('example.org', $result['Message-ID']);
  426. $_SERVER['HTTP_HOST'] = 'example.org:81';
  427. $result = $this->CakeEmail->getHeaders();
  428. $this->assertTextNotContains(':81', $result['Message-ID']);
  429. }
  430. /**
  431. * testSubject method
  432. *
  433. * @return void
  434. */
  435. public function testSubject() {
  436. $this->CakeEmail->subject('You have a new message.');
  437. $this->assertSame($this->CakeEmail->subject(), 'You have a new message.');
  438. $this->CakeEmail->subject('You have a new message, I think.');
  439. $this->assertSame($this->CakeEmail->subject(), 'You have a new message, I think.');
  440. $this->CakeEmail->subject(1);
  441. $this->assertSame($this->CakeEmail->subject(), '1');
  442. $this->CakeEmail->subject('هذه رسالة بعنوان طويل مرسل للمستلم');
  443. $expected = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
  444. $this->assertSame($this->CakeEmail->subject(), $expected);
  445. }
  446. /**
  447. * testSubjectJapanese
  448. *
  449. * @return void
  450. */
  451. public function testSubjectJapanese() {
  452. mb_internal_encoding('UTF-8');
  453. $this->CakeEmail->headerCharset = 'ISO-2022-JP';
  454. $this->CakeEmail->subject('日本語のSubjectにも対応するよ');
  455. $expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsJE4bKEJTdWJqZWN0GyRCJEskYkJQMX4kOSRrJGgbKEI=?=';
  456. $this->assertSame($this->CakeEmail->subject(), $expected);
  457. $this->CakeEmail->subject('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?');
  458. $expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
  459. " =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
  460. " =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
  461. $this->assertSame($this->CakeEmail->subject(), $expected);
  462. }
  463. /**
  464. * testHeaders method
  465. *
  466. * @return void
  467. */
  468. public function testHeaders() {
  469. $this->CakeEmail->messageId(false);
  470. $this->CakeEmail->setHeaders(array('X-Something' => 'nice'));
  471. $expected = array(
  472. 'X-Something' => 'nice',
  473. 'X-Mailer' => 'CakePHP Email',
  474. 'Date' => date(DATE_RFC2822),
  475. 'MIME-Version' => '1.0',
  476. 'Content-Type' => 'text/plain; charset=UTF-8',
  477. 'Content-Transfer-Encoding' => '8bit'
  478. );
  479. $this->assertSame($this->CakeEmail->getHeaders(), $expected);
  480. $this->CakeEmail->addHeaders(array('X-Something' => 'very nice', 'X-Other' => 'cool'));
  481. $expected = array(
  482. 'X-Something' => 'very nice',
  483. 'X-Other' => 'cool',
  484. 'X-Mailer' => 'CakePHP Email',
  485. 'Date' => date(DATE_RFC2822),
  486. 'MIME-Version' => '1.0',
  487. 'Content-Type' => 'text/plain; charset=UTF-8',
  488. 'Content-Transfer-Encoding' => '8bit'
  489. );
  490. $this->assertSame($this->CakeEmail->getHeaders(), $expected);
  491. $this->CakeEmail->from('cake@cakephp.org');
  492. $this->assertSame($this->CakeEmail->getHeaders(), $expected);
  493. $expected = array(
  494. 'From' => 'cake@cakephp.org',
  495. 'X-Something' => 'very nice',
  496. 'X-Other' => 'cool',
  497. 'X-Mailer' => 'CakePHP Email',
  498. 'Date' => date(DATE_RFC2822),
  499. 'MIME-Version' => '1.0',
  500. 'Content-Type' => 'text/plain; charset=UTF-8',
  501. 'Content-Transfer-Encoding' => '8bit'
  502. );
  503. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true)), $expected);
  504. $this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
  505. $expected['From'] = 'CakePHP <cake@cakephp.org>';
  506. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true)), $expected);
  507. $this->CakeEmail->to(array('cake@cakephp.org', 'php@cakephp.org' => 'CakePHP'));
  508. $expected = array(
  509. 'From' => 'CakePHP <cake@cakephp.org>',
  510. 'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
  511. 'X-Something' => 'very nice',
  512. 'X-Other' => 'cool',
  513. 'X-Mailer' => 'CakePHP Email',
  514. 'Date' => date(DATE_RFC2822),
  515. 'MIME-Version' => '1.0',
  516. 'Content-Type' => 'text/plain; charset=UTF-8',
  517. 'Content-Transfer-Encoding' => '8bit'
  518. );
  519. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
  520. $this->CakeEmail->charset = 'ISO-2022-JP';
  521. $expected = array(
  522. 'From' => 'CakePHP <cake@cakephp.org>',
  523. 'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
  524. 'X-Something' => 'very nice',
  525. 'X-Other' => 'cool',
  526. 'X-Mailer' => 'CakePHP Email',
  527. 'Date' => date(DATE_RFC2822),
  528. 'MIME-Version' => '1.0',
  529. 'Content-Type' => 'text/plain; charset=ISO-2022-JP',
  530. 'Content-Transfer-Encoding' => '7bit'
  531. );
  532. $this->assertSame($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
  533. $result = $this->CakeEmail->setHeaders(array());
  534. $this->assertInstanceOf('Cake\Network\Email\Email', $result);
  535. }
  536. /**
  537. * Data provider function for testInvalidHeaders
  538. *
  539. * @return array
  540. */
  541. public static function invalidHeaders() {
  542. return array(
  543. array(10),
  544. array(''),
  545. array('string'),
  546. array(false),
  547. array(null)
  548. );
  549. }
  550. /**
  551. * testInvalidHeaders
  552. *
  553. * @dataProvider invalidHeaders
  554. * @expectedException \Cake\Error\SocketException
  555. * @return void
  556. */
  557. public function testInvalidHeaders($value) {
  558. $this->CakeEmail->setHeaders($value);
  559. }
  560. /**
  561. * testInvalidAddHeaders
  562. *
  563. * @dataProvider invalidHeaders
  564. * @expectedException \Cake\Error\SocketException
  565. * @return void
  566. */
  567. public function testInvalidAddHeaders($value) {
  568. $this->CakeEmail->addHeaders($value);
  569. }
  570. /**
  571. * testTemplate method
  572. *
  573. * @return void
  574. */
  575. public function testTemplate() {
  576. $this->CakeEmail->template('template', 'layout');
  577. $expected = array('template' => 'template', 'layout' => 'layout');
  578. $this->assertSame($this->CakeEmail->template(), $expected);
  579. $this->CakeEmail->template('new_template');
  580. $expected = array('template' => 'new_template', 'layout' => 'layout');
  581. $this->assertSame($this->CakeEmail->template(), $expected);
  582. $this->CakeEmail->template('template', null);
  583. $expected = array('template' => 'template', 'layout' => null);
  584. $this->assertSame($this->CakeEmail->template(), $expected);
  585. $this->CakeEmail->template(null, null);
  586. $expected = array('template' => null, 'layout' => null);
  587. $this->assertSame($this->CakeEmail->template(), $expected);
  588. }
  589. /**
  590. * testTheme method
  591. *
  592. * @return void
  593. */
  594. public function testTheme() {
  595. $this->assertSame(null, $this->CakeEmail->theme());
  596. $this->CakeEmail->theme('default');
  597. $expected = 'default';
  598. $this->assertSame($expected, $this->CakeEmail->theme());
  599. }
  600. /**
  601. * testViewVars method
  602. *
  603. * @return void
  604. */
  605. public function testViewVars() {
  606. $this->assertSame($this->CakeEmail->viewVars(), array());
  607. $this->CakeEmail->viewVars(array('value' => 12345));
  608. $this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345));
  609. $this->CakeEmail->viewVars(array('name' => 'CakePHP'));
  610. $this->assertSame($this->CakeEmail->viewVars(), array('value' => 12345, 'name' => 'CakePHP'));
  611. $this->CakeEmail->viewVars(array('value' => 4567));
  612. $this->assertSame($this->CakeEmail->viewVars(), array('value' => 4567, 'name' => 'CakePHP'));
  613. }
  614. /**
  615. * testAttachments method
  616. *
  617. * @return void
  618. */
  619. public function testAttachments() {
  620. $this->CakeEmail->attachments(CAKE . 'basics.php');
  621. $expected = array(
  622. 'basics.php' => array(
  623. 'file' => CAKE . 'basics.php',
  624. 'mimetype' => 'application/octet-stream'
  625. )
  626. );
  627. $this->assertSame($this->CakeEmail->attachments(), $expected);
  628. $this->CakeEmail->attachments(array());
  629. $this->assertSame($this->CakeEmail->attachments(), array());
  630. $this->CakeEmail->attachments(array(
  631. array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')
  632. ));
  633. $this->CakeEmail->addAttachments(CAKE . 'bootstrap.php');
  634. $this->CakeEmail->addAttachments(array(CAKE . 'bootstrap.php'));
  635. $this->CakeEmail->addAttachments(array(
  636. 'other.txt' => CAKE . 'bootstrap.php',
  637. 'license' => CORE_PATH . 'LICENSE.txt'
  638. ));
  639. $expected = array(
  640. 'basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'),
  641. 'bootstrap.php' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
  642. 'other.txt' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
  643. 'license' => array('file' => CORE_PATH . 'LICENSE.txt', 'mimetype' => 'application/octet-stream')
  644. );
  645. $this->assertSame($this->CakeEmail->attachments(), $expected);
  646. $this->setExpectedException('Cake\Error\SocketException');
  647. $this->CakeEmail->attachments(array(array('nofile' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
  648. }
  649. /**
  650. * testTransport method
  651. *
  652. * @return void
  653. */
  654. public function testTransport() {
  655. $result = $this->CakeEmail->transport('debug');
  656. $this->assertSame($this->CakeEmail, $result);
  657. $result = $this->CakeEmail->transport();
  658. $this->assertInstanceOf('Cake\Network\Email\DebugTransport', $result);
  659. $instance = $this->getMock('Cake\Network\Email\DebugTransport');
  660. $this->CakeEmail->transport($instance);
  661. $this->assertSame($instance, $this->CakeEmail->transport());
  662. }
  663. /**
  664. * Test that using unknown transports fails.
  665. *
  666. * @expectedException \Cake\Error\Exception
  667. */
  668. public function testTransportInvalid() {
  669. $this->CakeEmail->transport('Invalid');
  670. }
  671. /**
  672. * Test that using classes with no send method fails.
  673. *
  674. * @expectedException \Cake\Error\Exception
  675. */
  676. public function testTransportInstanceInvalid() {
  677. $this->CakeEmail->transport(new \StdClass());
  678. }
  679. /**
  680. * Test configuring a transport.
  681. *
  682. * @return void
  683. */
  684. public function testConfigTransport() {
  685. Email::dropTransport('debug');
  686. $settings = [
  687. 'className' => 'Debug',
  688. 'log' => true
  689. ];
  690. $result = Email::configTransport('debug', $settings);
  691. $this->assertNull($result, 'No return.');
  692. $result = Email::configTransport('debug');
  693. $this->assertEquals($settings, $result);
  694. }
  695. /**
  696. * Test configuring multiple transports.
  697. */
  698. public function testConfigTransportMultiple() {
  699. Email::dropTransport('debug');
  700. $settings = [
  701. 'debug' => [
  702. 'className' => 'Debug',
  703. 'log' => true
  704. ],
  705. 'test_smtp' => [
  706. 'className' => 'Smtp',
  707. 'username' => 'mark',
  708. 'password' => 'password',
  709. 'host' => 'example.com'
  710. ]
  711. ];
  712. Email::configTransport($settings);
  713. $this->assertEquals($settings['debug'], Email::configTransport('debug'));
  714. $this->assertEquals($settings['test_smtp'], Email::configTransport('test_smtp'));
  715. }
  716. /**
  717. * Test that exceptions are raised when duplicate transports are configured.
  718. *
  719. * @expectedException \Cake\Error\Exception
  720. */
  721. public function testConfigTransportErrorOnDuplicate() {
  722. Email::dropTransport('debug');
  723. $settings = [
  724. 'className' => 'Debug',
  725. 'log' => true
  726. ];
  727. Email::configTransport('debug', $settings);
  728. Email::configTransport('debug', $settings);
  729. }
  730. /**
  731. * Test configTransport with an instance.
  732. *
  733. * @return void
  734. */
  735. public function testConfigTransportInstance() {
  736. Email::dropTransport('debug');
  737. $instance = new DebugTransport();
  738. Email::configTransport('debug', $instance);
  739. $this->assertEquals(['className' => $instance], Email::configTransport('debug'));
  740. }
  741. /**
  742. * Test dropping a transport configuration
  743. *
  744. * @return void
  745. */
  746. public function testDropTransport() {
  747. $result = Email::configTransport('debug');
  748. $this->assertInternalType('array', $result, 'Should have config data');
  749. Email::dropTransport('debug');
  750. $this->assertNull(Email::configTransport('debug'), 'Should not exist.');
  751. }
  752. /**
  753. * Test reading/writing configuration profiles.
  754. *
  755. * @return void
  756. */
  757. public function testConfig() {
  758. $settings = [
  759. 'to' => 'mark@example.com',
  760. 'from' => 'noreply@example.com',
  761. ];
  762. Email::config('test', $settings);
  763. $this->assertEquals($settings, Email::config('test'), 'Should be the same.');
  764. $email = new Email('test');
  765. $this->assertContains($settings['to'], $email->to());
  766. }
  767. /**
  768. * Test that exceptions are raised on duplicate config set.
  769. *
  770. * @expectedException \Cake\Error\Exception
  771. * @return void
  772. */
  773. public function testConfigErrorOnDuplicate() {
  774. $settings = [
  775. 'to' => 'mark@example.com',
  776. 'from' => 'noreply@example.com',
  777. ];
  778. Email::config('test', $settings);
  779. Email::config('test', $settings);
  780. }
  781. /**
  782. * test profile method
  783. *
  784. * @return void
  785. */
  786. public function testProfile() {
  787. $config = array('test' => 'ok', 'test2' => true);
  788. $this->CakeEmail->profile($config);
  789. $this->assertSame($this->CakeEmail->profile(), $config);
  790. $config = array('test' => 'test@example.com');
  791. $this->CakeEmail->profile($config);
  792. $expected = array('test' => 'test@example.com', 'test2' => true);
  793. $this->assertSame($expected, $this->CakeEmail->profile());
  794. }
  795. /**
  796. * Test that using an invalid profile fails.
  797. *
  798. * @expectedException \Cake\Error\Exception
  799. * @expectedExceptionMessage Unknown email configuration "derp".
  800. */
  801. public function testProfileInvalid() {
  802. $email = new Email();
  803. $email->profile('derp');
  804. }
  805. /**
  806. * testConfigString method
  807. *
  808. * @return void
  809. */
  810. public function testUseConfigString() {
  811. $config = [
  812. 'from' => array('some@example.com' => 'My website'),
  813. 'to' => array('test@example.com' => 'Testname'),
  814. 'subject' => 'Test mail subject',
  815. 'transport' => 'debug',
  816. 'theme' => 'TestTheme',
  817. 'helpers' => array('Html', 'Form'),
  818. ];
  819. Email::config('test', $config);
  820. $this->CakeEmail->profile('test');
  821. $result = $this->CakeEmail->to();
  822. $this->assertEquals($config['to'], $result);
  823. $result = $this->CakeEmail->from();
  824. $this->assertEquals($config['from'], $result);
  825. $result = $this->CakeEmail->subject();
  826. $this->assertEquals($config['subject'], $result);
  827. $result = $this->CakeEmail->theme();
  828. $this->assertEquals($config['theme'], $result);
  829. $result = $this->CakeEmail->transport();
  830. $this->assertInstanceOf('Cake\Network\Email\DebugTransport', $result);
  831. $result = $this->CakeEmail->helpers();
  832. $this->assertEquals($config['helpers'], $result);
  833. }
  834. /**
  835. * Calling send() with no parameters should not overwrite the view variables.
  836. *
  837. * @return void
  838. */
  839. public function testSendWithNoContentDoesNotOverwriteViewVar() {
  840. $this->CakeEmail->reset();
  841. $this->CakeEmail->transport('debug');
  842. $this->CakeEmail->from('cake@cakephp.org');
  843. $this->CakeEmail->to('you@cakephp.org');
  844. $this->CakeEmail->subject('My title');
  845. $this->CakeEmail->emailFormat('text');
  846. $this->CakeEmail->template('default');
  847. $this->CakeEmail->viewVars(array(
  848. 'content' => 'A message to you',
  849. ));
  850. $result = $this->CakeEmail->send();
  851. $this->assertContains('A message to you', $result['message']);
  852. }
  853. /**
  854. * testSendWithContent method
  855. *
  856. * @return void
  857. */
  858. public function testSendWithContent() {
  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->profile(array('empty'));
  865. $result = $this->CakeEmail->send("Here is my body, with multi lines.\nThis is the second line.\r\n\r\nAnd the last.");
  866. $expected = array('headers', 'message');
  867. $this->assertEquals($expected, array_keys($result));
  868. $expected = "Here is my body, with multi lines.\r\nThis is the second line.\r\n\r\nAnd the last.\r\n\r\n";
  869. $this->assertEquals($expected, $result['message']);
  870. $this->assertTrue((bool)strpos($result['headers'], 'Date: '));
  871. $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
  872. $this->assertTrue((bool)strpos($result['headers'], 'To: '));
  873. $result = $this->CakeEmail->send("Other body");
  874. $expected = "Other body\r\n\r\n";
  875. $this->assertSame($result['message'], $expected);
  876. $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
  877. $this->assertTrue((bool)strpos($result['headers'], 'To: '));
  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->profile(array('empty'));
  884. $result = $this->CakeEmail->send(array('Sending content', 'As array'));
  885. $expected = "Sending content\r\nAs array\r\n\r\n\r\n";
  886. $this->assertSame($result['message'], $expected);
  887. }
  888. /**
  889. * testSendWithoutFrom method
  890. *
  891. * @return void
  892. */
  893. public function testSendWithoutFrom() {
  894. $this->CakeEmail->transport('debug');
  895. $this->CakeEmail->to('cake@cakephp.org');
  896. $this->CakeEmail->subject('My title');
  897. $this->CakeEmail->profile(array('empty'));
  898. $this->setExpectedException('Cake\Error\SocketException');
  899. $this->CakeEmail->send("Forgot to set From");
  900. }
  901. /**
  902. * testSendWithoutTo method
  903. *
  904. * @return void
  905. */
  906. public function testSendWithoutTo() {
  907. $this->CakeEmail->transport('debug');
  908. $this->CakeEmail->from('cake@cakephp.org');
  909. $this->CakeEmail->subject('My title');
  910. $this->CakeEmail->profile(array('empty'));
  911. $this->setExpectedException('Cake\Error\SocketException');
  912. $this->CakeEmail->send("Forgot to set To");
  913. }
  914. /**
  915. * Test send() with no template.
  916. *
  917. * @return void
  918. */
  919. public function testSendNoTemplateWithAttachments() {
  920. $this->CakeEmail->transport('debug');
  921. $this->CakeEmail->from('cake@cakephp.org');
  922. $this->CakeEmail->to('cake@cakephp.org');
  923. $this->CakeEmail->subject('My title');
  924. $this->CakeEmail->emailFormat('text');
  925. $this->CakeEmail->attachments(array(CAKE . 'basics.php'));
  926. $result = $this->CakeEmail->send('Hello');
  927. $boundary = $this->CakeEmail->getBoundary();
  928. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  929. $expected = "--$boundary\r\n" .
  930. "Content-Type: text/plain; charset=UTF-8\r\n" .
  931. "Content-Transfer-Encoding: 8bit\r\n" .
  932. "\r\n" .
  933. "Hello" .
  934. "\r\n" .
  935. "\r\n" .
  936. "\r\n" .
  937. "--$boundary\r\n" .
  938. "Content-Disposition: attachment; filename=\"basics.php\"\r\n" .
  939. "Content-Type: application/octet-stream\r\n" .
  940. "Content-Transfer-Encoding: base64\r\n" .
  941. "\r\n";
  942. $this->assertContains($expected, $result['message']);
  943. }
  944. /**
  945. * Test send() with no template and data string attachment
  946. *
  947. * @return void
  948. */
  949. public function testSendNoTemplateWithDataStringAttachment() {
  950. $this->CakeEmail->transport('debug');
  951. $this->CakeEmail->from('cake@cakephp.org');
  952. $this->CakeEmail->to('cake@cakephp.org');
  953. $this->CakeEmail->subject('My title');
  954. $this->CakeEmail->emailFormat('text');
  955. $data = file_get_contents(TEST_APP . 'webroot/img/cake.power.gif');
  956. $this->CakeEmail->attachments(array('cake.icon.gif' => array(
  957. 'data' => $data,
  958. 'mimetype' => 'image/gif'
  959. )));
  960. $result = $this->CakeEmail->send('Hello');
  961. $boundary = $this->CakeEmail->getBoundary();
  962. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  963. $expected = "--$boundary\r\n" .
  964. "Content-Type: text/plain; charset=UTF-8\r\n" .
  965. "Content-Transfer-Encoding: 8bit\r\n" .
  966. "\r\n" .
  967. "Hello" .
  968. "\r\n" .
  969. "\r\n" .
  970. "\r\n" .
  971. "--$boundary\r\n" .
  972. "Content-Disposition: attachment; filename=\"cake.icon.gif\"\r\n" .
  973. "Content-Type: image/gif\r\n" .
  974. "Content-Transfer-Encoding: base64\r\n\r\n";
  975. $expected .= chunk_split(base64_encode($data), 76, "\r\n");
  976. $this->assertContains($expected, $result['message']);
  977. }
  978. /**
  979. * Test send() with no template as both
  980. *
  981. * @return void
  982. */
  983. public function testSendNoTemplateWithAttachmentsAsBoth() {
  984. $this->CakeEmail->transport('debug');
  985. $this->CakeEmail->from('cake@cakephp.org');
  986. $this->CakeEmail->to('cake@cakephp.org');
  987. $this->CakeEmail->subject('My title');
  988. $this->CakeEmail->emailFormat('both');
  989. $this->CakeEmail->attachments(array(CORE_PATH . 'VERSION.txt'));
  990. $result = $this->CakeEmail->send('Hello');
  991. $boundary = $this->CakeEmail->getBoundary();
  992. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  993. $expected = "--$boundary\r\n" .
  994. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  995. "\r\n" .
  996. "--alt-$boundary\r\n" .
  997. "Content-Type: text/plain; charset=UTF-8\r\n" .
  998. "Content-Transfer-Encoding: 8bit\r\n" .
  999. "\r\n" .
  1000. "Hello" .
  1001. "\r\n" .
  1002. "\r\n" .
  1003. "\r\n" .
  1004. "--alt-$boundary\r\n" .
  1005. "Content-Type: text/html; charset=UTF-8\r\n" .
  1006. "Content-Transfer-Encoding: 8bit\r\n" .
  1007. "\r\n" .
  1008. "Hello" .
  1009. "\r\n" .
  1010. "\r\n" .
  1011. "\r\n" .
  1012. "--alt-{$boundary}--\r\n" .
  1013. "\r\n" .
  1014. "--$boundary\r\n" .
  1015. "Content-Disposition: attachment; filename=\"VERSION.txt\"\r\n" .
  1016. "Content-Type: application/octet-stream\r\n" .
  1017. "Content-Transfer-Encoding: base64\r\n" .
  1018. "\r\n";
  1019. $this->assertContains($expected, $result['message']);
  1020. }
  1021. /**
  1022. * Test setting inline attachments and messages.
  1023. *
  1024. * @return void
  1025. */
  1026. public function testSendWithInlineAttachments() {
  1027. $this->CakeEmail->transport('debug');
  1028. $this->CakeEmail->from('cake@cakephp.org');
  1029. $this->CakeEmail->to('cake@cakephp.org');
  1030. $this->CakeEmail->subject('My title');
  1031. $this->CakeEmail->emailFormat('both');
  1032. $this->CakeEmail->attachments(array(
  1033. 'cake.png' => array(
  1034. 'file' => CORE_PATH . 'VERSION.txt',
  1035. 'contentId' => 'abc123'
  1036. )
  1037. ));
  1038. $result = $this->CakeEmail->send('Hello');
  1039. $boundary = $this->CakeEmail->getBoundary();
  1040. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1041. $expected = "--$boundary\r\n" .
  1042. "Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
  1043. "\r\n" .
  1044. "--rel-$boundary\r\n" .
  1045. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  1046. "\r\n" .
  1047. "--alt-$boundary\r\n" .
  1048. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1049. "Content-Transfer-Encoding: 8bit\r\n" .
  1050. "\r\n" .
  1051. "Hello" .
  1052. "\r\n" .
  1053. "\r\n" .
  1054. "\r\n" .
  1055. "--alt-$boundary\r\n" .
  1056. "Content-Type: text/html; charset=UTF-8\r\n" .
  1057. "Content-Transfer-Encoding: 8bit\r\n" .
  1058. "\r\n" .
  1059. "Hello" .
  1060. "\r\n" .
  1061. "\r\n" .
  1062. "\r\n" .
  1063. "--alt-{$boundary}--\r\n" .
  1064. "\r\n" .
  1065. "--rel-$boundary\r\n" .
  1066. "Content-Disposition: inline; filename=\"cake.png\"\r\n" .
  1067. "Content-Type: application/octet-stream\r\n" .
  1068. "Content-Transfer-Encoding: base64\r\n" .
  1069. "Content-ID: <abc123>\r\n" .
  1070. "\r\n";
  1071. $this->assertContains($expected, $result['message']);
  1072. $this->assertContains('--rel-' . $boundary . '--', $result['message']);
  1073. $this->assertContains('--' . $boundary . '--', $result['message']);
  1074. }
  1075. /**
  1076. * Test disabling content-disposition.
  1077. *
  1078. * @return void
  1079. */
  1080. public function testSendWithNoContentDispositionAttachments() {
  1081. $this->CakeEmail->transport('debug');
  1082. $this->CakeEmail->from('cake@cakephp.org');
  1083. $this->CakeEmail->to('cake@cakephp.org');
  1084. $this->CakeEmail->subject('My title');
  1085. $this->CakeEmail->emailFormat('text');
  1086. $this->CakeEmail->attachments(array(
  1087. 'cake.png' => array(
  1088. 'file' => CORE_PATH . 'VERSION.txt',
  1089. 'contentDisposition' => false
  1090. )
  1091. ));
  1092. $result = $this->CakeEmail->send('Hello');
  1093. $boundary = $this->CakeEmail->getBoundary();
  1094. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1095. $expected = "--$boundary\r\n" .
  1096. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1097. "Content-Transfer-Encoding: 8bit\r\n" .
  1098. "\r\n" .
  1099. "Hello" .
  1100. "\r\n" .
  1101. "\r\n" .
  1102. "\r\n" .
  1103. "--{$boundary}\r\n" .
  1104. "Content-Type: application/octet-stream\r\n" .
  1105. "Content-Transfer-Encoding: base64\r\n" .
  1106. "\r\n";
  1107. $this->assertContains($expected, $result['message']);
  1108. $this->assertContains('--' . $boundary . '--', $result['message']);
  1109. }
  1110. /**
  1111. * testSendWithLog method
  1112. *
  1113. * @return void
  1114. */
  1115. public function testSendWithLog() {
  1116. $log = $this->getMock('Cake\Log\Engine\BaseLog', ['write'], [['scopes' => 'email']]);
  1117. $message = 'Logging This';
  1118. $log->expects($this->once())
  1119. ->method('write')
  1120. ->with(
  1121. 'debug',
  1122. $this->logicalAnd(
  1123. $this->stringContains($message),
  1124. $this->stringContains('cake@cakephp.org'),
  1125. $this->stringContains('me@cakephp.org')
  1126. )
  1127. );
  1128. Log::config('email', $log);
  1129. $this->CakeEmail->transport('debug');
  1130. $this->CakeEmail->to('me@cakephp.org');
  1131. $this->CakeEmail->from('cake@cakephp.org');
  1132. $this->CakeEmail->subject('My title');
  1133. $this->CakeEmail->profile(array('log' => 'debug'));
  1134. $result = $this->CakeEmail->send($message);
  1135. }
  1136. /**
  1137. * testSendWithLogAndScope method
  1138. *
  1139. * @return void
  1140. */
  1141. public function testSendWithLogAndScope() {
  1142. $message = 'Logging This';
  1143. $log = $this->getMock('Cake\Log\Engine\BaseLog', ['write'], ['scopes' => ['email']]);
  1144. $log->expects($this->once())
  1145. ->method('write')
  1146. ->with(
  1147. 'debug',
  1148. $this->logicalAnd(
  1149. $this->stringContains($message),
  1150. $this->stringContains('cake@cakephp.org'),
  1151. $this->stringContains('me@cakephp.org')
  1152. )
  1153. );
  1154. Log::config('email', $log);
  1155. $this->CakeEmail->transport('debug');
  1156. $this->CakeEmail->to('me@cakephp.org');
  1157. $this->CakeEmail->from('cake@cakephp.org');
  1158. $this->CakeEmail->subject('My title');
  1159. $this->CakeEmail->profile(array('log' => array('scope' => 'email')));
  1160. $this->CakeEmail->send($message);
  1161. }
  1162. /**
  1163. * testSendRender method
  1164. *
  1165. * @return void
  1166. */
  1167. public function testSendRender() {
  1168. $this->CakeEmail->reset();
  1169. $this->CakeEmail->transport('debug');
  1170. $this->CakeEmail->from('cake@cakephp.org');
  1171. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1172. $this->CakeEmail->subject('My title');
  1173. $this->CakeEmail->profile(array('empty'));
  1174. $this->CakeEmail->template('default', 'default');
  1175. $result = $this->CakeEmail->send();
  1176. $this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
  1177. $this->assertContains('Message-ID: ', $result['headers']);
  1178. $this->assertContains('To: ', $result['headers']);
  1179. }
  1180. /**
  1181. * test sending and rendering with no layout
  1182. *
  1183. * @return void
  1184. */
  1185. public function testSendRenderNoLayout() {
  1186. $this->CakeEmail->reset();
  1187. $this->CakeEmail->transport('debug');
  1188. $this->CakeEmail->from('cake@cakephp.org');
  1189. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1190. $this->CakeEmail->subject('My title');
  1191. $this->CakeEmail->config(array('empty'));
  1192. $this->CakeEmail->template('default', null);
  1193. $result = $this->CakeEmail->send('message body.');
  1194. $this->assertContains('message body.', $result['message']);
  1195. $this->assertNotContains('This email was sent using the CakePHP Framework', $result['message']);
  1196. }
  1197. /**
  1198. * testSendRender method for ISO-2022-JP
  1199. *
  1200. * @return void
  1201. */
  1202. public function testSendRenderJapanese() {
  1203. $this->CakeEmail->reset();
  1204. $this->CakeEmail->transport('debug');
  1205. $this->CakeEmail->from('cake@cakephp.org');
  1206. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1207. $this->CakeEmail->subject('My title');
  1208. $this->CakeEmail->profile(array('empty'));
  1209. $this->CakeEmail->template('default', 'japanese');
  1210. $this->CakeEmail->charset = 'ISO-2022-JP';
  1211. $result = $this->CakeEmail->send();
  1212. $expected = mb_convert_encoding('CakePHP Framework を使って送信したメールです。 http://cakephp.org.', 'ISO-2022-JP');
  1213. $this->assertContains($expected, $result['message']);
  1214. $this->assertContains('Message-ID: ', $result['headers']);
  1215. $this->assertContains('To: ', $result['headers']);
  1216. }
  1217. /**
  1218. * testSendRenderThemed method
  1219. *
  1220. * @return void
  1221. */
  1222. public function testSendRenderThemed() {
  1223. $this->CakeEmail->reset();
  1224. $this->CakeEmail->transport('debug');
  1225. $this->CakeEmail->from('cake@cakephp.org');
  1226. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1227. $this->CakeEmail->subject('My title');
  1228. $this->CakeEmail->profile(array('empty'));
  1229. $this->CakeEmail->theme('TestTheme');
  1230. $this->CakeEmail->template('themed', 'default');
  1231. $result = $this->CakeEmail->send();
  1232. $this->assertContains('In TestTheme', $result['message']);
  1233. $this->assertContains('Message-ID: ', $result['headers']);
  1234. $this->assertContains('To: ', $result['headers']);
  1235. }
  1236. /**
  1237. * testSendRenderWithHTML method and assert line length is kept below the required limit
  1238. *
  1239. * @return void
  1240. */
  1241. public function testSendRenderWithHTML() {
  1242. $this->CakeEmail->reset();
  1243. $this->CakeEmail->transport('debug');
  1244. $this->CakeEmail->from('cake@cakephp.org');
  1245. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1246. $this->CakeEmail->subject('My title');
  1247. $this->CakeEmail->profile(array('empty'));
  1248. $this->CakeEmail->emailFormat('html');
  1249. $this->CakeEmail->template('html', 'default');
  1250. $result = $this->CakeEmail->send();
  1251. $this->assertTextContains('<h1>HTML Ipsum Presents</h1>', $result['message']);
  1252. $this->assertLineLengths($result['message']);
  1253. }
  1254. /**
  1255. * testSendRenderWithVars method
  1256. *
  1257. * @return void
  1258. */
  1259. public function testSendRenderWithVars() {
  1260. $this->CakeEmail->reset();
  1261. $this->CakeEmail->transport('debug');
  1262. $this->CakeEmail->from('cake@cakephp.org');
  1263. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1264. $this->CakeEmail->subject('My title');
  1265. $this->CakeEmail->profile(array('empty'));
  1266. $this->CakeEmail->template('custom', 'default');
  1267. $this->CakeEmail->viewVars(array('value' => 12345));
  1268. $result = $this->CakeEmail->send();
  1269. $this->assertContains('Here is your value: 12345', $result['message']);
  1270. }
  1271. /**
  1272. * testSendRenderWithVars method for ISO-2022-JP
  1273. *
  1274. * @return void
  1275. */
  1276. public function testSendRenderWithVarsJapanese() {
  1277. $this->CakeEmail->reset();
  1278. $this->CakeEmail->transport('debug');
  1279. $this->CakeEmail->from('cake@cakephp.org');
  1280. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1281. $this->CakeEmail->subject('My title');
  1282. $this->CakeEmail->profile(array('empty'));
  1283. $this->CakeEmail->template('japanese', 'default');
  1284. $this->CakeEmail->viewVars(array('value' => '日本語の差し込み123'));
  1285. $this->CakeEmail->charset = 'ISO-2022-JP';
  1286. $result = $this->CakeEmail->send();
  1287. $expected = mb_convert_encoding('ここにあなたの設定した値が入ります: 日本語の差し込み123', 'ISO-2022-JP');
  1288. $this->assertTrue((bool)strpos($result['message'], $expected));
  1289. }
  1290. /**
  1291. * testSendRenderWithHelpers method
  1292. *
  1293. * @return void
  1294. */
  1295. public function testSendRenderWithHelpers() {
  1296. $this->CakeEmail->reset();
  1297. $this->CakeEmail->transport('debug');
  1298. $timestamp = time();
  1299. $this->CakeEmail->from('cake@cakephp.org');
  1300. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1301. $this->CakeEmail->subject('My title');
  1302. $this->CakeEmail->profile(array('empty'));
  1303. $this->CakeEmail->template('custom_helper', 'default');
  1304. $this->CakeEmail->viewVars(array('time' => $timestamp));
  1305. $result = $this->CakeEmail->helpers(array('Time'));
  1306. $this->assertInstanceOf('Cake\Network\Email\Email', $result);
  1307. $result = $this->CakeEmail->send();
  1308. $dateTime = new \DateTime;
  1309. $dateTime->setTimestamp($timestamp);
  1310. $this->assertTrue((bool)strpos($result['message'], 'Right now: ' . $dateTime->format($dateTime::ATOM)));
  1311. $result = $this->CakeEmail->helpers();
  1312. $this->assertEquals(array('Time'), $result);
  1313. }
  1314. /**
  1315. * testSendRenderWithImage method
  1316. *
  1317. * @return void
  1318. */
  1319. public function testSendRenderWithImage() {
  1320. $this->CakeEmail->reset();
  1321. $this->CakeEmail->transport('debug');
  1322. $this->CakeEmail->from('cake@cakephp.org');
  1323. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1324. $this->CakeEmail->subject('My title');
  1325. $this->CakeEmail->profile(array('empty'));
  1326. $this->CakeEmail->template('image');
  1327. $this->CakeEmail->emailFormat('html');
  1328. $server = env('SERVER_NAME') ? env('SERVER_NAME') : 'localhost';
  1329. if (env('SERVER_PORT') && env('SERVER_PORT') != 80) {
  1330. $server .= ':' . env('SERVER_PORT');
  1331. }
  1332. $expected = '<img src="http://' . $server . '/img/image.gif" alt="cool image" width="100" height="100"';
  1333. $result = $this->CakeEmail->send();
  1334. $this->assertContains($expected, $result['message']);
  1335. }
  1336. /**
  1337. * testSendRenderPlugin method
  1338. *
  1339. * @return void
  1340. */
  1341. public function testSendRenderPlugin() {
  1342. Plugin::load('TestPlugin');
  1343. $this->CakeEmail->reset();
  1344. $this->CakeEmail->transport('debug');
  1345. $this->CakeEmail->from('cake@cakephp.org');
  1346. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1347. $this->CakeEmail->subject('My title');
  1348. $this->CakeEmail->profile(array('empty'));
  1349. $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'default')->send();
  1350. $this->assertContains('Into TestPlugin.', $result['message']);
  1351. $this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
  1352. $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'TestPlugin.plug_default')->send();
  1353. $this->assertContains('Into TestPlugin.', $result['message']);
  1354. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1355. $result = $this->CakeEmail->template('TestPlugin.test_plugin_tpl', 'plug_default')->send();
  1356. $this->assertContains('Into TestPlugin.', $result['message']);
  1357. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1358. // test plugin template overridden by theme
  1359. $this->CakeEmail->theme('TestTheme');
  1360. $result = $this->CakeEmail->send();
  1361. $this->assertContains('Into TestPlugin. (themed)', $result['message']);
  1362. $this->CakeEmail->viewVars(array('value' => 12345));
  1363. $result = $this->CakeEmail->template('custom', 'TestPlugin.plug_default')->send();
  1364. $this->assertContains('Here is your value: 12345', $result['message']);
  1365. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1366. $this->setExpectedException('Cake\Error\MissingViewException');
  1367. $this->CakeEmail->template('test_plugin_tpl', 'plug_default')->send();
  1368. }
  1369. /**
  1370. * testSendMultipleMIME method
  1371. *
  1372. * @return void
  1373. */
  1374. public function testSendMultipleMIME() {
  1375. $this->CakeEmail->reset();
  1376. $this->CakeEmail->transport('debug');
  1377. $this->CakeEmail->from('cake@cakephp.org');
  1378. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1379. $this->CakeEmail->subject('My title');
  1380. $this->CakeEmail->template('custom', 'default');
  1381. $this->CakeEmail->profile(array());
  1382. $this->CakeEmail->viewVars(array('value' => 12345));
  1383. $this->CakeEmail->emailFormat('both');
  1384. $this->CakeEmail->send();
  1385. $message = $this->CakeEmail->message();
  1386. $boundary = $this->CakeEmail->getBoundary();
  1387. $this->assertFalse(empty($boundary));
  1388. $this->assertContains('--' . $boundary, $message);
  1389. $this->assertContains('--' . $boundary . '--', $message);
  1390. $this->assertContains('--alt-' . $boundary, $message);
  1391. $this->assertContains('--alt-' . $boundary . '--', $message);
  1392. $this->CakeEmail->attachments(array('fake.php' => __FILE__));
  1393. $this->CakeEmail->send();
  1394. $message = $this->CakeEmail->message();
  1395. $boundary = $this->CakeEmail->getBoundary();
  1396. $this->assertFalse(empty($boundary));
  1397. $this->assertContains('--' . $boundary, $message);
  1398. $this->assertContains('--' . $boundary . '--', $message);
  1399. $this->assertContains('--alt-' . $boundary, $message);
  1400. $this->assertContains('--alt-' . $boundary . '--', $message);
  1401. }
  1402. /**
  1403. * testSendAttachment method
  1404. *
  1405. * @return void
  1406. */
  1407. public function testSendAttachment() {
  1408. $this->CakeEmail->reset();
  1409. $this->CakeEmail->transport('debug');
  1410. $this->CakeEmail->from('cake@cakephp.org');
  1411. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1412. $this->CakeEmail->subject('My title');
  1413. $this->CakeEmail->profile(array());
  1414. $this->CakeEmail->attachments(array(CAKE . 'basics.php'));
  1415. $result = $this->CakeEmail->send('body');
  1416. $expected = "Content-Disposition: attachment; filename=\"basics.php\"\r\n" .
  1417. "Content-Type: application/octet-stream\r\n" .
  1418. "Content-Transfer-Encoding: base64\r\n";
  1419. $this->assertContains($expected, $result['message']);
  1420. $this->CakeEmail->attachments(array('my.file.txt' => CAKE . 'basics.php'));
  1421. $result = $this->CakeEmail->send('body');
  1422. $expected = "Content-Disposition: attachment; filename=\"my.file.txt\"\r\n" .
  1423. "Content-Type: application/octet-stream\r\n" .
  1424. "Content-Transfer-Encoding: base64\r\n";
  1425. $this->assertContains($expected, $result['message']);
  1426. $this->CakeEmail->attachments(array('file.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
  1427. $result = $this->CakeEmail->send('body');
  1428. $expected = "Content-Disposition: attachment; filename=\"file.txt\"\r\n" .
  1429. "Content-Type: text/plain\r\n" .
  1430. "Content-Transfer-Encoding: base64\r\n";
  1431. $this->assertContains($expected, $result['message']);
  1432. $this->CakeEmail->attachments(array('file2.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain', 'contentId' => 'a1b1c1')));
  1433. $result = $this->CakeEmail->send('body');
  1434. $expected = "Content-Disposition: inline; filename=\"file2.txt\"\r\n" .
  1435. "Content-Type: text/plain\r\n" .
  1436. "Content-Transfer-Encoding: base64\r\n" .
  1437. "Content-ID: <a1b1c1>\r\n";
  1438. $this->assertContains($expected, $result['message']);
  1439. }
  1440. /**
  1441. * testDeliver method
  1442. *
  1443. * @return void
  1444. */
  1445. public function testDeliver() {
  1446. $instance = Email::deliver('all@cakephp.org', 'About', 'Everything ok', array('from' => 'root@cakephp.org'), false);
  1447. $this->assertInstanceOf('Cake\Network\Email\Email', $instance);
  1448. $this->assertSame($instance->to(), array('all@cakephp.org' => 'all@cakephp.org'));
  1449. $this->assertSame($instance->subject(), 'About');
  1450. $this->assertSame($instance->from(), array('root@cakephp.org' => 'root@cakephp.org'));
  1451. $config = array(
  1452. 'from' => 'cake@cakephp.org',
  1453. 'to' => 'debug@cakephp.org',
  1454. 'subject' => 'Update ok',
  1455. 'template' => 'custom',
  1456. 'layout' => 'custom_layout',
  1457. 'viewVars' => array('value' => 123),
  1458. 'cc' => array('cake@cakephp.org' => 'Myself')
  1459. );
  1460. $instance = Email::deliver(null, null, array('name' => 'CakePHP'), $config, false);
  1461. $this->assertSame($instance->from(), array('cake@cakephp.org' => 'cake@cakephp.org'));
  1462. $this->assertSame($instance->to(), array('debug@cakephp.org' => 'debug@cakephp.org'));
  1463. $this->assertSame($instance->subject(), 'Update ok');
  1464. $this->assertSame($instance->template(), array('template' => 'custom', 'layout' => 'custom_layout'));
  1465. $this->assertSame($instance->viewVars(), array('value' => 123, 'name' => 'CakePHP'));
  1466. $this->assertSame($instance->cc(), array('cake@cakephp.org' => 'Myself'));
  1467. $configs = array('from' => 'root@cakephp.org', 'message' => 'Message from configs', 'transport' => 'debug');
  1468. $instance = Email::deliver('all@cakephp.org', 'About', null, $configs, true);
  1469. $message = $instance->message();
  1470. $this->assertEquals($configs['message'], $message[0]);
  1471. }
  1472. /**
  1473. * testMessage method
  1474. *
  1475. * @return void
  1476. */
  1477. public function testMessage() {
  1478. $this->CakeEmail->reset();
  1479. $this->CakeEmail->transport('debug');
  1480. $this->CakeEmail->from('cake@cakephp.org');
  1481. $this->CakeEmail->to(array('you@cakephp.org' => 'You'));
  1482. $this->CakeEmail->subject('My title');
  1483. $this->CakeEmail->profile(array('empty'));
  1484. $this->CakeEmail->template('default', 'default');
  1485. $this->CakeEmail->emailFormat('both');
  1486. $this->CakeEmail->send();
  1487. $expected = '<p>This email was sent using the <a href="http://cakephp.org">CakePHP Framework</a></p>';
  1488. $this->assertContains($expected, $this->CakeEmail->message(Email::MESSAGE_HTML));
  1489. $expected = 'This email was sent using the CakePHP Framework, http://cakephp.org.';
  1490. $this->assertContains($expected, $this->CakeEmail->message(Email::MESSAGE_TEXT));
  1491. $message = $this->CakeEmail->message();
  1492. $this->assertContains('Content-Type: text/plain; charset=UTF-8', $message);
  1493. $this->assertContains('Content-Type: text/html; charset=UTF-8', $message);
  1494. // UTF-8 is 8bit
  1495. $this->assertTrue($this->_checkContentTransferEncoding($message, '8bit'));
  1496. $this->CakeEmail->charset = 'ISO-2022-JP';
  1497. $this->CakeEmail->send();
  1498. $message = $this->CakeEmail->message();
  1499. $this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $message);
  1500. $this->assertContains('Content-Type: text/html; charset=ISO-2022-JP', $message);
  1501. // ISO-2022-JP is 7bit
  1502. $this->assertTrue($this->_checkContentTransferEncoding($message, '7bit'));
  1503. }
  1504. /**
  1505. * testReset method
  1506. *
  1507. * @return void
  1508. */
  1509. public function testReset() {
  1510. $this->CakeEmail->to('cake@cakephp.org');
  1511. $this->CakeEmail->theme('TestTheme');
  1512. $this->CakeEmail->emailPattern('/.+@.+\..+/i');
  1513. $this->assertSame($this->CakeEmail->to(), array('cake@cakephp.org' => 'cake@cakephp.org'));
  1514. $this->CakeEmail->reset();
  1515. $this->assertSame($this->CakeEmail->to(), array());
  1516. $this->assertSame(null, $this->CakeEmail->theme());
  1517. $this->assertSame(null, $this->CakeEmail->emailPattern());
  1518. }
  1519. /**
  1520. * testReset with charset
  1521. *
  1522. * @return void
  1523. */
  1524. public function testResetWithCharset() {
  1525. $this->CakeEmail->charset = 'ISO-2022-JP';
  1526. $this->CakeEmail->reset();
  1527. $this->assertSame($this->CakeEmail->charset, 'utf-8', $this->CakeEmail->charset);
  1528. $this->assertSame($this->CakeEmail->headerCharset, null, $this->CakeEmail->headerCharset);
  1529. }
  1530. /**
  1531. * testWrap method
  1532. *
  1533. * @return void
  1534. */
  1535. public function testWrap() {
  1536. $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
  1537. $result = $this->CakeEmail->wrap($text, Email::LINE_LENGTH_SHOULD);
  1538. $expected = array(
  1539. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
  1540. 'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
  1541. ''
  1542. );
  1543. $this->assertSame($expected, $result);
  1544. $text = 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan amet.';
  1545. $result = $this->CakeEmail->wrap($text, Email::LINE_LENGTH_SHOULD);
  1546. $expected = array(
  1547. 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
  1548. 'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
  1549. 'amet.',
  1550. ''
  1551. );
  1552. $this->assertSame($expected, $result);
  1553. $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>';
  1554. $result = $this->CakeEmail->wrap($text, Email::LINE_LENGTH_SHOULD);
  1555. $expected = array(
  1556. '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac',
  1557. 'turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula',
  1558. 'pellentesque accumsan amet.<hr></p>',
  1559. ''
  1560. );
  1561. $this->assertSame($expected, $result);
  1562. $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.';
  1563. $result = $this->CakeEmail->wrap($text, Email::LINE_LENGTH_SHOULD);
  1564. $expected = array(
  1565. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac',
  1566. '<a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh',
  1567. 'nisi, vehicula pellentesque accumsan amet.',
  1568. ''
  1569. );
  1570. $this->assertSame($expected, $result);
  1571. $text = 'Lorem ipsum <a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">ok</a>';
  1572. $result = $this->CakeEmail->wrap($text, Email::LINE_LENGTH_SHOULD);
  1573. $expected = array(
  1574. 'Lorem ipsum',
  1575. '<a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">',
  1576. 'ok</a>',
  1577. ''
  1578. );
  1579. $this->assertSame($expected, $result);
  1580. $text = 'Lorem ipsum withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite ok.';
  1581. $result = $this->CakeEmail->wrap($text, Email::LINE_LENGTH_SHOULD);
  1582. $expected = array(
  1583. 'Lorem ipsum',
  1584. 'withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite',
  1585. 'ok.',
  1586. ''
  1587. );
  1588. $this->assertSame($expected, $result);
  1589. }
  1590. /**
  1591. * testRender method
  1592. *
  1593. * @return void
  1594. */
  1595. public function testRenderWithLayoutAndAttachment() {
  1596. $this->CakeEmail->emailFormat('html');
  1597. $this->CakeEmail->template('html', 'default');
  1598. $this->CakeEmail->attachments(array(CAKE . 'basics.php'));
  1599. $result = $this->CakeEmail->render(array());
  1600. $this->assertNotEmpty($result);
  1601. $result = $this->CakeEmail->getBoundary();
  1602. $this->assertNotEmpty($result);
  1603. }
  1604. /**
  1605. * testConstructWithConfigArray method
  1606. *
  1607. * @return void
  1608. */
  1609. public function testConstructWithConfigArray() {
  1610. $configs = array(
  1611. 'from' => array('some@example.com' => 'My website'),
  1612. 'to' => 'test@example.com',
  1613. 'subject' => 'Test mail subject',
  1614. 'transport' => 'debug',
  1615. );
  1616. $this->CakeEmail = new Email($configs);
  1617. $result = $this->CakeEmail->to();
  1618. $this->assertEquals(array($configs['to'] => $configs['to']), $result);
  1619. $result = $this->CakeEmail->from();
  1620. $this->assertEquals($configs['from'], $result);
  1621. $result = $this->CakeEmail->subject();
  1622. $this->assertEquals($configs['subject'], $result);
  1623. $result = $this->CakeEmail->transport();
  1624. $this->assertInstanceOf('Cake\Network\Email\DebugTransport', $result);
  1625. $result = $this->CakeEmail->send('This is the message');
  1626. $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
  1627. $this->assertTrue((bool)strpos($result['headers'], 'To: '));
  1628. }
  1629. /**
  1630. * testConstructWithConfigString method
  1631. *
  1632. * @return void
  1633. */
  1634. public function testConstructWithConfigString() {
  1635. $configs = array(
  1636. 'from' => array('some@example.com' => 'My website'),
  1637. 'to' => 'test@example.com',
  1638. 'subject' => 'Test mail subject',
  1639. 'transport' => 'debug',
  1640. );
  1641. Email::config('test', $configs);
  1642. $this->CakeEmail = new Email('test');
  1643. $result = $this->CakeEmail->to();
  1644. $this->assertEquals([$configs['to'] => $configs['to']], $result);
  1645. $result = $this->CakeEmail->from();
  1646. $this->assertEquals($configs['from'], $result);
  1647. $result = $this->CakeEmail->subject();
  1648. $this->assertEquals($configs['subject'], $result);
  1649. $result = $this->CakeEmail->transport();
  1650. $this->assertInstanceOf('Cake\Network\Email\DebugTransport', $result);
  1651. $result = $this->CakeEmail->send('This is the message');
  1652. $this->assertTrue((bool)strpos($result['headers'], 'Message-ID: '));
  1653. $this->assertTrue((bool)strpos($result['headers'], 'To: '));
  1654. }
  1655. /**
  1656. * testViewRender method
  1657. *
  1658. * @return void
  1659. */
  1660. public function testViewRender() {
  1661. $result = $this->CakeEmail->viewRender();
  1662. $this->assertEquals('Cake\View\View', $result);
  1663. $result = $this->CakeEmail->viewRender('Cake\View\ThemeView');
  1664. $this->assertInstanceOf('Cake\Network\Email\Email', $result);
  1665. $result = $this->CakeEmail->viewRender();
  1666. $this->assertEquals('Cake\View\ThemeView', $result);
  1667. }
  1668. /**
  1669. * testEmailFormat method
  1670. *
  1671. * @return void
  1672. */
  1673. public function testEmailFormat() {
  1674. $result = $this->CakeEmail->emailFormat();
  1675. $this->assertEquals('text', $result);
  1676. $result = $this->CakeEmail->emailFormat('html');
  1677. $this->assertInstanceOf('Cake\Network\Email\Email', $result);
  1678. $result = $this->CakeEmail->emailFormat();
  1679. $this->assertEquals('html', $result);
  1680. $this->setExpectedException('Cake\Error\SocketException');
  1681. $result = $this->CakeEmail->emailFormat('invalid');
  1682. }
  1683. /**
  1684. * Tests that it is possible to add charset configuration to a CakeEmail object
  1685. *
  1686. * @return void
  1687. */
  1688. public function testConfigCharset() {
  1689. $email = new Email();
  1690. $this->assertEquals(Configure::read('App.encoding'), $email->charset);
  1691. $this->assertEquals(Configure::read('App.encoding'), $email->headerCharset);
  1692. $email = new Email(array('charset' => 'iso-2022-jp', 'headerCharset' => 'iso-2022-jp-ms'));
  1693. $this->assertEquals('iso-2022-jp', $email->charset);
  1694. $this->assertEquals('iso-2022-jp-ms', $email->headerCharset);
  1695. $email = new Email(array('charset' => 'iso-2022-jp'));
  1696. $this->assertEquals('iso-2022-jp', $email->charset);
  1697. $this->assertEquals('iso-2022-jp', $email->headerCharset);
  1698. $email = new Email(array('headerCharset' => 'iso-2022-jp-ms'));
  1699. $this->assertEquals(Configure::read('App.encoding'), $email->charset);
  1700. $this->assertEquals('iso-2022-jp-ms', $email->headerCharset);
  1701. }
  1702. /**
  1703. * Tests that the header is encoded using the configured headerCharset
  1704. *
  1705. * @return void
  1706. */
  1707. public function testHeaderEncoding() {
  1708. $email = new Email(array('headerCharset' => 'iso-2022-jp-ms', 'transport' => 'debug'));
  1709. $email->subject('あれ?もしかしての前と');
  1710. $headers = $email->getHeaders(array('subject'));
  1711. $expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
  1712. $this->assertContains($expected, $headers['Subject']);
  1713. $email->to('someone@example.com')->from('someone@example.com');
  1714. $result = $email->send('ってテーブルを作ってやってたらう');
  1715. $this->assertContains('ってテーブルを作ってやってたらう', $result['message']);
  1716. }
  1717. /**
  1718. * Tests that the body is encoded using the configured charset
  1719. *
  1720. * @return void
  1721. */
  1722. public function testBodyEncoding() {
  1723. $email = new Email(array(
  1724. 'charset' => 'iso-2022-jp',
  1725. 'headerCharset' => 'iso-2022-jp-ms',
  1726. 'transport' => 'debug'
  1727. ));
  1728. $email->subject('あれ?もしかしての前と');
  1729. $headers = $email->getHeaders(array('subject'));
  1730. $expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
  1731. $this->assertContains($expected, $headers['Subject']);
  1732. $email->to('someone@example.com')->from('someone@example.com');
  1733. $result = $email->send('ってテーブルを作ってやってたらう');
  1734. $this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $result['headers']);
  1735. $this->assertContains(mb_convert_encoding('ってテーブルを作ってやってたらう', 'ISO-2022-JP'), $result['message']);
  1736. }
  1737. /**
  1738. * Tests that the body is encoded using the configured charset (Japanese standard encoding)
  1739. *
  1740. * @return void
  1741. */
  1742. public function testBodyEncodingIso2022Jp() {
  1743. $email = new Email(array(
  1744. 'charset' => 'iso-2022-jp',
  1745. 'headerCharset' => 'iso-2022-jp',
  1746. 'transport' => 'debug'
  1747. ));
  1748. $email->subject('あれ?もしかしての前と');
  1749. $headers = $email->getHeaders(array('subject'));
  1750. $expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
  1751. $this->assertContains($expected, $headers['Subject']);
  1752. $email->to('someone@example.com')->from('someone@example.com');
  1753. $result = $email->send('①㈱');
  1754. $this->assertTextContains("Content-Type: text/plain; charset=ISO-2022-JP", $result['headers']);
  1755. $this->assertTextNotContains("Content-Type: text/plain; charset=ISO-2022-JP-MS", $result['headers']); // not charset=iso-2022-jp-ms
  1756. $this->assertTextNotContains(mb_convert_encoding('①㈱', 'ISO-2022-JP-MS'), $result['message']);
  1757. }
  1758. /**
  1759. * Tests that the body is encoded using the configured charset (Japanese irregular encoding, but sometime use this)
  1760. *
  1761. * @return void
  1762. */
  1763. public function testBodyEncodingIso2022JpMs() {
  1764. $email = new Email(array(
  1765. 'charset' => 'iso-2022-jp-ms',
  1766. 'headerCharset' => 'iso-2022-jp-ms',
  1767. 'transport' => 'debug'
  1768. ));
  1769. $email->subject('あれ?もしかしての前と');
  1770. $headers = $email->getHeaders(array('subject'));
  1771. $expected = "?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=";
  1772. $this->assertContains($expected, $headers['Subject']);
  1773. $email->to('someone@example.com')->from('someone@example.com');
  1774. $result = $email->send('①㈱');
  1775. $this->assertTextContains("Content-Type: text/plain; charset=ISO-2022-JP", $result['headers']);
  1776. $this->assertTextNotContains("Content-Type: text/plain; charset=iso-2022-jp-ms", $result['headers']); // not charset=iso-2022-jp-ms
  1777. $this->assertContains(mb_convert_encoding('①㈱', 'ISO-2022-JP-MS'), $result['message']);
  1778. }
  1779. protected function _checkContentTransferEncoding($message, $charset) {
  1780. $boundary = '--alt-' . $this->CakeEmail->getBoundary();
  1781. $result['text'] = false;
  1782. $result['html'] = false;
  1783. $length = count($message);
  1784. for ($i = 0; $i < $length; ++$i) {
  1785. if ($message[$i] == $boundary) {
  1786. $flag = false;
  1787. $type = '';
  1788. while (!preg_match('/^$/', $message[$i])) {
  1789. if (preg_match('/^Content-Type: text\/plain/', $message[$i])) {
  1790. $type = 'text';
  1791. }
  1792. if (preg_match('/^Content-Type: text\/html/', $message[$i])) {
  1793. $type = 'html';
  1794. }
  1795. if ($message[$i] === 'Content-Transfer-Encoding: ' . $charset) {
  1796. $flag = true;
  1797. }
  1798. ++$i;
  1799. }
  1800. $result[$type] = $flag;
  1801. }
  1802. }
  1803. return $result['text'] && $result['html'];
  1804. }
  1805. /**
  1806. * Test CakeEmail::_encode function
  1807. *
  1808. * @return void
  1809. */
  1810. public function testEncode() {
  1811. $this->CakeEmail->headerCharset = 'ISO-2022-JP';
  1812. $result = $this->CakeEmail->encode('日本語');
  1813. $expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?=';
  1814. $this->assertSame($expected, $result);
  1815. $this->CakeEmail->headerCharset = 'ISO-2022-JP';
  1816. $result = $this->CakeEmail->encode('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?');
  1817. $expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
  1818. " =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
  1819. " =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=";
  1820. $this->assertSame($expected, $result);
  1821. }
  1822. /**
  1823. * Tests charset setter/getter
  1824. *
  1825. * @return void
  1826. */
  1827. public function testCharset() {
  1828. $this->CakeEmail->charset('UTF-8');
  1829. $this->assertSame($this->CakeEmail->charset(), 'UTF-8');
  1830. $this->CakeEmail->charset('ISO-2022-JP');
  1831. $this->assertSame($this->CakeEmail->charset(), 'ISO-2022-JP');
  1832. $charset = $this->CakeEmail->charset('Shift_JIS');
  1833. $this->assertSame($charset, 'Shift_JIS');
  1834. }
  1835. /**
  1836. * Tests headerCharset setter/getter
  1837. *
  1838. * @return void
  1839. */
  1840. public function testHeaderCharset() {
  1841. $this->CakeEmail->headerCharset('UTF-8');
  1842. $this->assertSame($this->CakeEmail->headerCharset(), 'UTF-8');
  1843. $this->CakeEmail->headerCharset('ISO-2022-JP');
  1844. $this->assertSame($this->CakeEmail->headerCharset(), 'ISO-2022-JP');
  1845. $charset = $this->CakeEmail->headerCharset('Shift_JIS');
  1846. $this->assertSame($charset, 'Shift_JIS');
  1847. }
  1848. /**
  1849. * Tests for compatible check.
  1850. * charset property and charset() method.
  1851. * headerCharset property and headerCharset() method.
  1852. */
  1853. public function testCharsetsCompatible() {
  1854. $checkHeaders = array(
  1855. 'from' => true,
  1856. 'to' => true,
  1857. 'cc' => true,
  1858. 'subject' => true,
  1859. );
  1860. // Header Charset : null (used by default UTF-8)
  1861. // Body Charset : ISO-2022-JP
  1862. $oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', null);
  1863. $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
  1864. $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', null);
  1865. $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
  1866. $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
  1867. $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
  1868. $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
  1869. $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
  1870. // Header Charset : UTF-8
  1871. // Boby Charset : ISO-2022-JP
  1872. $oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', 'utf-8');
  1873. $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
  1874. $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', 'utf-8');
  1875. $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
  1876. $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
  1877. $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
  1878. $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
  1879. $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
  1880. // Header Charset : ISO-2022-JP
  1881. // Boby Charset : UTF-8
  1882. $oldStyleEmail = $this->_getEmailByOldStyleCharset('utf-8', 'iso-2022-jp');
  1883. $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
  1884. $newStyleEmail = $this->_getEmailByNewStyleCharset('utf-8', 'iso-2022-jp');
  1885. $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
  1886. $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
  1887. $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
  1888. $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
  1889. $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
  1890. }
  1891. protected function _getEmailByOldStyleCharset($charset, $headerCharset) {
  1892. $email = new Email(array('transport' => 'debug'));
  1893. if (! empty($charset)) {
  1894. $email->charset = $charset;
  1895. }
  1896. if (! empty($headerCharset)) {
  1897. $email->headerCharset = $headerCharset;
  1898. }
  1899. $email->from('someone@example.com', 'どこかの誰か');
  1900. $email->to('someperson@example.jp', 'どこかのどなたか');
  1901. $email->cc('miku@example.net', 'ミク');
  1902. $email->subject('テストメール');
  1903. $email->send('テストメールの本文');
  1904. return $email;
  1905. }
  1906. protected function _getEmailByNewStyleCharset($charset, $headerCharset) {
  1907. $email = new Email(array('transport' => 'debug'));
  1908. if (! empty($charset)) {
  1909. $email->charset($charset);
  1910. }
  1911. if (! empty($headerCharset)) {
  1912. $email->headerCharset($headerCharset);
  1913. }
  1914. $email->from('someone@example.com', 'どこかの誰か');
  1915. $email->to('someperson@example.jp', 'どこかのどなたか');
  1916. $email->cc('miku@example.net', 'ミク');
  1917. $email->subject('テストメール');
  1918. $email->send('テストメールの本文');
  1919. return $email;
  1920. }
  1921. public function testWrapLongLine() {
  1922. $message = '<a href="http://cakephp.org">' . str_repeat('x', Email::LINE_LENGTH_MUST) . "</a>";
  1923. $this->CakeEmail->reset();
  1924. $this->CakeEmail->transport('debug');
  1925. $this->CakeEmail->from('cake@cakephp.org');
  1926. $this->CakeEmail->to('cake@cakephp.org');
  1927. $this->CakeEmail->subject('Wordwrap Test');
  1928. $this->CakeEmail->profile(array('empty'));
  1929. $result = $this->CakeEmail->send($message);
  1930. $expected = "<a\r\n" . 'href="http://cakephp.org">' . str_repeat('x', Email::LINE_LENGTH_MUST - 26) . "\r\n" .
  1931. str_repeat('x', 26) . "\r\n</a>\r\n\r\n";
  1932. $this->assertEquals($expected, $result['message']);
  1933. $this->assertLineLengths($result['message']);
  1934. $str1 = "a ";
  1935. $str2 = " b";
  1936. $length = strlen($str1) + strlen($str2);
  1937. $message = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length - 1) . $str2;
  1938. $result = $this->CakeEmail->send($message);
  1939. $expected = "{$message}\r\n\r\n";
  1940. $this->assertEquals($expected, $result['message']);
  1941. $this->assertLineLengths($result['message']);
  1942. $message = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length) . $str2;
  1943. $result = $this->CakeEmail->send($message);
  1944. $expected = "{$message}\r\n\r\n";
  1945. $this->assertEquals($expected, $result['message']);
  1946. $this->assertLineLengths($result['message']);
  1947. $message = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length + 1) . $str2;
  1948. $result = $this->CakeEmail->send($message);
  1949. $expected = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length + 1) . sprintf("\r\n%s\r\n\r\n", trim($str2));
  1950. $this->assertEquals($expected, $result['message']);
  1951. $this->assertLineLengths($result['message']);
  1952. }
  1953. public function testWrapWithTagsAcrossLines() {
  1954. $str = <<<HTML
  1955. <table>
  1956. <th align="right" valign="top"
  1957. style="font-weight: bold">The tag is across multiple lines</th>
  1958. </table>
  1959. HTML;
  1960. $length = strlen($str);
  1961. $message = $str . str_repeat('x', Email::LINE_LENGTH_MUST + 1);
  1962. $this->CakeEmail->reset();
  1963. $this->CakeEmail->transport('debug');
  1964. $this->CakeEmail->from('cake@cakephp.org');
  1965. $this->CakeEmail->to('cake@cakephp.org');
  1966. $this->CakeEmail->subject('Wordwrap Test');
  1967. $this->CakeEmail->profile(array('empty'));
  1968. $result = $this->CakeEmail->send($message);
  1969. $message = str_replace("\r\n", "\n", substr($message, 0, -9));
  1970. $message = str_replace("\n", "\r\n", $message);
  1971. $expected = "{$message}\r\nxxxxxxxxx\r\n\r\n";
  1972. $this->assertEquals($expected, $result['message']);
  1973. $this->assertLineLengths($result['message']);
  1974. }
  1975. public function testWrapIncludeLessThanSign() {
  1976. $str = 'foo<bar';
  1977. $length = strlen($str);
  1978. $message = $str . str_repeat('x', Email::LINE_LENGTH_MUST - $length + 1);
  1979. $this->CakeEmail->reset();
  1980. $this->CakeEmail->transport('debug');
  1981. $this->CakeEmail->from('cake@cakephp.org');
  1982. $this->CakeEmail->to('cake@cakephp.org');
  1983. $this->CakeEmail->subject('Wordwrap Test');
  1984. $this->CakeEmail->profile(array('empty'));
  1985. $result = $this->CakeEmail->send($message);
  1986. $message = substr($message, 0, -1);
  1987. $expected = "{$message}\r\nx\r\n\r\n";
  1988. $this->assertEquals($expected, $result['message']);
  1989. $this->assertLineLengths($result['message']);
  1990. }
  1991. public function testWrapForJapaneseEncoding() {
  1992. $this->skipIf(!function_exists('mb_convert_encoding'));
  1993. $message = mb_convert_encoding('受け付けました', 'iso-2022-jp', 'UTF-8');
  1994. $this->CakeEmail->reset();
  1995. $this->CakeEmail->transport('debug');
  1996. $this->CakeEmail->from('cake@cakephp.org');
  1997. $this->CakeEmail->to('cake@cakephp.org');
  1998. $this->CakeEmail->subject('Wordwrap Test');
  1999. $this->CakeEmail->profile(array('empty'));
  2000. $this->CakeEmail->charset('iso-2022-jp');
  2001. $this->CakeEmail->headerCharset('iso-2022-jp');
  2002. $result = $this->CakeEmail->send($message);
  2003. $expected = "{$message}\r\n\r\n";
  2004. $this->assertEquals($expected, $result['message']);
  2005. }
  2006. /**
  2007. * CakeEmailTest::assertLineLengths()
  2008. *
  2009. * @param string $message
  2010. * @return void
  2011. */
  2012. public function assertLineLengths($message) {
  2013. $lines = explode("\r\n", $message);
  2014. foreach ($lines as $line) {
  2015. $this->assertTrue(strlen($line) <= Email::LINE_LENGTH_MUST,
  2016. 'Line length exceeds the max. limit of Email::LINE_LENGTH_MUST');
  2017. }
  2018. }
  2019. }