EmailTest.php 80 KB

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