Merge pull request #8962 from nextcloud/chore/drop-use-of-deprecated-getImapConnection

This commit is contained in:
Daniel 2023-10-17 10:25:33 +02:00 коммит произвёл GitHub
Родитель 33a136d251 9562dec880
Коммит e2b2daf0a8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 50 добавлений и 4 удалений

Просмотреть файл

@ -33,6 +33,7 @@ use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\BackgroundJob\IJobList;
use function array_map;
@ -54,12 +55,17 @@ class AccountService {
/** @var IJobList */
private $jobList;
/** @var IMAPClientFactory*/
private $imapClientFactory;
public function __construct(MailAccountMapper $mapper,
AliasesService $aliasesService,
IJobList $jobList) {
IJobList $jobList,
IMAPClientFactory $imapClientFactory) {
$this->mapper = $mapper;
$this->aliasesService = $aliasesService;
$this->jobList = $jobList;
$this->imapClientFactory = $imapClientFactory;
}
/**
@ -192,9 +198,10 @@ class AccountService {
public function testAccountConnection(string $currentUserId, int $accountId) :bool {
$account = $this->find($currentUserId, $accountId);
try {
$account->getImapConnection();
$client = $this->imapClientFactory->getClient($account);
$client->close();
return true;
} catch (ServiceException $e) {
} catch (\Throwable $e) {
return false;
}
}

Просмотреть файл

@ -22,9 +22,12 @@
namespace OCA\Mail\Tests\Unit\Service;
use ChristophWurst\Nextcloud\Testing\TestCase;
use Horde_Imap_Client_Socket;
use OCA\Mail\Account;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\AliasesService;
use OCP\BackgroundJob\IJobList;
@ -56,6 +59,12 @@ class AccountServiceTest extends TestCase {
/** @var IJobList|MockObject */
private $jobList;
/** @var IMAPClientFactory|MockObject */
private $imapClientFactory;
/** @var Horde_Imap_Client_Socket|MockObject */
private $client;
protected function setUp(): void {
parent::setUp();
@ -63,14 +72,17 @@ class AccountServiceTest extends TestCase {
$this->l10n = $this->createMock(IL10N::class);
$this->aliasesService = $this->createMock(AliasesService::class);
$this->jobList = $this->createMock(IJobList::class);
$this->imapClientFactory = $this->createMock(IMAPClientFactory::class);
$this->accountService = new AccountService(
$this->mapper,
$this->aliasesService,
$this->jobList
$this->jobList,
$this->imapClientFactory
);
$this->account1 = $this->createMock(MailAccount::class);
$this->account2 = $this->createMock(MailAccount::class);
$this->client = $this->createMock(Horde_Imap_Client_Socket::class);
}
public function testFindByUserId() {
@ -179,4 +191,31 @@ class AccountServiceTest extends TestCase {
$this->accountService->updateSignature($id, $uid, $signature);
}
public function testAccountsFailedConnection() {
$accountId = 1;
$this->imapClientFactory->expects($this->once())
->method('getClient')
->willThrowException(new ClientException());
$this->mapper->expects($this->once())
->method('find')
->with($this->user, $accountId)
->willReturn($this->account1);
$connected = $this->accountService->testAccountConnection($this->user, $accountId);
$this->assertFalse($connected);
}
public function testAccountsSuccesfulConnection() {
$accountId = 1;
$this->imapClientFactory->expects($this->once())
->method('getClient')
->willReturn($this->client);
$this->client->expects($this->once())
->method('close')
->willReturn(null);
$this->mapper->expects($this->once())
->method('find')
->with($this->user, $accountId)
->willReturn($this->account1);
$connected = $this->accountService->testAccountConnection($this->user, $accountId);
$this->assertTrue($connected);
}
}