EmailTest.php 81 KB

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