MailerTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 Swift_SwiftException;
  24. use Test\TestCase;
  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. ['', false],
  158. ['lukas@owncloud.org@owncloud.com', false],
  159. ];
  160. }
  161. /**
  162. * @dataProvider mailAddressProvider
  163. */
  164. public function testValidateMailAddress($email, $expected) {
  165. $this->assertSame($expected, $this->mailer->validateMailAddress($email));
  166. }
  167. public function testCreateEMailTemplate() {
  168. $this->config->method('getSystemValue')
  169. ->with('mail_template_class', '')
  170. ->willReturnArgument(1);
  171. $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
  172. }
  173. public function testStreamingOptions() {
  174. $this->config->method('getSystemValue')
  175. ->willReturnMap([
  176. ['mail_smtpmode', 'smtp', 'smtp'],
  177. ['mail_smtpstreamoptions', [], ['foo' => 1]]
  178. ]);
  179. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  180. $this->assertEquals(1, count($mailer->getTransport()->getStreamOptions()));
  181. $this->assertTrue(isset($mailer->getTransport()->getStreamOptions()['foo']));
  182. }
  183. public function testStreamingOptionsWrongType() {
  184. $this->config->method('getSystemValue')
  185. ->willReturnMap([
  186. ['mail_smtpmode', 'smtp', 'smtp'],
  187. ['mail_smtpstreamoptions', [], 'bar']
  188. ]);
  189. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  190. $this->assertEquals(0, count($mailer->getTransport()->getStreamOptions()));
  191. }
  192. public function testLocalDomain(): void {
  193. $this->config->method('getSystemValue')
  194. ->willReturnMap([
  195. ['mail_smtpmode', 'smtp', 'smtp']
  196. ]);
  197. $this->config->method('getSystemValueString')
  198. ->with('overwrite.cli.url', '')
  199. ->willReturn('https://some.valid.url.com:8080');
  200. /** @var \Swift_Mailer $mailer */
  201. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  202. self::assertInstanceOf(\Swift_Mailer::class, $mailer);
  203. /** @var \Swift_Transport_EsmtpTransport $transport */
  204. $transport = $mailer->getTransport();
  205. self::assertInstanceOf(\Swift_Transport_EsmtpTransport::class, $transport);
  206. self::assertEquals('some.valid.url.com', $transport->getLocalDomain());
  207. }
  208. public function testLocalDomainInvalidUrl(): void {
  209. $this->config->method('getSystemValue')
  210. ->willReturnMap([
  211. ['mail_smtpmode', 'smtp', 'smtp']
  212. ]);
  213. $this->config->method('getSystemValueString')
  214. ->with('overwrite.cli.url', '')
  215. ->willReturn('https:only.slash.does.not.work:8080');
  216. /** @var \Swift_Mailer $mailer */
  217. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  218. self::assertInstanceOf(\Swift_Mailer::class, $mailer);
  219. /** @var \Swift_Transport_EsmtpTransport $transport */
  220. $transport = $mailer->getTransport();
  221. self::assertInstanceOf(\Swift_Transport_EsmtpTransport::class, $transport);
  222. self::assertEquals('[127.0.0.1]', $transport->getLocalDomain());
  223. }
  224. }