MailerTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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&MockObject */
  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. /** @var Message&MockObject */
  174. $message = $this->getMockBuilder('\OC\Mail\Message')
  175. ->disableOriginalConstructor()->getMock();
  176. $message->expects($this->once())
  177. ->method('getSymfonyEmail')
  178. ->willReturn(new Email());
  179. $this->mailer->send($message);
  180. }
  181. /**
  182. * @return array
  183. */
  184. public function mailAddressProvider() {
  185. return [
  186. ['lukas@owncloud.com', true, false],
  187. ['lukas@localhost', true, false],
  188. ['lukas@192.168.1.1', true, false],
  189. ['lukas@éxämplè.com', true, false],
  190. ['asdf', false, false],
  191. ['', false, false],
  192. ['lukas@owncloud.org@owncloud.com', false, false],
  193. ['test@localhost', true, false],
  194. ['test@localhost', false, true],
  195. ];
  196. }
  197. /**
  198. * @dataProvider mailAddressProvider
  199. */
  200. public function testValidateMailAddress($email, $expected, $strict) {
  201. $this->config
  202. ->expects($this->atMost(1))
  203. ->method('getAppValue')
  204. ->with('core', 'enforce_strict_email_check')
  205. ->willReturn($strict ? 'yes' : 'no');
  206. $this->assertSame($expected, $this->mailer->validateMailAddress($email));
  207. }
  208. public function testCreateEMailTemplate() {
  209. $this->config->method('getSystemValueString')
  210. ->with('mail_template_class', '')
  211. ->willReturnArgument(1);
  212. $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
  213. }
  214. public function testStreamingOptions() {
  215. $this->config->method('getSystemValue')
  216. ->willReturnMap([
  217. ['mail_smtpstreamoptions', [], ['foo' => 1]],
  218. ]);
  219. $this->config->method('getSystemValueString')
  220. ->willReturnMap([
  221. ['mail_smtpmode', 'smtp', 'smtp'],
  222. ['overwrite.cli.url', '', ''],
  223. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  224. ]);
  225. $this->config->method('getSystemValueInt')
  226. ->willReturnMap([
  227. ['mail_smtpport', 25, 25],
  228. ['mail_smtptimeout', 10, 10],
  229. ]);
  230. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  231. /** @var EsmtpTransport $transport */
  232. $transport = self::invokePrivate($mailer, 'transport');
  233. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  234. $this->assertEquals(1, count($transport->getStream()->getStreamOptions()));
  235. $this->assertTrue(isset($transport->getStream()->getStreamOptions()['foo']));
  236. }
  237. public function testStreamingOptionsWrongType() {
  238. $this->config->method('getSystemValue')
  239. ->willReturnMap([
  240. ['mail_smtpstreamoptions', [], 'bar'],
  241. ]);
  242. $this->config->method('getSystemValueString')
  243. ->willReturnMap([
  244. ['mail_smtpmode', 'smtp', 'smtp'],
  245. ['overwrite.cli.url', '', ''],
  246. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  247. ]);
  248. $this->config->method('getSystemValueInt')
  249. ->willReturnMap([
  250. ['mail_smtpport', 25, 25],
  251. ['mail_smtptimeout', 10, 10],
  252. ]);
  253. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  254. /** @var EsmtpTransport $transport */
  255. $transport = self::invokePrivate($mailer, 'transport');
  256. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  257. $this->assertEquals(0, count($transport->getStream()->getStreamOptions()));
  258. }
  259. public function testLocalDomain(): void {
  260. $this->config->method('getSystemValueString')
  261. ->willReturnMap([
  262. ['mail_smtpmode', 'smtp', 'smtp'],
  263. ['overwrite.cli.url', '', 'https://some.valid.url.com:8080'],
  264. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  265. ]);
  266. $this->config->method('getSystemValueInt')
  267. ->willReturnMap([
  268. ['mail_smtpport', 25, 25],
  269. ['mail_smtptimeout', 10, 10],
  270. ]);
  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('some.valid.url.com', $transport->getLocalDomain());
  278. }
  279. public function testLocalDomainInvalidUrl(): void {
  280. $this->config->method('getSystemValueString')
  281. ->willReturnMap([
  282. ['mail_smtpmode', 'smtp', 'smtp'],
  283. ['overwrite.cli.url', '', 'https:only.slash.does.not.work: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('[127.0.0.1]', $transport->getLocalDomain());
  298. }
  299. }