EmailTest.php 103 KB

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