EmailTest.php 99 KB

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