EmailTest.php 103 KB

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