EmailTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <?php
  2. namespace Tools\Test\TestCase\Network\Email;
  3. use Tools\Network\Email\Email;
  4. use Cake\Core\Configure;
  5. use Cake\Core\Plugin;
  6. use Cake\TestSuite\TestCase;
  7. use Cake\Log\Log;
  8. /**
  9. * Help to test Email
  10. *
  11. */
  12. class TestEmail extends Email {
  13. /**
  14. * Wrap to protected method
  15. *
  16. * @return array
  17. */
  18. public function formatAddress($address) {
  19. return parent::_formatAddress($address);
  20. }
  21. /**
  22. * Wrap to protected method
  23. *
  24. * @return array
  25. */
  26. public function wrap($text, $length = Email::LINE_LENGTH_MUST) {
  27. return parent::_wrap($text, $length);
  28. }
  29. /**
  30. * Get the boundary attribute
  31. *
  32. * @return string
  33. */
  34. public function getBoundary() {
  35. return $this->_boundary;
  36. }
  37. /**
  38. * Encode to protected method
  39. *
  40. * @return string
  41. */
  42. public function encode($text) {
  43. return $this->_encode($text);
  44. }
  45. /**
  46. * Render to protected method
  47. *
  48. * @return array
  49. */
  50. public function render($content) {
  51. return $this->_render($content);
  52. }
  53. }
  54. /**
  55. * EmailTest class
  56. */
  57. class EmailTest extends TestCase {
  58. /**
  59. * setUp
  60. *
  61. * @return void
  62. */
  63. public function setUp() {
  64. parent::setUp();
  65. $this->CakeEmail = new TestEmail();
  66. Email::configTransport('debug', [
  67. 'className' => 'Debug'
  68. ]);
  69. }
  70. /**
  71. * tearDown method
  72. *
  73. * @return void
  74. */
  75. public function tearDown() {
  76. parent::tearDown();
  77. Log::drop('email');
  78. Email::drop('test');
  79. Email::dropTransport('debug');
  80. Email::dropTransport('test_smtp');
  81. }
  82. /**
  83. * testFrom method
  84. *
  85. * @return void
  86. */
  87. public function testFrom() {
  88. $this->assertSame(array(), $this->CakeEmail->from());
  89. $this->CakeEmail->from('cake@cakephp.org');
  90. $expected = array('cake@cakephp.org' => 'cake@cakephp.org');
  91. $this->assertSame($expected, $this->CakeEmail->from());
  92. $this->CakeEmail->from(array('cake@cakephp.org'));
  93. $this->assertSame($expected, $this->CakeEmail->from());
  94. $this->CakeEmail->from('cake@cakephp.org', 'CakePHP');
  95. $expected = array('cake@cakephp.org' => 'CakePHP');
  96. $this->assertSame($expected, $this->CakeEmail->from());
  97. $result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP'));
  98. $this->assertSame($expected, $this->CakeEmail->from());
  99. $this->assertSame($this->CakeEmail, $result);
  100. $this->setExpectedException('InvalidArgumentException');
  101. $result = $this->CakeEmail->from(array('cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address'));
  102. }
  103. /**
  104. * EmailTest::testAddAttachment()
  105. *
  106. * @return void
  107. */
  108. public function testAddAttachment() {
  109. $file = Plugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  110. $this->assertTrue(file_exists($file));
  111. $this->Email->addAttachment($file);
  112. $res = $this->Email->getProtected('attachments');
  113. $expected = array(
  114. 'hotel.png' => array(
  115. 'file' => $file,
  116. 'mimetype' => 'image/png',
  117. )
  118. );
  119. $this->assertEquals($expected, $res);
  120. $this->Email->addAttachment($file, 'my_image.jpg');
  121. $res = $this->Email->getProtected('attachments');
  122. $expected = array(
  123. 'file' => $file,
  124. 'mimetype' => 'image/jpeg',
  125. );
  126. $this->assertEquals($expected, $res['my_image.jpg']);
  127. }
  128. /**
  129. * EmailTest::testAddAttachment()
  130. *
  131. * @return void
  132. */
  133. public function testAddAttachmentSend() {
  134. $this->skipIf(!$this->sendEmails);
  135. $file = Plugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  136. $this->assertTrue(file_exists($file));
  137. Configure::write('debug', 0);
  138. $this->Email->to(Configure::read('Config.adminEmail'));
  139. $this->Email->addAttachment($file);
  140. $res = $this->Email->send('test_default', 'default');
  141. if ($error = $this->Email->getError()) {
  142. $this->out($error);
  143. }
  144. $this->assertEquals('', $this->Email->getError());
  145. $this->assertTrue($res);
  146. $this->Email->resetAndSet();
  147. $this->Email->to(Configure::read('Config.adminEmail'));
  148. $this->Email->addAttachment($file, 'x.jpg');
  149. $res = $this->Email->send('test_custom_filename');
  150. Configure::write('debug', 2);
  151. $this->assertEquals('', $this->Email->getError());
  152. $this->assertTrue($res);
  153. }
  154. /**
  155. * EmailTest::testAddBlobAttachment()
  156. *
  157. * @return void
  158. */
  159. public function testAddBlobAttachment() {
  160. $file = Plugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  161. $content = file_get_contents($file);
  162. $this->Email->addBlobAttachment($content, 'hotel.png');
  163. $res = $this->Email->getProtected('attachments');
  164. $expected = array(
  165. 'hotel.png' => array(
  166. 'content' => $content,
  167. 'mimetype' => 'image/png',
  168. )
  169. );
  170. $this->assertEquals($expected, $res);
  171. $this->Email->addBlobAttachment($content, 'hotel.gif', 'image/jpeg');
  172. $res = $this->Email->getProtected('attachments');
  173. $expected = array(
  174. 'content' => $content,
  175. 'mimetype' => 'image/jpeg',
  176. );
  177. $this->assertEquals($expected, $res['hotel.gif']);#
  178. $this->assertSame(2, count($res));
  179. }
  180. /**
  181. * EmailTest::testAddEmbeddedAttachment()
  182. *
  183. * @return void
  184. */
  185. public function testAddEmbeddedAttachment() {
  186. $file = Plugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  187. $this->assertTrue(file_exists($file));
  188. $this->Email = new TestEmail();
  189. $this->Email->emailFormat('both');
  190. $cid = $this->Email->addEmbeddedAttachment($file);
  191. $cid2 = $this->Email->addEmbeddedAttachment($file);
  192. $this->assertSame($cid, $cid2);
  193. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  194. $res = $this->Email->getProtected('attachments');
  195. $expected = array(
  196. 'hotel.png' => array(
  197. 'file' => $file,
  198. 'mimetype' => 'image/png',
  199. 'contentId' => $cid
  200. )
  201. );
  202. $this->assertSame($expected, $res);
  203. }
  204. /**
  205. * Html email
  206. *
  207. * @return void
  208. */
  209. public function testAddEmbeddedAttachmentSend() {
  210. $file = Plugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  211. Configure::write('debug', 0);
  212. $this->Email = new TestEmail();
  213. $this->Email->emailFormat('both');
  214. $this->Email->to(Configure::read('Config.adminEmail'));
  215. $cid = $this->Email->addEmbeddedAttachment($file);
  216. $cid2 = $this->Email->addEmbeddedAttachment($file);
  217. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  218. $html = '<head>
  219. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  220. <meta name="author" content="ohyeah" />
  221. <title>Untitled 6</title>
  222. </head>
  223. <body>
  224. test_embedded_default äöü <img src="cid:' . $cid . '" /> end
  225. another image <img src="cid:' . $cid2 . '" /> end
  226. html-part
  227. </body>
  228. </html>';
  229. $text = trim(strip_tags($html));
  230. $this->Email->viewVars(compact('text', 'html'));
  231. if (!$this->sendEmails) {
  232. Configure::write('debug', 2);
  233. }
  234. $this->skipIf(!$this->sendEmails);
  235. $res = $this->Email->send();
  236. Configure::write('debug', 2);
  237. if ($error = $this->Email->getError()) {
  238. $this->out($error);
  239. }
  240. $this->assertEquals('', $this->Email->getError());
  241. $this->assertTrue($res);
  242. }
  243. /**
  244. * EmailTest::testAddEmbeddedBlobAttachment()
  245. *
  246. * @return void
  247. */
  248. public function testAddEmbeddedBlobAttachment() {
  249. $file = Plugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  250. $this->assertTrue(file_exists($file));
  251. $this->Email = new TestEmail();
  252. $this->Email->emailFormat('both');
  253. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
  254. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  255. $res = $this->Email->getProtected('attachments');
  256. $expected = array(
  257. 'my_hotel.png' => array(
  258. 'content' => file_get_contents($file),
  259. 'mimetype' => 'image/png',
  260. 'contentId' => $cid,
  261. )
  262. );
  263. $this->assertEquals($expected, $res);
  264. $options = array(
  265. 'contentDisposition' => true,
  266. );
  267. $cid = 'abcdef';
  268. $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_other_hotel.png', 'image/jpeg', $cid, $options);
  269. $res = $this->Email->getProtected('attachments');
  270. $expected = array(
  271. 'contentDisposition' => true,
  272. 'content' => file_get_contents($file),
  273. 'mimetype' => 'image/jpeg',
  274. 'contentId' => $cid,
  275. );
  276. $this->assertEquals($expected, $res['my_other_hotel.png']);
  277. }
  278. /**
  279. * EmailTest::testValidates()
  280. *
  281. * @return void
  282. */
  283. public function testValidates() {
  284. $this->Email = new TestEmail();
  285. $this->Email->transport('debug');
  286. $res = $this->Email->validates();
  287. $this->assertFalse($res);
  288. $res = $this->Email->send();
  289. $this->assertFalse($res);
  290. $this->Email->subject('foo');
  291. $res = $this->Email->validates();
  292. $this->assertFalse($res);
  293. $res = $this->Email->send();
  294. $this->assertFalse($res);
  295. $this->Email->to('some@web.de');
  296. $res = $this->Email->validates();
  297. $this->assertTrue($res);
  298. $res = $this->Email->send();
  299. $this->assertTrue($res);
  300. }
  301. /**
  302. * Html email
  303. *
  304. * @return void
  305. */
  306. public function testAddEmbeddedBlobAttachmentSend() {
  307. $file = Plugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  308. Configure::write('debug', 0);
  309. $this->Email = new TestEmail();
  310. $this->Email->emailFormat('both');
  311. $this->Email->to(Configure::read('Config.adminEmail'));
  312. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png', 'image/png');
  313. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  314. $html = '<head>
  315. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  316. <meta name="author" content="ohyeah" />
  317. <title>Untitled 6</title>
  318. </head>
  319. <body>
  320. test_embedded_blob_default äöü <img src="cid:' . $cid . '" /> end
  321. html-part
  322. </body>
  323. </html>';
  324. $text = trim(strip_tags($html));
  325. $this->Email->viewVars(compact('text', 'html'));
  326. if (!$this->sendEmails) {
  327. Configure::write('debug', 2);
  328. }
  329. $this->skipIf(!$this->sendEmails);
  330. $res = $this->Email->send();
  331. Configure::write('debug', 2);
  332. if ($error = $this->Email->getError()) {
  333. $this->out($error);
  334. }
  335. $this->assertEquals('', $this->Email->getError());
  336. $this->assertTrue($res);
  337. }
  338. public function _testComplexeHtmlWithEmbeddedImages() {
  339. $file = Plugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  340. $this->assertTrue(file_exists($file));
  341. //TODO
  342. }
  343. public function testWrapLongEmailContent() {
  344. $this->Email = new TestEmail();
  345. $html = <<<HTML
  346. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  347. <html><head></head><body style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px; text-align: left; vertical-align: top; margin: 0;">
  348. sjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsf
  349. </body></html>
  350. HTML;
  351. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  352. $is = $this->Email->wrap($html);
  353. foreach ($is as $line => $content) {
  354. $this->assertTrue(strlen($content) <= Email::LINE_LENGTH_MUST);
  355. }
  356. $this->debug($is);
  357. $this->assertTrue(count($is) >= 5);
  358. }
  359. public function testWrapCustomized() {
  360. $this->Email = new TestEmail();
  361. $html = <<<HTML
  362. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  363. <html><head></head><body style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px; text-align: left; vertical-align: top; margin: 0;">
  364. sjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsf
  365. </body></html>
  366. HTML;
  367. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  368. $this->Email->wrapLength(100);
  369. $is = $this->Email->wrap($html);
  370. foreach ($is as $line => $content) {
  371. $this->assertTrue(strlen($content) <= Email::LINE_LENGTH_MUST);
  372. }
  373. $this->debug($is);
  374. $this->assertTrue(count($is) >= 16);
  375. }
  376. }