1
0

MailerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Copyright (c) 2014-2015 Lukas Reschke <lukas@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Mail;
  9. use OC\Mail\EMailTemplate;
  10. use OC\Mail\Mailer;
  11. use OCP\Defaults;
  12. use OCP\IConfig;
  13. use OCP\IL10N;
  14. use OCP\ILogger;
  15. use OCP\IURLGenerator;
  16. use Test\TestCase;
  17. class MailerTest extends TestCase {
  18. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  19. private $config;
  20. /** @var Defaults|\PHPUnit_Framework_MockObject_MockObject */
  21. private $defaults;
  22. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  23. private $logger;
  24. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  25. private $urlGenerator;
  26. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  27. private $l10n;
  28. /** @var Mailer */
  29. private $mailer;
  30. public function setUp() {
  31. parent::setUp();
  32. $this->config = $this->createMock(IConfig::class);
  33. $this->defaults = $this->createMock(Defaults::class);
  34. $this->logger = $this->createMock(ILogger::class);
  35. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  36. $this->l10n = $this->createMock(IL10N::class);
  37. $this->mailer = new Mailer(
  38. $this->config,
  39. $this->logger,
  40. $this->defaults,
  41. $this->urlGenerator,
  42. $this->l10n
  43. );
  44. }
  45. /**
  46. * @return array
  47. */
  48. public function sendmailModeProvider(): array {
  49. return [
  50. 'smtp' => ['smtp', ' -bs'],
  51. 'pipe' => ['pipe', ' -t'],
  52. ];
  53. }
  54. /**
  55. * @dataProvider sendmailModeProvider
  56. * @param $sendmailMode
  57. * @param $binaryParam
  58. */
  59. public function testGetSendmailInstanceSendMail($sendmailMode, $binaryParam) {
  60. $this->config
  61. ->expects($this->exactly(2))
  62. ->method('getSystemValue')
  63. ->will($this->returnValueMap([
  64. ['mail_smtpmode', 'smtp', 'sendmail'],
  65. ['mail_sendmailmode', 'smtp', $sendmailMode],
  66. ]));
  67. $path = \OC_Helper::findBinaryPath('sendmail');
  68. if ($path === null) {
  69. $path = '/usr/sbin/sendmail';
  70. }
  71. $expected = new \Swift_SendmailTransport($path . $binaryParam);
  72. $this->assertEquals($expected, self::invokePrivate($this->mailer, 'getSendMailInstance'));
  73. }
  74. /**
  75. * @dataProvider sendmailModeProvider
  76. * @param $sendmailMode
  77. * @param $binaryParam
  78. */
  79. public function testGetSendmailInstanceSendMailQmail($sendmailMode, $binaryParam) {
  80. $this->config
  81. ->expects($this->exactly(2))
  82. ->method('getSystemValue')
  83. ->will($this->returnValueMap([
  84. ['mail_smtpmode', 'smtp', 'qmail'],
  85. ['mail_sendmailmode', 'smtp', $sendmailMode],
  86. ]));
  87. $this->assertEquals(new \Swift_SendmailTransport('/var/qmail/bin/sendmail' . $binaryParam), self::invokePrivate($this->mailer, 'getSendMailInstance'));
  88. }
  89. public function testGetInstanceDefault() {
  90. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  91. $this->assertInstanceOf(\Swift_Mailer::class, $mailer);
  92. $this->assertInstanceOf(\Swift_SmtpTransport::class, $mailer->getTransport());
  93. }
  94. public function testGetInstanceSendmail() {
  95. $this->config
  96. ->method('getSystemValue')
  97. ->will($this->returnValueMap([
  98. ['mail_smtpmode', 'smtp', 'sendmail'],
  99. ['mail_sendmailmode', 'smtp', 'smtp'],
  100. ]));
  101. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  102. $this->assertInstanceOf(\Swift_Mailer::class, $mailer);
  103. $this->assertInstanceOf(\Swift_SendmailTransport::class, $mailer->getTransport());
  104. }
  105. public function testCreateMessage() {
  106. $this->config
  107. ->expects($this->any())
  108. ->method('getSystemValue')
  109. ->with('mail_send_plaintext_only', false)
  110. ->will($this->returnValue(false));
  111. $this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
  112. }
  113. /**
  114. * @expectedException \Exception
  115. */
  116. public function testSendInvalidMailException() {
  117. $message = $this->getMockBuilder('\OC\Mail\Message')
  118. ->disableOriginalConstructor()->getMock();
  119. $message->expects($this->once())
  120. ->method('getSwiftMessage')
  121. ->will($this->returnValue(new \Swift_Message()));
  122. $this->mailer->send($message);
  123. }
  124. /**
  125. * @return array
  126. */
  127. public function mailAddressProvider() {
  128. return [
  129. ['lukas@owncloud.com', true],
  130. ['lukas@localhost', true],
  131. ['lukas@192.168.1.1', true],
  132. ['lukas@éxämplè.com', true],
  133. ['asdf', false],
  134. ['lukas@owncloud.org@owncloud.com', false],
  135. ];
  136. }
  137. /**
  138. * @dataProvider mailAddressProvider
  139. */
  140. public function testValidateMailAddress($email, $expected) {
  141. $this->assertSame($expected, $this->mailer->validateMailAddress($email));
  142. }
  143. public function testCreateEMailTemplate() {
  144. $this->config->method('getSystemValue')
  145. ->with('mail_template_class', '')
  146. ->willReturnArgument(1);
  147. $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
  148. }
  149. public function testStreamingOptions() {
  150. $this->config->method('getSystemValue')
  151. ->will($this->returnValueMap([
  152. ['mail_smtpmode', 'smtp', 'smtp'],
  153. ['mail_smtpstreamoptions', [], ['foo' => 1]]
  154. ]));
  155. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  156. $this->assertEquals(1, count($mailer->getTransport()->getStreamOptions()));
  157. $this->assertTrue(isset($mailer->getTransport()->getStreamOptions()['foo']));
  158. }
  159. public function testStreamingOptionsWrongType() {
  160. $this->config->method('getSystemValue')
  161. ->will($this->returnValueMap([
  162. ['mail_smtpmode', 'smtp', 'smtp'],
  163. ['mail_smtpstreamoptions', [], 'bar']
  164. ]));
  165. $mailer = self::invokePrivate($this->mailer, 'getInstance');
  166. $this->assertEquals(0, count($mailer->getTransport()->getStreamOptions()));
  167. }
  168. }