MailerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 testEventForNullTransport(): void {
  105. $this->config
  106. ->expects($this->exactly(1))
  107. ->method('getSystemValueString')
  108. ->with('mail_smtpmode', 'smtp')
  109. ->willReturn('null');
  110. $message = $this->createMock(Message::class);
  111. $message->expects($this->once())
  112. ->method('getSymfonyEmail')
  113. ->willReturn((new Email())->to('foo@bar.com')->from('bar@foo.com')->text(''));
  114. $event = new BeforeMessageSent($message);
  115. $this->dispatcher->expects($this->once())
  116. ->method('dispatchTyped')
  117. ->with($this->equalTo($event));
  118. $this->mailer->send($message);
  119. }
  120. public function testGetInstanceDefault(): void {
  121. $this->config
  122. ->method('getSystemValue')
  123. ->willReturnMap([
  124. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  125. ['mail_smtpport', 25, 25],
  126. ['mail_smtptimeout', 10, 10],
  127. ]);
  128. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  129. $this->assertInstanceOf(SymfonyMailer::class, $mailer);
  130. $transport = self::invokePrivate($mailer, 'transport');
  131. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  132. }
  133. public function testGetInstanceSendmail(): void {
  134. $this->config
  135. ->method('getSystemValueString')
  136. ->willReturnMap([
  137. ['mail_smtpmode', 'smtp', 'sendmail'],
  138. ['mail_sendmailmode', 'smtp', 'smtp'],
  139. ]);
  140. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  141. $this->assertInstanceOf(SymfonyMailer::class, $mailer);
  142. $transport = self::invokePrivate($mailer, 'transport');
  143. $this->assertInstanceOf(SendmailTransport::class, $transport);
  144. }
  145. public function testEvents(): void {
  146. $this->config
  147. ->method('getSystemValue')
  148. ->willReturnMap([
  149. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  150. ['mail_smtpport', 25, 25],
  151. ]);
  152. $this->mailer = $this->getMockBuilder(Mailer::class)
  153. ->setMethods(['getInstance'])
  154. ->setConstructorArgs(
  155. [
  156. $this->config,
  157. $this->logger,
  158. $this->defaults,
  159. $this->urlGenerator,
  160. $this->l10n,
  161. $this->dispatcher,
  162. $this->createMock(IFactory::class)
  163. ]
  164. )
  165. ->getMock();
  166. $message = $this->createMock(Message::class);
  167. $event = new BeforeMessageSent($message);
  168. $this->dispatcher->expects($this->once())
  169. ->method('dispatchTyped')
  170. ->with($this->equalTo($event));
  171. $this->mailer->send($message);
  172. }
  173. public function testCreateMessage(): void {
  174. $this->config
  175. ->expects($this->any())
  176. ->method('getSystemValueBool')
  177. ->with('mail_send_plaintext_only', false)
  178. ->willReturn(false);
  179. $this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
  180. }
  181. public function testSendInvalidMailException(): void {
  182. $this->config
  183. ->method('getSystemValue')
  184. ->willReturnMap([
  185. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  186. ['mail_smtpport', 25, 25],
  187. ['mail_smtptimeout', 10, 10],
  188. ]);
  189. $this->expectException(\Exception::class);
  190. /** @var Message&MockObject */
  191. $message = $this->getMockBuilder('\OC\Mail\Message')
  192. ->disableOriginalConstructor()->getMock();
  193. $message->expects($this->once())
  194. ->method('getSymfonyEmail')
  195. ->willReturn(new Email());
  196. $this->mailer->send($message);
  197. }
  198. /**
  199. * @return array
  200. */
  201. public function mailAddressProvider() {
  202. return [
  203. ['lukas@owncloud.com', true, false],
  204. ['lukas@localhost', true, false],
  205. ['lukas@192.168.1.1', true, false],
  206. ['lukas@éxämplè.com', true, false],
  207. ['asdf', false, false],
  208. ['', false, false],
  209. ['lukas@owncloud.org@owncloud.com', false, false],
  210. ['test@localhost', true, false],
  211. ['test@localhost', false, true],
  212. ];
  213. }
  214. /**
  215. * @dataProvider mailAddressProvider
  216. */
  217. public function testValidateMailAddress($email, $expected, $strict): void {
  218. $this->config
  219. ->expects($this->atMost(1))
  220. ->method('getAppValue')
  221. ->with('core', 'enforce_strict_email_check')
  222. ->willReturn($strict ? 'yes' : 'no');
  223. $this->assertSame($expected, $this->mailer->validateMailAddress($email));
  224. }
  225. public function testCreateEMailTemplate(): void {
  226. $this->config->method('getSystemValueString')
  227. ->with('mail_template_class', '')
  228. ->willReturnArgument(1);
  229. $this->config->method('getAppValue')
  230. ->with('theming', 'logoDimensions', Mailer::DEFAULT_DIMENSIONS)
  231. ->willReturn(Mailer::DEFAULT_DIMENSIONS);
  232. $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
  233. }
  234. public function testStreamingOptions(): void {
  235. $this->config->method('getSystemValue')
  236. ->willReturnMap([
  237. ['mail_smtpstreamoptions', [], ['foo' => 1]],
  238. ]);
  239. $this->config->method('getSystemValueString')
  240. ->willReturnMap([
  241. ['mail_smtpmode', 'smtp', 'smtp'],
  242. ['overwrite.cli.url', '', ''],
  243. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  244. ]);
  245. $this->config->method('getSystemValueInt')
  246. ->willReturnMap([
  247. ['mail_smtpport', 25, 25],
  248. ['mail_smtptimeout', 10, 10],
  249. ]);
  250. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  251. /** @var EsmtpTransport $transport */
  252. $transport = self::invokePrivate($mailer, 'transport');
  253. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  254. $this->assertEquals(1, count($transport->getStream()->getStreamOptions()));
  255. $this->assertTrue(isset($transport->getStream()->getStreamOptions()['foo']));
  256. }
  257. public function testStreamingOptionsWrongType(): void {
  258. $this->config->method('getSystemValue')
  259. ->willReturnMap([
  260. ['mail_smtpstreamoptions', [], 'bar'],
  261. ]);
  262. $this->config->method('getSystemValueString')
  263. ->willReturnMap([
  264. ['mail_smtpmode', 'smtp', 'smtp'],
  265. ['overwrite.cli.url', '', ''],
  266. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  267. ]);
  268. $this->config->method('getSystemValueInt')
  269. ->willReturnMap([
  270. ['mail_smtpport', 25, 25],
  271. ['mail_smtptimeout', 10, 10],
  272. ]);
  273. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  274. /** @var EsmtpTransport $transport */
  275. $transport = self::invokePrivate($mailer, 'transport');
  276. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  277. $this->assertEquals(0, count($transport->getStream()->getStreamOptions()));
  278. }
  279. public function testLocalDomain(): void {
  280. $this->config->method('getSystemValueString')
  281. ->willReturnMap([
  282. ['mail_smtpmode', 'smtp', 'smtp'],
  283. ['overwrite.cli.url', '', 'https://some.valid.url.com:8080'],
  284. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  285. ]);
  286. $this->config->method('getSystemValueInt')
  287. ->willReturnMap([
  288. ['mail_smtpport', 25, 25],
  289. ['mail_smtptimeout', 10, 10],
  290. ]);
  291. /** @var SymfonyMailer $mailer */
  292. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  293. self::assertInstanceOf(SymfonyMailer::class, $mailer);
  294. /** @var EsmtpTransport $transport */
  295. $transport = self::invokePrivate($mailer, 'transport');
  296. self::assertInstanceOf(EsmtpTransport::class, $transport);
  297. self::assertEquals('some.valid.url.com', $transport->getLocalDomain());
  298. }
  299. public function testLocalDomainInvalidUrl(): void {
  300. $this->config->method('getSystemValueString')
  301. ->willReturnMap([
  302. ['mail_smtpmode', 'smtp', 'smtp'],
  303. ['overwrite.cli.url', '', 'https:only.slash.does.not.work:8080'],
  304. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  305. ]);
  306. $this->config->method('getSystemValueInt')
  307. ->willReturnMap([
  308. ['mail_smtpport', 25, 25],
  309. ['mail_smtptimeout', 10, 10],
  310. ]);
  311. /** @var SymfonyMailer $mailer */
  312. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  313. self::assertInstanceOf(SymfonyMailer::class, $mailer);
  314. /** @var EsmtpTransport $transport */
  315. $transport = self::invokePrivate($mailer, 'transport');
  316. self::assertInstanceOf(EsmtpTransport::class, $transport);
  317. self::assertEquals('[127.0.0.1]', $transport->getLocalDomain());
  318. }
  319. public function testCaching(): void {
  320. $symfonyMailer1 = self::invokePrivate($this->mailer, 'getInstance');
  321. $symfonyMailer2 = self::invokePrivate($this->mailer, 'getInstance');
  322. self::assertSame($symfonyMailer1, $symfonyMailer2);
  323. }
  324. }