2016-01-25 05:39:57 -05:00
|
|
|
<?php
|
2024-05-28 10:42:42 -04:00
|
|
|
|
2025-05-28 18:25:25 -04:00
|
|
|
declare(strict_types=1);
|
2016-03-01 11:25:15 -05:00
|
|
|
/**
|
2024-05-28 10:42:42 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2016-03-01 11:25:15 -05:00
|
|
|
*/
|
2016-05-09 09:37:51 -04:00
|
|
|
namespace OCA\Federation\Tests;
|
2016-01-25 05:39:57 -05:00
|
|
|
|
2017-03-09 05:36:54 -05:00
|
|
|
use OC\OCS\DiscoveryService;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCA\DAV\CardDAV\SyncService;
|
2016-01-25 05:39:57 -05:00
|
|
|
use OCA\Federation\DbHandler;
|
|
|
|
|
use OCA\Federation\SyncFederationAddressBooks;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCA\Federation\TrustedServers;
|
2022-07-04 09:43:04 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2016-01-25 05:39:57 -05:00
|
|
|
|
|
|
|
|
class SyncFederationAddressbooksTest extends \Test\TestCase {
|
2025-05-28 18:25:25 -04:00
|
|
|
private array $callBacks = [];
|
|
|
|
|
private DiscoveryService&MockObject $discoveryService;
|
|
|
|
|
private LoggerInterface&MockObject $logger;
|
2022-07-04 09:43:04 -04:00
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2017-03-09 05:36:54 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2025-05-28 18:25:25 -04:00
|
|
|
$this->discoveryService = $this->createMock(DiscoveryService::class);
|
2017-03-09 05:36:54 -05:00
|
|
|
$this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
|
2022-07-04 09:43:04 -04:00
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
2017-03-09 05:36:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSync(): void {
|
2025-05-28 18:25:25 -04:00
|
|
|
/** @var DbHandler&MockObject $dbHandler */
|
|
|
|
|
$dbHandler = $this->createMock(DbHandler::class);
|
2022-06-28 05:26:30 -04:00
|
|
|
$dbHandler->method('getAllServer')
|
|
|
|
|
->willReturn([
|
2020-04-09 03:22:29 -04:00
|
|
|
[
|
|
|
|
|
'url' => 'https://cloud.example.org',
|
|
|
|
|
'url_hash' => 'sha1',
|
|
|
|
|
'shared_secret' => 'ilovenextcloud',
|
|
|
|
|
'sync_token' => '0'
|
|
|
|
|
]
|
|
|
|
|
]);
|
2016-01-25 05:39:57 -05:00
|
|
|
$dbHandler->expects($this->once())->method('setServerStatus')
|
|
|
|
|
->with('https://cloud.example.org', 1, '1');
|
2025-05-28 18:25:25 -04:00
|
|
|
$syncService = $this->createMock(SyncService::class);
|
2016-01-25 05:39:57 -05:00
|
|
|
$syncService->expects($this->once())->method('syncRemoteAddressBook')
|
2025-06-17 13:32:00 -04:00
|
|
|
->willReturn(['1', false]);
|
2016-01-25 05:39:57 -05:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
/** @var SyncService $syncService */
|
2022-07-04 09:43:04 -04:00
|
|
|
$s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
|
2024-09-20 11:38:36 -04:00
|
|
|
$s->syncThemAll(function ($url, $ex): void {
|
2016-01-25 05:39:57 -05:00
|
|
|
$this->callBacks[] = [$url, $ex];
|
|
|
|
|
});
|
2025-05-28 18:25:25 -04:00
|
|
|
$this->assertCount(1, $this->callBacks);
|
2016-01-25 05:39:57 -05:00
|
|
|
}
|
|
|
|
|
|
2017-03-09 05:36:54 -05:00
|
|
|
public function testException(): void {
|
2025-05-28 18:25:25 -04:00
|
|
|
/** @var DbHandler&MockObject $dbHandler */
|
|
|
|
|
$dbHandler = $this->createMock(DbHandler::class);
|
2016-01-25 05:39:57 -05:00
|
|
|
$dbHandler->method('getAllServer')
|
|
|
|
|
->willReturn([
|
|
|
|
|
[
|
|
|
|
|
'url' => 'https://cloud.example.org',
|
2016-02-26 11:02:13 -05:00
|
|
|
'url_hash' => 'sha1',
|
2016-01-25 05:39:57 -05:00
|
|
|
'shared_secret' => 'ilovenextcloud',
|
|
|
|
|
'sync_token' => '0'
|
|
|
|
|
]
|
|
|
|
|
]);
|
2025-05-28 18:25:25 -04:00
|
|
|
$syncService = $this->createMock(SyncService::class);
|
2016-01-25 05:39:57 -05:00
|
|
|
$syncService->expects($this->once())->method('syncRemoteAddressBook')
|
|
|
|
|
->willThrowException(new \Exception('something did not work out'));
|
|
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
/** @var SyncService $syncService */
|
2022-07-04 09:43:04 -04:00
|
|
|
$s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
|
2024-09-20 11:38:36 -04:00
|
|
|
$s->syncThemAll(function ($url, $ex): void {
|
2016-01-25 05:39:57 -05:00
|
|
|
$this->callBacks[] = [$url, $ex];
|
|
|
|
|
});
|
2025-05-28 18:25:25 -04:00
|
|
|
$this->assertCount(2, $this->callBacks);
|
2016-01-25 05:39:57 -05:00
|
|
|
}
|
2024-08-22 04:22:30 -04:00
|
|
|
|
|
|
|
|
public function testSuccessfulSyncWithoutChangesAfterFailure(): void {
|
2025-05-28 18:25:25 -04:00
|
|
|
/** @var DbHandler&MockObject $dbHandler */
|
|
|
|
|
$dbHandler = $this->createMock(DbHandler::class);
|
2024-08-22 04:22:30 -04:00
|
|
|
$dbHandler->method('getAllServer')
|
|
|
|
|
->willReturn([
|
|
|
|
|
[
|
|
|
|
|
'url' => 'https://cloud.example.org',
|
|
|
|
|
'url_hash' => 'sha1',
|
|
|
|
|
'shared_secret' => 'ilovenextcloud',
|
|
|
|
|
'sync_token' => '0'
|
|
|
|
|
]
|
|
|
|
|
]);
|
2024-10-10 06:40:31 -04:00
|
|
|
$dbHandler->method('getServerStatus')->willReturn(TrustedServers::STATUS_FAILURE);
|
2024-08-22 04:22:30 -04:00
|
|
|
$dbHandler->expects($this->once())->method('setServerStatus')
|
|
|
|
|
->with('https://cloud.example.org', 1);
|
2025-05-28 18:25:25 -04:00
|
|
|
$syncService = $this->createMock(SyncService::class);
|
2024-08-22 04:22:30 -04:00
|
|
|
$syncService->expects($this->once())->method('syncRemoteAddressBook')
|
2025-06-17 13:32:00 -04:00
|
|
|
->willReturn(['0', false]);
|
2024-08-22 04:22:30 -04:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
/** @var SyncService $syncService */
|
2024-08-22 04:22:30 -04:00
|
|
|
$s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
|
2024-09-20 11:38:36 -04:00
|
|
|
$s->syncThemAll(function ($url, $ex): void {
|
2024-08-22 04:22:30 -04:00
|
|
|
$this->callBacks[] = [$url, $ex];
|
|
|
|
|
});
|
2025-05-28 18:25:25 -04:00
|
|
|
$this->assertCount(1, $this->callBacks);
|
2024-08-22 04:22:30 -04:00
|
|
|
}
|
2016-01-26 11:27:58 -05:00
|
|
|
}
|