EmailTest.php 76 KB

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