MailerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Copyright (c) 2014-2015 Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @author Arne Hamann <github@arne.email>
  6. *
  7. * This file is licensed under the Affero General Public License version 3 or
  8. * later.
  9. * See the COPYING-README file.
  10. */
  11. namespace Test\Mail;
  12. use OC\Mail\EMailTemplate;
  13. use OC\Mail\Mailer;
  14. use OC\Mail\Message;
  15. use OCP\Defaults;
  16. use OCP\EventDispatcher\IEventDispatcher;
  17. use OCP\IConfig;
  18. use OCP\IL10N;
  19. use OCP\ILogger;
  20. use OCP\IURLGenerator;
  21. use OCP\L10N\IFactory;
  22. use OCP\Mail\Events\BeforeMessageSent;
  23. use Test\TestCase;
  24. use Swift_SwiftException;
  25. class MailerTest extends TestCase {
  26. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  27. private $config;
  28. /** @var Defaults|\PHPUnit\Framework\MockObject\MockObject */
  29. private $defaults;
  30. /** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
  31. private $logger;
  32. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  33. private $urlGenerator;
  34. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  35. private $l10n;
  36. /** @var Mailer */
  37. private $mailer;
  38. /** @var IEventDispatcher */
  39. private $dispatcher;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->config = $this->createMock(IConfig::class);
  43. $this->defaults = $this->createMock(Defaults::class);
  44. $this->logger = $this->createMock(ILogger::class);
  45. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  46. $this->l10n = $this->createMock(IL10N::class);
  47. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  48. $this->mailer = new Mailer(
  49. $this->config,
  50. $this->logger,
  51. $this->defaults,
  52. $this->urlGenerator,
  53. $this->l10n,
  54. $this->dispatcher,
  55. $this->createMock(IFactory::class)
  56. );
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function sendmailModeProvider(): array {
  62. return [
  63. 'smtp' => ['smtp', ' -bs'],
  64. 'pipe' => ['pipe', ' -t'],
  65. ];
  66. }
  67. /**
  68. * @dataProvider sendmailModeProvider
  69. * @param $sendmailMode
  70. * @param $binaryParam
  71. */
  72. public function testGetSendmailInstanceSendMail($sendmailMode, $binaryParam) {
  73. $this->config
  74. ->expects($this->exactly(2))
  75. ->method('getSystemValue')
  76. ->willReturnMap([
  77. ['mail_smtpmode', 'smtp', 'sendmail'],
  78. ['mail_sendmailmode', 'smtp', $sendmailMode],
  79. ]);
  80. $path = \OC_Helper::findBinaryPath('sendmail');
  81. if ($path === null) {
  82. $path = '/usr/sbin/sendmail';
  83. }
  84. $expected = new \Swift_SendmailTransport($path . $binaryParam);
  85. $this->assertEquals($expected, self::invokePrivate($this->mailer, 'getSendMailInstance'));
  86. }
  87. /**
  88. * @dataProvider sendmailModeProvider
  89. * @param $sendmailMode
  90. * @param $binaryParam
  91. */
  92. public function testGetSendmailInstanceSendMailQmail($sendmailMode, $binaryParam) {
  93. $this->config
  94. ->expects($this->exactly(2))
  95. ->method('getSystemValue')
  96. ->willReturnMap([
  97. ['mail_smtpmode', 'smtp', 'qmail'],
  98. ['mail_sendmailmode', 'smtp', $sendmailMode],
  99. ]);
  100. $this->assertEquals(new \Swift_SendmailTransport('/var/qmail/bin/sendmail' . $binaryParam), self::invokePrivate($this->mailer, 'getSendMailInstance'));
  101. }
  102. public function testGetInstanceDefault() {
  103. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  104. $this->assertInstanceOf(\Swift_Mailer::class, $mailer);
  105. $this->assertInstanceOf(\Swift_SmtpTransport::class, $mailer->getTransport());
  106. }
  107. public function testGetInstanceSendmail() {
  108. $this->config
  109. ->method('getSystemValue')
  110. ->willReturnMap([
  111. ['mail_smtpmode', 'smtp', 'sendmail'],
  112. ['mail_sendmailmode', 'smtp', 'smtp'],
  113. ]);
  114. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  115. $this->assertInstanceOf(\Swift_Mailer::class, $mailer);
  116. $this->assertInstanceOf(\Swift_SendmailTransport::class, $mailer->getTransport());
  117. }
  118. public function testEvents() {
  119. $message = $this->createMock(Message::class);
  120. $event = new BeforeMessageSent($message);
  121. $this->dispatcher->expects($this->at(0))
  122. ->method('dispatchTyped')
  123. ->with($this->equalTo($event));
  124. # We do not care at this point about errors in Swiftmailer
  125. try {
  126. $this->mailer->send($message);
  127. } catch (Swift_SwiftException $e) {
  128. }
  129. }
  130. public function testCreateMessage() {
  131. $this->config
  132. ->expects($this->any())
  133. ->method('getSystemValue')
  134. ->with('mail_send_plaintext_only', false)
  135. ->willReturn(false);
  136. $this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
  137. }
  138. public function testSendInvalidMailException() {
  139. $this->expectException(\Exception::class);
  140. $message = $this->getMockBuilder('\OC\Mail\Message')
  141. ->disableOriginalConstructor()->getMock();
  142. $message->expects($this->once())
  143. ->method('getSwiftMessage')
  144. ->willReturn(new \Swift_Message());
  145. $this->mailer->send($message);
  146. }
  147. /**
  148. * @return array
  149. */
  150. public function mailAddressProvider() {
  151. return [
  152. ['lukas@owncloud.com', true],
  153. ['lukas@localhost', true],
  154. ['lukas@192.168.1.1', true],
  155. ['lukas@éxämplè.com', true],
  156. ['asdf', false],
  157. ['lukas@owncloud.org@owncloud.com', false],
  158. ];
  159. }
  160. /**
  161. * @dataProvider mailAddressProvider
  162. */
  163. public function testValidateMailAddress($email, $expected) {
  164. $this->assertSame($expected, $this->mailer->validateMailAddress($email));
  165. }
  166. public function testCreateEMailTemplate() {
  167. $this->config->method('getSystemValue')
  168. ->with('mail_template_class', '')
  169. ->willReturnArgument(1);
  170. $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
  171. }
  172. public function testStreamingOptions() {
  173. $this->config->method('getSystemValue')
  174. ->willReturnMap([
  175. ['mail_smtpmode', 'smtp', 'smtp'],
  176. ['mail_smtpstreamoptions', [], ['foo' => 1]]
  177. ]);
  178. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  179. $this->assertEquals(1, count($mailer->getTransport()->getStreamOptions()));
  180. $this->assertTrue(isset($mailer->getTransport()->getStreamOptions()['foo']));
  181. }
  182. public function testStreamingOptionsWrongType() {
  183. $this->config->method('getSystemValue')
  184. ->willReturnMap([
  185. ['mail_smtpmode', 'smtp', 'smtp'],
  186. ['mail_smtpstreamoptions', [], 'bar']
  187. ]);
  188. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  189. $this->assertEquals(0, count($mailer->getTransport()->getStreamOptions()));
  190. }
  191. }