EmailTest.php 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053
  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. }
  717. /**
  718. * testTemplate method
  719. *
  720. * @return void
  721. */
  722. public function testTemplate()
  723. {
  724. $this->Email->viewBuilder()->setTemplate('template');
  725. $this->assertSame('template', $this->Email->viewBuilder()->getTemplate());
  726. }
  727. /**
  728. * testLayout method
  729. *
  730. * @return void
  731. */
  732. public function testLayout()
  733. {
  734. $this->Email->viewBuilder()->setLayout('layout');
  735. $this->assertSame('layout', $this->Email->viewBuilder()->getLayout());
  736. }
  737. /**
  738. * testTheme method
  739. *
  740. * @return void
  741. */
  742. public function testTheme()
  743. {
  744. $this->assertNull($this->Email->viewBuilder()->getTheme());
  745. $this->Email->viewBuilder()->setTheme('default');
  746. $expected = 'default';
  747. $this->assertSame($expected, $this->Email->viewBuilder()->getTheme());
  748. }
  749. /**
  750. * testViewVars method
  751. *
  752. * @return void
  753. */
  754. public function testViewVars()
  755. {
  756. $this->assertSame([], $this->Email->getViewVars());
  757. $this->Email->setViewVars(['value' => 12345]);
  758. $this->assertSame(['value' => 12345], $this->Email->getViewVars());
  759. $this->Email->setViewVars(['name' => 'CakePHP']);
  760. $this->assertEquals(['value' => 12345, 'name' => 'CakePHP'], $this->Email->getViewVars());
  761. $this->Email->setViewVars(['value' => 4567]);
  762. $this->assertSame(['value' => 4567, 'name' => 'CakePHP'], $this->Email->getViewVars());
  763. }
  764. /**
  765. * testAttachments method
  766. *
  767. * @return void
  768. */
  769. public function testSetAttachments()
  770. {
  771. $this->Email->setAttachments(CAKE . 'basics.php');
  772. $expected = [
  773. 'basics.php' => [
  774. 'file' => CAKE . 'basics.php',
  775. 'mimetype' => 'text/x-php'
  776. ]
  777. ];
  778. $this->assertSame($expected, $this->Email->getAttachments());
  779. $this->Email->setAttachments([]);
  780. $this->assertSame([], $this->Email->getAttachments());
  781. $this->Email->setAttachments([
  782. ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain']
  783. ]);
  784. $this->Email->addAttachments(CORE_PATH . 'config' . DS . 'bootstrap.php');
  785. $this->Email->addAttachments([CORE_PATH . 'config' . DS . 'bootstrap.php']);
  786. $this->Email->addAttachments([
  787. 'other.txt' => CORE_PATH . 'config' . DS . 'bootstrap.php',
  788. 'license' => CORE_PATH . 'LICENSE'
  789. ]);
  790. $expected = [
  791. 'basics.php' => ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'],
  792. 'bootstrap.php' => ['file' => CORE_PATH . 'config' . DS . 'bootstrap.php', 'mimetype' => 'text/x-php'],
  793. 'other.txt' => ['file' => CORE_PATH . 'config' . DS . 'bootstrap.php', 'mimetype' => 'text/x-php'],
  794. 'license' => ['file' => CORE_PATH . 'LICENSE', 'mimetype' => 'text/plain']
  795. ];
  796. $this->assertSame($expected, $this->Email->getAttachments());
  797. $this->expectException(\InvalidArgumentException::class);
  798. $this->Email->setAttachments([['nofile' => CAKE . 'basics.php', 'mimetype' => 'text/plain']]);
  799. }
  800. /**
  801. * Test send() with no template and data string attachment and no mimetype
  802. *
  803. * @return void
  804. */
  805. public function testSetAttachmentDataNoMimetype()
  806. {
  807. $this->Email->setAttachments(['cake.icon.gif' => [
  808. 'data' => 'test',
  809. ]]);
  810. $result = $this->Email->getAttachments();
  811. $expected = [
  812. 'cake.icon.gif' => [
  813. 'data' => base64_encode('test') . "\r\n",
  814. 'mimetype' => 'application/octet-stream'
  815. ],
  816. ];
  817. $this->assertSame($expected, $this->Email->getAttachments());
  818. }
  819. /**
  820. * testTransport method
  821. *
  822. * @return void
  823. */
  824. public function testTransport()
  825. {
  826. $result = $this->Email->setTransport('debug');
  827. $this->assertSame($this->Email, $result);
  828. $result = $this->Email->getTransport();
  829. $this->assertInstanceOf('Cake\Mailer\Transport\DebugTransport', $result);
  830. $instance = $this->getMockBuilder('Cake\Mailer\Transport\DebugTransport')->getMock();
  831. $this->Email->setTransport($instance);
  832. $this->assertSame($instance, $this->Email->getTransport());
  833. }
  834. /**
  835. * Test that using unknown transports fails.
  836. *
  837. */
  838. public function testTransportInvalid()
  839. {
  840. $this->expectException(\InvalidArgumentException::class);
  841. $this->expectExceptionMessage('The "Invalid" transport configuration does not exist');
  842. $this->Email->setTransport('Invalid');
  843. }
  844. /**
  845. * Test that using classes with no send method fails.
  846. *
  847. */
  848. public function testTransportInstanceInvalid()
  849. {
  850. $this->expectException(\LogicException::class);
  851. $this->Email->setTransport(new \StdClass());
  852. }
  853. /**
  854. * Test that using unknown transports fails.
  855. *
  856. */
  857. public function testTransportTypeInvalid()
  858. {
  859. $this->expectException(\InvalidArgumentException::class);
  860. $this->expectExceptionMessage('The value passed for the "$name" argument must be either a string, or an object, integer given.');
  861. $this->Email->setTransport(123);
  862. }
  863. /**
  864. * Test that using misconfigured transports fails.
  865. *
  866. */
  867. public function testTransportMissingClassName()
  868. {
  869. $this->expectException(\InvalidArgumentException::class);
  870. $this->expectExceptionMessage('Transport config "debug" is invalid, the required `className` option is missing');
  871. $this->deprecated(function () {
  872. Email::dropTransport('debug');
  873. Email::setConfigTransport('debug', []);
  874. });
  875. $this->Email->setTransport('debug');
  876. }
  877. /**
  878. * Test configuring a transport.
  879. *
  880. * @return void
  881. */
  882. public function testConfigTransport()
  883. {
  884. $settings = [
  885. 'className' => 'Debug',
  886. 'log' => true
  887. ];
  888. $this->deprecated(function () use ($settings) {
  889. Email::dropTransport('debug');
  890. $result = Email::setConfigTransport('debug', $settings);
  891. $this->assertNull($result, 'No return.');
  892. $result = Email::getConfigTransport('debug');
  893. $this->assertEquals($settings, $result);
  894. });
  895. }
  896. /**
  897. * Test configuring multiple transports.
  898. */
  899. public function testConfigTransportMultiple()
  900. {
  901. $settings = [
  902. 'debug' => [
  903. 'className' => 'Debug',
  904. 'log' => true
  905. ],
  906. 'test_smtp' => [
  907. 'className' => 'Smtp',
  908. 'username' => 'mark',
  909. 'password' => 'password',
  910. 'host' => 'example.com'
  911. ]
  912. ];
  913. $this->deprecated(function () use ($settings) {
  914. Email::dropTransport('debug');
  915. Email::setConfigTransport($settings);
  916. $this->assertEquals($settings['debug'], Email::getConfigTransport('debug'));
  917. $this->assertEquals($settings['test_smtp'], Email::getConfigTransport('test_smtp'));
  918. });
  919. }
  920. /**
  921. * Test that exceptions are raised when duplicate transports are configured.
  922. *
  923. */
  924. public function testConfigTransportErrorOnDuplicate()
  925. {
  926. $this->expectException(\BadMethodCallException::class);
  927. $settings = [
  928. 'className' => 'Debug',
  929. 'log' => true
  930. ];
  931. $this->deprecated(function () use ($settings) {
  932. Email::setConfigTransport('debug', $settings);
  933. Email::setConfigTransport('debug', $settings);
  934. Email::dropTransport('debug');
  935. });
  936. }
  937. /**
  938. * Test configTransport with an instance.
  939. *
  940. * @return void
  941. */
  942. public function testConfigTransportInstance()
  943. {
  944. $this->deprecated(function () {
  945. Email::dropTransport('debug');
  946. $instance = new DebugTransport();
  947. Email::setConfigTransport('debug', $instance);
  948. $this->assertEquals(['className' => $instance], Email::getConfigTransport('debug'));
  949. });
  950. }
  951. /**
  952. * Test enumerating all transport configurations
  953. *
  954. * @return void
  955. */
  956. public function testConfiguredTransport()
  957. {
  958. $this->deprecated(function () {
  959. $result = Email::configuredTransport();
  960. $this->assertInternalType('array', $result, 'Should have config keys');
  961. $this->assertEquals(
  962. array_keys($this->transports),
  963. $result,
  964. 'Loaded transports should be present in enumeration.'
  965. );
  966. });
  967. }
  968. /**
  969. * Test dropping a transport configuration
  970. *
  971. * @return void
  972. */
  973. public function testDropTransport()
  974. {
  975. $this->deprecated(function () {
  976. $result = Email::getConfigTransport('debug');
  977. $this->assertInternalType('array', $result, 'Should have config data');
  978. Email::dropTransport('debug');
  979. $this->assertNull(Email::getConfigTransport('debug'), 'Should not exist.');
  980. });
  981. }
  982. /**
  983. * Test reading/writing configuration profiles.
  984. *
  985. * @return void
  986. */
  987. public function testConfig()
  988. {
  989. $settings = [
  990. 'to' => 'mark@example.com',
  991. 'from' => 'noreply@example.com',
  992. ];
  993. Email::setConfig('test', $settings);
  994. $this->assertEquals($settings, Email::getConfig('test'), 'Should be the same.');
  995. $email = new Email('test');
  996. $this->assertContains($settings['to'], $email->getTo());
  997. }
  998. /**
  999. * Test that exceptions are raised on duplicate config set.
  1000. *
  1001. * @return void
  1002. */
  1003. public function testConfigErrorOnDuplicate()
  1004. {
  1005. $this->expectException(\BadMethodCallException::class);
  1006. $settings = [
  1007. 'to' => 'mark@example.com',
  1008. 'from' => 'noreply@example.com',
  1009. ];
  1010. Email::setConfig('test', $settings);
  1011. Email::setConfig('test', $settings);
  1012. }
  1013. /**
  1014. * test profile method
  1015. *
  1016. * @return void
  1017. */
  1018. public function testProfile()
  1019. {
  1020. $config = ['test' => 'ok', 'test2' => true];
  1021. $this->Email->setProfile($config);
  1022. $this->assertSame($this->Email->getProfile(), $config);
  1023. $config = ['test' => 'test@example.com'];
  1024. $this->Email->setProfile($config);
  1025. $expected = ['test' => 'test@example.com', 'test2' => true];
  1026. $this->assertSame($expected, $this->Email->getProfile());
  1027. }
  1028. /**
  1029. * test that default profile is used by constructor if available.
  1030. *
  1031. * @return void
  1032. */
  1033. public function testDefaultProfile()
  1034. {
  1035. $config = ['test' => 'ok', 'test2' => true];
  1036. Configure::write('Email.default', $config);
  1037. Email::setConfig(Configure::consume('Email'));
  1038. $Email = new Email();
  1039. $this->assertSame($Email->getProfile(), $config);
  1040. Configure::delete('Email');
  1041. Email::drop('default');
  1042. }
  1043. /**
  1044. * Test that using an invalid profile fails.
  1045. *
  1046. */
  1047. public function testProfileInvalid()
  1048. {
  1049. $this->expectException(\InvalidArgumentException::class);
  1050. $this->expectExceptionMessage('Unknown email configuration "derp".');
  1051. $email = new Email();
  1052. $email->setProfile('derp');
  1053. }
  1054. /**
  1055. * testConfigString method
  1056. *
  1057. * @return void
  1058. */
  1059. public function testUseConfigString()
  1060. {
  1061. $config = [
  1062. 'from' => ['some@example.com' => 'My website'],
  1063. 'to' => ['test@example.com' => 'Testname'],
  1064. 'subject' => 'Test mail subject',
  1065. 'transport' => 'debug',
  1066. 'theme' => 'TestTheme',
  1067. 'helpers' => ['Html', 'Form'],
  1068. ];
  1069. Email::setConfig('test', $config);
  1070. $this->Email->setProfile('test');
  1071. $result = $this->Email->getTo();
  1072. $this->assertEquals($config['to'], $result);
  1073. $result = $this->Email->getFrom();
  1074. $this->assertEquals($config['from'], $result);
  1075. $result = $this->Email->getSubject();
  1076. $this->assertEquals($config['subject'], $result);
  1077. $result = $this->Email->viewBuilder()->getTheme();
  1078. $this->assertEquals($config['theme'], $result);
  1079. $result = $this->Email->getTransport();
  1080. $this->assertInstanceOf('Cake\Mailer\Transport\DebugTransport', $result);
  1081. $result = $this->Email->viewBuilder()->getHelpers();
  1082. $this->assertEquals($config['helpers'], $result);
  1083. }
  1084. /**
  1085. * Calling send() with no parameters should not overwrite the view variables.
  1086. *
  1087. * @return void
  1088. */
  1089. public function testSendWithNoContentDoesNotOverwriteViewVar()
  1090. {
  1091. $this->Email->reset();
  1092. $this->Email->setTransport('debug');
  1093. $this->Email->setFrom('cake@cakephp.org');
  1094. $this->Email->setTo('you@cakephp.org');
  1095. $this->Email->setSubject('My title');
  1096. $this->Email->setEmailFormat('text');
  1097. $this->Email->viewBuilder()->setTemplate('default');
  1098. $this->Email->setViewVars([
  1099. 'content' => 'A message to you',
  1100. ]);
  1101. $result = $this->Email->send();
  1102. $this->assertContains('A message to you', $result['message']);
  1103. }
  1104. /**
  1105. * testSendWithContent method
  1106. *
  1107. * @return void
  1108. */
  1109. public function testSendWithContent()
  1110. {
  1111. $this->Email->reset();
  1112. $this->Email->setTransport('debug');
  1113. $this->Email->setFrom('cake@cakephp.org');
  1114. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1115. $this->Email->setSubject('My title');
  1116. $this->Email->setProfile(['empty']);
  1117. $result = $this->Email->send("Here is my body, with multi lines.\nThis is the second line.\r\n\r\nAnd the last.");
  1118. $expected = ['headers', 'message'];
  1119. $this->assertEquals($expected, array_keys($result));
  1120. $expected = "Here is my body, with multi lines.\r\nThis is the second line.\r\n\r\nAnd the last.\r\n\r\n";
  1121. $this->assertEquals($expected, $result['message']);
  1122. $this->assertContains('Date: ', $result['headers']);
  1123. $this->assertContains('Message-ID: ', $result['headers']);
  1124. $this->assertContains('To: ', $result['headers']);
  1125. $result = $this->Email->send('Other body');
  1126. $expected = "Other body\r\n\r\n";
  1127. $this->assertSame($expected, $result['message']);
  1128. $this->assertContains('Message-ID: ', $result['headers']);
  1129. $this->assertContains('To: ', $result['headers']);
  1130. $this->Email->reset();
  1131. $this->Email->setTransport('debug');
  1132. $this->Email->setFrom('cake@cakephp.org');
  1133. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1134. $this->Email->setSubject('My title');
  1135. $this->Email->setProfile(['empty']);
  1136. $result = $this->Email->send(['Sending content', 'As array']);
  1137. $expected = "Sending content\r\nAs array\r\n\r\n\r\n";
  1138. $this->assertSame($expected, $result['message']);
  1139. }
  1140. /**
  1141. * testSendWithoutFrom method
  1142. *
  1143. * @return void
  1144. */
  1145. public function testSendWithoutFrom()
  1146. {
  1147. $this->expectException(\BadMethodCallException::class);
  1148. $this->Email->setTransport('debug');
  1149. $this->Email->setTo('cake@cakephp.org');
  1150. $this->Email->setSubject('My title');
  1151. $this->Email->setProfile(['empty']);
  1152. $this->Email->send('Forgot to set From');
  1153. }
  1154. /**
  1155. * testSendWithoutTo method
  1156. *
  1157. * @return void
  1158. */
  1159. public function testSendWithoutTo()
  1160. {
  1161. $this->expectException(\BadMethodCallException::class);
  1162. $this->Email->setTransport('debug');
  1163. $this->Email->setFrom('cake@cakephp.org');
  1164. $this->Email->setSubject('My title');
  1165. $this->Email->setProfile(['empty']);
  1166. $this->Email->send('Forgot to set To');
  1167. }
  1168. /**
  1169. * test send without a transport method
  1170. *
  1171. * @return void
  1172. */
  1173. public function testSendWithoutTransport()
  1174. {
  1175. $this->expectException(\BadMethodCallException::class);
  1176. $this->expectExceptionMessage('Cannot send email, transport was not defined.');
  1177. $this->Email->setTo('cake@cakephp.org');
  1178. $this->Email->setFrom('cake@cakephp.org');
  1179. $this->Email->setSubject('My title');
  1180. $this->Email->send('Forgot to set To');
  1181. }
  1182. /**
  1183. * Test send() with no template.
  1184. *
  1185. * @return void
  1186. */
  1187. public function testSendNoTemplateWithAttachments()
  1188. {
  1189. $this->Email->setTransport('debug');
  1190. $this->Email->setFrom('cake@cakephp.org');
  1191. $this->Email->setTo('cake@cakephp.org');
  1192. $this->Email->setSubject('My title');
  1193. $this->Email->setEmailFormat('text');
  1194. $this->Email->setAttachments([CAKE . 'basics.php']);
  1195. $result = $this->Email->send('Hello');
  1196. $boundary = $this->Email->getBoundary();
  1197. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1198. $expected = "--$boundary\r\n" .
  1199. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1200. "Content-Transfer-Encoding: 8bit\r\n" .
  1201. "\r\n" .
  1202. 'Hello' .
  1203. "\r\n" .
  1204. "\r\n" .
  1205. "\r\n" .
  1206. "--$boundary\r\n" .
  1207. "Content-Disposition: attachment; filename=\"basics.php\"\r\n" .
  1208. "Content-Type: text/x-php\r\n" .
  1209. "Content-Transfer-Encoding: base64\r\n" .
  1210. "\r\n";
  1211. $this->assertContains($expected, $result['message']);
  1212. }
  1213. /**
  1214. * Test send() with no template and data string attachment
  1215. *
  1216. * @return void
  1217. */
  1218. public function testSendNoTemplateWithDataStringAttachment()
  1219. {
  1220. $this->Email->setTransport('debug');
  1221. $this->Email->setFrom('cake@cakephp.org');
  1222. $this->Email->setTo('cake@cakephp.org');
  1223. $this->Email->setSubject('My title');
  1224. $this->Email->setEmailFormat('text');
  1225. $data = file_get_contents(TEST_APP . 'webroot/img/cake.power.gif');
  1226. $this->Email->setAttachments(['cake.icon.gif' => [
  1227. 'data' => $data,
  1228. 'mimetype' => 'image/gif'
  1229. ]]);
  1230. $result = $this->Email->send('Hello');
  1231. $boundary = $this->Email->getBoundary();
  1232. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1233. $expected = "--$boundary\r\n" .
  1234. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1235. "Content-Transfer-Encoding: 8bit\r\n" .
  1236. "\r\n" .
  1237. 'Hello' .
  1238. "\r\n" .
  1239. "\r\n" .
  1240. "\r\n" .
  1241. "--$boundary\r\n" .
  1242. "Content-Disposition: attachment; filename=\"cake.icon.gif\"\r\n" .
  1243. "Content-Type: image/gif\r\n" .
  1244. "Content-Transfer-Encoding: base64\r\n\r\n";
  1245. $expected .= chunk_split(base64_encode($data), 76, "\r\n");
  1246. $this->assertContains($expected, $result['message']);
  1247. }
  1248. /**
  1249. * Test send() with no template as both
  1250. *
  1251. * @return void
  1252. */
  1253. public function testSendNoTemplateWithAttachmentsAsBoth()
  1254. {
  1255. $this->Email->setTransport('debug');
  1256. $this->Email->setFrom('cake@cakephp.org');
  1257. $this->Email->setTo('cake@cakephp.org');
  1258. $this->Email->setSubject('My title');
  1259. $this->Email->setEmailFormat('both');
  1260. $this->Email->setAttachments([CORE_PATH . 'VERSION.txt']);
  1261. $result = $this->Email->send('Hello');
  1262. $boundary = $this->Email->getBoundary();
  1263. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1264. $expected = "--$boundary\r\n" .
  1265. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  1266. "\r\n" .
  1267. "--alt-$boundary\r\n" .
  1268. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1269. "Content-Transfer-Encoding: 8bit\r\n" .
  1270. "\r\n" .
  1271. 'Hello' .
  1272. "\r\n" .
  1273. "\r\n" .
  1274. "\r\n" .
  1275. "--alt-$boundary\r\n" .
  1276. "Content-Type: text/html; charset=UTF-8\r\n" .
  1277. "Content-Transfer-Encoding: 8bit\r\n" .
  1278. "\r\n" .
  1279. 'Hello' .
  1280. "\r\n" .
  1281. "\r\n" .
  1282. "\r\n" .
  1283. "--alt-{$boundary}--\r\n" .
  1284. "\r\n" .
  1285. "--$boundary\r\n" .
  1286. "Content-Disposition: attachment; filename=\"VERSION.txt\"\r\n" .
  1287. "Content-Type: text/plain\r\n" .
  1288. "Content-Transfer-Encoding: base64\r\n" .
  1289. "\r\n";
  1290. $this->assertContains($expected, $result['message']);
  1291. }
  1292. /**
  1293. * Test setting inline attachments and messages.
  1294. *
  1295. * @return void
  1296. */
  1297. public function testSendWithInlineAttachments()
  1298. {
  1299. $this->Email->setTransport('debug');
  1300. $this->Email->setFrom('cake@cakephp.org');
  1301. $this->Email->setTo('cake@cakephp.org');
  1302. $this->Email->setSubject('My title');
  1303. $this->Email->setEmailFormat('both');
  1304. $this->Email->setAttachments([
  1305. 'cake.png' => [
  1306. 'file' => CORE_PATH . 'VERSION.txt',
  1307. 'contentId' => 'abc123'
  1308. ]
  1309. ]);
  1310. $result = $this->Email->send('Hello');
  1311. $boundary = $this->Email->getBoundary();
  1312. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1313. $expected = "--$boundary\r\n" .
  1314. "Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
  1315. "\r\n" .
  1316. "--rel-$boundary\r\n" .
  1317. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  1318. "\r\n" .
  1319. "--alt-$boundary\r\n" .
  1320. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1321. "Content-Transfer-Encoding: 8bit\r\n" .
  1322. "\r\n" .
  1323. 'Hello' .
  1324. "\r\n" .
  1325. "\r\n" .
  1326. "\r\n" .
  1327. "--alt-$boundary\r\n" .
  1328. "Content-Type: text/html; charset=UTF-8\r\n" .
  1329. "Content-Transfer-Encoding: 8bit\r\n" .
  1330. "\r\n" .
  1331. 'Hello' .
  1332. "\r\n" .
  1333. "\r\n" .
  1334. "\r\n" .
  1335. "--alt-{$boundary}--\r\n" .
  1336. "\r\n" .
  1337. "--rel-$boundary\r\n" .
  1338. "Content-Disposition: inline; filename=\"cake.png\"\r\n" .
  1339. "Content-Type: text/plain\r\n" .
  1340. "Content-Transfer-Encoding: base64\r\n" .
  1341. "Content-ID: <abc123>\r\n" .
  1342. "\r\n";
  1343. $this->assertContains($expected, $result['message']);
  1344. $this->assertContains('--rel-' . $boundary . '--', $result['message']);
  1345. $this->assertContains('--' . $boundary . '--', $result['message']);
  1346. }
  1347. /**
  1348. * Test setting inline attachments and HTML only messages.
  1349. *
  1350. * @return void
  1351. */
  1352. public function testSendWithInlineAttachmentsHtmlOnly()
  1353. {
  1354. $this->Email->setTransport('debug');
  1355. $this->Email->setFrom('cake@cakephp.org');
  1356. $this->Email->setTo('cake@cakephp.org');
  1357. $this->Email->setSubject('My title');
  1358. $this->Email->setEmailFormat('html');
  1359. $this->Email->setAttachments([
  1360. 'cake.png' => [
  1361. 'file' => CORE_PATH . 'VERSION.txt',
  1362. 'contentId' => 'abc123'
  1363. ]
  1364. ]);
  1365. $result = $this->Email->send('Hello');
  1366. $boundary = $this->Email->getBoundary();
  1367. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1368. $expected = "--$boundary\r\n" .
  1369. "Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
  1370. "\r\n" .
  1371. "--rel-$boundary\r\n" .
  1372. "Content-Type: text/html; charset=UTF-8\r\n" .
  1373. "Content-Transfer-Encoding: 8bit\r\n" .
  1374. "\r\n" .
  1375. 'Hello' .
  1376. "\r\n" .
  1377. "\r\n" .
  1378. "\r\n" .
  1379. "--rel-$boundary\r\n" .
  1380. "Content-Disposition: inline; filename=\"cake.png\"\r\n" .
  1381. "Content-Type: text/plain\r\n" .
  1382. "Content-Transfer-Encoding: base64\r\n" .
  1383. "Content-ID: <abc123>\r\n" .
  1384. "\r\n";
  1385. $this->assertContains($expected, $result['message']);
  1386. $this->assertContains('--rel-' . $boundary . '--', $result['message']);
  1387. $this->assertContains('--' . $boundary . '--', $result['message']);
  1388. }
  1389. /**
  1390. * Test disabling content-disposition.
  1391. *
  1392. * @return void
  1393. */
  1394. public function testSendWithNoContentDispositionAttachments()
  1395. {
  1396. $this->Email->setTransport('debug');
  1397. $this->Email->setFrom('cake@cakephp.org');
  1398. $this->Email->setTo('cake@cakephp.org');
  1399. $this->Email->setSubject('My title');
  1400. $this->Email->setEmailFormat('text');
  1401. $this->Email->setAttachments([
  1402. 'cake.png' => [
  1403. 'file' => CORE_PATH . 'VERSION.txt',
  1404. 'contentDisposition' => false
  1405. ]
  1406. ]);
  1407. $result = $this->Email->send('Hello');
  1408. $boundary = $this->Email->getBoundary();
  1409. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1410. $expected = "--$boundary\r\n" .
  1411. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1412. "Content-Transfer-Encoding: 8bit\r\n" .
  1413. "\r\n" .
  1414. 'Hello' .
  1415. "\r\n" .
  1416. "\r\n" .
  1417. "\r\n" .
  1418. "--{$boundary}\r\n" .
  1419. "Content-Type: text/plain\r\n" .
  1420. "Content-Transfer-Encoding: base64\r\n" .
  1421. "\r\n";
  1422. $this->assertContains($expected, $result['message']);
  1423. $this->assertContains('--' . $boundary . '--', $result['message']);
  1424. }
  1425. /**
  1426. * testSendWithLog method
  1427. *
  1428. * @return void
  1429. */
  1430. public function testSendWithLog()
  1431. {
  1432. $log = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  1433. ->setMethods(['log'])
  1434. ->setConstructorArgs([['scopes' => 'email']])
  1435. ->getMock();
  1436. $message = 'Logging This';
  1437. $log->expects($this->once())
  1438. ->method('log')
  1439. ->with(
  1440. 'debug',
  1441. $this->logicalAnd(
  1442. $this->stringContains($message),
  1443. $this->stringContains('cake@cakephp.org'),
  1444. $this->stringContains('me@cakephp.org')
  1445. )
  1446. );
  1447. Log::setConfig('email', $log);
  1448. $this->Email->setTransport('debug');
  1449. $this->Email->setTo('me@cakephp.org');
  1450. $this->Email->setFrom('cake@cakephp.org');
  1451. $this->Email->setSubject('My title');
  1452. $this->Email->setProfile(['log' => 'debug']);
  1453. $result = $this->Email->send($message);
  1454. }
  1455. /**
  1456. * testSendWithLogAndScope method
  1457. *
  1458. * @return void
  1459. */
  1460. public function testSendWithLogAndScope()
  1461. {
  1462. $message = 'Logging This';
  1463. $log = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  1464. ->setMethods(['log'])
  1465. ->setConstructorArgs(['scopes' => ['email']])
  1466. ->getMock();
  1467. $log->expects($this->once())
  1468. ->method('log')
  1469. ->with(
  1470. 'debug',
  1471. $this->logicalAnd(
  1472. $this->stringContains($message),
  1473. $this->stringContains('cake@cakephp.org'),
  1474. $this->stringContains('me@cakephp.org')
  1475. )
  1476. );
  1477. Log::setConfig('email', $log);
  1478. $this->Email->setTransport('debug');
  1479. $this->Email->setTo('me@cakephp.org');
  1480. $this->Email->setFrom('cake@cakephp.org');
  1481. $this->Email->setSubject('My title');
  1482. $this->Email->setProfile(['log' => ['scope' => 'email']]);
  1483. $this->Email->send($message);
  1484. }
  1485. /**
  1486. * testSendRender method
  1487. *
  1488. * @return void
  1489. */
  1490. public function testSendRender()
  1491. {
  1492. $this->Email->reset();
  1493. $this->Email->setTransport('debug');
  1494. $this->Email->setFrom('cake@cakephp.org');
  1495. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1496. $this->Email->setSubject('My title');
  1497. $this->Email->setProfile(['empty']);
  1498. $this->Email->viewBuilder()->setTemplate('default', 'default');
  1499. $result = $this->Email->send();
  1500. $this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
  1501. $this->assertContains('Message-ID: ', $result['headers']);
  1502. $this->assertContains('To: ', $result['headers']);
  1503. }
  1504. /**
  1505. * test sending and rendering with no layout
  1506. *
  1507. * @return void
  1508. */
  1509. public function testSendRenderNoLayout()
  1510. {
  1511. $this->Email->reset();
  1512. $this->Email->setTransport('debug');
  1513. $this->Email->setFrom('cake@cakephp.org');
  1514. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1515. $this->Email->setSubject('My title');
  1516. $this->Email->setConfig(['empty']);
  1517. $this->Email->viewBuilder()
  1518. ->setTemplate('default')
  1519. ->setLayout(false);
  1520. $result = $this->Email->send('message body.');
  1521. $this->assertContains('message body.', $result['message']);
  1522. $this->assertNotContains('This email was sent using the CakePHP Framework', $result['message']);
  1523. }
  1524. /**
  1525. * testSendRender both method
  1526. *
  1527. * @return void
  1528. */
  1529. public function testSendRenderBoth()
  1530. {
  1531. $this->Email->reset();
  1532. $this->Email->setTransport('debug');
  1533. $this->Email->setFrom('cake@cakephp.org');
  1534. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1535. $this->Email->setSubject('My title');
  1536. $this->Email->setProfile(['empty']);
  1537. $this->Email->viewBuilder()->setTemplate('default', 'default');
  1538. $this->Email->setEmailFormat('both');
  1539. $result = $this->Email->send();
  1540. $this->assertContains('Message-ID: ', $result['headers']);
  1541. $this->assertContains('To: ', $result['headers']);
  1542. $boundary = $this->Email->getBoundary();
  1543. $this->assertContains('Content-Type: multipart/alternative; boundary="' . $boundary . '"', $result['headers']);
  1544. $expected = "--$boundary\r\n" .
  1545. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1546. "Content-Transfer-Encoding: 8bit\r\n" .
  1547. "\r\n" .
  1548. "\r\n" .
  1549. "\r\n" .
  1550. 'This email was sent using the CakePHP Framework, https://cakephp.org.' .
  1551. "\r\n" .
  1552. "\r\n" .
  1553. "--$boundary\r\n" .
  1554. "Content-Type: text/html; charset=UTF-8\r\n" .
  1555. "Content-Transfer-Encoding: 8bit\r\n" .
  1556. "\r\n" .
  1557. '<!DOCTYPE html';
  1558. $this->assertStringStartsWith($expected, $result['message']);
  1559. $expected = "</html>\r\n" .
  1560. "\r\n" .
  1561. "\r\n" .
  1562. "--$boundary--\r\n";
  1563. $this->assertStringEndsWith($expected, $result['message']);
  1564. }
  1565. /**
  1566. * testSendRender method for ISO-2022-JP
  1567. *
  1568. * @return void
  1569. */
  1570. public function testSendRenderJapanese()
  1571. {
  1572. $this->Email->reset();
  1573. $this->Email->setTransport('debug');
  1574. $this->Email->setFrom('cake@cakephp.org');
  1575. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1576. $this->Email->setSubject('My title');
  1577. $this->Email->setProfile(['empty']);
  1578. $this->Email->viewBuilder()->setTemplate('default');
  1579. $this->Email->viewBuilder()->setLayout('japanese');
  1580. $this->Email->charset = 'ISO-2022-JP';
  1581. $result = $this->Email->send();
  1582. $expected = mb_convert_encoding('CakePHP Framework を使って送信したメールです。 https://cakephp.org.', 'ISO-2022-JP');
  1583. $this->assertContains($expected, $result['message']);
  1584. $this->assertContains('Message-ID: ', $result['headers']);
  1585. $this->assertContains('To: ', $result['headers']);
  1586. }
  1587. /**
  1588. * testSendRenderThemed method
  1589. *
  1590. * @return void
  1591. */
  1592. public function testSendRenderThemed()
  1593. {
  1594. $this->loadPlugins(['TestTheme']);
  1595. $this->Email->reset();
  1596. $this->Email->setTransport('debug');
  1597. $this->Email->setFrom('cake@cakephp.org');
  1598. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1599. $this->Email->setSubject('My title');
  1600. $this->Email->setProfile(['empty']);
  1601. $this->Email->viewBuilder()->setTheme('TestTheme');
  1602. $this->Email->viewBuilder()->setTemplate('themed', 'default');
  1603. $result = $this->Email->send();
  1604. $this->assertContains('In TestTheme', $result['message']);
  1605. $this->assertContains('/test_theme/img/test.jpg', $result['message']);
  1606. $this->assertContains('Message-ID: ', $result['headers']);
  1607. $this->assertContains('To: ', $result['headers']);
  1608. $this->assertContains('/test_theme/img/test.jpg', $result['message']);
  1609. Plugin::unload();
  1610. }
  1611. /**
  1612. * testSendRenderWithHTML method and assert line length is kept below the required limit
  1613. *
  1614. * @return void
  1615. */
  1616. public function testSendRenderWithHTML()
  1617. {
  1618. $this->Email->reset();
  1619. $this->Email->setTransport('debug');
  1620. $this->Email->setFrom('cake@cakephp.org');
  1621. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1622. $this->Email->setSubject('My title');
  1623. $this->Email->setProfile(['empty']);
  1624. $this->Email->setEmailFormat('html');
  1625. $this->Email->viewBuilder()->setTemplate('html', 'default');
  1626. $result = $this->Email->send();
  1627. $this->assertTextContains('<h1>HTML Ipsum Presents</h1>', $result['message']);
  1628. $this->assertLineLengths($result['message']);
  1629. }
  1630. /**
  1631. * testSendRenderWithVars method
  1632. *
  1633. * @return void
  1634. */
  1635. public function testSendRenderWithVars()
  1636. {
  1637. $this->Email->reset();
  1638. $this->Email->setTransport('debug');
  1639. $this->Email->setFrom('cake@cakephp.org');
  1640. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1641. $this->Email->setSubject('My title');
  1642. $this->Email->setProfile(['empty']);
  1643. $this->Email->viewBuilder()->setTemplate('custom', 'default');
  1644. $this->Email->setViewVars(['value' => 12345]);
  1645. $result = $this->Email->send();
  1646. $this->assertContains('Here is your value: 12345', $result['message']);
  1647. }
  1648. /**
  1649. * testSendRenderWithVars method for ISO-2022-JP
  1650. *
  1651. * @return void
  1652. */
  1653. public function testSendRenderWithVarsJapanese()
  1654. {
  1655. $this->Email->reset();
  1656. $this->Email->setTransport('debug');
  1657. $this->Email->setFrom('cake@cakephp.org');
  1658. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1659. $this->Email->setSubject('My title');
  1660. $this->Email->setProfile(['empty']);
  1661. $this->Email->viewBuilder()->setTemplate('japanese', 'default');
  1662. $this->Email->setViewVars(['value' => '日本語の差し込み123']);
  1663. $this->Email->charset = 'ISO-2022-JP';
  1664. $result = $this->Email->send();
  1665. $expected = mb_convert_encoding('ここにあなたの設定した値が入ります: 日本語の差し込み123', 'ISO-2022-JP');
  1666. $this->assertContains($expected, $result['message']);
  1667. }
  1668. /**
  1669. * testSendRenderWithHelpers method
  1670. *
  1671. * @return void
  1672. */
  1673. public function testSendRenderWithHelpers()
  1674. {
  1675. $this->Email->reset();
  1676. $this->Email->setTransport('debug');
  1677. $timestamp = time();
  1678. $this->Email->setFrom('cake@cakephp.org');
  1679. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1680. $this->Email->setSubject('My title');
  1681. $this->Email->setProfile(['empty']);
  1682. $this->Email->viewBuilder()
  1683. ->setTemplate('custom_helper')
  1684. ->setLayout('default')
  1685. ->setHelpers(['Time'], false);
  1686. $this->Email->setViewVars(['time' => $timestamp]);
  1687. $result = $this->Email->send();
  1688. $dateTime = new \DateTime;
  1689. $dateTime->setTimestamp($timestamp);
  1690. $this->assertContains('Right now: ' . $dateTime->format($dateTime::ATOM), $result['message']);
  1691. $result = $this->Email->viewBuilder()->getHelpers();
  1692. $this->assertEquals(['Time'], $result);
  1693. }
  1694. /**
  1695. * testSendRenderWithImage method
  1696. *
  1697. * @return void
  1698. */
  1699. public function testSendRenderWithImage()
  1700. {
  1701. $this->Email->reset();
  1702. $this->Email->setTransport('debug');
  1703. $this->Email->setFrom('cake@cakephp.org');
  1704. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1705. $this->Email->setSubject('My title');
  1706. $this->Email->setProfile(['empty']);
  1707. $this->Email->viewBuilder()->setTemplate('image');
  1708. $this->Email->setEmailFormat('html');
  1709. $server = env('SERVER_NAME') ? env('SERVER_NAME') : 'localhost';
  1710. if (env('SERVER_PORT') && env('SERVER_PORT') != 80) {
  1711. $server .= ':' . env('SERVER_PORT');
  1712. }
  1713. $expected = '<img src="http://' . $server . '/img/image.gif" alt="cool image" width="100" height="100"';
  1714. $result = $this->Email->send();
  1715. $this->assertContains($expected, $result['message']);
  1716. }
  1717. /**
  1718. * testSendRenderPlugin method
  1719. *
  1720. * @return void
  1721. */
  1722. public function testSendRenderPlugin()
  1723. {
  1724. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'TestTheme']);
  1725. $this->Email->reset();
  1726. $this->Email->setTransport('debug');
  1727. $this->Email->setFrom('cake@cakephp.org');
  1728. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1729. $this->Email->setSubject('My title');
  1730. $this->Email->setProfile(['empty']);
  1731. $this->Email->viewBuilder()
  1732. ->setTemplate('TestPlugin.test_plugin_tpl')
  1733. ->setLayout('default');
  1734. $result = $this->Email->send();
  1735. $this->assertContains('Into TestPlugin.', $result['message']);
  1736. $this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
  1737. $this->Email->viewBuilder()
  1738. ->setTemplate('TestPlugin.test_plugin_tpl')
  1739. ->setLayout('TestPlugin.plug_default');
  1740. $result = $this->Email->send();
  1741. $this->assertContains('Into TestPlugin.', $result['message']);
  1742. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1743. $this->Email->viewBuilder()
  1744. ->setTemplate('TestPlugin.test_plugin_tpl')
  1745. ->setLayout('plug_default');
  1746. $result = $this->Email->send();
  1747. $this->assertContains('Into TestPlugin.', $result['message']);
  1748. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1749. $this->Email->viewBuilder()
  1750. ->setTemplate('TestPlugin.test_plugin_tpl')
  1751. ->setLayout('TestPluginTwo.default');
  1752. $result = $this->Email->send();
  1753. $this->assertContains('Into TestPlugin.', $result['message']);
  1754. $this->assertContains('This email was sent using TestPluginTwo.', $result['message']);
  1755. // test plugin template overridden by theme
  1756. $this->Email->viewBuilder()->setTheme('TestTheme');
  1757. $result = $this->Email->send();
  1758. $this->assertContains('Into TestPlugin. (themed)', $result['message']);
  1759. $this->Email->setViewVars(['value' => 12345]);
  1760. $this->Email->viewBuilder()
  1761. ->setTemplate('custom')
  1762. ->setLayout('TestPlugin.plug_default');
  1763. $result = $this->Email->send();
  1764. $this->assertContains('Here is your value: 12345', $result['message']);
  1765. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1766. Plugin::unload();
  1767. }
  1768. /**
  1769. * Test that a MissingTemplateException is thrown
  1770. *
  1771. * @return void
  1772. * @expectedException \Cake\View\Exception\MissingTemplateException
  1773. */
  1774. public function testMissingTemplateException()
  1775. {
  1776. $this->Email->reset();
  1777. $this->Email->setTransport('debug');
  1778. $this->Email->setFrom('cake@cakephp.org');
  1779. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1780. $this->Email->setSubject('My title');
  1781. $this->Email->viewBuilder()->setTemplate('fooo');
  1782. $this->Email->send();
  1783. }
  1784. /**
  1785. * testSendMultipleMIME method
  1786. *
  1787. * @return void
  1788. */
  1789. public function testSendMultipleMIME()
  1790. {
  1791. $this->Email->reset();
  1792. $this->Email->setTransport('debug');
  1793. $this->Email->setFrom('cake@cakephp.org');
  1794. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1795. $this->Email->setSubject('My title');
  1796. $this->Email->viewBuilder()->setTemplate('custom', 'default');
  1797. $this->Email->setProfile([]);
  1798. $this->Email->setViewVars(['value' => 12345]);
  1799. $this->Email->setEmailFormat('both');
  1800. $this->Email->send();
  1801. $message = $this->Email->message();
  1802. $boundary = $this->Email->getBoundary();
  1803. $this->assertNotEmpty($boundary);
  1804. $this->assertContains('--' . $boundary, $message);
  1805. $this->assertContains('--' . $boundary . '--', $message);
  1806. $this->Email->setAttachments(['fake.php' => __FILE__]);
  1807. $this->Email->send();
  1808. $message = $this->Email->message();
  1809. $boundary = $this->Email->getBoundary();
  1810. $this->assertNotEmpty($boundary);
  1811. $this->assertContains('--' . $boundary, $message);
  1812. $this->assertContains('--' . $boundary . '--', $message);
  1813. $this->assertContains('--alt-' . $boundary, $message);
  1814. $this->assertContains('--alt-' . $boundary . '--', $message);
  1815. }
  1816. /**
  1817. * testSendAttachment method
  1818. *
  1819. * @return void
  1820. */
  1821. public function testSendAttachment()
  1822. {
  1823. $this->Email->reset();
  1824. $this->Email->setTransport('debug');
  1825. $this->Email->setFrom('cake@cakephp.org');
  1826. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1827. $this->Email->setSubject('My title');
  1828. $this->Email->setProfile([]);
  1829. $this->Email->setAttachments([CAKE . 'basics.php']);
  1830. $result = $this->Email->send('body');
  1831. $expected = "Content-Disposition: attachment; filename=\"basics.php\"\r\n" .
  1832. "Content-Type: text/x-php\r\n" .
  1833. "Content-Transfer-Encoding: base64\r\n";
  1834. $this->assertContains($expected, $result['message']);
  1835. $this->Email->setAttachments(['my.file.txt' => CAKE . 'basics.php']);
  1836. $result = $this->Email->send('body');
  1837. $expected = "Content-Disposition: attachment; filename=\"my.file.txt\"\r\n" .
  1838. "Content-Type: text/x-php\r\n" .
  1839. "Content-Transfer-Encoding: base64\r\n";
  1840. $this->assertContains($expected, $result['message']);
  1841. $this->Email->setAttachments(['file.txt' => ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain']]);
  1842. $result = $this->Email->send('body');
  1843. $expected = "Content-Disposition: attachment; filename=\"file.txt\"\r\n" .
  1844. "Content-Type: text/plain\r\n" .
  1845. "Content-Transfer-Encoding: base64\r\n";
  1846. $this->assertContains($expected, $result['message']);
  1847. $this->Email->setAttachments(['file2.txt' => ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain', 'contentId' => 'a1b1c1']]);
  1848. $result = $this->Email->send('body');
  1849. $expected = "Content-Disposition: inline; filename=\"file2.txt\"\r\n" .
  1850. "Content-Type: text/plain\r\n" .
  1851. "Content-Transfer-Encoding: base64\r\n" .
  1852. "Content-ID: <a1b1c1>\r\n";
  1853. $this->assertContains($expected, $result['message']);
  1854. }
  1855. /**
  1856. * testDeliver method
  1857. *
  1858. * @return void
  1859. */
  1860. public function testDeliver()
  1861. {
  1862. $this->deprecated(function () {
  1863. Email::dropTransport('default');
  1864. Email::setConfigTransport('default', ['className' => 'Debug']);
  1865. });
  1866. $instance = Email::deliver('all@cakephp.org', 'About', 'Everything ok', ['from' => 'root@cakephp.org'], false);
  1867. $this->assertInstanceOf('Cake\Mailer\Email', $instance);
  1868. $this->assertSame($instance->getTo(), ['all@cakephp.org' => 'all@cakephp.org']);
  1869. $this->assertSame($instance->getSubject(), 'About');
  1870. $this->assertSame($instance->getFrom(), ['root@cakephp.org' => 'root@cakephp.org']);
  1871. $this->assertInstanceOf('Cake\Mailer\AbstractTransport', $instance->getTransport());
  1872. $config = [
  1873. 'from' => 'cake@cakephp.org',
  1874. 'to' => 'debug@cakephp.org',
  1875. 'subject' => 'Update ok',
  1876. 'template' => 'custom',
  1877. 'layout' => 'custom_layout',
  1878. 'viewVars' => ['value' => 123],
  1879. 'cc' => ['cake@cakephp.org' => 'Myself']
  1880. ];
  1881. $instance = Email::deliver(null, null, ['name' => 'CakePHP'], $config, false);
  1882. $this->assertSame($instance->getFrom(), ['cake@cakephp.org' => 'cake@cakephp.org']);
  1883. $this->assertSame($instance->getTo(), ['debug@cakephp.org' => 'debug@cakephp.org']);
  1884. $this->assertSame($instance->getSubject(), 'Update ok');
  1885. $this->assertSame($instance->viewBuilder()->getTemplate(), 'custom');
  1886. $this->assertSame($instance->viewBuilder()->getLayout(), 'custom_layout');
  1887. $this->assertEquals($instance->getViewVars(), ['value' => 123, 'name' => 'CakePHP']);
  1888. $this->assertSame($instance->getCc(), ['cake@cakephp.org' => 'Myself']);
  1889. $configs = ['from' => 'root@cakephp.org', 'message' => 'Message from configs', 'transport' => 'debug'];
  1890. $instance = Email::deliver('all@cakephp.org', 'About', null, $configs, true);
  1891. $message = $instance->message();
  1892. $this->assertEquals($configs['message'], $message[0]);
  1893. }
  1894. /**
  1895. * testMessage method
  1896. *
  1897. * @return void
  1898. */
  1899. public function testMessage()
  1900. {
  1901. $this->Email->reset();
  1902. $this->Email->setTransport('debug');
  1903. $this->Email->setFrom('cake@cakephp.org');
  1904. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1905. $this->Email->setSubject('My title');
  1906. $this->Email->setProfile(['empty']);
  1907. $this->Email->viewBuilder()->setTemplate('default', 'default');
  1908. $this->Email->setEmailFormat('both');
  1909. $this->Email->send();
  1910. $expected = '<p>This email was sent using the <a href="https://cakephp.org">CakePHP Framework</a></p>';
  1911. $this->assertContains($expected, $this->Email->message(Email::MESSAGE_HTML));
  1912. $expected = 'This email was sent using the CakePHP Framework, https://cakephp.org.';
  1913. $this->assertContains($expected, $this->Email->message(Email::MESSAGE_TEXT));
  1914. $message = $this->Email->message();
  1915. $this->assertContains('Content-Type: text/plain; charset=UTF-8', $message);
  1916. $this->assertContains('Content-Type: text/html; charset=UTF-8', $message);
  1917. // UTF-8 is 8bit
  1918. $this->assertTrue($this->_checkContentTransferEncoding($message, '8bit'));
  1919. $this->Email->charset = 'ISO-2022-JP';
  1920. $this->Email->send();
  1921. $message = $this->Email->message();
  1922. $this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $message);
  1923. $this->assertContains('Content-Type: text/html; charset=ISO-2022-JP', $message);
  1924. // ISO-2022-JP is 7bit
  1925. $this->assertTrue($this->_checkContentTransferEncoding($message, '7bit'));
  1926. }
  1927. /**
  1928. * testReset method
  1929. *
  1930. * @return void
  1931. */
  1932. public function testReset()
  1933. {
  1934. $this->Email->setTo('cake@cakephp.org');
  1935. $this->Email->viewBuilder()->setTheme('TestTheme');
  1936. $this->Email->setEmailPattern('/.+@.+\..+/i');
  1937. $this->assertSame(['cake@cakephp.org' => 'cake@cakephp.org'], $this->Email->getTo());
  1938. $this->Email->reset();
  1939. $this->assertSame([], $this->Email->getTo());
  1940. $this->assertFalse($this->Email->viewBuilder()->getTheme());
  1941. $this->assertSame(Email::EMAIL_PATTERN, $this->Email->getEmailPattern());
  1942. }
  1943. /**
  1944. * testReset with charset
  1945. *
  1946. * @return void
  1947. */
  1948. public function testResetWithCharset()
  1949. {
  1950. $this->Email->charset = 'ISO-2022-JP';
  1951. $this->Email->reset();
  1952. $this->assertSame('utf-8', $this->Email->charset, $this->Email->charset);
  1953. $this->assertNull($this->Email->headerCharset, $this->Email->headerCharset);
  1954. }
  1955. /**
  1956. * testWrap method
  1957. *
  1958. * @return void
  1959. */
  1960. public function testWrap()
  1961. {
  1962. $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
  1963. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  1964. $expected = [
  1965. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
  1966. 'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
  1967. ''
  1968. ];
  1969. $this->assertSame($expected, $result);
  1970. $text = 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan amet.';
  1971. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  1972. $expected = [
  1973. 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
  1974. 'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
  1975. 'amet.',
  1976. ''
  1977. ];
  1978. $this->assertSame($expected, $result);
  1979. $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>';
  1980. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  1981. $expected = [
  1982. '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac',
  1983. 'turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula',
  1984. 'pellentesque accumsan amet.<hr></p>',
  1985. ''
  1986. ];
  1987. $this->assertSame($expected, $result);
  1988. $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.';
  1989. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  1990. $expected = [
  1991. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac',
  1992. '<a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh',
  1993. 'nisi, vehicula pellentesque accumsan amet.',
  1994. ''
  1995. ];
  1996. $this->assertSame($expected, $result);
  1997. $text = 'Lorem ipsum <a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">ok</a>';
  1998. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  1999. $expected = [
  2000. 'Lorem ipsum',
  2001. '<a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">',
  2002. 'ok</a>',
  2003. ''
  2004. ];
  2005. $this->assertSame($expected, $result);
  2006. $text = 'Lorem ipsum withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite ok.';
  2007. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  2008. $expected = [
  2009. 'Lorem ipsum',
  2010. 'withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite',
  2011. 'ok.',
  2012. ''
  2013. ];
  2014. $this->assertSame($expected, $result);
  2015. }
  2016. /**
  2017. * testRender method
  2018. *
  2019. * @return void
  2020. */
  2021. public function testRenderWithLayoutAndAttachment()
  2022. {
  2023. $this->Email->setEmailFormat('html');
  2024. $this->Email->viewBuilder()->setTemplate('html', 'default');
  2025. $this->Email->setAttachments([CAKE . 'basics.php']);
  2026. $result = $this->Email->render([]);
  2027. $this->assertNotEmpty($result);
  2028. $result = $this->Email->getBoundary();
  2029. $this->assertRegExp('/^[0-9a-f]{32}$/', $result);
  2030. }
  2031. /**
  2032. * testConstructWithConfigArray method
  2033. *
  2034. * @return void
  2035. */
  2036. public function testConstructWithConfigArray()
  2037. {
  2038. $configs = [
  2039. 'from' => ['some@example.com' => 'My website'],
  2040. 'to' => 'test@example.com',
  2041. 'subject' => 'Test mail subject',
  2042. 'transport' => 'debug',
  2043. ];
  2044. $this->Email = new Email($configs);
  2045. $result = $this->Email->getTo();
  2046. $this->assertEquals([$configs['to'] => $configs['to']], $result);
  2047. $result = $this->Email->getFrom();
  2048. $this->assertEquals($configs['from'], $result);
  2049. $result = $this->Email->getSubject();
  2050. $this->assertEquals($configs['subject'], $result);
  2051. $result = $this->Email->getTransport();
  2052. $this->assertInstanceOf('Cake\Mailer\Transport\DebugTransport', $result);
  2053. $result = $this->Email->send('This is the message');
  2054. $this->assertContains('Message-ID: ', $result['headers']);
  2055. $this->assertContains('To: ', $result['headers']);
  2056. }
  2057. /**
  2058. * testConfigArrayWithLayoutWithoutTemplate method
  2059. *
  2060. * @return void
  2061. */
  2062. public function testConfigArrayWithLayoutWithoutTemplate()
  2063. {
  2064. $configs = [
  2065. 'from' => ['some@example.com' => 'My website'],
  2066. 'to' => 'test@example.com',
  2067. 'subject' => 'Test mail subject',
  2068. 'transport' => 'debug',
  2069. 'layout' => 'custom'
  2070. ];
  2071. $this->Email = new Email($configs);
  2072. $template = $this->Email->viewBuilder()->getTemplate();
  2073. $layout = $this->Email->viewBuilder()->getLayout();
  2074. $this->assertEquals('', $template);
  2075. $this->assertEquals($configs['layout'], $layout);
  2076. }
  2077. /**
  2078. * testConstructWithConfigString method
  2079. *
  2080. * @return void
  2081. */
  2082. public function testConstructWithConfigString()
  2083. {
  2084. $configs = [
  2085. 'from' => ['some@example.com' => 'My website'],
  2086. 'to' => 'test@example.com',
  2087. 'subject' => 'Test mail subject',
  2088. 'transport' => 'debug',
  2089. ];
  2090. Email::setConfig('test', $configs);
  2091. $this->Email = new Email('test');
  2092. $result = $this->Email->getTo();
  2093. $this->assertEquals([$configs['to'] => $configs['to']], $result);
  2094. $result = $this->Email->getFrom();
  2095. $this->assertEquals($configs['from'], $result);
  2096. $result = $this->Email->getSubject();
  2097. $this->assertEquals($configs['subject'], $result);
  2098. $result = $this->Email->getTransport();
  2099. $this->assertInstanceOf('Cake\Mailer\Transport\DebugTransport', $result);
  2100. $result = $this->Email->send('This is the message');
  2101. $this->assertContains('Message-ID: ', $result['headers']);
  2102. $this->assertContains('To: ', $result['headers']);
  2103. }
  2104. /**
  2105. * testViewRender method
  2106. *
  2107. * @return void
  2108. */
  2109. public function testViewRender()
  2110. {
  2111. $result = $this->Email->getViewRenderer();
  2112. $this->assertEquals('Cake\View\View', $result);
  2113. $result = $this->Email->setViewRenderer('Cake\View\ThemeView');
  2114. $this->assertInstanceOf('Cake\Mailer\Email', $result);
  2115. $result = $this->Email->getViewRenderer();
  2116. $this->assertEquals('Cake\View\ThemeView', $result);
  2117. }
  2118. /**
  2119. * testEmailFormat method
  2120. *
  2121. * @return void
  2122. */
  2123. public function testEmailFormat()
  2124. {
  2125. $result = $this->Email->getEmailFormat();
  2126. $this->assertEquals('text', $result);
  2127. $result = $this->Email->setEmailFormat('html');
  2128. $this->assertInstanceOf('Cake\Mailer\Email', $result);
  2129. $result = $this->Email->getEmailFormat();
  2130. $this->assertEquals('html', $result);
  2131. $this->expectException(\InvalidArgumentException::class);
  2132. $result = $this->Email->setEmailFormat('invalid');
  2133. }
  2134. /**
  2135. * Tests that it is possible to add charset configuration to a CakeEmail object
  2136. *
  2137. * @return void
  2138. */
  2139. public function testConfigCharset()
  2140. {
  2141. $email = new Email();
  2142. $this->assertEquals(Configure::read('App.encoding'), $email->charset);
  2143. $this->assertEquals(Configure::read('App.encoding'), $email->headerCharset);
  2144. $email = new Email(['charset' => 'iso-2022-jp', 'headerCharset' => 'iso-2022-jp-ms']);
  2145. $this->assertEquals('iso-2022-jp', $email->charset);
  2146. $this->assertEquals('iso-2022-jp-ms', $email->headerCharset);
  2147. $email = new Email(['charset' => 'iso-2022-jp']);
  2148. $this->assertEquals('iso-2022-jp', $email->charset);
  2149. $this->assertEquals('iso-2022-jp', $email->headerCharset);
  2150. $email = new Email(['headerCharset' => 'iso-2022-jp-ms']);
  2151. $this->assertEquals(Configure::read('App.encoding'), $email->charset);
  2152. $this->assertEquals('iso-2022-jp-ms', $email->headerCharset);
  2153. }
  2154. /**
  2155. * Tests that the header is encoded using the configured headerCharset
  2156. *
  2157. * @return void
  2158. */
  2159. public function testHeaderEncoding()
  2160. {
  2161. $email = new Email(['headerCharset' => 'iso-2022-jp-ms', 'transport' => 'debug']);
  2162. $email->setSubject('あれ?もしかしての前と');
  2163. $headers = $email->getHeaders(['subject']);
  2164. $expected = '?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=';
  2165. $this->assertContains($expected, $headers['Subject']);
  2166. $email->setTo('someone@example.com')->setFrom('someone@example.com');
  2167. $result = $email->send('ってテーブルを作ってやってたらう');
  2168. $this->assertContains('ってテーブルを作ってやってたらう', $result['message']);
  2169. }
  2170. /**
  2171. * Tests that the body is encoded using the configured charset
  2172. *
  2173. * @return void
  2174. */
  2175. public function testBodyEncoding()
  2176. {
  2177. $email = new Email([
  2178. 'charset' => 'iso-2022-jp',
  2179. 'headerCharset' => 'iso-2022-jp-ms',
  2180. 'transport' => 'debug'
  2181. ]);
  2182. $email->setSubject('あれ?もしかしての前と');
  2183. $headers = $email->getHeaders(['subject']);
  2184. $expected = '?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=';
  2185. $this->assertContains($expected, $headers['Subject']);
  2186. $email->setTo('someone@example.com')->setFrom('someone@example.com');
  2187. $result = $email->send('ってテーブルを作ってやってたらう');
  2188. $this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $result['headers']);
  2189. $this->assertContains(mb_convert_encoding('ってテーブルを作ってやってたらう', 'ISO-2022-JP'), $result['message']);
  2190. }
  2191. /**
  2192. * Tests that the body is encoded using the configured charset (Japanese standard encoding)
  2193. *
  2194. * @return void
  2195. */
  2196. public function testBodyEncodingIso2022Jp()
  2197. {
  2198. $email = new Email([
  2199. 'charset' => 'iso-2022-jp',
  2200. 'headerCharset' => 'iso-2022-jp',
  2201. 'transport' => 'debug'
  2202. ]);
  2203. $email->setSubject('あれ?もしかしての前と');
  2204. $headers = $email->getHeaders(['subject']);
  2205. $expected = '?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=';
  2206. $this->assertContains($expected, $headers['Subject']);
  2207. $email->setTo('someone@example.com')->setFrom('someone@example.com');
  2208. $result = $email->send('①㈱');
  2209. $this->assertTextContains('Content-Type: text/plain; charset=ISO-2022-JP', $result['headers']);
  2210. $this->assertTextNotContains('Content-Type: text/plain; charset=ISO-2022-JP-MS', $result['headers']); // not charset=iso-2022-jp-ms
  2211. $this->assertTextNotContains(mb_convert_encoding('①㈱', 'ISO-2022-JP-MS'), $result['message']);
  2212. }
  2213. /**
  2214. * Tests that the body is encoded using the configured charset (Japanese irregular encoding, but sometime use this)
  2215. *
  2216. * @return void
  2217. */
  2218. public function testBodyEncodingIso2022JpMs()
  2219. {
  2220. $email = new Email([
  2221. 'charset' => 'iso-2022-jp-ms',
  2222. 'headerCharset' => 'iso-2022-jp-ms',
  2223. 'transport' => 'debug'
  2224. ]);
  2225. $email->setSubject('あれ?もしかしての前と');
  2226. $headers = $email->getHeaders(['subject']);
  2227. $expected = '?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=';
  2228. $this->assertContains($expected, $headers['Subject']);
  2229. $email->setTo('someone@example.com')->setFrom('someone@example.com');
  2230. $result = $email->send('①㈱');
  2231. $this->assertTextContains('Content-Type: text/plain; charset=ISO-2022-JP', $result['headers']);
  2232. $this->assertTextNotContains('Content-Type: text/plain; charset=iso-2022-jp-ms', $result['headers']); // not charset=iso-2022-jp-ms
  2233. $this->assertContains(mb_convert_encoding('①㈱', 'ISO-2022-JP-MS'), $result['message']);
  2234. }
  2235. protected function _checkContentTransferEncoding($message, $charset)
  2236. {
  2237. $boundary = '--' . $this->Email->getBoundary();
  2238. $result['text'] = false;
  2239. $result['html'] = false;
  2240. $length = count($message);
  2241. for ($i = 0; $i < $length; ++$i) {
  2242. if ($message[$i] === $boundary) {
  2243. $flag = false;
  2244. $type = '';
  2245. while (!preg_match('/^$/', $message[$i])) {
  2246. if (preg_match('/^Content-Type: text\/plain/', $message[$i])) {
  2247. $type = 'text';
  2248. }
  2249. if (preg_match('/^Content-Type: text\/html/', $message[$i])) {
  2250. $type = 'html';
  2251. }
  2252. if ($message[$i] === 'Content-Transfer-Encoding: ' . $charset) {
  2253. $flag = true;
  2254. }
  2255. ++$i;
  2256. }
  2257. $result[$type] = $flag;
  2258. }
  2259. }
  2260. return $result['text'] && $result['html'];
  2261. }
  2262. /**
  2263. * Test CakeEmail::_encode function
  2264. *
  2265. * @return void
  2266. */
  2267. public function testEncode()
  2268. {
  2269. $this->Email->headerCharset = 'ISO-2022-JP';
  2270. $result = $this->Email->encode('日本語');
  2271. $expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?=';
  2272. $this->assertSame($expected, $result);
  2273. $this->Email->headerCharset = 'ISO-2022-JP';
  2274. $result = $this->Email->encode('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?');
  2275. $expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
  2276. " =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
  2277. ' =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=';
  2278. $this->assertSame($expected, $result);
  2279. }
  2280. /**
  2281. * Test CakeEmail::_decode function
  2282. *
  2283. * @return void
  2284. */
  2285. public function testDecode()
  2286. {
  2287. $this->Email->headerCharset = 'ISO-2022-JP';
  2288. $result = $this->Email->decode('=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?=');
  2289. $expected = '日本語';
  2290. $this->assertSame($expected, $result);
  2291. $this->Email->headerCharset = 'ISO-2022-JP';
  2292. $result = $this->Email->decode("=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
  2293. " =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
  2294. ' =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=');
  2295. $expected = '長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?';
  2296. $this->assertSame($expected, $result);
  2297. }
  2298. /**
  2299. * Tests charset setter/getter
  2300. *
  2301. * @return void
  2302. */
  2303. public function testCharset()
  2304. {
  2305. $this->Email->setCharset('UTF-8');
  2306. $this->assertSame($this->Email->getCharset(), 'UTF-8');
  2307. $this->Email->setCharset('ISO-2022-JP');
  2308. $this->assertSame($this->Email->getCharset(), 'ISO-2022-JP');
  2309. $charset = $this->Email->setCharset('Shift_JIS');
  2310. $this->assertSame('Shift_JIS', $charset->getCharset());
  2311. }
  2312. /**
  2313. * Tests headerCharset setter/getter
  2314. *
  2315. * @return void
  2316. */
  2317. public function testHeaderCharset()
  2318. {
  2319. $this->Email->setHeaderCharset('UTF-8');
  2320. $this->assertSame($this->Email->getHeaderCharset(), 'UTF-8');
  2321. $this->Email->setHeaderCharset('ISO-2022-JP');
  2322. $this->assertSame($this->Email->getHeaderCharset(), 'ISO-2022-JP');
  2323. $charset = $this->Email->setHeaderCharset('Shift_JIS');
  2324. $this->assertSame('Shift_JIS', $charset->getHeaderCharset());
  2325. }
  2326. /**
  2327. * Test transferEncoding
  2328. *
  2329. * @return void
  2330. */
  2331. public function testTransferEncoding()
  2332. {
  2333. // Test new transfer encoding
  2334. $expected = 'quoted-printable';
  2335. $this->Email->setTransferEncoding($expected);
  2336. $this->assertSame($expected, $this->Email->getTransferEncoding());
  2337. $this->assertSame($expected, $this->Email->getContentTransferEncoding());
  2338. // Test default charset/encoding : utf8/8bit
  2339. $expected = '8bit';
  2340. $this->Email->reset();
  2341. $this->assertNull($this->Email->getTransferEncoding());
  2342. $this->assertSame($expected, $this->Email->getContentTransferEncoding());
  2343. //Test wrong encoding
  2344. $this->expectException(\InvalidArgumentException::class);
  2345. $this->Email->setTransferEncoding('invalid');
  2346. }
  2347. /**
  2348. * Tests for compatible check.
  2349. * charset property and charset() method.
  2350. * headerCharset property and headerCharset() method.
  2351. *
  2352. * @return void
  2353. */
  2354. public function testCharsetsCompatible()
  2355. {
  2356. $checkHeaders = [
  2357. 'from' => true,
  2358. 'to' => true,
  2359. 'cc' => true,
  2360. 'subject' => true,
  2361. ];
  2362. // Header Charset : null (used by default UTF-8)
  2363. // Body Charset : ISO-2022-JP
  2364. $oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', null);
  2365. $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
  2366. $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', null);
  2367. $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
  2368. $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
  2369. $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
  2370. $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
  2371. $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
  2372. // Header Charset : UTF-8
  2373. // Boby Charset : ISO-2022-JP
  2374. $oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', 'utf-8');
  2375. $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
  2376. $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', 'utf-8');
  2377. $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
  2378. $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
  2379. $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
  2380. $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
  2381. $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
  2382. // Header Charset : ISO-2022-JP
  2383. // Boby Charset : UTF-8
  2384. $oldStyleEmail = $this->_getEmailByOldStyleCharset('utf-8', 'iso-2022-jp');
  2385. $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
  2386. $newStyleEmail = $this->_getEmailByNewStyleCharset('utf-8', 'iso-2022-jp');
  2387. $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
  2388. $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
  2389. $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
  2390. $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
  2391. $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
  2392. }
  2393. /**
  2394. * @param mixed $charset
  2395. * @param mixed $headerCharset
  2396. * @return \Cake\Mailer\Email
  2397. */
  2398. protected function _getEmailByOldStyleCharset($charset, $headerCharset)
  2399. {
  2400. $email = new Email(['transport' => 'debug']);
  2401. if (! empty($charset)) {
  2402. $email->charset = $charset;
  2403. }
  2404. if (! empty($headerCharset)) {
  2405. $email->headerCharset = $headerCharset;
  2406. }
  2407. $email->setFrom('someone@example.com', 'どこかの誰か');
  2408. $email->setTo('someperson@example.jp', 'どこかのどなたか');
  2409. $email->setCc('miku@example.net', 'ミク');
  2410. $email->setSubject('テストメール');
  2411. $email->send('テストメールの本文');
  2412. return $email;
  2413. }
  2414. /**
  2415. * @param mixed $charset
  2416. * @param mixed $headerCharset
  2417. * @return \Cake\Mailer\Email
  2418. */
  2419. protected function _getEmailByNewStyleCharset($charset, $headerCharset)
  2420. {
  2421. $email = new Email(['transport' => 'debug']);
  2422. if (! empty($charset)) {
  2423. $email->setCharset($charset);
  2424. }
  2425. if (! empty($headerCharset)) {
  2426. $email->setHeaderCharset($headerCharset);
  2427. }
  2428. $email->setFrom('someone@example.com', 'どこかの誰か');
  2429. $email->setTo('someperson@example.jp', 'どこかのどなたか');
  2430. $email->setCc('miku@example.net', 'ミク');
  2431. $email->setSubject('テストメール');
  2432. $email->send('テストメールの本文');
  2433. return $email;
  2434. }
  2435. /**
  2436. * testWrapLongLine()
  2437. *
  2438. * @return void
  2439. */
  2440. public function testWrapLongLine()
  2441. {
  2442. $message = '<a href="http://cakephp.org">' . str_repeat('x', Email::LINE_LENGTH_MUST) . '</a>';
  2443. $this->Email->reset();
  2444. $this->Email->setTransport('debug');
  2445. $this->Email->setFrom('cake@cakephp.org');
  2446. $this->Email->setTo('cake@cakephp.org');
  2447. $this->Email->setSubject('Wordwrap Test');
  2448. $this->Email->setProfile(['empty']);
  2449. $result = $this->Email->send($message);
  2450. $expected = "<a\r\n" . 'href="http://cakephp.org">' . str_repeat('x', Email::LINE_LENGTH_MUST - 26) . "\r\n" .
  2451. str_repeat('x', 26) . "\r\n</a>\r\n\r\n";
  2452. $this->assertEquals($expected, $result['message']);
  2453. $this->assertLineLengths($result['message']);
  2454. $str1 = 'a ';
  2455. $str2 = ' b';
  2456. $length = strlen($str1) + strlen($str2);
  2457. $message = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length - 1) . $str2;
  2458. $result = $this->Email->send($message);
  2459. $expected = "{$message}\r\n\r\n";
  2460. $this->assertEquals($expected, $result['message']);
  2461. $this->assertLineLengths($result['message']);
  2462. $message = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length) . $str2;
  2463. $result = $this->Email->send($message);
  2464. $expected = "{$message}\r\n\r\n";
  2465. $this->assertEquals($expected, $result['message']);
  2466. $this->assertLineLengths($result['message']);
  2467. $message = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length + 1) . $str2;
  2468. $result = $this->Email->send($message);
  2469. $expected = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length + 1) . sprintf("\r\n%s\r\n\r\n", trim($str2));
  2470. $this->assertEquals($expected, $result['message']);
  2471. $this->assertLineLengths($result['message']);
  2472. }
  2473. /**
  2474. * testWrapWithTagsAcrossLines()
  2475. *
  2476. * @return void
  2477. */
  2478. public function testWrapWithTagsAcrossLines()
  2479. {
  2480. $str = <<<HTML
  2481. <table>
  2482. <th align="right" valign="top"
  2483. style="font-weight: bold">The tag is across multiple lines</th>
  2484. </table>
  2485. HTML;
  2486. $length = strlen($str);
  2487. $message = $str . str_repeat('x', Email::LINE_LENGTH_MUST + 1);
  2488. $this->Email->reset();
  2489. $this->Email->setTransport('debug');
  2490. $this->Email->setFrom('cake@cakephp.org');
  2491. $this->Email->setTo('cake@cakephp.org');
  2492. $this->Email->setSubject('Wordwrap Test');
  2493. $this->Email->setProfile(['empty']);
  2494. $result = $this->Email->send($message);
  2495. $message = str_replace("\r\n", "\n", substr($message, 0, -9));
  2496. $message = str_replace("\n", "\r\n", $message);
  2497. $expected = "{$message}\r\nxxxxxxxxx\r\n\r\n";
  2498. $this->assertEquals($expected, $result['message']);
  2499. $this->assertLineLengths($result['message']);
  2500. }
  2501. /**
  2502. * CakeEmailTest::testWrapIncludeLessThanSign()
  2503. *
  2504. * @return void
  2505. */
  2506. public function testWrapIncludeLessThanSign()
  2507. {
  2508. $str = 'foo<bar';
  2509. $length = strlen($str);
  2510. $message = $str . str_repeat('x', Email::LINE_LENGTH_MUST - $length + 1);
  2511. $this->Email->reset();
  2512. $this->Email->setTransport('debug');
  2513. $this->Email->setFrom('cake@cakephp.org');
  2514. $this->Email->setTo('cake@cakephp.org');
  2515. $this->Email->setSubject('Wordwrap Test');
  2516. $this->Email->setProfile(['empty']);
  2517. $result = $this->Email->send($message);
  2518. $message = substr($message, 0, -1);
  2519. $expected = "{$message}\r\nx\r\n\r\n";
  2520. $this->assertEquals($expected, $result['message']);
  2521. $this->assertLineLengths($result['message']);
  2522. }
  2523. /**
  2524. * CakeEmailTest::testWrapForJapaneseEncoding()
  2525. *
  2526. * @return void
  2527. */
  2528. public function testWrapForJapaneseEncoding()
  2529. {
  2530. $this->skipIf(!function_exists('mb_convert_encoding'));
  2531. $message = mb_convert_encoding('受け付けました', 'iso-2022-jp', 'UTF-8');
  2532. $this->Email->reset();
  2533. $this->Email->setTransport('debug');
  2534. $this->Email->setFrom('cake@cakephp.org');
  2535. $this->Email->setTo('cake@cakephp.org');
  2536. $this->Email->setSubject('Wordwrap Test');
  2537. $this->Email->setProfile(['empty']);
  2538. $this->Email->setCharset('iso-2022-jp');
  2539. $this->Email->setHeaderCharset('iso-2022-jp');
  2540. $result = $this->Email->send($message);
  2541. $expected = "{$message}\r\n\r\n";
  2542. $this->assertEquals($expected, $result['message']);
  2543. }
  2544. /**
  2545. * CakeEmailTest::testMockTransport()
  2546. */
  2547. public function testMockTransport()
  2548. {
  2549. $this->deprecated(function () {
  2550. Email::dropTransport('default');
  2551. });
  2552. $mock = $this->getMockBuilder('\Cake\Mailer\AbstractTransport')->getMock();
  2553. $config = ['from' => 'tester@example.org', 'transport' => 'default'];
  2554. Email::setConfig('default', $config);
  2555. $this->deprecated(function () use ($mock) {
  2556. Email::setConfigTransport('default', $mock);
  2557. });
  2558. $em = new Email('default');
  2559. $this->assertSame($mock, $em->getTransport());
  2560. }
  2561. /**
  2562. * testZeroOnlyLinesNotBeingEmptied()
  2563. *
  2564. * @return void
  2565. */
  2566. public function testZeroOnlyLinesNotBeingEmptied()
  2567. {
  2568. $message = "Lorem\r\n0\r\n0\r\nipsum";
  2569. $this->Email->reset();
  2570. $this->Email->setTransport('debug');
  2571. $this->Email->setFrom('cake@cakephp.org');
  2572. $this->Email->setTo('cake@cakephp.org');
  2573. $this->Email->setSubject('Wordwrap Test');
  2574. $result = $this->Email->send($message);
  2575. $expected = "{$message}\r\n\r\n";
  2576. $this->assertEquals($expected, $result['message']);
  2577. }
  2578. /**
  2579. * testJsonSerialize()
  2580. *
  2581. * @return void
  2582. */
  2583. public function testJsonSerialize()
  2584. {
  2585. $xmlstr = <<<XML
  2586. <?xml version='1.0' standalone='yes'?>
  2587. <framework>
  2588. <name>CakePHP</name>
  2589. <url>http://cakephp.org</url>
  2590. </framework>
  2591. XML;
  2592. $this->Email->reset()
  2593. ->setTo(['cakephp@cakephp.org' => 'CakePHP'])
  2594. ->setFrom('noreply@cakephp.org')
  2595. ->setReplyTo('cakephp@cakephp.org')
  2596. ->setCc(['mark@cakephp.org', 'juan@cakephp.org' => 'Juan Basso'])
  2597. ->setBcc('phpnut@cakephp.org')
  2598. ->setSubject('Test Serialize')
  2599. ->setMessageId('<uuid@server.com>')
  2600. ->setDomain('foo.bar')
  2601. ->setViewVars([
  2602. 'users' => $this->getTableLocator()->get('Users')->get(1, ['fields' => ['id', 'username']]),
  2603. 'xml' => new SimpleXmlElement($xmlstr),
  2604. 'exception' => new Exception('test')
  2605. ])
  2606. ->setAttachments([
  2607. 'test.txt' => TEST_APP . 'config' . DS . 'empty.ini',
  2608. 'image' => [
  2609. 'data' => file_get_contents(TEST_APP . 'webroot' . DS . 'img' . DS . 'cake.icon.png'),
  2610. 'mimetype' => 'image/png'
  2611. ]
  2612. ]);
  2613. $this->Email->viewBuilder()
  2614. ->setTemplate('default')
  2615. ->setLayout('test');
  2616. $result = json_decode(json_encode($this->Email), true);
  2617. $this->assertContains('test', $result['viewVars']['exception']);
  2618. unset($result['viewVars']['exception']);
  2619. $encode = function ($path) {
  2620. return chunk_split(base64_encode(file_get_contents($path)), 76, "\r\n");
  2621. };
  2622. $expected = [
  2623. '_to' => ['cakephp@cakephp.org' => 'CakePHP'],
  2624. '_from' => ['noreply@cakephp.org' => 'noreply@cakephp.org'],
  2625. '_replyTo' => ['cakephp@cakephp.org' => 'cakephp@cakephp.org'],
  2626. '_cc' => ['mark@cakephp.org' => 'mark@cakephp.org', 'juan@cakephp.org' => 'Juan Basso'],
  2627. '_bcc' => ['phpnut@cakephp.org' => 'phpnut@cakephp.org'],
  2628. '_subject' => 'Test Serialize',
  2629. '_emailFormat' => 'text',
  2630. '_messageId' => '<uuid@server.com>',
  2631. '_domain' => 'foo.bar',
  2632. '_appCharset' => 'UTF-8',
  2633. 'charset' => 'utf-8',
  2634. 'headerCharset' => 'utf-8',
  2635. 'viewConfig' => [
  2636. '_template' => 'default',
  2637. '_layout' => 'test',
  2638. '_helpers' => ['Html'],
  2639. '_className' => 'Cake\View\View',
  2640. ],
  2641. 'viewVars' => [
  2642. 'users' => [
  2643. 'id' => 1,
  2644. 'username' => 'mariano'
  2645. ],
  2646. 'xml' => [
  2647. 'name' => 'CakePHP',
  2648. 'url' => 'http://cakephp.org'
  2649. ],
  2650. ],
  2651. '_attachments' => [
  2652. 'test.txt' => [
  2653. 'data' => $encode(TEST_APP . 'config' . DS . 'empty.ini'),
  2654. 'mimetype' => 'text/plain'
  2655. ],
  2656. 'image' => [
  2657. 'data' => $encode(TEST_APP . 'webroot' . DS . 'img' . DS . 'cake.icon.png'),
  2658. 'mimetype' => 'image/png'
  2659. ]
  2660. ],
  2661. '_emailPattern' => '/^((?:[\p{L}0-9.!#$%&\'*+\/=?^_`{|}~-]+)*@[\p{L}0-9-._]+)$/ui'
  2662. ];
  2663. $this->assertEquals($expected, $result);
  2664. $result = json_decode(json_encode(unserialize(serialize($this->Email))), true);
  2665. $this->assertContains('test', $result['viewVars']['exception']);
  2666. unset($result['viewVars']['exception']);
  2667. $this->assertEquals($expected, $result);
  2668. }
  2669. /**
  2670. * CakeEmailTest::assertLineLengths()
  2671. *
  2672. * @param string $message
  2673. * @return void
  2674. */
  2675. public function assertLineLengths($message)
  2676. {
  2677. $lines = explode("\r\n", $message);
  2678. foreach ($lines as $line) {
  2679. $this->assertTrue(
  2680. strlen($line) <= Email::LINE_LENGTH_MUST,
  2681. 'Line length exceeds the max. limit of Email::LINE_LENGTH_MUST'
  2682. );
  2683. }
  2684. }
  2685. /**
  2686. * Test deprecated methods
  2687. *
  2688. * @return void
  2689. */
  2690. public function testDeprecatedMethods()
  2691. {
  2692. $this->deprecated(function () {
  2693. $this->Email
  2694. ->setTemplate('foo')
  2695. ->setLayout('bar')
  2696. ->setTheme('baz')
  2697. ->setHelpers(['A', 'B']);
  2698. $this->assertSame('foo', $this->Email->getTemplate());
  2699. $this->assertSame('bar', $this->Email->getLayout());
  2700. $this->assertSame('baz', $this->Email->getTheme());
  2701. $this->assertSame(['A', 'B'], $this->Email->getHelpers());
  2702. $this->Email->setLayout('');
  2703. $this->assertFalse($this->Email->getLayout());
  2704. });
  2705. }
  2706. }