MailSettingsControllerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @author Lukas Reschke
  4. * @copyright 2014 Lukas Reschke lukas@owncloud.com
  5. *
  6. * This file is licensed under the Affero General Public License version 3 or
  7. * later.
  8. * See the COPYING-README file.
  9. */
  10. namespace Tests\Settings\Controller;
  11. use OC\Mail\Message;
  12. use OC\Settings\Controller\MailSettingsController;
  13. use OCP\IConfig;
  14. use OCP\IL10N;
  15. use OCP\IRequest;
  16. use OCP\IUserSession;
  17. use OCP\Mail\IMailer;
  18. /**
  19. * @package Tests\Settings\Controller
  20. */
  21. class MailSettingsControllerTest extends \Test\TestCase {
  22. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  23. private $config;
  24. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  25. private $userSession;
  26. /** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */
  27. private $mailer;
  28. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  29. private $l;
  30. /** @var MailSettingsController */
  31. private $mailController;
  32. protected function setUp() {
  33. parent::setUp();
  34. $this->l = $this->createMock(IL10N::class);
  35. $this->config = $this->createMock(IConfig::class);
  36. $this->userSession = $this->createMock(IUserSession::class);
  37. $this->mailer = $this->createMock(IMailer::class);
  38. // $this->mailer = $this->getMockBuilder(IMailer::class)
  39. // ->setMethods(['send'])
  40. // ->getMock();
  41. $this->mailController = new MailSettingsController(
  42. 'settings',
  43. $this->createMock(IRequest::class),
  44. $this->l,
  45. $this->config,
  46. $this->userSession,
  47. $this->mailer,
  48. 'no-reply@owncloud.com'
  49. );
  50. }
  51. public function testSetMailSettings() {
  52. $this->l
  53. ->expects($this->exactly(2))
  54. ->method('t')
  55. ->will($this->returnValue('Saved'));
  56. $this->config->expects($this->exactly(2))
  57. ->method('setSystemValues')
  58. ->withConsecutive(
  59. [[
  60. 'mail_domain' => 'owncloud.com',
  61. 'mail_from_address' => 'demo@owncloud.com',
  62. 'mail_smtpmode' => 'smtp',
  63. 'mail_smtpsecure' => 'ssl',
  64. 'mail_smtphost' => 'mx.owncloud.org',
  65. 'mail_smtpauthtype' => 'NTLM',
  66. 'mail_smtpauth' => 1,
  67. 'mail_smtpport' => '25',
  68. ]],
  69. [[
  70. 'mail_domain' => 'owncloud.com',
  71. 'mail_from_address' => 'demo@owncloud.com',
  72. 'mail_smtpmode' => 'smtp',
  73. 'mail_smtpsecure' => 'ssl',
  74. 'mail_smtphost' => 'mx.owncloud.org',
  75. 'mail_smtpauthtype' => 'NTLM',
  76. 'mail_smtpauth' => null,
  77. 'mail_smtpport' => '25',
  78. 'mail_smtpname' => null,
  79. 'mail_smtppassword' => null,
  80. ]]
  81. );
  82. // With authentication
  83. $response = $this->mailController->setMailSettings(
  84. 'owncloud.com',
  85. 'demo@owncloud.com',
  86. 'smtp',
  87. 'ssl',
  88. 'mx.owncloud.org',
  89. 'NTLM',
  90. 1,
  91. '25'
  92. );
  93. $expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success');
  94. $this->assertSame($expectedResponse, $response);
  95. // Without authentication (testing the deletion of the stored password)
  96. $response = $this->mailController->setMailSettings(
  97. 'owncloud.com',
  98. 'demo@owncloud.com',
  99. 'smtp',
  100. 'ssl',
  101. 'mx.owncloud.org',
  102. 'NTLM',
  103. 0,
  104. '25'
  105. );
  106. $expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success');
  107. $this->assertSame($expectedResponse, $response);
  108. }
  109. public function testStoreCredentials() {
  110. $this->l
  111. ->expects($this->once())
  112. ->method('t')
  113. ->will($this->returnValue('Saved'));
  114. $this->config
  115. ->expects($this->once())
  116. ->method('setSystemValues')
  117. ->with([
  118. 'mail_smtpname' => 'UsernameToStore',
  119. 'mail_smtppassword' => 'PasswordToStore',
  120. ]);
  121. $response = $this->mailController->storeCredentials('UsernameToStore', 'PasswordToStore');
  122. $expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success');
  123. $this->assertSame($expectedResponse, $response);
  124. }
  125. public function testSendTestMail() {
  126. $user = $this->getMockBuilder('\OC\User\User')
  127. ->disableOriginalConstructor()
  128. ->getMock();
  129. $user->expects($this->any())
  130. ->method('getUID')
  131. ->will($this->returnValue('Werner'));
  132. $user->expects($this->any())
  133. ->method('getDisplayName')
  134. ->will($this->returnValue('Werner Brösel'));
  135. $this->l
  136. ->expects($this->any())
  137. ->method('t')
  138. ->will(
  139. $this->returnValueMap(
  140. array(
  141. array('You need to set your user email before being able to send test emails.', array(),
  142. 'You need to set your user email before being able to send test emails.'),
  143. array('A problem occurred while sending the e-mail. Please revisit your settings.', array(),
  144. 'A problem occurred while sending the e-mail. Please revisit your settings.'),
  145. array('Email sent', array(), 'Email sent'),
  146. array('test email settings', array(), 'test email settings'),
  147. array('If you received this email, the settings seem to be correct.', array(),
  148. 'If you received this email, the settings seem to be correct.')
  149. )
  150. ));
  151. $this->userSession
  152. ->expects($this->any())
  153. ->method('getUser')
  154. ->will($this->returnValue($user));
  155. // Ensure that it fails when no mail address has been specified
  156. $response = $this->mailController->sendTestMail();
  157. $expectedResponse = array('data' => array('message' =>'You need to set your user email before being able to send test emails.'), 'status' => 'error');
  158. $this->assertSame($expectedResponse, $response);
  159. // If no exception is thrown it should work
  160. $this->config
  161. ->expects($this->any())
  162. ->method('getUserValue')
  163. ->will($this->returnValue('mail@example.invalid'));
  164. $this->mailer->expects($this->once())
  165. ->method('createMessage')
  166. ->willReturn($this->createMock(Message::class));
  167. $response = $this->mailController->sendTestMail();
  168. $expectedResponse = array('data' => array('message' =>'Email sent'), 'status' => 'success');
  169. $this->assertSame($expectedResponse, $response);
  170. }
  171. }