MailerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Mailer;
  10. use OCP\Defaults;
  11. use OCP\IConfig;
  12. use OCP\ILogger;
  13. use Test\TestCase;
  14. class MailerTest extends TestCase {
  15. /** @var IConfig */
  16. private $config;
  17. /** @var Defaults */
  18. private $defaults;
  19. /** @var ILogger */
  20. private $logger;
  21. /** @var Mailer */
  22. private $mailer;
  23. function setUp() {
  24. parent::setUp();
  25. $this->config = $this->getMockBuilder('\OCP\IConfig')
  26. ->disableOriginalConstructor()->getMock();
  27. $this->defaults = $this->getMockBuilder('\OCP\Defaults')
  28. ->disableOriginalConstructor()->getMock();
  29. $this->logger = $this->getMockBuilder('\OCP\ILogger')
  30. ->disableOriginalConstructor()->getMock();
  31. $this->mailer = new Mailer($this->config, $this->logger, $this->defaults);
  32. }
  33. public function testGetMailInstance() {
  34. $this->assertEquals(\Swift_MailTransport::newInstance(), self::invokePrivate($this->mailer, 'getMailinstance'));
  35. }
  36. public function testGetSendMailInstanceSendMail() {
  37. $this->config
  38. ->expects($this->once())
  39. ->method('getSystemValue')
  40. ->with('mail_smtpmode', 'php')
  41. ->will($this->returnValue('sendmail'));
  42. $this->assertEquals(\Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
  43. }
  44. public function testGetSendMailInstanceSendMailQmail() {
  45. $this->config
  46. ->expects($this->once())
  47. ->method('getSystemValue')
  48. ->with('mail_smtpmode', 'php')
  49. ->will($this->returnValue('qmail'));
  50. $this->assertEquals(\Swift_SendmailTransport::newInstance('/var/qmail/bin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
  51. }
  52. public function testGetInstanceDefault() {
  53. $this->assertInstanceOf('\Swift_MailTransport', self::invokePrivate($this->mailer, 'getInstance'));
  54. }
  55. public function testGetInstancePhp() {
  56. $this->config
  57. ->expects($this->any())
  58. ->method('getSystemValue')
  59. ->will($this->returnValue('php'));
  60. $this->assertInstanceOf('\Swift_MailTransport', self::invokePrivate($this->mailer, 'getInstance'));
  61. }
  62. public function testGetInstanceSendmail() {
  63. $this->config
  64. ->expects($this->any())
  65. ->method('getSystemValue')
  66. ->will($this->returnValue('sendmail'));
  67. $this->assertInstanceOf('\Swift_Mailer', self::invokePrivate($this->mailer, 'getInstance'));
  68. }
  69. public function testCreateMessage() {
  70. $this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
  71. }
  72. /**
  73. * @expectedException \Exception
  74. */
  75. public function testSendInvalidMailException() {
  76. $message = $this->getMockBuilder('\OC\Mail\Message')
  77. ->disableOriginalConstructor()->getMock();
  78. $message->expects($this->once())
  79. ->method('getSwiftMessage')
  80. ->will($this->returnValue(new \Swift_Message()));
  81. $this->mailer->send($message);
  82. }
  83. /**
  84. * @return array
  85. */
  86. public function mailAddressProvider() {
  87. return [
  88. ['lukas@owncloud.com', true],
  89. ['lukas@localhost', true],
  90. ['lukas@192.168.1.1', true],
  91. ['lukas@éxämplè.com', true],
  92. ['asdf', false],
  93. ['lukas@owncloud.org@owncloud.com', false],
  94. ];
  95. }
  96. /**
  97. * @dataProvider mailAddressProvider
  98. */
  99. public function testValidateMailAddress($email, $expected) {
  100. $this->assertSame($expected, $this->mailer->validateMailAddress($email));
  101. }
  102. }