mailer.php 3.4 KB

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