EmailTest.php 97 KB

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