2014-10-03 09:14:22 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2024-06-03 04:23:34 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2014-10-03 09:14:22 -04:00
|
|
|
*/
|
2019-09-17 10:33:27 -04:00
|
|
|
namespace OCA\Settings\Tests\Controller;
|
2016-05-19 05:17:01 -04:00
|
|
|
|
2016-10-06 08:05:52 -04:00
|
|
|
use OC\Mail\Message;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OC\User\User;
|
2019-09-17 10:33:27 -04:00
|
|
|
use OCA\Settings\Controller\MailSettingsController;
|
2017-04-18 10:08:29 -04:00
|
|
|
use OCP\AppFramework\Http;
|
2016-10-06 08:05:52 -04:00
|
|
|
use OCP\IConfig;
|
|
|
|
|
use OCP\IL10N;
|
|
|
|
|
use OCP\IRequest;
|
2023-11-23 04:22:34 -05:00
|
|
|
use OCP\IURLGenerator;
|
2016-10-06 08:05:52 -04:00
|
|
|
use OCP\IUserSession;
|
2017-04-18 15:30:31 -04:00
|
|
|
use OCP\Mail\IEMailTemplate;
|
2016-10-06 08:05:52 -04:00
|
|
|
use OCP\Mail\IMailer;
|
2014-10-03 09:14:22 -04:00
|
|
|
|
|
|
|
|
/**
|
2016-05-19 05:17:01 -04:00
|
|
|
* @package Tests\Settings\Controller
|
2014-10-03 09:14:22 -04:00
|
|
|
*/
|
2014-11-10 16:59:50 -05:00
|
|
|
class MailSettingsControllerTest extends \Test\TestCase {
|
2014-10-03 09:14:22 -04:00
|
|
|
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-06 08:05:52 -04:00
|
|
|
private $config;
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-06 08:05:52 -04:00
|
|
|
private $userSession;
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var IMailer|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-06 08:05:52 -04:00
|
|
|
private $mailer;
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-06 08:05:52 -04:00
|
|
|
private $l;
|
2022-02-03 06:28:37 -05:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
|
private $urlGenerator;
|
2016-10-06 08:05:52 -04:00
|
|
|
|
|
|
|
|
/** @var MailSettingsController */
|
|
|
|
|
private $mailController;
|
2014-10-03 09:14:22 -04:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-10 16:59:50 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2016-10-06 08:05:52 -04:00
|
|
|
$this->l = $this->createMock(IL10N::class);
|
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
|
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
|
$this->mailer = $this->createMock(IMailer::class);
|
2022-02-03 06:28:37 -05:00
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var IRequest|\PHPUnit\Framework\MockObject\MockObject $request */
|
2017-04-18 10:11:29 -04:00
|
|
|
$request = $this->createMock(IRequest::class);
|
2016-10-06 08:05:52 -04:00
|
|
|
$this->mailController = new MailSettingsController(
|
|
|
|
|
'settings',
|
2017-04-18 10:11:29 -04:00
|
|
|
$request,
|
2016-10-06 08:05:52 -04:00
|
|
|
$this->l,
|
|
|
|
|
$this->config,
|
|
|
|
|
$this->userSession,
|
2022-02-03 06:28:37 -05:00
|
|
|
$this->urlGenerator,
|
2016-10-06 08:05:52 -04:00
|
|
|
$this->mailer,
|
2017-04-18 10:11:29 -04:00
|
|
|
'no-reply@nextcloud.com'
|
2016-10-06 08:05:52 -04:00
|
|
|
);
|
2014-10-03 09:14:22 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetMailSettings(): void {
|
2016-10-06 08:05:52 -04:00
|
|
|
$this->config->expects($this->exactly(2))
|
2016-10-06 08:07:32 -04:00
|
|
|
->method('setSystemValues')
|
2014-10-03 09:14:22 -04:00
|
|
|
->withConsecutive(
|
2015-01-23 05:13:47 -05:00
|
|
|
[[
|
2017-04-18 10:11:29 -04:00
|
|
|
'mail_domain' => 'nextcloud.com',
|
|
|
|
|
'mail_from_address' => 'demo@nextcloud.com',
|
2015-01-23 05:13:47 -05:00
|
|
|
'mail_smtpmode' => 'smtp',
|
|
|
|
|
'mail_smtpsecure' => 'ssl',
|
2017-04-18 10:11:29 -04:00
|
|
|
'mail_smtphost' => 'mx.nextcloud.org',
|
2015-01-23 05:13:47 -05:00
|
|
|
'mail_smtpauth' => 1,
|
|
|
|
|
'mail_smtpport' => '25',
|
2018-11-28 14:06:48 -05:00
|
|
|
'mail_sendmailmode' => null,
|
2015-01-23 05:13:47 -05:00
|
|
|
]],
|
|
|
|
|
[[
|
2017-04-18 10:11:29 -04:00
|
|
|
'mail_domain' => 'nextcloud.com',
|
|
|
|
|
'mail_from_address' => 'demo@nextcloud.com',
|
2015-01-23 05:13:47 -05:00
|
|
|
'mail_smtpmode' => 'smtp',
|
|
|
|
|
'mail_smtpsecure' => 'ssl',
|
2017-04-18 10:11:29 -04:00
|
|
|
'mail_smtphost' => 'mx.nextcloud.org',
|
2015-01-23 05:13:47 -05:00
|
|
|
'mail_smtpauth' => null,
|
|
|
|
|
'mail_smtpport' => '25',
|
|
|
|
|
'mail_smtpname' => null,
|
|
|
|
|
'mail_smtppassword' => null,
|
2018-11-28 14:06:48 -05:00
|
|
|
'mail_sendmailmode' => null,
|
2015-01-23 05:13:47 -05:00
|
|
|
]]
|
2014-10-03 09:14:22 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// With authentication
|
2016-10-06 08:05:52 -04:00
|
|
|
$response = $this->mailController->setMailSettings(
|
2017-04-18 10:11:29 -04:00
|
|
|
'nextcloud.com',
|
|
|
|
|
'demo@nextcloud.com',
|
2014-10-03 09:14:22 -04:00
|
|
|
'smtp',
|
|
|
|
|
'ssl',
|
2017-04-18 10:11:29 -04:00
|
|
|
'mx.nextcloud.org',
|
2014-10-03 09:14:22 -04:00
|
|
|
1,
|
2018-11-28 14:06:48 -05:00
|
|
|
'25',
|
|
|
|
|
null
|
2014-10-03 09:14:22 -04:00
|
|
|
);
|
2017-04-18 10:08:29 -04:00
|
|
|
$this->assertSame(Http::STATUS_OK, $response->getStatus());
|
2014-10-03 09:14:22 -04:00
|
|
|
|
|
|
|
|
// Without authentication (testing the deletion of the stored password)
|
2016-10-06 08:05:52 -04:00
|
|
|
$response = $this->mailController->setMailSettings(
|
2017-04-18 10:11:29 -04:00
|
|
|
'nextcloud.com',
|
|
|
|
|
'demo@nextcloud.com',
|
2014-10-03 09:14:22 -04:00
|
|
|
'smtp',
|
|
|
|
|
'ssl',
|
2017-04-18 10:11:29 -04:00
|
|
|
'mx.nextcloud.org',
|
2014-10-03 09:14:22 -04:00
|
|
|
0,
|
2018-11-28 14:06:48 -05:00
|
|
|
'25',
|
|
|
|
|
null
|
2014-10-03 09:14:22 -04:00
|
|
|
);
|
2017-04-18 10:08:29 -04:00
|
|
|
$this->assertSame(Http::STATUS_OK, $response->getStatus());
|
2014-10-03 09:14:22 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testStoreCredentials(): void {
|
2016-10-06 08:05:52 -04:00
|
|
|
$this->config
|
2015-01-23 05:13:47 -05:00
|
|
|
->expects($this->once())
|
|
|
|
|
->method('setSystemValues')
|
|
|
|
|
->with([
|
|
|
|
|
'mail_smtpname' => 'UsernameToStore',
|
|
|
|
|
'mail_smtppassword' => 'PasswordToStore',
|
|
|
|
|
]);
|
2014-10-03 09:14:22 -04:00
|
|
|
|
2016-10-06 08:05:52 -04:00
|
|
|
$response = $this->mailController->storeCredentials('UsernameToStore', 'PasswordToStore');
|
2017-04-18 10:08:29 -04:00
|
|
|
$this->assertSame(Http::STATUS_OK, $response->getStatus());
|
2014-10-03 09:14:22 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSendTestMail(): void {
|
2017-04-18 10:08:29 -04:00
|
|
|
$user = $this->createMock(User::class);
|
2014-10-03 09:14:22 -04:00
|
|
|
$user->expects($this->any())
|
|
|
|
|
->method('getUID')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('Werner');
|
2014-10-03 09:14:22 -04:00
|
|
|
$user->expects($this->any())
|
|
|
|
|
->method('getDisplayName')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('Werner Brösel');
|
2014-10-03 09:14:22 -04:00
|
|
|
|
2017-04-18 10:08:29 -04:00
|
|
|
$this->l->expects($this->any())
|
2014-10-03 09:14:22 -04:00
|
|
|
->method('t')
|
2020-04-09 07:53:40 -04:00
|
|
|
->willReturnCallback(function ($text, $parameters = []) {
|
2017-04-18 10:08:29 -04:00
|
|
|
return vsprintf($text, $parameters);
|
|
|
|
|
});
|
2016-10-06 08:05:52 -04:00
|
|
|
$this->userSession
|
2014-10-03 09:14:22 -04:00
|
|
|
->expects($this->any())
|
|
|
|
|
->method('getUser')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($user);
|
2014-10-03 09:14:22 -04:00
|
|
|
|
|
|
|
|
// Ensure that it fails when no mail address has been specified
|
2016-10-06 08:05:52 -04:00
|
|
|
$response = $this->mailController->sendTestMail();
|
2017-04-18 10:08:29 -04:00
|
|
|
$this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus());
|
2022-09-21 11:44:32 -04:00
|
|
|
$this->assertSame('You need to set your account email before being able to send test emails. Go to for that.', $response->getData());
|
2014-10-03 09:14:22 -04:00
|
|
|
|
|
|
|
|
// If no exception is thrown it should work
|
2016-10-06 08:05:52 -04:00
|
|
|
$this->config
|
2014-10-03 09:14:22 -04:00
|
|
|
->expects($this->any())
|
|
|
|
|
->method('getUserValue')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('mail@example.invalid');
|
2016-10-06 08:05:52 -04:00
|
|
|
$this->mailer->expects($this->once())
|
|
|
|
|
->method('createMessage')
|
|
|
|
|
->willReturn($this->createMock(Message::class));
|
2017-04-18 15:30:31 -04:00
|
|
|
$emailTemplate = $this->createMock(IEMailTemplate::class);
|
|
|
|
|
$this->mailer
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('createEMailTemplate')
|
|
|
|
|
->willReturn($emailTemplate);
|
2016-10-06 08:05:52 -04:00
|
|
|
$response = $this->mailController->sendTestMail();
|
2019-11-22 02:22:10 -05:00
|
|
|
$this->assertSame(Http::STATUS_OK, $response->getStatus());
|
2014-10-03 09:14:22 -04:00
|
|
|
}
|
2014-10-16 09:41:45 -04:00
|
|
|
}
|