EmailTest.php 77 KB

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