EmailTest.php 109 KB

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