EmailTest.php 106 KB

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