EmailTest.php 104 KB

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