EmailTest.php 14 KB

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