EmailTest.php 14 KB

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