EmailTest.php 77 KB

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