EmailTest.php 96 KB

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