EmailTest.php 80 KB

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