mailsettingscontrollertest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 OC\Settings\Controller;
  11. use \OC\Settings\Application;
  12. /**
  13. * @package OC\Settings\Controller
  14. */
  15. class MailSettingsControllerTest extends \Test\TestCase {
  16. private $container;
  17. protected function setUp() {
  18. parent::setUp();
  19. $app = new Application();
  20. $this->container = $app->getContainer();
  21. $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig')
  22. ->disableOriginalConstructor()->getMock();
  23. $this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N')
  24. ->disableOriginalConstructor()->getMock();
  25. $this->container['AppName'] = 'settings';
  26. $this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session')
  27. ->disableOriginalConstructor()->getMock();
  28. $this->container['MailMessage'] = $this->getMockBuilder('\OCP\Mail\IMessage')
  29. ->disableOriginalConstructor()->getMock();
  30. $this->container['Mailer'] = $this->getMockBuilder('\OC\Mail\Mailer')
  31. ->setMethods(['send'])
  32. ->disableOriginalConstructor()->getMock();
  33. $this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults')
  34. ->disableOriginalConstructor()->getMock();
  35. $this->container['DefaultMailAddress'] = 'no-reply@owncloud.com';
  36. }
  37. public function testSetMailSettings() {
  38. $this->container['L10N']
  39. ->expects($this->exactly(2))
  40. ->method('t')
  41. ->will($this->returnValue('Saved'));
  42. /**
  43. * FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1
  44. */
  45. /*
  46. $this->container['Config']
  47. ->expects($this->exactly(15))
  48. ->method('setSystemValue')
  49. ->withConsecutive(
  50. array($this->equalTo('mail_domain'), $this->equalTo('owncloud.com')),
  51. array($this->equalTo('mail_from_address'), $this->equalTo('demo@owncloud.com')),
  52. array($this->equalTo('mail_smtpmode'), $this->equalTo('smtp')),
  53. array($this->equalTo('mail_smtpsecure'), $this->equalTo('ssl')),
  54. array($this->equalTo('mail_smtphost'), $this->equalTo('mx.owncloud.org')),
  55. array($this->equalTo('mail_smtpauthtype'), $this->equalTo('NTLM')),
  56. array($this->equalTo('mail_smtpauth'), $this->equalTo(1)),
  57. array($this->equalTo('mail_smtpport'), $this->equalTo('25')),
  58. array($this->equalTo('mail_domain'), $this->equalTo('owncloud.com')),
  59. array($this->equalTo('mail_from_address'), $this->equalTo('demo@owncloud.com')),
  60. array($this->equalTo('mail_smtpmode'), $this->equalTo('smtp')),
  61. array($this->equalTo('mail_smtpsecure'), $this->equalTo('ssl')),
  62. array($this->equalTo('mail_smtphost'), $this->equalTo('mx.owncloud.org')),
  63. array($this->equalTo('mail_smtpauthtype'), $this->equalTo('NTLM')),
  64. array($this->equalTo('mail_smtpport'), $this->equalTo('25'))
  65. );
  66. */
  67. /** @var \PHPUnit_Framework_MockObject_MockObject $config */
  68. $config = $this->container['Config'];
  69. $config->expects($this->exactly(2))
  70. ->method('setSystemValues');
  71. /**
  72. * FIXME: Use the following block once Jenkins uses PHPUnit >= 4.1
  73. ->withConsecutive(
  74. [[
  75. 'mail_domain' => 'owncloud.com',
  76. 'mail_from_address' => 'demo@owncloud.com',
  77. 'mail_smtpmode' => 'smtp',
  78. 'mail_smtpsecure' => 'ssl',
  79. 'mail_smtphost' => 'mx.owncloud.org',
  80. 'mail_smtpauthtype' => 'NTLM',
  81. 'mail_smtpauth' => 1,
  82. 'mail_smtpport' => '25',
  83. ]],
  84. [[
  85. 'mail_domain' => 'owncloud.com',
  86. 'mail_from_address' => 'demo@owncloud.com',
  87. 'mail_smtpmode' => 'smtp',
  88. 'mail_smtpsecure' => 'ssl',
  89. 'mail_smtphost' => 'mx.owncloud.org',
  90. 'mail_smtpauthtype' => 'NTLM',
  91. 'mail_smtpauth' => null,
  92. 'mail_smtpport' => '25',
  93. 'mail_smtpname' => null,
  94. 'mail_smtppassword' => null,
  95. ]]
  96. );
  97. */
  98. // With authentication
  99. $response = $this->container['MailSettingsController']->setMailSettings(
  100. 'owncloud.com',
  101. 'demo@owncloud.com',
  102. 'smtp',
  103. 'ssl',
  104. 'mx.owncloud.org',
  105. 'NTLM',
  106. 1,
  107. '25'
  108. );
  109. $expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success');
  110. $this->assertSame($expectedResponse, $response);
  111. // Without authentication (testing the deletion of the stored password)
  112. $response = $this->container['MailSettingsController']->setMailSettings(
  113. 'owncloud.com',
  114. 'demo@owncloud.com',
  115. 'smtp',
  116. 'ssl',
  117. 'mx.owncloud.org',
  118. 'NTLM',
  119. 0,
  120. '25'
  121. );
  122. $expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success');
  123. $this->assertSame($expectedResponse, $response);
  124. }
  125. public function testStoreCredentials() {
  126. $this->container['L10N']
  127. ->expects($this->once())
  128. ->method('t')
  129. ->will($this->returnValue('Saved'));
  130. $this->container['Config']
  131. ->expects($this->once())
  132. ->method('setSystemValues')
  133. ->with([
  134. 'mail_smtpname' => 'UsernameToStore',
  135. 'mail_smtppassword' => 'PasswordToStore',
  136. ]);
  137. $response = $this->container['MailSettingsController']->storeCredentials('UsernameToStore', 'PasswordToStore');
  138. $expectedResponse = array('data' => array('message' =>'Saved'), 'status' => 'success');
  139. $this->assertSame($expectedResponse, $response);
  140. }
  141. public function testSendTestMail() {
  142. $user = $this->getMockBuilder('\OC\User\User')
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $user->expects($this->any())
  146. ->method('getUID')
  147. ->will($this->returnValue('Werner'));
  148. $user->expects($this->any())
  149. ->method('getDisplayName')
  150. ->will($this->returnValue('Werner Brösel'));
  151. $this->container['L10N']
  152. ->expects($this->any())
  153. ->method('t')
  154. ->will(
  155. $this->returnValueMap(
  156. array(
  157. array('You need to set your user email before being able to send test emails.', array(),
  158. 'You need to set your user email before being able to send test emails.'),
  159. array('A problem occurred while sending the e-mail. Please revisit your settings.', array(),
  160. 'A problem occurred while sending the e-mail. Please revisit your settings.'),
  161. array('Email sent', array(), 'Email sent'),
  162. array('test email settings', array(), 'test email settings'),
  163. array('If you received this email, the settings seem to be correct.', array(),
  164. 'If you received this email, the settings seem to be correct.')
  165. )
  166. ));
  167. $this->container['UserSession']
  168. ->expects($this->any())
  169. ->method('getUser')
  170. ->will($this->returnValue($user));
  171. // Ensure that it fails when no mail address has been specified
  172. $response = $this->container['MailSettingsController']->sendTestMail();
  173. $expectedResponse = array('data' => array('message' =>'You need to set your user email before being able to send test emails.'), 'status' => 'error');
  174. $this->assertSame($expectedResponse, $response);
  175. // If no exception is thrown it should work
  176. $this->container['Config']
  177. ->expects($this->any())
  178. ->method('getUserValue')
  179. ->will($this->returnValue('mail@example.invalid'));
  180. $response = $this->container['MailSettingsController']->sendTestMail();
  181. $expectedResponse = array('data' => array('message' =>'Email sent'), 'status' => 'success');
  182. $this->assertSame($expectedResponse, $response);
  183. }
  184. }