MailSettingsControllerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Settings\Tests\Controller;
  7. use OC\Mail\Message;
  8. use OC\User\User;
  9. use OCA\Settings\Controller\MailSettingsController;
  10. use OCP\AppFramework\Http;
  11. use OCP\IConfig;
  12. use OCP\IL10N;
  13. use OCP\IRequest;
  14. use OCP\IURLGenerator;
  15. use OCP\IUserSession;
  16. use OCP\Mail\IEMailTemplate;
  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 IURLGenerator */
  31. private $urlGenerator;
  32. /** @var MailSettingsController */
  33. private $mailController;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->l = $this->createMock(IL10N::class);
  37. $this->config = $this->createMock(IConfig::class);
  38. $this->userSession = $this->createMock(IUserSession::class);
  39. $this->mailer = $this->createMock(IMailer::class);
  40. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  41. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject $request */
  42. $request = $this->createMock(IRequest::class);
  43. $this->mailController = new MailSettingsController(
  44. 'settings',
  45. $request,
  46. $this->l,
  47. $this->config,
  48. $this->userSession,
  49. $this->urlGenerator,
  50. $this->mailer,
  51. 'no-reply@nextcloud.com'
  52. );
  53. }
  54. public function testSetMailSettings(): void {
  55. $this->config->expects($this->exactly(2))
  56. ->method('setSystemValues')
  57. ->withConsecutive(
  58. [[
  59. 'mail_domain' => 'nextcloud.com',
  60. 'mail_from_address' => 'demo@nextcloud.com',
  61. 'mail_smtpmode' => 'smtp',
  62. 'mail_smtpsecure' => 'ssl',
  63. 'mail_smtphost' => 'mx.nextcloud.org',
  64. 'mail_smtpauth' => 1,
  65. 'mail_smtpport' => '25',
  66. 'mail_sendmailmode' => null,
  67. ]],
  68. [[
  69. 'mail_domain' => 'nextcloud.com',
  70. 'mail_from_address' => 'demo@nextcloud.com',
  71. 'mail_smtpmode' => 'smtp',
  72. 'mail_smtpsecure' => 'ssl',
  73. 'mail_smtphost' => 'mx.nextcloud.org',
  74. 'mail_smtpauth' => null,
  75. 'mail_smtpport' => '25',
  76. 'mail_smtpname' => null,
  77. 'mail_smtppassword' => null,
  78. 'mail_sendmailmode' => null,
  79. ]]
  80. );
  81. // With authentication
  82. $response = $this->mailController->setMailSettings(
  83. 'nextcloud.com',
  84. 'demo@nextcloud.com',
  85. 'smtp',
  86. 'ssl',
  87. 'mx.nextcloud.org',
  88. 1,
  89. '25',
  90. null
  91. );
  92. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  93. // Without authentication (testing the deletion of the stored password)
  94. $response = $this->mailController->setMailSettings(
  95. 'nextcloud.com',
  96. 'demo@nextcloud.com',
  97. 'smtp',
  98. 'ssl',
  99. 'mx.nextcloud.org',
  100. 0,
  101. '25',
  102. null
  103. );
  104. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  105. }
  106. public function testStoreCredentials(): void {
  107. $this->config
  108. ->expects($this->once())
  109. ->method('setSystemValues')
  110. ->with([
  111. 'mail_smtpname' => 'UsernameToStore',
  112. 'mail_smtppassword' => 'PasswordToStore',
  113. ]);
  114. $response = $this->mailController->storeCredentials('UsernameToStore', 'PasswordToStore');
  115. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  116. }
  117. public function testSendTestMail(): void {
  118. $user = $this->createMock(User::class);
  119. $user->expects($this->any())
  120. ->method('getUID')
  121. ->willReturn('Werner');
  122. $user->expects($this->any())
  123. ->method('getDisplayName')
  124. ->willReturn('Werner Brösel');
  125. $this->l->expects($this->any())
  126. ->method('t')
  127. ->willReturnCallback(function ($text, $parameters = []) {
  128. return vsprintf($text, $parameters);
  129. });
  130. $this->userSession
  131. ->expects($this->any())
  132. ->method('getUser')
  133. ->willReturn($user);
  134. // Ensure that it fails when no mail address has been specified
  135. $response = $this->mailController->sendTestMail();
  136. $this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus());
  137. $this->assertSame('You need to set your account email before being able to send test emails. Go to for that.', $response->getData());
  138. // If no exception is thrown it should work
  139. $this->config
  140. ->expects($this->any())
  141. ->method('getUserValue')
  142. ->willReturn('mail@example.invalid');
  143. $this->mailer->expects($this->once())
  144. ->method('createMessage')
  145. ->willReturn($this->createMock(Message::class));
  146. $emailTemplate = $this->createMock(IEMailTemplate::class);
  147. $this->mailer
  148. ->expects($this->once())
  149. ->method('createEMailTemplate')
  150. ->willReturn($emailTemplate);
  151. $response = $this->mailController->sendTestMail();
  152. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  153. }
  154. }