MessageTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace Tools\Test\TestCase\Mailer;
  3. use Cake\Core\Configure;
  4. use Cake\Core\Plugin;
  5. use Shim\TestSuite\TestCase;
  6. use Tools\Mailer\Message as MailerMessage;
  7. class MessageTest extends TestCase {
  8. /**
  9. * @var \Tools\Mailer\Message
  10. */
  11. protected MailerMessage $message;
  12. /**
  13. * setUp
  14. *
  15. * @return void
  16. */
  17. public function setUp(): void {
  18. parent::setUp();
  19. $this->message = new MailerMessage();
  20. }
  21. /**
  22. * tearDown method
  23. *
  24. * @return void
  25. */
  26. public function tearDown(): void {
  27. parent::tearDown();
  28. //Log::drop('email');
  29. }
  30. /**
  31. * @return void
  32. */
  33. public function testAddAttachment() {
  34. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  35. $this->assertTrue(file_exists($file));
  36. $this->message->addAttachment($file);
  37. $res = $this->message->getAttachments();
  38. $expected = [
  39. 'hotel.png' => [
  40. 'file' => $file,
  41. 'mimetype' => 'image/png',
  42. ],
  43. ];
  44. $this->assertEquals($expected, $res);
  45. $this->message->addAttachment($file, 'my_image.jpg');
  46. $res = $this->message->getAttachments();
  47. $expected = [
  48. 'file' => $file,
  49. 'mimetype' => 'image/jpeg',
  50. ];
  51. $this->assertEquals($expected, $res['my_image.jpg']);
  52. }
  53. /**
  54. * @return void
  55. */
  56. public function testAddAttachmentSend() {
  57. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  58. $this->assertTrue(file_exists($file));
  59. $this->message->setTo(Configure::read('Config.adminEmail'));
  60. $this->message->addAttachment($file);
  61. $res = trim($this->message->getBodyString());
  62. $this->assertNotEmpty($res);
  63. }
  64. /**
  65. * @return void
  66. */
  67. public function testAddEmbeddedAttachmentByContentId() {
  68. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  69. $this->message->addEmbeddedAttachmentByContentId('123', $file);
  70. $attachments = $this->message->getAttachments();
  71. $attachment = array_shift($attachments);
  72. $this->assertSame('image/png', $attachment['mimetype']);
  73. $this->assertSame('123', $attachment['contentId']);
  74. }
  75. /**
  76. * @return void
  77. */
  78. public function testAddEmbeddedBlobAttachmentByContentId() {
  79. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  80. $content = file_get_contents($file);
  81. $this->message->addEmbeddedBlobAttachmentByContentId('123', $content, $file);
  82. $attachments = $this->message->getAttachments();
  83. $attachment = array_shift($attachments);
  84. $this->assertNotEmpty($attachment['data']);
  85. $this->assertSame('image/png', $attachment['mimetype']);
  86. $this->assertSame('123', $attachment['contentId']);
  87. $this->message->addEmbeddedBlobAttachmentByContentId('123', $content, $file, 'png');
  88. $attachments = $this->message->getAttachments();
  89. $attachment = array_shift($attachments);
  90. $this->assertSame('png', $attachment['mimetype']);
  91. }
  92. /**
  93. * @return void
  94. */
  95. public function testAddBlobAttachment() {
  96. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  97. $content = file_get_contents($file);
  98. $this->message->addBlobAttachment($content, 'hotel.png');
  99. $res = $this->message->getAttachments();
  100. $this->assertTrue(!empty($res['hotel.png']['data']));
  101. unset($res['hotel.png']['data']);
  102. $expected = [
  103. 'hotel.png' => [
  104. //'data' => $content,
  105. 'mimetype' => 'image/png',
  106. ],
  107. ];
  108. $this->assertEquals($expected, $res);
  109. $this->message->addBlobAttachment($content, 'hotel.gif', 'image/jpeg');
  110. $res = $this->message->getAttachments();
  111. $this->assertTrue(!empty($res['hotel.gif']['data']));
  112. unset($res['hotel.gif']['data']);
  113. $expected = [
  114. //'data' => $content,
  115. 'mimetype' => 'image/jpeg',
  116. ];
  117. $this->assertEquals($expected, $res['hotel.gif']);
  118. $this->assertSame(2, count($res));
  119. }
  120. /**
  121. * @return void
  122. */
  123. public function testAddEmbeddedAttachment() {
  124. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  125. $this->assertTrue(file_exists($file));
  126. $this->message->setEmailFormat('both');
  127. $cid = $this->message->addEmbeddedAttachment($file);
  128. $cid2 = $this->message->addEmbeddedAttachment($file);
  129. $this->assertSame($cid, $cid2);
  130. $this->assertStringContainsString('@' . env('HTTP_HOST'), $cid);
  131. $res = $this->message->getAttachments();
  132. $this->assertSame(1, count($res));
  133. $image = array_shift($res);
  134. $expected = [
  135. 'file' => $file,
  136. 'mimetype' => 'image/png',
  137. 'contentId' => $cid,
  138. ];
  139. $this->assertSame($expected, $image);
  140. }
  141. /**
  142. * @return void
  143. */
  144. public function testAddEmbeddedBlobAttachment() {
  145. $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
  146. $this->assertTrue(file_exists($file));
  147. $this->message->setEmailFormat('both');
  148. $cid = $this->message->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
  149. $this->assertStringContainsString('@' . env('HTTP_HOST'), $cid);
  150. $res = $this->message->getAttachments();
  151. $this->assertSame(1, count($res));
  152. $images = $res;
  153. $image = array_shift($images);
  154. unset($image['data']);
  155. $expected = [
  156. 'mimetype' => 'image/png',
  157. 'contentId' => $cid,
  158. ];
  159. $this->assertEquals($expected, $image);
  160. $options = [
  161. 'contentDisposition' => true,
  162. ];
  163. $cid = 'abcdef';
  164. $this->message->addEmbeddedBlobAttachment(file_get_contents($file), 'my_other_hotel.png', 'image/jpeg', $cid, $options);
  165. $res = $this->message->getAttachments();
  166. $this->assertSame(2, count($res));
  167. $keys = array_keys($res);
  168. $keyLastRecord = $keys[count($keys) - 1];
  169. $this->assertSame('image/jpeg', $res[$keyLastRecord]['mimetype']);
  170. $this->assertTrue($res[$keyLastRecord]['contentDisposition']);
  171. $cid3 = $this->message->addEmbeddedBlobAttachment(file_get_contents($file) . 'xxx', 'my_hotel.png');
  172. $this->assertNotSame($cid3, $cid);
  173. $res = $this->message->getAttachments();
  174. $this->assertSame(3, count($res));
  175. }
  176. }