MailerTest.php 10 KB

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