EmailTest.php 96 KB

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