MailerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. public function testGetMailInstance() {
  46. $this->assertEquals(\Swift_MailTransport::newInstance(), self::invokePrivate($this->mailer, 'getMailinstance'));
  47. }
  48. public function testGetSendMailInstanceSendMail() {
  49. $this->config
  50. ->expects($this->once())
  51. ->method('getSystemValue')
  52. ->with('mail_smtpmode', 'php')
  53. ->will($this->returnValue('sendmail'));
  54. $this->assertEquals(\Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
  55. }
  56. public function testGetSendMailInstanceSendMailQmail() {
  57. $this->config
  58. ->expects($this->once())
  59. ->method('getSystemValue')
  60. ->with('mail_smtpmode', 'php')
  61. ->will($this->returnValue('qmail'));
  62. $this->assertEquals(\Swift_SendmailTransport::newInstance('/var/qmail/bin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
  63. }
  64. public function testGetInstanceDefault() {
  65. $this->assertInstanceOf('\Swift_MailTransport', self::invokePrivate($this->mailer, 'getInstance'));
  66. }
  67. public function testGetInstancePhp() {
  68. $this->config
  69. ->expects($this->any())
  70. ->method('getSystemValue')
  71. ->will($this->returnValue('php'));
  72. $this->assertInstanceOf('\Swift_MailTransport', self::invokePrivate($this->mailer, 'getInstance'));
  73. }
  74. public function testGetInstanceSendmail() {
  75. $this->config
  76. ->expects($this->any())
  77. ->method('getSystemValue')
  78. ->will($this->returnValue('sendmail'));
  79. $this->assertInstanceOf('\Swift_Mailer', self::invokePrivate($this->mailer, 'getInstance'));
  80. }
  81. public function testCreateMessage() {
  82. $this->config
  83. ->expects($this->any())
  84. ->method('getSystemValue')
  85. ->with('mail_send_plaintext_only', false)
  86. ->will($this->returnValue(false));
  87. $this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
  88. }
  89. /**
  90. * @expectedException \Exception
  91. */
  92. public function testSendInvalidMailException() {
  93. $message = $this->getMockBuilder('\OC\Mail\Message')
  94. ->disableOriginalConstructor()->getMock();
  95. $message->expects($this->once())
  96. ->method('getSwiftMessage')
  97. ->will($this->returnValue(new \Swift_Message()));
  98. $this->mailer->send($message);
  99. }
  100. /**
  101. * @return array
  102. */
  103. public function mailAddressProvider() {
  104. return [
  105. ['lukas@owncloud.com', true],
  106. ['lukas@localhost', true],
  107. ['lukas@192.168.1.1', true],
  108. ['lukas@éxämplè.com', true],
  109. ['asdf', false],
  110. ['lukas@owncloud.org@owncloud.com', false],
  111. ];
  112. }
  113. /**
  114. * @dataProvider mailAddressProvider
  115. */
  116. public function testValidateMailAddress($email, $expected) {
  117. $this->assertSame($expected, $this->mailer->validateMailAddress($email));
  118. }
  119. public function testCreateEMailTemplate() {
  120. $this->config->method('getSystemValue')
  121. ->with('mail_template_class', '')
  122. ->willReturnArgument(1);
  123. $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest')));
  124. }
  125. }