EmailTest.php 92 KB

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