EmailTest.php 100 KB

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