EmailTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. if ($error = $this->Email->getError()) {
  155. $this->out($error);
  156. }
  157. $this->assertEquals('', $this->Email->getError());
  158. $this->assertTrue((bool)$res);
  159. $this->Email->reset();
  160. $this->Email->to(Configure::read('Config.adminEmail'));
  161. $this->Email->addAttachment($file, 'x.jpg');
  162. $res = $this->Email->send('test_custom_filename');
  163. //Configure::write('debug', 2);
  164. //$this->assertEquals('', $this->Email->getError());
  165. //$this->assertTrue($res);
  166. }
  167. /**
  168. * EmailTest::testAddBlobAttachment()
  169. *
  170. * @return void
  171. */
  172. public function testAddBlobAttachment() {
  173. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  174. $content = file_get_contents($file);
  175. $this->Email->addBlobAttachment($content, 'hotel.png');
  176. $res = $this->Email->getProtected('attachments');
  177. $this->assertTrue(!empty($res['hotel.png']['data']));
  178. unset($res['hotel.png']['data']);
  179. $expected = [
  180. 'hotel.png' => [
  181. //'data' => $content,
  182. 'mimetype' => 'image/png',
  183. ]
  184. ];
  185. $this->assertEquals($expected, $res);
  186. $this->Email->addBlobAttachment($content, 'hotel.gif', 'image/jpeg');
  187. $res = $this->Email->getProtected('attachments');
  188. $this->assertTrue(!empty($res['hotel.gif']['data']));
  189. unset($res['hotel.gif']['data']);
  190. $expected = [
  191. //'data' => $content,
  192. 'mimetype' => 'image/jpeg',
  193. ];
  194. $this->assertEquals($expected, $res['hotel.gif']);
  195. $this->assertSame(2, count($res));
  196. }
  197. /**
  198. * EmailTest::testAddEmbeddedAttachment()
  199. *
  200. * @return void
  201. */
  202. public function testAddEmbeddedAttachment() {
  203. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  204. $this->assertTrue(file_exists($file));
  205. $this->Email = new TestEmail();
  206. $this->Email->emailFormat('both');
  207. $cid = $this->Email->addEmbeddedAttachment($file);
  208. $cid2 = $this->Email->addEmbeddedAttachment($file);
  209. $this->assertSame($cid, $cid2);
  210. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  211. $res = $this->Email->getProtected('attachments');
  212. $this->assertSame(1, count($res));
  213. $image = array_shift($res);
  214. $expected = [
  215. 'file' => $file,
  216. 'mimetype' => 'image/png',
  217. 'contentId' => $cid
  218. ];
  219. $this->assertSame($expected, $image);
  220. }
  221. /**
  222. * Html email
  223. *
  224. * @return void
  225. */
  226. public function testAddEmbeddedAttachmentSend() {
  227. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  228. Configure::write('debug', 0);
  229. $this->Email = new TestEmail();
  230. $this->Email->emailFormat('both');
  231. $this->Email->to(Configure::read('Config.adminEmail'));
  232. $cid = $this->Email->addEmbeddedAttachment($file);
  233. $cid2 = $this->Email->addEmbeddedAttachment($file);
  234. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  235. $html = '<head>
  236. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  237. <meta name="author" content="ohyeah" />
  238. <title>Untitled 6</title>
  239. </head>
  240. <body>
  241. test_embedded_default äöü <img src="cid:' . $cid . '" /> end
  242. another image <img src="cid:' . $cid2 . '" /> end
  243. html-part
  244. </body>
  245. </html>';
  246. $text = trim(strip_tags($html));
  247. $this->Email->viewVars(compact('text', 'html'));
  248. $res = $this->Email->send();
  249. Configure::write('debug', 2);
  250. if ($error = $this->Email->getError()) {
  251. $this->out($error);
  252. }
  253. $this->assertEquals('', $this->Email->getError());
  254. $this->assertTrue((bool)$res);
  255. }
  256. /**
  257. * EmailTest::testAddEmbeddedBlobAttachment()
  258. *
  259. * @return void
  260. */
  261. public function testAddEmbeddedBlobAttachment() {
  262. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  263. $this->assertTrue(file_exists($file));
  264. $this->Email = new TestEmail();
  265. $this->Email->emailFormat('both');
  266. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
  267. $cid2 = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
  268. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  269. $res = $this->Email->getProtected('attachments');
  270. $this->assertSame(1, count($res));
  271. $images = $res;
  272. $image = array_shift($images);
  273. unset($image['data']);
  274. $expected = [
  275. 'mimetype' => 'image/png',
  276. 'contentId' => $cid,
  277. ];
  278. $this->assertEquals($expected, $image);
  279. $options = [
  280. 'contentDisposition' => true,
  281. ];
  282. $cid = 'abcdef';
  283. $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_other_hotel.png', 'image/jpeg', $cid, $options);
  284. $res = $this->Email->getProtected('attachments');
  285. $this->assertSame(2, count($res));
  286. $keys = array_keys($res);
  287. $keyLastRecord = $keys[count($keys) - 1];
  288. $this->assertSame('image/jpeg', $res[$keyLastRecord]['mimetype']);
  289. $this->assertTrue($res[$keyLastRecord]['contentDisposition']);
  290. $cid3 = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file) . 'xxx', 'my_hotel.png');
  291. $this->assertNotSame($cid3, $cid);
  292. $res = $this->Email->getProtected('attachments');
  293. $this->assertSame(3, count($res));
  294. }
  295. /**
  296. * EmailTest::testValidates()
  297. *
  298. * @return void
  299. */
  300. public function testValidates() {
  301. $this->Email = new TestEmail();
  302. $this->Email->transport('debug');
  303. $res = $this->Email->validates();
  304. $this->assertFalse($res);
  305. //$res = $this->Email->send();
  306. //$this->assertFalse($res);
  307. $this->Email->subject('foo');
  308. $res = $this->Email->validates();
  309. $this->assertFalse($res);
  310. //$res = $this->Email->send();
  311. //$this->assertFalse($res);
  312. $this->Email->to('some@web.de');
  313. $res = $this->Email->validates();
  314. $this->assertTrue($res);
  315. //$res = $this->Email->send();
  316. //$this->assertTrue($res);
  317. }
  318. /**
  319. * Html email
  320. *
  321. * @return void
  322. */
  323. public function testAddEmbeddedBlobAttachmentSend() {
  324. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  325. $this->Email = new TestEmail();
  326. $this->Email->emailFormat('both');
  327. $this->Email->to(Configure::read('Config.adminEmail'));
  328. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png', 'image/png');
  329. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  330. $html = '<head>
  331. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  332. <meta name="author" content="ohyeah" />
  333. <title>Untitled 6</title>
  334. </head>
  335. <body>
  336. test_embedded_blob_default äöü <img src="cid:' . $cid . '" /> end
  337. html-part
  338. </body>
  339. </html>';
  340. $text = trim(strip_tags($html));
  341. $this->Email->viewVars(compact('text', 'html'));
  342. $res = $this->Email->send();
  343. if ($error = $this->Email->getError()) {
  344. $this->out($error);
  345. }
  346. $this->assertEquals('', $this->Email->getError());
  347. $this->assertTrue((bool)$res);
  348. }
  349. public function _testComplexeHtmlWithEmbeddedImages() {
  350. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  351. $this->assertTrue(file_exists($file));
  352. //TODO
  353. }
  354. /**
  355. * EmailTest::testWrapLongEmailContent()
  356. *
  357. * @return void
  358. */
  359. public function testWrapLongEmailContent() {
  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. $is = $this->Email->wrap($html);
  369. foreach ($is as $line => $content) {
  370. $this->assertTrue(strlen($content) <= Email::LINE_LENGTH_MUST);
  371. }
  372. $this->debug($is);
  373. $this->assertTrue(count($is) >= 5);
  374. }
  375. /**
  376. * EmailTest::testWrapCustomized()
  377. *
  378. * @return void
  379. */
  380. public function testWrapCustomized() {
  381. $this->Email = new TestEmail();
  382. $html = <<<HTML
  383. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  384. <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;">
  385. 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
  386. </body></html>
  387. HTML;
  388. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  389. $this->Email->wrapLength(100);
  390. $is = $this->Email->wrap($html);
  391. foreach ($is as $line => $content) {
  392. $this->assertTrue(strlen($content) <= Email::LINE_LENGTH_MUST);
  393. }
  394. $this->debug($is);
  395. $this->assertTrue(count($is) >= 16);
  396. }
  397. }