EmailTest.php 14 KB

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