EmailTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. namespace Tools\Test\TestCase\Mailer;
  3. use Cake\Core\Configure;
  4. use Cake\Core\Plugin;
  5. use Cake\Log\Log;
  6. use TestApp\Mailer\TestEmail;
  7. use Tools\Mailer\Email;
  8. use Tools\TestSuite\TestCase;
  9. /**
  10. * EmailTest class
  11. */
  12. class EmailTest extends TestCase {
  13. /**
  14. * @var \TestApp\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. $this->setExpectedException('InvalidArgumentException');
  69. $this->Email->from(['cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address']);
  70. }
  71. /**
  72. * @return void
  73. */
  74. public function testAddAttachment() {
  75. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  76. $this->assertTrue(file_exists($file));
  77. $this->Email->addAttachment($file);
  78. $res = $this->Email->getProtected('attachments');
  79. $expected = [
  80. 'hotel.png' => [
  81. 'file' => $file,
  82. 'mimetype' => 'image/png',
  83. ]
  84. ];
  85. $this->assertEquals($expected, $res);
  86. $this->Email->addAttachment($file, 'my_image.jpg');
  87. $res = $this->Email->getProtected('attachments');
  88. $expected = [
  89. 'file' => $file,
  90. 'mimetype' => 'image/jpeg',
  91. ];
  92. $this->assertEquals($expected, $res['my_image.jpg']);
  93. }
  94. /**
  95. * @return void
  96. */
  97. public function testAddAttachmentSend() {
  98. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  99. $this->assertTrue(file_exists($file));
  100. $this->Email->to(Configure::read('Config.adminEmail'));
  101. $this->Email->addAttachment($file);
  102. $res = $this->Email->send('test_default', 'default');
  103. $error = $this->Email->getError();
  104. if ($error) {
  105. $this->out($error);
  106. }
  107. $this->assertEquals('', $this->Email->getError());
  108. $this->assertTrue((bool)$res);
  109. $this->Email->reset();
  110. $this->Email->to(Configure::read('Config.adminEmail'));
  111. $this->Email->addAttachment($file, 'x.jpg');
  112. $res = $this->Email->send('test_custom_filename');
  113. $this->assertTrue((bool)$res);
  114. }
  115. /**
  116. * @return void
  117. */
  118. public function testAddEmbeddedAttachmentByContentId() {
  119. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  120. $this->Email->addEmbeddedAttachmentByContentId('123', $file);
  121. $attachments = $this->Email->getProtected('attachments');
  122. $attachment = array_shift($attachments);
  123. $this->assertSame('image/png', $attachment['mimetype']);
  124. $this->assertSame('123', $attachment['contentId']);
  125. }
  126. /**
  127. * @return void
  128. */
  129. public function testAddEmbeddedBlobAttachmentByContentId() {
  130. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  131. $content = file_get_contents($file);
  132. $this->Email->addEmbeddedBlobAttachmentByContentId('123', $content, $file);
  133. $attachments = $this->Email->getProtected('attachments');
  134. $attachment = array_shift($attachments);
  135. $this->assertNotEmpty($attachment['data']);
  136. $this->assertSame('image/png', $attachment['mimetype']);
  137. $this->assertSame('123', $attachment['contentId']);
  138. $this->Email->addEmbeddedBlobAttachmentByContentId('123', $content, $file, 'png');
  139. $attachments = $this->Email->getProtected('attachments');
  140. $attachment = array_shift($attachments);
  141. $this->assertSame('png', $attachment['mimetype']);
  142. }
  143. /**
  144. * @return void
  145. */
  146. public function testAddBlobAttachment() {
  147. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  148. $content = file_get_contents($file);
  149. $this->Email->addBlobAttachment($content, 'hotel.png');
  150. $res = $this->Email->getProtected('attachments');
  151. $this->assertTrue(!empty($res['hotel.png']['data']));
  152. unset($res['hotel.png']['data']);
  153. $expected = [
  154. 'hotel.png' => [
  155. //'data' => $content,
  156. 'mimetype' => 'image/png',
  157. ]
  158. ];
  159. $this->assertEquals($expected, $res);
  160. $this->Email->addBlobAttachment($content, 'hotel.gif', 'image/jpeg');
  161. $res = $this->Email->getProtected('attachments');
  162. $this->assertTrue(!empty($res['hotel.gif']['data']));
  163. unset($res['hotel.gif']['data']);
  164. $expected = [
  165. //'data' => $content,
  166. 'mimetype' => 'image/jpeg',
  167. ];
  168. $this->assertEquals($expected, $res['hotel.gif']);
  169. $this->assertSame(2, count($res));
  170. }
  171. /**
  172. * @return void
  173. */
  174. public function testAddEmbeddedAttachment() {
  175. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  176. $this->assertTrue(file_exists($file));
  177. $this->Email = new TestEmail();
  178. $this->Email->emailFormat('both');
  179. $cid = $this->Email->addEmbeddedAttachment($file);
  180. $cid2 = $this->Email->addEmbeddedAttachment($file);
  181. $this->assertSame($cid, $cid2);
  182. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  183. $res = $this->Email->getProtected('attachments');
  184. $this->assertSame(1, count($res));
  185. $image = array_shift($res);
  186. $expected = [
  187. 'file' => $file,
  188. 'mimetype' => 'image/png',
  189. 'contentId' => $cid
  190. ];
  191. $this->assertSame($expected, $image);
  192. }
  193. /**
  194. * Html email
  195. *
  196. * @return void
  197. */
  198. public function testAddEmbeddedAttachmentSend() {
  199. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  200. Configure::write('debug', 0);
  201. $this->Email = new TestEmail();
  202. $this->Email->emailFormat('both');
  203. $this->Email->to(Configure::read('Config.adminEmail'));
  204. $cid = $this->Email->addEmbeddedAttachment($file);
  205. $cid2 = $this->Email->addEmbeddedAttachment($file);
  206. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  207. $html = '<head>
  208. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  209. <meta name="author" content="ohyeah" />
  210. <title>Untitled 6</title>
  211. </head>
  212. <body>
  213. test_embedded_default äöü <img src="cid:' . $cid . '" /> end
  214. another image <img src="cid:' . $cid2 . '" /> end
  215. html-part
  216. </body>
  217. </html>';
  218. $text = trim(strip_tags($html));
  219. $this->Email->viewVars(compact('text', 'html'));
  220. $res = $this->Email->send();
  221. Configure::write('debug', 2);
  222. $error = $this->Email->getError();
  223. $this->assertEquals('', $this->Email->getError());
  224. $this->assertTrue((bool)$res);
  225. }
  226. /**
  227. * @return void
  228. */
  229. public function testAddEmbeddedBlobAttachment() {
  230. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  231. $this->assertTrue(file_exists($file));
  232. $this->Email = new TestEmail();
  233. $this->Email->emailFormat('both');
  234. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
  235. $cid2 = $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->transport('debug');
  269. $res = $this->Email->validates();
  270. $this->assertFalse($res);
  271. $this->Email->subject('foo');
  272. $res = $this->Email->validates();
  273. $this->assertFalse($res);
  274. $this->Email->to('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->emailFormat('both');
  287. $this->Email->to(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->viewVars(compact('text', 'html'));
  302. $res = $this->Email->send();
  303. $error = $this->Email->getError();
  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) <= Email::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->wrapLength(100);
  351. $is = $this->Email->wrap($html);
  352. foreach ($is as $line => $content) {
  353. $this->assertTrue(strlen($content) <= Email::LINE_LENGTH_MUST);
  354. }
  355. $this->debug($is);
  356. $this->assertTrue(count($is) >= 16);
  357. }
  358. }