EmailTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. namespace Tools\Test\TestCase\Network\Email;
  3. use Tools\Network\Email\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(array('test@example.com' => 'Mark'), $this->Email->from());
  99. $this->Email->from('cake@cakephp.org');
  100. $expected = array('cake@cakephp.org' => 'cake@cakephp.org');
  101. $this->assertSame($expected, $this->Email->from());
  102. $this->Email->from(array('cake@cakephp.org'));
  103. $this->assertSame($expected, $this->Email->from());
  104. $this->Email->from('cake@cakephp.org', 'CakePHP');
  105. $expected = array('cake@cakephp.org' => 'CakePHP');
  106. $this->assertSame($expected, $this->Email->from());
  107. $result = $this->Email->from(array('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(array('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 = array(
  124. 'hotel.png' => array(
  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 = array(
  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 = array(
  176. 'hotel.png' => array(
  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 = array(
  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->assertTrue(!empty($res['hotel.png']['file']));
  209. unset($res['hotel.png']['file']);
  210. $expected = array(
  211. 'hotel.png' => array(
  212. //'file' => $file,
  213. 'mimetype' => 'image/png',
  214. 'contentId' => $cid
  215. )
  216. );
  217. $this->assertSame($expected, $res);
  218. }
  219. /**
  220. * Html email
  221. *
  222. * @return void
  223. */
  224. public function testAddEmbeddedAttachmentSend() {
  225. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  226. Configure::write('debug', 0);
  227. $this->Email = new TestEmail();
  228. $this->Email->emailFormat('both');
  229. $this->Email->to(Configure::read('Config.adminEmail'));
  230. $cid = $this->Email->addEmbeddedAttachment($file);
  231. $cid2 = $this->Email->addEmbeddedAttachment($file);
  232. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  233. $html = '<head>
  234. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  235. <meta name="author" content="ohyeah" />
  236. <title>Untitled 6</title>
  237. </head>
  238. <body>
  239. test_embedded_default äöü <img src="cid:' . $cid . '" /> end
  240. another image <img src="cid:' . $cid2 . '" /> end
  241. html-part
  242. </body>
  243. </html>';
  244. $text = trim(strip_tags($html));
  245. $this->Email->viewVars(compact('text', 'html'));
  246. $res = $this->Email->send();
  247. Configure::write('debug', 2);
  248. if ($error = $this->Email->getError()) {
  249. $this->out($error);
  250. }
  251. $this->assertEquals('', $this->Email->getError());
  252. $this->assertTrue((bool)$res);
  253. }
  254. /**
  255. * EmailTest::testAddEmbeddedBlobAttachment()
  256. *
  257. * @return void
  258. */
  259. public function testAddEmbeddedBlobAttachment() {
  260. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  261. $this->assertTrue(file_exists($file));
  262. $this->Email = new TestEmail();
  263. $this->Email->emailFormat('both');
  264. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
  265. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  266. $res = $this->Email->getProtected('attachments');
  267. $this->assertTrue(!empty($res['my_hotel.png']['data']));
  268. unset($res['my_hotel.png']['data']);
  269. $expected = array(
  270. 'my_hotel.png' => array(
  271. //'data' => file_get_contents($file),
  272. 'mimetype' => 'image/png',
  273. 'contentId' => $cid,
  274. )
  275. );
  276. $this->assertEquals($expected, $res);
  277. $options = array(
  278. 'contentDisposition' => true,
  279. );
  280. $cid = 'abcdef';
  281. $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_other_hotel.png', 'image/jpeg', $cid, $options);
  282. $res = $this->Email->getProtected('attachments');
  283. $this->assertTrue(!empty($res['my_other_hotel.png']['data']));
  284. unset($res['my_other_hotel.png']['data']);
  285. $expected = array(
  286. 'contentDisposition' => true,
  287. //'data' => file_get_contents($file),
  288. 'mimetype' => 'image/jpeg',
  289. 'contentId' => $cid,
  290. );
  291. $this->assertEquals($expected, $res['my_other_hotel.png']);
  292. }
  293. /**
  294. * EmailTest::testValidates()
  295. *
  296. * @return void
  297. */
  298. public function testValidates() {
  299. $this->Email = new TestEmail();
  300. $this->Email->transport('debug');
  301. $res = $this->Email->validates();
  302. $this->assertFalse($res);
  303. $res = $this->Email->send();
  304. $this->assertFalse($res);
  305. $this->Email->subject('foo');
  306. $res = $this->Email->validates();
  307. $this->assertFalse($res);
  308. $res = $this->Email->send();
  309. $this->assertFalse($res);
  310. $this->Email->to('some@web.de');
  311. $res = $this->Email->validates();
  312. $this->assertTrue($res);
  313. $res = $this->Email->send();
  314. $this->assertTrue($res);
  315. }
  316. /**
  317. * Html email
  318. *
  319. * @return void
  320. */
  321. public function testAddEmbeddedBlobAttachmentSend() {
  322. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  323. $this->Email = new TestEmail();
  324. $this->Email->emailFormat('both');
  325. $this->Email->to(Configure::read('Config.adminEmail'));
  326. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png', 'image/png');
  327. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  328. $html = '<head>
  329. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  330. <meta name="author" content="ohyeah" />
  331. <title>Untitled 6</title>
  332. </head>
  333. <body>
  334. test_embedded_blob_default äöü <img src="cid:' . $cid . '" /> end
  335. html-part
  336. </body>
  337. </html>';
  338. $text = trim(strip_tags($html));
  339. $this->Email->viewVars(compact('text', 'html'));
  340. $res = $this->Email->send();
  341. if ($error = $this->Email->getError()) {
  342. $this->out($error);
  343. }
  344. $this->assertEquals('', $this->Email->getError());
  345. $this->assertTrue((bool)$res);
  346. }
  347. public function _testComplexeHtmlWithEmbeddedImages() {
  348. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  349. $this->assertTrue(file_exists($file));
  350. //TODO
  351. }
  352. public function testWrapLongEmailContent() {
  353. $this->Email = new TestEmail();
  354. $html = <<<HTML
  355. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  356. <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;">
  357. 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
  358. </body></html>
  359. HTML;
  360. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  361. $is = $this->Email->wrap($html);
  362. foreach ($is as $line => $content) {
  363. $this->assertTrue(strlen($content) <= Email::LINE_LENGTH_MUST);
  364. }
  365. $this->debug($is);
  366. $this->assertTrue(count($is) >= 5);
  367. }
  368. public function testWrapCustomized() {
  369. $this->Email = new TestEmail();
  370. $html = <<<HTML
  371. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  372. <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;">
  373. 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
  374. </body></html>
  375. HTML;
  376. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  377. $this->Email->wrapLength(100);
  378. $is = $this->Email->wrap($html);
  379. foreach ($is as $line => $content) {
  380. $this->assertTrue(strlen($content) <= Email::LINE_LENGTH_MUST);
  381. }
  382. $this->debug($is);
  383. $this->assertTrue(count($is) >= 16);
  384. }
  385. }