MailerTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * Copyright (c) 2014-2015 Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @author Arne Hamann <github@arne.email>
  6. *
  7. * This file is licensed under the Affero General Public License version 3 or
  8. * later.
  9. * See the COPYING-README file.
  10. */
  11. namespace Test\Mail;
  12. use OC\Mail\EMailTemplate;
  13. use OC\Mail\Mailer;
  14. use OC\Mail\Message;
  15. use OCP\Defaults;
  16. use OCP\EventDispatcher\IEventDispatcher;
  17. use OCP\IConfig;
  18. use OCP\IL10N;
  19. use OCP\IURLGenerator;
  20. use OCP\L10N\IFactory;
  21. use OCP\Mail\Events\BeforeMessageSent;
  22. use PHPUnit\Framework\MockObject\MockObject;
  23. use Psr\Log\LoggerInterface;
  24. use Symfony\Component\Mailer\Mailer as SymfonyMailer;
  25. use Symfony\Component\Mailer\Transport\SendmailTransport;
  26. use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
  27. use Symfony\Component\Mime\Email;
  28. use Test\TestCase;
  29. class MailerTest extends TestCase {
  30. /** @var IConfig|MockObject */
  31. private $config;
  32. /** @var Defaults|MockObject */
  33. private $defaults;
  34. /** @var LoggerInterface|MockObject */
  35. private $logger;
  36. /** @var IURLGenerator|MockObject */
  37. private $urlGenerator;
  38. /** @var IL10N|MockObject */
  39. private $l10n;
  40. /** @var Mailer */
  41. private $mailer;
  42. /** @var IEventDispatcher&MockObject */
  43. private $dispatcher;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->config = $this->createMock(IConfig::class);
  47. $this->defaults = $this->createMock(Defaults::class);
  48. $this->logger = $this->createMock(LoggerInterface::class);
  49. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  50. $this->l10n = $this->createMock(IL10N::class);
  51. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  52. $this->mailer = new Mailer(
  53. $this->config,
  54. $this->logger,
  55. $this->defaults,
  56. $this->urlGenerator,
  57. $this->l10n,
  58. $this->dispatcher,
  59. $this->createMock(IFactory::class)
  60. );
  61. }
  62. /**
  63. * @return array
  64. */
  65. public function sendmailModeProvider(): array {
  66. return [
  67. 'smtp' => ['smtp', ' -bs'],
  68. 'pipe' => ['pipe', ' -t -i'],
  69. ];
  70. }
  71. /**
  72. * @dataProvider sendmailModeProvider
  73. * @param $sendmailMode
  74. * @param $binaryParam
  75. */
  76. public function testGetSendmailInstanceSendMail($sendmailMode, $binaryParam) {
  77. $this->config
  78. ->expects($this->exactly(2))
  79. ->method('getSystemValueString')
  80. ->willReturnMap([
  81. ['mail_smtpmode', 'smtp', 'sendmail'],
  82. ['mail_sendmailmode', 'smtp', $sendmailMode],
  83. ]);
  84. $path = \OC_Helper::findBinaryPath('sendmail');
  85. if ($path === false) {
  86. $path = '/usr/sbin/sendmail';
  87. }
  88. $expected = new SendmailTransport($path . $binaryParam, null, $this->logger);
  89. $this->assertEquals($expected, self::invokePrivate($this->mailer, 'getSendMailInstance'));
  90. }
  91. /**
  92. * @dataProvider sendmailModeProvider
  93. * @param $sendmailMode
  94. * @param $binaryParam
  95. */
  96. public function testGetSendmailInstanceSendMailQmail($sendmailMode, $binaryParam) {
  97. $this->config
  98. ->expects($this->exactly(2))
  99. ->method('getSystemValueString')
  100. ->willReturnMap([
  101. ['mail_smtpmode', 'smtp', 'qmail'],
  102. ['mail_sendmailmode', 'smtp', $sendmailMode],
  103. ]);
  104. $sendmail = new SendmailTransport('/var/qmail/bin/sendmail' . $binaryParam, null, $this->logger);
  105. $this->assertEquals($sendmail, self::invokePrivate($this->mailer, 'getSendMailInstance'));
  106. }
  107. public function testGetInstanceDefault() {
  108. $this->config
  109. ->method('getSystemValue')
  110. ->willReturnMap([
  111. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  112. ['mail_smtpport', 25, 25],
  113. ['mail_smtptimeout', 10, 10],
  114. ]);
  115. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  116. $this->assertInstanceOf(SymfonyMailer::class, $mailer);
  117. $transport = self::invokePrivate($mailer, 'transport');
  118. $this->assertInstanceOf(EsmtpTransport::class, $transport);
  119. }
  120. public function testGetInstanceSendmail() {
  121. $this->config
  122. ->method('getSystemValueString')
  123. ->willReturnMap([
  124. ['mail_smtpmode', 'smtp', 'sendmail'],
  125. ['mail_sendmailmode', 'smtp', 'smtp'],
  126. ]);
  127. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  128. $this->assertInstanceOf(SymfonyMailer::class, $mailer);
  129. $transport = self::invokePrivate($mailer, 'transport');
  130. $this->assertInstanceOf(SendmailTransport::class, $transport);
  131. }
  132. public function testEvents() {
  133. $this->config
  134. ->method('getSystemValue')
  135. ->willReturnMap([
  136. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  137. ['mail_smtpport', 25, 25],
  138. ]);
  139. $this->mailer = $this->getMockBuilder(Mailer::class)
  140. ->setMethods(['getInstance'])
  141. ->setConstructorArgs(
  142. [
  143. $this->config,
  144. $this->logger,
  145. $this->defaults,
  146. $this->urlGenerator,
  147. $this->l10n,
  148. $this->dispatcher,
  149. $this->createMock(IFactory::class)
  150. ]
  151. )
  152. ->getMock();
  153. $message = $this->createMock(Message::class);
  154. $event = new BeforeMessageSent($message);
  155. $this->dispatcher->expects($this->once())
  156. ->method('dispatchTyped')
  157. ->with($this->equalTo($event));
  158. $this->mailer->send($message);
  159. }
  160. public function testCreateMessage() {
  161. $this->config
  162. ->expects($this->any())
  163. ->method('getSystemValueBool')
  164. ->with('mail_send_plaintext_only', false)
  165. ->willReturn(false);
  166. $this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
  167. }
  168. public function testSendInvalidMailException() {
  169. $this->config
  170. ->method('getSystemValue')
  171. ->willReturnMap([
  172. ['mail_smtphost', '127.0.0.1', '127.0.0.1'],
  173. ['mail_smtpport', 25, 25],
  174. ['mail_smtptimeout', 10, 10],
  175. ]);
  176. $this->expectException(\Exception::class);
  177. /** @var Message&MockObject */
  178. $message = $this->getMockBuilder('\OC\Mail\Message')
  179. ->disableOriginalConstructor()->getMock();
  180. $message->expects($this->once())
  181. ->method('getSymfonyEmail')
  182. ->willReturn(new Email());
  183. $this->mailer->send($message);
  184. }
  185. /**
  186. * @return array
  187. */
  188. public function mailAddressProvider() {
  189. return [
  190. ['lukas@owncloud.com', true, false],
  191. ['lukas@localhost', true, false],
  192. ['lukas@192.168.1.1', true, false],
  193. ['lukas@éxämplè.com', true, false],
  194. ['asdf', false, false],
  195. ['', false, false],
  196. ['lukas@owncloud.org@owncloud.com', false, false],
  197. ['test@localhost', true, false],
  198. ['test@localhost', false, true],
  199. ];
  200. }
  201. /**
  202. * @dataProvider mailAddressProvider
  203. */
  204. public function testValidateMailAddress($email, $expected, $strict) {
  205. $this->config
  206. ->expects($this->atMost(1))
  207. ->method('getAppValue')
  208. ->with('core', 'enforce_strict_email_check')
  209. ->willReturn($strict ? 'yes' : 'no');
  210. $this->assertSame($expected, $this->mailer->validateMailAddress($email));
  211. }
  212. public function testCreateEMailTemplate() {
  213. $this->config->method('getSystemValueString')
  214. ->with('mail_template_class', '')
  215. ->willReturnArgument(1);
  216. $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
  217. }
  218. public function testStreamingOptions() {
  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() {
  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. }