EmailTest.php 14 KB

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