diff options
Diffstat (limited to 'apps/user_ldap/tests/Jobs')
-rw-r--r-- | apps/user_ldap/tests/Jobs/CleanUpTest.php | 54 | ||||
-rw-r--r-- | apps/user_ldap/tests/Jobs/SyncTest.php | 187 | ||||
-rw-r--r-- | apps/user_ldap/tests/Jobs/UpdateGroupsTest.php | 190 |
3 files changed, 106 insertions, 325 deletions
diff --git a/apps/user_ldap/tests/Jobs/CleanUpTest.php b/apps/user_ldap/tests/Jobs/CleanUpTest.php index 2e66d114755..5a1e563a1e8 100644 --- a/apps/user_ldap/tests/Jobs/CleanUpTest.php +++ b/apps/user_ldap/tests/Jobs/CleanUpTest.php @@ -1,29 +1,11 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\User_LDAP\Tests\Jobs; use Exception; @@ -31,20 +13,19 @@ use OCA\User_LDAP\Helper; use OCA\User_LDAP\Jobs\CleanUp; use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User_Proxy; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\IConfig; use OCP\IDBConnection; use Test\TestCase; class CleanUpTest extends TestCase { - /** @var CleanUp */ - protected $bgJob; - - /** @var array */ - protected $mocks; + protected CleanUp $bgJob; + protected array $mocks; public function setUp(): void { + parent::setUp(); $this->createMocks(); - $this->bgJob = new CleanUp($this->mocks['userBackend'], $this->mocks['deletedUsersIndex']); + $this->bgJob = new CleanUp($this->mocks['timeFactory'], $this->mocks['userBackend'], $this->mocks['deletedUsersIndex']); $this->bgJob->setArguments($this->mocks); } @@ -55,12 +36,13 @@ class CleanUpTest extends TestCase { $this->mocks['ocConfig'] = $this->createMock(IConfig::class); $this->mocks['db'] = $this->createMock(IDBConnection::class); $this->mocks['helper'] = $this->createMock(Helper::class); + $this->mocks['timeFactory'] = $this->createMock(ITimeFactory::class); } /** * clean up job must not run when there are disabled configurations */ - public function test_runNotAllowedByDisabledConfigurations() { + public function test_runNotAllowedByDisabledConfigurations(): void { $this->mocks['helper']->expects($this->once()) ->method('haveDisabledConfigurations') ->willReturn(true); @@ -76,10 +58,10 @@ class CleanUpTest extends TestCase { * clean up job must not run when LDAP Helper is broken i.e. * returning unexpected results */ - public function test_runNotAllowedByBrokenHelper() { + public function test_runNotAllowedByBrokenHelper(): void { $this->mocks['helper']->expects($this->once()) ->method('haveDisabledConfigurations') - ->will($this->throwException(new Exception())); + ->willThrowException(new Exception()); $this->mocks['ocConfig']->expects($this->never()) ->method('getSystemValue'); @@ -91,7 +73,7 @@ class CleanUpTest extends TestCase { /** * clean up job must not run when it is not enabled */ - public function test_runNotAllowedBySysConfig() { + public function test_runNotAllowedBySysConfig(): void { $this->mocks['helper']->expects($this->once()) ->method('haveDisabledConfigurations') ->willReturn(false); @@ -107,7 +89,7 @@ class CleanUpTest extends TestCase { /** * clean up job is allowed to run */ - public function test_runIsAllowed() { + public function test_runIsAllowed(): void { $this->mocks['helper']->expects($this->once()) ->method('haveDisabledConfigurations') ->willReturn(false); @@ -123,7 +105,7 @@ class CleanUpTest extends TestCase { /** * check whether offset will be reset when it needs to */ - public function test_OffsetResetIsNecessary() { + public function test_OffsetResetIsNecessary(): void { $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize() - 1); $this->assertSame(true, $result); } @@ -131,7 +113,7 @@ class CleanUpTest extends TestCase { /** * make sure offset is not reset when it is not due */ - public function test_OffsetResetIsNotNecessary() { + public function test_OffsetResetIsNotNecessary(): void { $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize()); $this->assertSame(false, $result); } diff --git a/apps/user_ldap/tests/Jobs/SyncTest.php b/apps/user_ldap/tests/Jobs/SyncTest.php index 6ee186d0da4..f6ecf984ab0 100644 --- a/apps/user_ldap/tests/Jobs/SyncTest.php +++ b/apps/user_ldap/tests/Jobs/SyncTest.php @@ -1,29 +1,9 @@ <?php + /** - * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\User_LDAP\Tests\Jobs; use OCA\User_LDAP\Access; @@ -35,41 +15,35 @@ use OCA\User_LDAP\Jobs\Sync; use OCA\User_LDAP\LDAP; use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\User\Manager; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IAvatarManager; use OCP\IConfig; use OCP\IDBConnection; use OCP\IUserManager; use OCP\Notification\IManager; +use OCP\Server; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Test\TestCase; +/** + * @group DB + */ class SyncTest extends TestCase { - - /** @var array */ - protected $arguments; - /** @var Helper|\PHPUnit\Framework\MockObject\MockObject */ - protected $helper; - /** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */ - protected $ldapWrapper; - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */ - protected $userManager; - /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */ - protected $mapper; - /** @var Sync */ - protected $sync; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - protected $config; - /** @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $avatarManager; - /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */ - protected $dbc; - /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $ncUserManager; - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $notificationManager; - /** @var ConnectionFactory|\PHPUnit\Framework\MockObject\MockObject */ - protected $connectionFactory; - /** @var AccessFactory|\PHPUnit\Framework\MockObject\MockObject */ - protected $accessFactory; + protected Helper&MockObject $helper; + protected LDAP&MockObject $ldapWrapper; + protected Manager&MockObject $userManager; + protected UserMapping&MockObject $mapper; + protected IConfig&MockObject $config; + protected IAvatarManager&MockObject $avatarManager; + protected IDBConnection&MockObject $dbc; + protected IUserManager&MockObject $ncUserManager; + protected IManager&MockObject $notificationManager; + protected ConnectionFactory&MockObject $connectionFactory; + protected AccessFactory&MockObject $accessFactory; + protected array $arguments = []; + protected Sync $sync; protected function setUp(): void { parent::setUp(); @@ -83,26 +57,32 @@ class SyncTest extends TestCase { $this->dbc = $this->createMock(IDBConnection::class); $this->ncUserManager = $this->createMock(IUserManager::class); $this->notificationManager = $this->createMock(IManager::class); - $this->connectionFactory = $this->createMock(ConnectionFactory::class); + $this->connectionFactory = $this->getMockBuilder(ConnectionFactory::class) + ->setConstructorArgs([ + $this->ldapWrapper, + ]) + ->getMock(); $this->accessFactory = $this->createMock(AccessFactory::class); - $this->arguments = [ - 'helper' => $this->helper, - 'ldapWrapper' => $this->ldapWrapper, - 'mapper' => $this->mapper, - 'config' => $this->config, - 'avatarManager' => $this->avatarManager, - 'dbc' => $this->dbc, - 'ncUserManager' => $this->ncUserManager, - 'notificationManager' => $this->notificationManager, - 'connectionFactory' => $this->connectionFactory, - 'accessFactory' => $this->accessFactory, - ]; - - $this->sync = new Sync($this->userManager); + $this->sync = new Sync( + Server::get(ITimeFactory::class), + Server::get(IEventDispatcher::class), + $this->config, + $this->dbc, + $this->avatarManager, + $this->ncUserManager, + Server::get(LoggerInterface::class), + $this->notificationManager, + $this->mapper, + $this->helper, + $this->connectionFactory, + $this->accessFactory, + ); + + $this->sync->overwritePropertiesForTest($this->ldapWrapper); } - public function intervalDataProvider() { + public static function intervalDataProvider(): array { return [ [ 0, 1000, 750 @@ -122,10 +102,8 @@ class SyncTest extends TestCase { ]; } - /** - * @dataProvider intervalDataProvider - */ - public function testUpdateInterval($userCount, $pagingSize1, $pagingSize2) { + #[\PHPUnit\Framework\Attributes\DataProvider('intervalDataProvider')] + public function testUpdateInterval(int $userCount, int $pagingSize1, int $pagingSize2): void { $this->config->expects($this->once()) ->method('setAppValue') ->with('user_ldap', 'background_sync_interval', $this->anything()) @@ -155,7 +133,7 @@ class SyncTest extends TestCase { $this->sync->updateInterval(); } - public function moreResultsProvider() { + public static function moreResultsProvider(): array { return [ [ 3, 3, true ], [ 3, 5, true ], @@ -165,11 +143,13 @@ class SyncTest extends TestCase { ]; } - /** - * @dataProvider moreResultsProvider - */ - public function testMoreResults($pagingSize, $results, $expected) { - $connection = $this->createMock(Connection::class); + #[\PHPUnit\Framework\Attributes\DataProvider('moreResultsProvider')] + public function testMoreResults($pagingSize, $results, $expected): void { + $connection = $this->getMockBuilder(Connection::class) + ->setConstructorArgs([ + $this->ldapWrapper, + ]) + ->getMock(); $this->connectionFactory->expects($this->any()) ->method('get') ->willReturn($connection); @@ -182,7 +162,7 @@ class SyncTest extends TestCase { return null; }); - /** @var Access|\PHPUnit\Framework\MockObject\MockObject $access */ + /** @var Access&MockObject $access */ $access = $this->createMock(Access::class); $this->accessFactory->expects($this->any()) ->method('get') @@ -207,7 +187,7 @@ class SyncTest extends TestCase { $this->assertSame($expected, $hasMoreResults); } - public function cycleDataProvider() { + public static function cycleDataProvider(): array { $lastCycle = ['prefix' => 's01', 'offset' => 1000]; $lastCycle2 = ['prefix' => '', 'offset' => 1000]; return [ @@ -220,22 +200,24 @@ class SyncTest extends TestCase { ]; } - /** - * @dataProvider cycleDataProvider - */ - public function testDetermineNextCycle($cycleData, $prefixes, $expectedCycle) { + #[\PHPUnit\Framework\Attributes\DataProvider('cycleDataProvider')] + public function testDetermineNextCycle(?array $cycleData, array $prefixes, ?array $expectedCycle): void { $this->helper->expects($this->any()) ->method('getServerConfigurationPrefixes') ->with(true) ->willReturn($prefixes); if (is_array($expectedCycle)) { + $calls = [ + ['user_ldap', 'background_sync_prefix', $expectedCycle['prefix']], + ['user_ldap', 'background_sync_offset', $expectedCycle['offset']], + ]; $this->config->expects($this->exactly(2)) ->method('setAppValue') - ->withConsecutive( - ['user_ldap', 'background_sync_prefix', $expectedCycle['prefix']], - ['user_ldap', 'background_sync_offset', $expectedCycle['offset']] - ); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); } else { $this->config->expects($this->never()) ->method('setAppValue'); @@ -252,7 +234,7 @@ class SyncTest extends TestCase { } } - public function testQualifiesToRun() { + public function testQualifiesToRun(): void { $cycleData = ['prefix' => 's01']; $this->config->expects($this->exactly(2)) @@ -264,7 +246,7 @@ class SyncTest extends TestCase { $this->assertFalse($this->sync->qualifiesToRun($cycleData)); } - public function runDataProvider() { + public static function runDataProvider(): array { return [ #0 - one LDAP server, reset [[ @@ -296,10 +278,8 @@ class SyncTest extends TestCase { ]; } - /** - * @dataProvider runDataProvider - */ - public function testRun($runData) { + #[\PHPUnit\Framework\Attributes\DataProvider('runDataProvider')] + public function testRun(array $runData): void { $this->config->expects($this->any()) ->method('getAppValue') ->willReturnCallback(function ($app, $key, $default) use ($runData) { @@ -326,13 +306,18 @@ class SyncTest extends TestCase { return $default; }); + + $calls = [ + ['user_ldap', 'background_sync_prefix', $runData['expectedNextCycle']['prefix']], + ['user_ldap', 'background_sync_offset', $runData['expectedNextCycle']['offset']], + ['user_ldap', 'background_sync_interval', '43200'], + ]; $this->config->expects($this->exactly(3)) ->method('setAppValue') - ->withConsecutive( - ['user_ldap', 'background_sync_prefix', $runData['expectedNextCycle']['prefix']], - ['user_ldap', 'background_sync_offset', $runData['expectedNextCycle']['offset']], - ['user_ldap', 'background_sync_interval', $this->anything()] - ); + ->willReturnCallback(function () use (&$calls): void { + $expected = array_shift($calls); + $this->assertEquals($expected, func_get_args()); + }); $this->config->expects($this->any()) ->method('getAppKeys') ->with('user_ldap') @@ -343,7 +328,11 @@ class SyncTest extends TestCase { ->with(true) ->willReturn($runData['prefixes']); - $connection = $this->createMock(Connection::class); + $connection = $this->getMockBuilder(Connection::class) + ->setConstructorArgs([ + $this->ldapWrapper, + ]) + ->getMock(); $this->connectionFactory->expects($this->any()) ->method('get') ->willReturn($connection); @@ -356,7 +345,7 @@ class SyncTest extends TestCase { return null; }); - /** @var Access|\PHPUnit\Framework\MockObject\MockObject $access */ + /** @var Access&MockObject $access */ $access = $this->createMock(Access::class); $this->accessFactory->expects($this->any()) ->method('get') diff --git a/apps/user_ldap/tests/Jobs/UpdateGroupsTest.php b/apps/user_ldap/tests/Jobs/UpdateGroupsTest.php deleted file mode 100644 index cb947c81a6f..00000000000 --- a/apps/user_ldap/tests/Jobs/UpdateGroupsTest.php +++ /dev/null @@ -1,190 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -namespace OCA\user_ldap\tests\Jobs; - -use Doctrine\DBAL\Driver\Statement; -use OCA\User_LDAP\Group_Proxy; -use OCA\User_LDAP\Jobs\UpdateGroups; -use OCP\DB\QueryBuilder\IExpressionBuilder; -use OCP\DB\QueryBuilder\IQueryBuilder; -use OCP\EventDispatcher\IEventDispatcher; -use OCP\Group\Events\UserAddedEvent; -use OCP\Group\Events\UserRemovedEvent; -use OCP\IDBConnection; -use OCP\IGroup; -use OCP\IGroupManager; -use OCP\IUser; -use OCP\IUserManager; -use Psr\Log\LoggerInterface; -use Test\TestCase; - -class UpdateGroupsTest extends TestCase { - - /** @var Group_Proxy|\PHPUnit\Framework\MockObject\MockObject */ - protected $groupBackend; - /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */ - protected $dispatcher; - /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $groupManager; - /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $userManager; - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ - protected $logger; - /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */ - protected $dbc; - - /** @var UpdateGroups */ - protected $updateGroupsJob; - - public function setUp(): void { - $this->groupBackend = $this->createMock(Group_Proxy::class); - $this->dispatcher = $this->createMock(IEventDispatcher::class); - $this->groupManager = $this->createMock(IGroupManager::class); - $this->userManager = $this->createMock(IUserManager::class); - $this->logger = $this->createMock(LoggerInterface::class); - $this->dbc = $this->createMock(IDBConnection::class); - - $this->updateGroupsJob = new UpdateGroups( - $this->groupBackend, - $this->dispatcher, - $this->groupManager, - $this->userManager, - $this->logger, - $this->dbc - ); - } - - public function testHandleKnownGroups() { - $knownGroups = [ - 'emptyGroup' => \serialize([]), - 'stableGroup' => \serialize(['userA', 'userC', 'userE']), - 'groupWithAdditions' => \serialize(['userA', 'userC', 'userE']), - 'groupWithRemovals' => \serialize(['userA', 'userC', 'userDeleted', 'userE']), - 'groupWithAdditionsAndRemovals' => \serialize(['userA', 'userC', 'userE']), - 'vanishedGroup' => \serialize(['userB', 'userDeleted']) - ]; - $knownGroupsDB = []; - foreach ($knownGroups as $gid => $members) { - $knownGroupsDB[] = [ - 'owncloudname' => $gid, - 'owncloudusers' => $members - ]; - } - $actualGroups = [ - 'emptyGroup' => [], - 'stableGroup' => ['userA', 'userC', 'userE'], - 'groupWithAdditions' => ['userA', 'userC', 'userE', 'userF'], - 'groupWithRemovals' => ['userA', 'userE'], - 'groupWithAdditionsAndRemovals' => ['userC', 'userE', 'userF'], - 'newGroup' => ['userB', 'userF'], - ]; - $groups = array_intersect(array_keys($knownGroups), array_keys($actualGroups)); - - /** @var IQueryBuilder|\PHPUnit\Framework\MockObject\MockObject $updateQb */ - $updateQb = $this->createMock(IQueryBuilder::class); - $updateQb->expects($this->once()) - ->method('update') - ->willReturn($updateQb); - $updateQb->expects($this->once()) - ->method('set') - ->willReturn($updateQb); - $updateQb->expects($this->once()) - ->method('where') - ->willReturn($updateQb); - // three groups need to be updated - $updateQb->expects($this->exactly(3)) - ->method('setParameters'); - $updateQb->expects($this->exactly(3)) - ->method('execute'); - $updateQb->expects($this->any()) - ->method('expr') - ->willReturn($this->createMock(IExpressionBuilder::class)); - - $stmt = $this->createMock(Statement::class); - $stmt->expects($this->once()) - ->method('fetchAll') - ->willReturn($knownGroupsDB); - - $selectQb = $this->createMock(IQueryBuilder::class); - $selectQb->expects($this->once()) - ->method('select') - ->willReturn($selectQb); - $selectQb->expects($this->once()) - ->method('from') - ->willReturn($selectQb); - $selectQb->expects($this->once()) - ->method('execute') - ->willReturn($stmt); - - $this->dbc->expects($this->any()) - ->method('getQueryBuilder') - ->willReturnOnConsecutiveCalls($updateQb, $selectQb); - - $this->groupBackend->expects($this->any()) - ->method('usersInGroup') - ->willReturnCallback(function ($groupID) use ($actualGroups) { - return isset($actualGroups[$groupID]) ? $actualGroups[$groupID] : []; - }); - - $this->groupManager->expects($this->any()) - ->method('get') - ->willReturnCallback(function (string $groupId): ?IGroup { - if ($groupId === 'vanishedGroup') { - return null; - } - return $this->createMock(IGroup::class); - }); - - $this->userManager->expects($this->exactly(5)) - ->method('get') - ->willReturnCallback(function (string $userId) { - if ($userId === 'userDeleted') { - // user already deleted - return null; - } - return $this->createMock(IUser::class); - }); - - $addedEvents = 0; - $removedEvents = 0; - $this->dispatcher->expects($this->exactly(4)) - ->method('dispatchTyped') - ->willReturnCallback(function ($event) use (&$addedEvents, &$removedEvents) { - if ($event instanceof UserRemovedEvent) { - $removedEvents++; - } elseif ($event instanceof UserAddedEvent) { - $addedEvents++; - } - }); - - $this->invokePrivate($this->updateGroupsJob, 'handleKnownGroups', [$groups]); - - $this->assertSame(2, $removedEvents); - $this->assertSame(2, $addedEvents); - // and no event for the user that is already deleted, the DB is nevertheless updated, hence 5 - } -} |