EmailTest.php 79 KB

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