1
0

MailerTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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->config->method('getAppValue')
  213. ->with('theming', 'logoDimensions', Mailer::DEFAULT_DIMENSIONS)
  214. ->willReturn(Mailer::DEFAULT_DIMENSIONS);
  215. $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
  216. }
  217. public function testStreamingOptions() {
  218. $this->config->method('getSystemValue')
  219. ->willReturnMap([
  220. ['mail_smtpstreamoptions', [], ['foo' => 1]],
  221. ]);
  222. $this->config->method('getSystemValueString')
  223. ->willReturnMap([
  224. ['mail_smtpmode', 'smtp', 'smtp'],
  225. ['overwrite.cli.url', '', ''],
  226. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  227. ]);
  228. $this->config->method('getSystemValueInt')
  229. ->willReturnMap([
  230. ['mail_smtpport', 25, 25],
  231. ['mail_smtptimeout', 10, 10],
  232. ]);
  233. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  234. /** @var EsmtpTransport $transport */
  235. $transport = self::invokePrivate($mailer, 'transport');
  236. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  237. $this->assertEquals(1, count($transport->getStream()->getStreamOptions()));
  238. $this->assertTrue(isset($transport->getStream()->getStreamOptions()['foo']));
  239. }
  240. public function testStreamingOptionsWrongType() {
  241. $this->config->method('getSystemValue')
  242. ->willReturnMap([
  243. ['mail_smtpstreamoptions', [], 'bar'],
  244. ]);
  245. $this->config->method('getSystemValueString')
  246. ->willReturnMap([
  247. ['mail_smtpmode', 'smtp', 'smtp'],
  248. ['overwrite.cli.url', '', ''],
  249. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  250. ]);
  251. $this->config->method('getSystemValueInt')
  252. ->willReturnMap([
  253. ['mail_smtpport', 25, 25],
  254. ['mail_smtptimeout', 10, 10],
  255. ]);
  256. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  257. /** @var EsmtpTransport $transport */
  258. $transport = self::invokePrivate($mailer, 'transport');
  259. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  260. $this->assertEquals(0, count($transport->getStream()->getStreamOptions()));
  261. }
  262. public function testLocalDomain(): void {
  263. $this->config->method('getSystemValueString')
  264. ->willReturnMap([
  265. ['mail_smtpmode', 'smtp', 'smtp'],
  266. ['overwrite.cli.url', '', 'https://some.valid.url.com:8080'],
  267. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  268. ]);
  269. $this->config->method('getSystemValueInt')
  270. ->willReturnMap([
  271. ['mail_smtpport', 25, 25],
  272. ['mail_smtptimeout', 10, 10],
  273. ]);
  274. /** @var SymfonyMailer $mailer */
  275. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  276. self::assertInstanceOf(SymfonyMailer::class, $mailer);
  277. /** @var EsmtpTransport $transport */
  278. $transport = self::invokePrivate($mailer, 'transport');
  279. self::assertInstanceOf(EsmtpTransport::class, $transport);
  280. self::assertEquals('some.valid.url.com', $transport->getLocalDomain());
  281. }
  282. public function testLocalDomainInvalidUrl(): void {
  283. $this->config->method('getSystemValueString')
  284. ->willReturnMap([
  285. ['mail_smtpmode', 'smtp', 'smtp'],
  286. ['overwrite.cli.url', '', 'https:only.slash.does.not.work:8080'],
  287. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  288. ]);
  289. $this->config->method('getSystemValueInt')
  290. ->willReturnMap([
  291. ['mail_smtpport', 25, 25],
  292. ['mail_smtptimeout', 10, 10],
  293. ]);
  294. /** @var SymfonyMailer $mailer */
  295. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  296. self::assertInstanceOf(SymfonyMailer::class, $mailer);
  297. /** @var EsmtpTransport $transport */
  298. $transport = self::invokePrivate($mailer, 'transport');
  299. self::assertInstanceOf(EsmtpTransport::class, $transport);
  300. self::assertEquals('[127.0.0.1]', $transport->getLocalDomain());
  301. }
  302. }