EmailTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. namespace Tools\Test\TestCase\Mailer;
  3. use Tools\Mailer\Email;
  4. use Cake\Core\Configure;
  5. use Cake\Core\Plugin;
  6. use Tools\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. * TestEmail::getProtected()
  55. *
  56. * @param string $attribute
  57. * @return mixed
  58. */
  59. public function getProtected($attribute) {
  60. $attribute = '_' . $attribute;
  61. return $this->$attribute;
  62. }
  63. }
  64. /**
  65. * EmailTest class
  66. */
  67. class EmailTest extends TestCase {
  68. /**
  69. * setUp
  70. *
  71. * @return void
  72. */
  73. public function setUp() {
  74. parent::setUp();
  75. $this->Email = new TestEmail();
  76. Email::configTransport('debug', [
  77. 'className' => 'Debug'
  78. ]);
  79. }
  80. /**
  81. * tearDown method
  82. *
  83. * @return void
  84. */
  85. public function tearDown() {
  86. parent::tearDown();
  87. Log::drop('email');
  88. Email::drop('test');
  89. Email::dropTransport('debug');
  90. Email::dropTransport('test_smtp');
  91. }
  92. /**
  93. * testFrom method
  94. *
  95. * @return void
  96. */
  97. public function testFrom() {
  98. $this->assertSame(['test@example.com' => 'Mark'], $this->Email->from());
  99. $this->Email->from('cake@cakephp.org');
  100. $expected = ['cake@cakephp.org' => 'cake@cakephp.org'];
  101. $this->assertSame($expected, $this->Email->from());
  102. $this->Email->from(['cake@cakephp.org']);
  103. $this->assertSame($expected, $this->Email->from());
  104. $this->Email->from('cake@cakephp.org', 'CakePHP');
  105. $expected = ['cake@cakephp.org' => 'CakePHP'];
  106. $this->assertSame($expected, $this->Email->from());
  107. $result = $this->Email->from(['cake@cakephp.org' => 'CakePHP']);
  108. $this->assertSame($expected, $this->Email->from());
  109. $this->assertSame($this->Email, $result);
  110. $this->setExpectedException('InvalidArgumentException');
  111. $result = $this->Email->from(['cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address']);
  112. }
  113. /**
  114. * EmailTest::testAddAttachment()
  115. *
  116. * @return void
  117. */
  118. public function testAddAttachment() {
  119. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  120. $this->assertTrue(file_exists($file));
  121. $this->Email->addAttachment($file);
  122. $res = $this->Email->getProtected('attachments');
  123. $expected = [
  124. 'hotel.png' => [
  125. 'file' => $file,
  126. 'mimetype' => 'image/png',
  127. ]
  128. ];
  129. $this->assertEquals($expected, $res);
  130. $this->Email->addAttachment($file, 'my_image.jpg');
  131. $res = $this->Email->getProtected('attachments');
  132. $expected = [
  133. 'file' => $file,
  134. 'mimetype' => 'image/jpeg',
  135. ];
  136. $this->assertEquals($expected, $res['my_image.jpg']);
  137. }
  138. /**
  139. * EmailTest::testAddAttachment()
  140. *
  141. * @return void
  142. */
  143. public function testAddAttachmentSend() {
  144. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  145. $this->assertTrue(file_exists($file));
  146. //Configure::write('debug', 0);
  147. $this->Email->to(Configure::read('Config.adminEmail'));
  148. $this->Email->addAttachment($file);
  149. $res = $this->Email->send('test_default', 'default');
  150. if ($error = $this->Email->getError()) {
  151. $this->out($error);
  152. }
  153. $this->assertEquals('', $this->Email->getError());
  154. $this->assertTrue((bool)$res);
  155. $this->Email->reset();
  156. $this->Email->to(Configure::read('Config.adminEmail'));
  157. $this->Email->addAttachment($file, 'x.jpg');
  158. $res = $this->Email->send('test_custom_filename');
  159. //Configure::write('debug', 2);
  160. //$this->assertEquals('', $this->Email->getError());
  161. //$this->assertTrue($res);
  162. }
  163. /**
  164. * EmailTest::testAddBlobAttachment()
  165. *
  166. * @return void
  167. */
  168. public function testAddBlobAttachment() {
  169. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  170. $content = file_get_contents($file);
  171. $this->Email->addBlobAttachment($content, 'hotel.png');
  172. $res = $this->Email->getProtected('attachments');
  173. $this->assertTrue(!empty($res['hotel.png']['data']));
  174. unset($res['hotel.png']['data']);
  175. $expected = [
  176. 'hotel.png' => [
  177. //'data' => $content,
  178. 'mimetype' => 'image/png',
  179. ]
  180. ];
  181. $this->assertEquals($expected, $res);
  182. $this->Email->addBlobAttachment($content, 'hotel.gif', 'image/jpeg');
  183. $res = $this->Email->getProtected('attachments');
  184. $this->assertTrue(!empty($res['hotel.gif']['data']));
  185. unset($res['hotel.gif']['data']);
  186. $expected = [
  187. //'data' => $content,
  188. 'mimetype' => 'image/jpeg',
  189. ];
  190. $this->assertEquals($expected, $res['hotel.gif']);
  191. $this->assertSame(2, count($res));
  192. }
  193. /**
  194. * EmailTest::testAddEmbeddedAttachment()
  195. *
  196. * @return void
  197. */
  198. public function testAddEmbeddedAttachment() {
  199. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  200. $this->assertTrue(file_exists($file));
  201. $this->Email = new TestEmail();
  202. $this->Email->emailFormat('both');
  203. $cid = $this->Email->addEmbeddedAttachment($file);
  204. $cid2 = $this->Email->addEmbeddedAttachment($file);
  205. $this->assertSame($cid, $cid2);
  206. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  207. $res = $this->Email->getProtected('attachments');
  208. $this->assertSame(1, count($res));
  209. $image = array_shift($res);
  210. $expected = [
  211. 'file' => $file,
  212. 'mimetype' => 'image/png',
  213. 'contentId' => $cid
  214. ];
  215. $this->assertSame($expected, $image);
  216. }
  217. /**
  218. * Html email
  219. *
  220. * @return void
  221. */
  222. public function testAddEmbeddedAttachmentSend() {
  223. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  224. Configure::write('debug', 0);
  225. $this->Email = new TestEmail();
  226. $this->Email->emailFormat('both');
  227. $this->Email->to(Configure::read('Config.adminEmail'));
  228. $cid = $this->Email->addEmbeddedAttachment($file);
  229. $cid2 = $this->Email->addEmbeddedAttachment($file);
  230. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  231. $html = '<head>
  232. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  233. <meta name="author" content="ohyeah" />
  234. <title>Untitled 6</title>
  235. </head>
  236. <body>
  237. test_embedded_default äöü <img src="cid:' . $cid . '" /> end
  238. another image <img src="cid:' . $cid2 . '" /> end
  239. html-part
  240. </body>
  241. </html>';
  242. $text = trim(strip_tags($html));
  243. $this->Email->viewVars(compact('text', 'html'));
  244. $res = $this->Email->send();
  245. Configure::write('debug', 2);
  246. if ($error = $this->Email->getError()) {
  247. $this->out($error);
  248. }
  249. $this->assertEquals('', $this->Email->getError());
  250. $this->assertTrue((bool)$res);
  251. }
  252. /**
  253. * EmailTest::testAddEmbeddedBlobAttachment()
  254. *
  255. * @return void
  256. */
  257. public function testAddEmbeddedBlobAttachment() {
  258. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  259. $this->assertTrue(file_exists($file));
  260. $this->Email = new TestEmail();
  261. $this->Email->emailFormat('both');
  262. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
  263. $cid2 = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
  264. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  265. $res = $this->Email->getProtected('attachments');
  266. $this->assertSame(1, count($res));
  267. $images = $res;
  268. $image = array_shift($images);
  269. unset($image['data']);
  270. $expected = [
  271. 'mimetype' => 'image/png',
  272. 'contentId' => $cid,
  273. ];
  274. $this->assertEquals($expected, $image);
  275. $options = [
  276. 'contentDisposition' => true,
  277. ];
  278. $cid = 'abcdef';
  279. $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_other_hotel.png', 'image/jpeg', $cid, $options);
  280. $res = $this->Email->getProtected('attachments');
  281. $this->assertSame(2, count($res));
  282. $keys = array_keys($res);
  283. $keyLastRecord = $keys[count($keys) - 1];
  284. $this->assertSame('image/jpeg', $res[$keyLastRecord]['mimetype']);
  285. $this->assertTrue($res[$keyLastRecord]['contentDisposition']);
  286. $cid3 = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file) . 'xxx', 'my_hotel.png');
  287. $this->assertNotSame($cid3, $cid);
  288. $res = $this->Email->getProtected('attachments');
  289. $this->assertSame(3, count($res));
  290. }
  291. /**
  292. * EmailTest::testValidates()
  293. *
  294. * @return void
  295. */
  296. public function testValidates() {
  297. $this->Email = new TestEmail();
  298. $this->Email->transport('debug');
  299. $res = $this->Email->validates();
  300. $this->assertFalse($res);
  301. //$res = $this->Email->send();
  302. //$this->assertFalse($res);
  303. $this->Email->subject('foo');
  304. $res = $this->Email->validates();
  305. $this->assertFalse($res);
  306. //$res = $this->Email->send();
  307. //$this->assertFalse($res);
  308. $this->Email->to('some@web.de');
  309. $res = $this->Email->validates();
  310. $this->assertTrue($res);
  311. //$res = $this->Email->send();
  312. //$this->assertTrue($res);
  313. }
  314. /**
  315. * Html email
  316. *
  317. * @return void
  318. */
  319. public function testAddEmbeddedBlobAttachmentSend() {
  320. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  321. $this->Email = new TestEmail();
  322. $this->Email->emailFormat('both');
  323. $this->Email->to(Configure::read('Config.adminEmail'));
  324. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png', 'image/png');
  325. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  326. $html = '<head>
  327. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  328. <meta name="author" content="ohyeah" />
  329. <title>Untitled 6</title>
  330. </head>
  331. <body>
  332. test_embedded_blob_default äöü <img src="cid:' . $cid . '" /> end
  333. html-part
  334. </body>
  335. </html>';
  336. $text = trim(strip_tags($html));
  337. $this->Email->viewVars(compact('text', 'html'));
  338. $res = $this->Email->send();
  339. if ($error = $this->Email->getError()) {
  340. $this->out($error);
  341. }
  342. $this->assertEquals('', $this->Email->getError());
  343. $this->assertTrue((bool)$res);
  344. }
  345. public function _testComplexeHtmlWithEmbeddedImages() {
  346. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  347. $this->assertTrue(file_exists($file));
  348. //TODO
  349. }
  350. /**
  351. * EmailTest::testWrapLongEmailContent()
  352. *
  353. * @return void
  354. */
  355. public function testWrapLongEmailContent() {
  356. $this->Email = new TestEmail();
  357. $html = <<<HTML
  358. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  359. <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;">
  360. 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
  361. </body></html>
  362. HTML;
  363. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  364. $is = $this->Email->wrap($html);
  365. foreach ($is as $line => $content) {
  366. $this->assertTrue(strlen($content) <= Email::LINE_LENGTH_MUST);
  367. }
  368. $this->debug($is);
  369. $this->assertTrue(count($is) >= 5);
  370. }
  371. /**
  372. * EmailTest::testWrapCustomized()
  373. *
  374. * @return void
  375. */
  376. public function testWrapCustomized() {
  377. $this->Email = new TestEmail();
  378. $html = <<<HTML
  379. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  380. <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;">
  381. 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
  382. </body></html>
  383. HTML;
  384. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  385. $this->Email->wrapLength(100);
  386. $is = $this->Email->wrap($html);
  387. foreach ($is as $line => $content) {
  388. $this->assertTrue(strlen($content) <= Email::LINE_LENGTH_MUST);
  389. }
  390. $this->debug($is);
  391. $this->assertTrue(count($is) >= 16);
  392. }
  393. }