EmailTest.php 103 KB

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