You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MailerTest.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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\IURLGenerator;
  20. use OCP\L10N\IFactory;
  21. use OCP\Mail\Events\BeforeMessageSent;
  22. use Psr\Log\LoggerInterface;
  23. use Test\TestCase;
  24. use PHPUnit\Framework\MockObject\MockObject;
  25. use Symfony\Component\Mailer\Mailer as SymfonyMailer;
  26. use Symfony\Component\Mime\Email;
  27. use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
  28. use Symfony\Component\Mailer\Transport\SendmailTransport;
  29. class MailerTest extends TestCase {
  30. /** @var IConfig|MockObject */
  31. private $config;
  32. /** @var Defaults|MockObject */
  33. private $defaults;
  34. /** @var LoggerInterface|MockObject */
  35. private $logger;
  36. /** @var IURLGenerator|MockObject */
  37. private $urlGenerator;
  38. /** @var IL10N|MockObject */
  39. private $l10n;
  40. /** @var Mailer */
  41. private $mailer;
  42. /** @var IEventDispatcher */
  43. private $dispatcher;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->config = $this->createMock(IConfig::class);
  47. $this->defaults = $this->createMock(Defaults::class);
  48. $this->logger = $this->createMock(LoggerInterface::class);
  49. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  50. $this->l10n = $this->createMock(IL10N::class);
  51. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  52. $this->mailer = new Mailer(
  53. $this->config,
  54. $this->logger,
  55. $this->defaults,
  56. $this->urlGenerator,
  57. $this->l10n,
  58. $this->dispatcher,
  59. $this->createMock(IFactory::class)
  60. );
  61. }
  62. /**
  63. * @return array
  64. */
  65. public function sendmailModeProvider(): array {
  66. return [
  67. 'smtp' => ['smtp', ' -bs'],
  68. 'pipe' => ['pipe', ' -t'],
  69. ];
  70. }
  71. /**
  72. * @dataProvider sendmailModeProvider
  73. * @param $sendmailMode
  74. * @param $binaryParam
  75. */
  76. public function testGetSendmailInstanceSendMail($sendmailMode, $binaryParam) {
  77. $this->config
  78. ->expects($this->exactly(2))
  79. ->method('getSystemValue')
  80. ->willReturnMap([
  81. ['mail_smtpmode', 'smtp', 'sendmail'],
  82. ['mail_sendmailmode', 'smtp', $sendmailMode],
  83. ]);
  84. $path = \OC_Helper::findBinaryPath('sendmail');
  85. if ($path === false) {
  86. $path = '/usr/sbin/sendmail';
  87. }
  88. $expected = new SendmailTransport($path . $binaryParam, null, $this->logger);
  89. $this->assertEquals($expected, self::invokePrivate($this->mailer, 'getSendMailInstance'));
  90. }
  91. /**
  92. * @dataProvider sendmailModeProvider
  93. * @param $sendmailMode
  94. * @param $binaryParam
  95. */
  96. public function testGetSendmailInstanceSendMailQmail($sendmailMode, $binaryParam) {
  97. $this->config
  98. ->expects($this->exactly(2))
  99. ->method('getSystemValue')
  100. ->willReturnMap([
  101. ['mail_smtpmode', 'smtp', 'qmail'],
  102. ['mail_sendmailmode', 'smtp', $sendmailMode],
  103. ]);
  104. $sendmail = new SendmailTransport('/var/qmail/bin/sendmail' . $binaryParam, null, $this->logger);
  105. $this->assertEquals($sendmail, self::invokePrivate($this->mailer, 'getSendMailInstance'));
  106. }
  107. public function testGetInstanceDefault() {
  108. $this->config
  109. ->method('getSystemValue')
  110. ->willReturnMap([
  111. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  112. ['mail_smtpport', 25, 25],
  113. ['mail_smtptimeout', 10, 10],
  114. ]);
  115. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  116. $this->assertInstanceOf(SymfonyMailer::class, $mailer);
  117. $transport = self::invokePrivate($mailer, 'transport');
  118. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  119. }
  120. public function testGetInstanceSendmail() {
  121. $this->config
  122. ->method('getSystemValue')
  123. ->willReturnMap([
  124. ['mail_smtpmode', 'smtp', 'sendmail'],
  125. ['mail_sendmailmode', 'smtp', 'smtp'],
  126. ]);
  127. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  128. $this->assertInstanceOf(SymfonyMailer::class, $mailer);
  129. $transport = self::invokePrivate($mailer, 'transport');
  130. $this->assertInstanceOf(SendmailTransport::class, $transport);
  131. }
  132. public function testEvents() {
  133. $this->config
  134. ->method('getSystemValue')
  135. ->willReturnMap([
  136. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  137. ['mail_smtpport', 25, 25],
  138. ]);
  139. $this->mailer = $this->getMockBuilder(Mailer::class)
  140. ->setMethods(['getInstance'])
  141. ->setConstructorArgs(
  142. [
  143. $this->config,
  144. $this->logger,
  145. $this->defaults,
  146. $this->urlGenerator,
  147. $this->l10n,
  148. $this->dispatcher,
  149. $this->createMock(IFactory::class)
  150. ]
  151. )
  152. ->getMock();
  153. $message = $this->createMock(Message::class);
  154. $event = new BeforeMessageSent($message);
  155. $this->dispatcher->expects($this->once())
  156. ->method('dispatchTyped')
  157. ->with($this->equalTo($event));
  158. $this->mailer->send($message);
  159. }
  160. public function testCreateMessage() {
  161. $this->config
  162. ->expects($this->any())
  163. ->method('getSystemValue')
  164. ->with('mail_send_plaintext_only', false)
  165. ->willReturn(false);
  166. $this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
  167. }
  168. public function testSendInvalidMailException() {
  169. $this->config
  170. ->method('getSystemValue')
  171. ->willReturnMap([
  172. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  173. ['mail_smtpport', 25, 25],
  174. ['mail_smtptimeout', 10, 10],
  175. ]);
  176. $this->expectException(\Exception::class);
  177. $message = $this->getMockBuilder('\OC\Mail\Message')
  178. ->disableOriginalConstructor()->getMock();
  179. $message->expects($this->once())
  180. ->method('getSymfonyEmail')
  181. ->willReturn(new Email());
  182. $this->mailer->send($message);
  183. }
  184. /**
  185. * @return array
  186. */
  187. public function mailAddressProvider() {
  188. return [
  189. ['lukas@owncloud.com', true],
  190. ['lukas@localhost', true],
  191. ['lukas@192.168.1.1', true],
  192. ['lukas@éxämplè.com', true],
  193. ['asdf', false],
  194. ['', false],
  195. ['lukas@owncloud.org@owncloud.com', false],
  196. ];
  197. }
  198. /**
  199. * @dataProvider mailAddressProvider
  200. */
  201. public function testValidateMailAddress($email, $expected) {
  202. $this->assertSame($expected, $this->mailer->validateMailAddress($email));
  203. }
  204. public function testCreateEMailTemplate() {
  205. $this->config->method('getSystemValue')
  206. ->with('mail_template_class', '')
  207. ->willReturnArgument(1);
  208. $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
  209. }
  210. public function testStreamingOptions() {
  211. $this->config->method('getSystemValue')
  212. ->willReturnMap([
  213. ['mail_smtpmode', 'smtp', 'smtp'],
  214. ['mail_smtpstreamoptions', [], ['foo' => 1]],
  215. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  216. ['mail_smtpport', 25, 25],
  217. ['mail_smtptimeout', 10, 10],
  218. ]);
  219. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  220. /** @var EsmtpTransport $transport */
  221. $transport = self::invokePrivate($mailer, 'transport');
  222. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  223. $this->assertEquals(1, count($transport->getStream()->getStreamOptions()));
  224. $this->assertTrue(isset($transport->getStream()->getStreamOptions()['foo']));
  225. }
  226. public function testStreamingOptionsWrongType() {
  227. $this->config->method('getSystemValue')
  228. ->willReturnMap([
  229. ['mail_smtpmode', 'smtp', 'smtp'],
  230. ['mail_smtpstreamoptions', [], 'bar'],
  231. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  232. ['mail_smtpport', 25, 25],
  233. ['mail_smtptimeout', 10, 10],
  234. ]);
  235. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  236. /** @var EsmtpTransport $transport */
  237. $transport = self::invokePrivate($mailer, 'transport');
  238. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  239. $this->assertEquals(0, count($transport->getStream()->getStreamOptions()));
  240. }
  241. public function testLocalDomain(): void {
  242. $this->config->method('getSystemValue')
  243. ->willReturnMap([
  244. ['mail_smtpmode', 'smtp', 'smtp'],
  245. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  246. ['mail_smtpport', 25, 25],
  247. ['mail_smtptimeout', 10, 10],
  248. ]);
  249. $this->config->method('getSystemValueString')
  250. ->with('overwrite.cli.url', '')
  251. ->willReturn('https://some.valid.url.com:8080');
  252. /** @var SymfonyMailer $mailer */
  253. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  254. self::assertInstanceOf(SymfonyMailer::class, $mailer);
  255. /** @var EsmtpTransport $transport */
  256. $transport = self::invokePrivate($mailer, 'transport');
  257. self::assertInstanceOf(EsmtpTransport::class, $transport);
  258. self::assertEquals('some.valid.url.com', $transport->getLocalDomain());
  259. }
  260. public function testLocalDomainInvalidUrl(): void {
  261. $this->config->method('getSystemValue')
  262. ->willReturnMap([
  263. ['mail_smtpmode', 'smtp', 'smtp'],
  264. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  265. ['mail_smtpport', 25, 25],
  266. ['mail_smtptimeout', 10, 10],
  267. ]);
  268. $this->config->method('getSystemValueString')
  269. ->with('overwrite.cli.url', '')
  270. ->willReturn('https:only.slash.does.not.work:8080');
  271. /** @var SymfonyMailer $mailer */
  272. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  273. self::assertInstanceOf(SymfonyMailer::class, $mailer);
  274. /** @var EsmtpTransport $transport */
  275. $transport = self::invokePrivate($mailer, 'transport');
  276. self::assertInstanceOf(EsmtpTransport::class, $transport);
  277. self::assertEquals('[127.0.0.1]', $transport->getLocalDomain());
  278. }
  279. }