EmailTest.php 97 KB

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