MailerTest.php 9.4 KB

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