EmailTest.php 103 KB

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