EmailTest.php 109 KB

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