MailerTest.php 10 KB

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