EmailTest.php 14 KB

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