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