MailerTest.php 9.5 KB

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