MailerTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Mail;
  8. use OC\Mail\EMailTemplate;
  9. use OC\Mail\Mailer;
  10. use OC\Mail\Message;
  11. use OCP\Defaults;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\IConfig;
  14. use OCP\IL10N;
  15. use OCP\IURLGenerator;
  16. use OCP\L10N\IFactory;
  17. use OCP\Mail\Events\BeforeMessageSent;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. use Psr\Log\LoggerInterface;
  20. use Symfony\Component\Mailer\Mailer as SymfonyMailer;
  21. use Symfony\Component\Mailer\Transport\SendmailTransport;
  22. use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
  23. use Symfony\Component\Mime\Email;
  24. use Test\TestCase;
  25. class MailerTest extends TestCase {
  26. /** @var IConfig|MockObject */
  27. private $config;
  28. /** @var Defaults|MockObject */
  29. private $defaults;
  30. /** @var LoggerInterface|MockObject */
  31. private $logger;
  32. /** @var IURLGenerator|MockObject */
  33. private $urlGenerator;
  34. /** @var IL10N|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(LoggerInterface::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 -i'],
  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('getSystemValueString')
  76. ->willReturnMap([
  77. ['mail_smtpmode', 'smtp', 'sendmail'],
  78. ['mail_sendmailmode', 'smtp', $sendmailMode],
  79. ]);
  80. $path = \OC_Helper::findBinaryPath('sendmail');
  81. if ($path === false) {
  82. $path = '/usr/sbin/sendmail';
  83. }
  84. $expected = new SendmailTransport($path . $binaryParam, null, $this->logger);
  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('getSystemValueString')
  96. ->willReturnMap([
  97. ['mail_smtpmode', 'smtp', 'qmail'],
  98. ['mail_sendmailmode', 'smtp', $sendmailMode],
  99. ]);
  100. $sendmail = new SendmailTransport('/var/qmail/bin/sendmail' . $binaryParam, null, $this->logger);
  101. $this->assertEquals($sendmail, self::invokePrivate($this->mailer, 'getSendMailInstance'));
  102. }
  103. public function testGetInstanceDefault() {
  104. $this->config
  105. ->method('getSystemValue')
  106. ->willReturnMap([
  107. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  108. ['mail_smtpport', 25, 25],
  109. ['mail_smtptimeout', 10, 10],
  110. ]);
  111. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  112. $this->assertInstanceOf(SymfonyMailer::class, $mailer);
  113. $transport = self::invokePrivate($mailer, 'transport');
  114. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  115. }
  116. public function testGetInstanceSendmail() {
  117. $this->config
  118. ->method('getSystemValueString')
  119. ->willReturnMap([
  120. ['mail_smtpmode', 'smtp', 'sendmail'],
  121. ['mail_sendmailmode', 'smtp', 'smtp'],
  122. ]);
  123. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  124. $this->assertInstanceOf(SymfonyMailer::class, $mailer);
  125. $transport = self::invokePrivate($mailer, 'transport');
  126. $this->assertInstanceOf(SendmailTransport::class, $transport);
  127. }
  128. public function testEvents() {
  129. $this->config
  130. ->method('getSystemValue')
  131. ->willReturnMap([
  132. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  133. ['mail_smtpport', 25, 25],
  134. ]);
  135. $this->mailer = $this->getMockBuilder(Mailer::class)
  136. ->setMethods(['getInstance'])
  137. ->setConstructorArgs(
  138. [
  139. $this->config,
  140. $this->logger,
  141. $this->defaults,
  142. $this->urlGenerator,
  143. $this->l10n,
  144. $this->dispatcher,
  145. $this->createMock(IFactory::class)
  146. ]
  147. )
  148. ->getMock();
  149. $message = $this->createMock(Message::class);
  150. $event = new BeforeMessageSent($message);
  151. $this->dispatcher->expects($this->once())
  152. ->method('dispatchTyped')
  153. ->with($this->equalTo($event));
  154. $this->mailer->send($message);
  155. }
  156. public function testCreateMessage() {
  157. $this->config
  158. ->expects($this->any())
  159. ->method('getSystemValueBool')
  160. ->with('mail_send_plaintext_only', false)
  161. ->willReturn(false);
  162. $this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
  163. }
  164. public function testSendInvalidMailException() {
  165. $this->config
  166. ->method('getSystemValue')
  167. ->willReturnMap([
  168. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  169. ['mail_smtpport', 25, 25],
  170. ['mail_smtptimeout', 10, 10],
  171. ]);
  172. $this->expectException(\Exception::class);
  173. $message = $this->getMockBuilder('\OC\Mail\Message')
  174. ->disableOriginalConstructor()->getMock();
  175. $message->expects($this->once())
  176. ->method('getSymfonyEmail')
  177. ->willReturn(new Email());
  178. $this->mailer->send($message);
  179. }
  180. /**
  181. * @return array
  182. */
  183. public function mailAddressProvider() {
  184. return [
  185. ['lukas@owncloud.com', true],
  186. ['lukas@localhost', true],
  187. ['lukas@192.168.1.1', true],
  188. ['lukas@éxämplè.com', true],
  189. ['asdf', false],
  190. ['', false],
  191. ['lukas@owncloud.org@owncloud.com', false],
  192. ];
  193. }
  194. /**
  195. * @dataProvider mailAddressProvider
  196. */
  197. public function testValidateMailAddress($email, $expected) {
  198. $this->assertSame($expected, $this->mailer->validateMailAddress($email));
  199. }
  200. public function testCreateEMailTemplate() {
  201. $this->config->method('getSystemValueString')
  202. ->with('mail_template_class', '')
  203. ->willReturnArgument(1);
  204. $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
  205. }
  206. public function testStreamingOptions() {
  207. $this->config->method('getSystemValue')
  208. ->willReturnMap([
  209. ['mail_smtpstreamoptions', [], ['foo' => 1]],
  210. ]);
  211. $this->config->method('getSystemValueString')
  212. ->willReturnMap([
  213. ['mail_smtpmode', 'smtp', 'smtp'],
  214. ['overwrite.cli.url', '', ''],
  215. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  216. ]);
  217. $this->config->method('getSystemValueInt')
  218. ->willReturnMap([
  219. ['mail_smtpport', 25, 25],
  220. ['mail_smtptimeout', 10, 10],
  221. ]);
  222. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  223. /** @var EsmtpTransport $transport */
  224. $transport = self::invokePrivate($mailer, 'transport');
  225. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  226. $this->assertEquals(1, count($transport->getStream()->getStreamOptions()));
  227. $this->assertTrue(isset($transport->getStream()->getStreamOptions()['foo']));
  228. }
  229. public function testStreamingOptionsWrongType() {
  230. $this->config->method('getSystemValue')
  231. ->willReturnMap([
  232. ['mail_smtpstreamoptions', [], 'bar'],
  233. ]);
  234. $this->config->method('getSystemValueString')
  235. ->willReturnMap([
  236. ['mail_smtpmode', 'smtp', 'smtp'],
  237. ['overwrite.cli.url', '', ''],
  238. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  239. ]);
  240. $this->config->method('getSystemValueInt')
  241. ->willReturnMap([
  242. ['mail_smtpport', 25, 25],
  243. ['mail_smtptimeout', 10, 10],
  244. ]);
  245. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  246. /** @var EsmtpTransport $transport */
  247. $transport = self::invokePrivate($mailer, 'transport');
  248. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  249. $this->assertEquals(0, count($transport->getStream()->getStreamOptions()));
  250. }
  251. public function testLocalDomain(): void {
  252. $this->config->method('getSystemValueString')
  253. ->willReturnMap([
  254. ['mail_smtpmode', 'smtp', 'smtp'],
  255. ['overwrite.cli.url', '', 'https://some.valid.url.com:8080'],
  256. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  257. ]);
  258. $this->config->method('getSystemValueInt')
  259. ->willReturnMap([
  260. ['mail_smtpport', 25, 25],
  261. ['mail_smtptimeout', 10, 10],
  262. ]);
  263. /** @var SymfonyMailer $mailer */
  264. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  265. self::assertInstanceOf(SymfonyMailer::class, $mailer);
  266. /** @var EsmtpTransport $transport */
  267. $transport = self::invokePrivate($mailer, 'transport');
  268. self::assertInstanceOf(EsmtpTransport::class, $transport);
  269. self::assertEquals('some.valid.url.com', $transport->getLocalDomain());
  270. }
  271. public function testLocalDomainInvalidUrl(): void {
  272. $this->config->method('getSystemValueString')
  273. ->willReturnMap([
  274. ['mail_smtpmode', 'smtp', 'smtp'],
  275. ['overwrite.cli.url', '', 'https:only.slash.does.not.work:8080'],
  276. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  277. ]);
  278. $this->config->method('getSystemValueInt')
  279. ->willReturnMap([
  280. ['mail_smtpport', 25, 25],
  281. ['mail_smtptimeout', 10, 10],
  282. ]);
  283. /** @var SymfonyMailer $mailer */
  284. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  285. self::assertInstanceOf(SymfonyMailer::class, $mailer);
  286. /** @var EsmtpTransport $transport */
  287. $transport = self::invokePrivate($mailer, 'transport');
  288. self::assertInstanceOf(EsmtpTransport::class, $transport);
  289. self::assertEquals('[127.0.0.1]', $transport->getLocalDomain());
  290. }
  291. }