EmailLibTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. App::uses('MyCakeTestCase', 'Tools.Lib');
  3. App::uses('EmailLib', 'Tools.Lib');
  4. //Configure::write('Config.admin_email', '...');
  5. class EmailLibTest extends MyCakeTestCase {
  6. public $Email;
  7. public function startTest() {
  8. $this->Email = new EmailLib();
  9. }
  10. public function testObject() {
  11. $this->assertTrue(is_object($this->Email));
  12. $this->assertTrue(is_a($this->Email, 'EmailLib'));
  13. ;
  14. }
  15. public function testSendDefault() {
  16. # start
  17. $this->Email->to(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
  18. $this->Email->subject('Test Subject');
  19. $res = $this->Email->send('xyz xyz');
  20. # end
  21. if ($error = $this->Email->getError()) {
  22. $this->out($error);
  23. }
  24. $this->assertEquals('', $this->Email->getError());
  25. $this->assertTrue($res);
  26. $this->Email->resetAndSet();
  27. # start
  28. $this->Email->to(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
  29. $this->Email->subject('Test Subject 2');
  30. $this->Email->template('default', 'internal');
  31. $this->Email->viewVars(array('x'=>'y', 'xx'=>'yy', 'text'=>''));
  32. $this->Email->addAttachments(array(APP.'webroot'.DS.'img'.DS.'icons'.DS.'edit.gif'));
  33. $res = $this->Email->send('xyz');
  34. # end
  35. if ($error = $this->Email->getError()) {
  36. $this->out($error);
  37. }
  38. $this->assertEquals('', $this->Email->getError());
  39. $this->assertTrue($res);
  40. }
  41. public function testSendFast() {
  42. //$this->Email->resetAndSet();
  43. //$this->Email->from(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
  44. $res = EmailLib::systemEmail('system-mail test', 'some fast email to admin test');
  45. //debug($res);
  46. $this->assertTrue($res);
  47. }
  48. public function _testAddAttachment() {
  49. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  50. $this->assertTrue(file_exists($file));
  51. Configure::write('debug', 0);
  52. $this->Email->to(Configure::read('Config.admin_email'));
  53. $this->Email->addAttachment($file);
  54. $res = $this->Email->send('test_default', 'internal');
  55. if ($error = $this->Email->getError()) {
  56. $this->out($error);
  57. }
  58. $this->assertEquals('', $this->Email->getError());
  59. $this->assertTrue($res);
  60. $this->Email->resetAndSet();
  61. $this->Email->to(Configure::read('Config.admin_email'));
  62. $this->Email->addAttachment($file, 'x.jpg');
  63. $res = $this->Email->send('test_custom_filename');
  64. Configure::write('debug', 2);
  65. $this->assertEquals('', $this->Email->getError());
  66. $this->assertTrue($res);
  67. }
  68. /**
  69. * html email
  70. */
  71. public function testAddEmbeddedAttachment() {
  72. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  73. $this->assertTrue(file_exists($file));
  74. Configure::write('debug', 0);
  75. $this->Email = new EmailLib();
  76. $this->Email->emailFormat('both');
  77. $this->Email->to(Configure::read('Config.admin_email'));
  78. $cid = $this->Email->addEmbeddedAttachment($file);
  79. $this->assertContains('@'.env('HTTP_HOST'), $cid);
  80. $html = '<head>
  81. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  82. <meta name="author" content="ohyeah" />
  83. <title>Untitled 6</title>
  84. </head>
  85. <body>
  86. test_embedded_default äöü <img src="cid:'.$cid.'" /> end
  87. html-part
  88. </body>
  89. </html>';
  90. $text = trim(strip_tags($html));
  91. $this->Email->viewVars(compact('text', 'html'));
  92. $res = $this->Email->send();
  93. Configure::write('debug', 2);
  94. if ($error = $this->Email->getError()) {
  95. $this->out($error);
  96. }
  97. $this->assertEquals('', $this->Email->getError());
  98. $this->assertTrue($res);
  99. }
  100. public function _testComplexeHtmlWithEmbeddedImages() {
  101. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  102. $this->assertTrue(file_exists($file));
  103. }
  104. }