EmailTest.php 96 KB

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