EmailTest.php 14 KB

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