EmailTest.php 14 KB

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