Skip to content

Commit

Permalink
Adjust code and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jvillafanez committed Oct 19, 2023
1 parent d7ee212 commit efb31c9
Show file tree
Hide file tree
Showing 5 changed files with 1,309 additions and 6 deletions.
20 changes: 19 additions & 1 deletion lib/private/Sync/User/UserSyncDBBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ public function __construct(Database $dbUserBackend) {
$this->dbUserBackend = $dbUserBackend;
}

/**
* This is intended to be used just for unit tests
*/
public function getPointer(): int {
return $this->pointer;
}

/**
* This is intended to be used just for unit tests
*/
public function getCachedUserData(): array {
return $this->cachedUserData;
}

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -106,7 +120,11 @@ public function getSyncingUser(string $id): ?SyncingUser {
* @inheritDoc
*/
public function userCount(): ?int {
return $this->dbUserBackend->countUsers();
$nUsers = $this->dbUserBackend->countUsers();
if ($nUsers === false) {
return null;
}
return $nUsers;
}

/**
Expand Down
14 changes: 9 additions & 5 deletions lib/private/Sync/User/UserSyncer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ public function registerBackend(IUserSyncBackend $userSyncBackend) {
*
* Using "missingAction" as option won't do anything here. It will be ignored.
*
* This method won't take into account whether the backends (including the
* requested ones) are registered or not.
*
* Custom options:
* - "backends" => "back1,back2,back3"
* Only those backends will be counted, assuming they're registered. The
* rest of the backends will be ignored.
* Only those backends will be counted. The rest of the backends will
* be ignored.
*/
public function localItemCount($opts = []): ?int {
$backends = $this->extractBackendsFromOpts($opts);
Expand Down Expand Up @@ -104,8 +107,9 @@ public function localItemCount($opts = []): ?int {
* - "missingAction" => "remove" or "disable".
* The action to do if the account is missing in the backend
* - "backends" => "back1,back2,back3"
* Only those backends will be synced, assuming they're registered. The
* rest of the backends will be ignored.
* Only those backends will be checked. The rest of the backends will
* be ignored. If the backends aren't registered, an error will be
* send through the callback.
*/
public function check($callback, $opts = []) {
$backends = $this->extractBackendsFromOpts($opts);
Expand Down Expand Up @@ -140,7 +144,7 @@ public function check($callback, $opts = []) {

$targetUserSyncBackend = $backendToUserSync[$targetBackend] ?? null;
if ($targetUserSyncBackend === null) {
// send and exception to the callback
// backend not registered -> send and exception to the callback
$callback(new SyncException("{$a->getUserId()}, backend {$targetBackend} is not found"), ISyncer::CHECK_STATE_ERROR);
return;
}
Expand Down
92 changes: 92 additions & 0 deletions tests/lib/Sync/SyncManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* @copyright Copyright (c) 2023, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace Tests\Sync;

use OC\Sync\SyncManager;
use OCP\Sync\ISyncer;
use OCP\Sync\User\IUserSyncer;
use Test\TestCase;

class SyncManagerTest extends TestCase {
/** @var SyncManager */
private $syncManager;

protected function setUp(): void {
$this->syncManager = new SyncManager();
}

public function testRegisterSyncer() {
$syncer = $this->createMock(ISyncer::class);
$this->assertTrue($this->syncManager->registerSyncer('mySyncer', $syncer));
}

public function testRegisterSyncerTwice() {
$syncer = $this->createMock(ISyncer::class);
$syncer2 = $this->createMock(ISyncer::class);
$this->assertTrue($this->syncManager->registerSyncer('mySyncer', $syncer));
$this->assertFalse($this->syncManager->registerSyncer('mySyncer', $syncer2));
}

public function testOverwriteSyncer() {
$syncer = $this->createMock(ISyncer::class);
$syncer2 = $this->createMock(ISyncer::class);
$this->assertTrue($this->syncManager->registerSyncer('mySyncer', $syncer));
$this->assertTrue($this->syncManager->overwriteSyncer('mySyncer', $syncer2));
}

public function testOverwriteSyncerNotOverwrite() {
$syncer = $this->createMock(ISyncer::class);
$this->assertFalse($this->syncManager->overwriteSyncer('mySyncer', $syncer));
}

public function testGetSyncerMissing() {
$this->assertNull($this->syncManager->getSyncer('mySyncer'));
}

public function testGetSyncerSetAndGet() {
$syncer = $this->createMock(ISyncer::class);
$this->assertTrue($this->syncManager->registerSyncer('mySyncer', $syncer));
$this->assertSame($syncer, $this->syncManager->getSyncer('mySyncer'));
}

public function testGetUserSyncerMissing() {
$this->assertNull($this->syncManager->getUserSyncer());
}

public function testGetUserSyncerSetAndGet() {
$userSyncer = $this->createMock(IUserSyncer::class);
$this->assertTrue($this->syncManager->registerSyncer('user', $userSyncer));
$this->assertSame($userSyncer, $this->syncManager->getUserSyncer());
}

public function testGetUserSyncerSetAndGetOverwrite() {
$userSyncer = $this->createMock(IUserSyncer::class);
$userSyncer2 = $this->createMock(IUserSyncer::class);
$this->assertTrue($this->syncManager->registerSyncer('user', $userSyncer));
$this->assertTrue($this->syncManager->overwriteSyncer('user', $userSyncer2));
$this->assertSame($userSyncer2, $this->syncManager->getUserSyncer());
}

public function testGetUserSyncerNotUserSyncer() {
$syncer = $this->createMock(ISyncer::class);
$this->assertTrue($this->syncManager->registerSyncer('user', $syncer));
$this->assertNull($this->syncManager->getUserSyncer());
}
}
Loading

0 comments on commit efb31c9

Please sign in to comment.