diff options
Diffstat (limited to 'apps/user_ldap/tests/Group_LDAPTest.php')
-rw-r--r-- | apps/user_ldap/tests/Group_LDAPTest.php | 198 |
1 files changed, 89 insertions, 109 deletions
diff --git a/apps/user_ldap/tests/Group_LDAPTest.php b/apps/user_ldap/tests/Group_LDAPTest.php index 06d64b99a4f..10182111768 100644 --- a/apps/user_ldap/tests/Group_LDAPTest.php +++ b/apps/user_ldap/tests/Group_LDAPTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -21,6 +22,8 @@ use OCP\GroupInterface; use OCP\IConfig; use OCP\IUser; use OCP\IUserManager; +use OCP\Security\ISecureRandom; +use OCP\Server; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; @@ -32,10 +35,10 @@ use Test\TestCase; * @package OCA\User_LDAP\Tests */ class Group_LDAPTest extends TestCase { - private MockObject|Access $access; - private MockObject|GroupPluginManager $pluginManager; - private MockObject|IConfig $config; - private MockObject|IUserManager $ncUserManager; + private Access&MockObject $access; + private GroupPluginManager&MockObject $pluginManager; + private IConfig&MockObject $config; + private IUserManager&MockObject $ncUserManager; private GroupLDAP $groupBackend; public function setUp(): void { @@ -51,7 +54,7 @@ class Group_LDAPTest extends TestCase { $this->groupBackend = new GroupLDAP($this->access, $this->pluginManager, $this->config, $this->ncUserManager); } - public function testCountEmptySearchString() { + public function testCountEmptySearchString(): void { $groupDN = 'cn=group,dc=foo,dc=bar'; $this->enableGroups(); @@ -94,30 +97,19 @@ class Group_LDAPTest extends TestCase { * @return MockObject|Access */ private function getAccessMock() { - static $conMethods; - static $accMethods; - - if (is_null($conMethods) || is_null($accMethods)) { - $conMethods = get_class_methods(Connection::class); - $accMethods = get_class_methods(Access::class); - } $lw = $this->createMock(ILDAPWrapper::class); - $connector = $this->getMockBuilder(Connection::class) - ->setMethods($conMethods) ->setConstructorArgs([$lw, '', null]) ->getMock(); $this->access = $this->createMock(Access::class); - $this->access->connection = $connector; - $this->access->userManager = $this->createMock(Manager::class); return $this->access; } - private function enableGroups() { + private function enableGroups(): void { $this->access->connection->expects($this->any()) ->method('__get') ->willReturnCallback(function ($name) { @@ -130,7 +122,7 @@ class Group_LDAPTest extends TestCase { }); } - public function testCountWithSearchString() { + public function testCountWithSearchString(): void { $this->enableGroups(); $this->access->expects($this->any()) @@ -155,7 +147,7 @@ class Group_LDAPTest extends TestCase { $this->access->expects($this->any()) ->method('dn2username') ->willReturnCallback(function () { - return 'foobar' . \OC::$server->getSecureRandom()->generate(7); + return 'foobar' . Server::get(ISecureRandom::class)->generate(7); }); $this->access->expects($this->any()) ->method('isDNPartOfBase') @@ -174,10 +166,10 @@ class Group_LDAPTest extends TestCase { $this->assertSame(2, $users); } - public function testCountUsersWithPlugin() { + public function testCountUsersWithPlugin(): void { /** @var GroupPluginManager|MockObject $pluginManager */ $this->pluginManager = $this->getMockBuilder(GroupPluginManager::class) - ->setMethods(['implementsActions', 'countUsersInGroup']) + ->onlyMethods(['implementsActions', 'countUsersInGroup']) ->getMock(); $this->pluginManager->expects($this->once()) @@ -194,7 +186,7 @@ class Group_LDAPTest extends TestCase { $this->assertEquals($this->groupBackend->countUsersInGroup('gid', 'search'), 42); } - public function testGidNumber2NameSuccess() { + public function testGidNumber2NameSuccess(): void { $this->enableGroups(); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -214,7 +206,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame('MyGroup', $group); } - public function testGidNumberID2NameNoGroup() { + public function testGidNumberID2NameNoGroup(): void { $this->enableGroups(); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -232,7 +224,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame(false, $group); } - public function testGidNumberID2NameNoName() { + public function testGidNumberID2NameNoName(): void { $this->enableGroups(); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -251,7 +243,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame(false, $group); } - public function testGetEntryGidNumberValue() { + public function testGetEntryGidNumberValue(): void { $this->enableGroups(); $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar'; @@ -268,7 +260,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame('3117', $gid); } - public function testGetEntryGidNumberNoValue() { + public function testGetEntryGidNumberNoValue(): void { $this->enableGroups(); $dn = 'cn=foobar,cn=foo,dc=barfoo,dc=bar'; @@ -285,7 +277,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame(false, $gid); } - public function testPrimaryGroupID2NameSuccessCache() { + public function testPrimaryGroupID2NameSuccessCache(): void { $this->enableGroups(); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -313,7 +305,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame('MyGroup', $group); } - public function testPrimaryGroupID2NameSuccess() { + public function testPrimaryGroupID2NameSuccess(): void { $this->enableGroups(); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -338,7 +330,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame('MyGroup', $group); } - public function testPrimaryGroupID2NameNoSID() { + public function testPrimaryGroupID2NameNoSID(): void { $this->enableGroups(); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -360,7 +352,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame(false, $group); } - public function testPrimaryGroupID2NameNoGroup() { + public function testPrimaryGroupID2NameNoGroup(): void { $this->enableGroups(); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -383,7 +375,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame(false, $group); } - public function testPrimaryGroupID2NameNoName() { + public function testPrimaryGroupID2NameNoName(): void { $this->enableGroups(); $userDN = 'cn=alice,cn=foo,dc=barfoo,dc=bar'; @@ -407,7 +399,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame(false, $group); } - public function testGetEntryGroupIDValue() { + public function testGetEntryGroupIDValue(): void { //tests getEntryGroupID via getGroupPrimaryGroupID //which is basically identical to getUserPrimaryGroupIDs $this->enableGroups(); @@ -426,7 +418,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame('3117', $gid); } - public function testGetEntryGroupIDNoValue() { + public function testGetEntryGroupIDNoValue(): void { //tests getEntryGroupID via getGroupPrimaryGroupID //which is basically identical to getUserPrimaryGroupIDs $this->enableGroups(); @@ -449,7 +441,7 @@ class Group_LDAPTest extends TestCase { * tests whether Group Backend behaves correctly when cache with uid and gid * is hit */ - public function testInGroupHitsUidGidCache() { + public function testInGroupHitsUidGidCache(): void { $this->enableGroups(); $uid = 'someUser'; @@ -468,7 +460,7 @@ class Group_LDAPTest extends TestCase { $this->groupBackend->inGroup($uid, $gid); } - public function groupWithMembersProvider() { + public static function groupWithMembersProvider(): array { return [ [ 'someGroup', @@ -483,10 +475,8 @@ class Group_LDAPTest extends TestCase { ]; } - /** - * @dataProvider groupWithMembersProvider - */ - public function testInGroupMember(string $gid, string $groupDn, array $memberDNs) { + #[\PHPUnit\Framework\Attributes\DataProvider('groupWithMembersProvider')] + public function testInGroupMember(string $gid, string $groupDn, array $memberDNs): void { $uid = 'someUser'; $userDn = $memberDNs[0]; @@ -524,10 +514,8 @@ class Group_LDAPTest extends TestCase { $this->assertTrue($this->groupBackend->inGroup($uid, $gid)); } - /** - * @dataProvider groupWithMembersProvider - */ - public function testInGroupMemberNot(string $gid, string $groupDn, array $memberDNs) { + #[\PHPUnit\Framework\Attributes\DataProvider('groupWithMembersProvider')] + public function testInGroupMemberNot(string $gid, string $groupDn, array $memberDNs): void { $uid = 'unelatedUser'; $userDn = 'uid=unrelatedUser,ou=unrelatedTeam,ou=unrelatedDepartment,dc=someDomain,dc=someTld'; @@ -565,14 +553,12 @@ class Group_LDAPTest extends TestCase { $this->assertFalse($this->groupBackend->inGroup($uid, $gid)); } - /** - * @dataProvider groupWithMembersProvider - */ - public function testInGroupMemberUid(string $gid, string $groupDn, array $memberDNs) { + #[\PHPUnit\Framework\Attributes\DataProvider('groupWithMembersProvider')] + public function testInGroupMemberUid(string $gid, string $groupDn, array $memberDNs): void { $memberUids = []; $userRecords = []; foreach ($memberDNs as $dn) { - $memberUids[] = ldap_explode_dn($dn, false)[0]; + $memberUids[] = ldap_explode_dn($dn, 0)[0]; $userRecords[] = ['dn' => [$dn]]; } @@ -625,7 +611,7 @@ class Group_LDAPTest extends TestCase { $this->assertTrue($this->groupBackend->inGroup($uid, $gid)); } - public function testGetGroupsWithOffset() { + public function testGetGroupsWithOffset(): void { $this->enableGroups(); $this->access->expects($this->once()) @@ -642,7 +628,7 @@ class Group_LDAPTest extends TestCase { * tests that a user listing is complete, if all its members have the group * as their primary. */ - public function testUsersInGroupPrimaryMembersOnly() { + public function testUsersInGroupPrimaryMembersOnly(): void { $this->enableGroups(); $this->access->connection->expects($this->any()) @@ -685,7 +671,7 @@ class Group_LDAPTest extends TestCase { * tests that a user listing is complete, if all its members have the group * as their primary. */ - public function testUsersInGroupPrimaryAndUnixMembers() { + public function testUsersInGroupPrimaryAndUnixMembers(): void { $this->enableGroups(); $this->access->connection->expects($this->any()) @@ -726,7 +712,7 @@ class Group_LDAPTest extends TestCase { * tests that a user counting is complete, if all its members have the group * as their primary. */ - public function testCountUsersInGroupPrimaryMembersOnly() { + public function testCountUsersInGroupPrimaryMembersOnly(): void { $this->enableGroups(); $this->access->connection->expects($this->any()) @@ -761,7 +747,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame(4, $users); } - public function testGetUserGroupsMemberOf() { + public function testGetUserGroupsMemberOf(): void { $this->enableGroups(); $dn = 'cn=userX,dc=foobar'; @@ -775,8 +761,7 @@ class Group_LDAPTest extends TestCase { ->method('username2dn') ->willReturn($dn); $this->access->expects($this->exactly(5)) - ->method('readAttribute') - ->will($this->onConsecutiveCalls($expectedGroups, [], [], [], [])); + ->method('readAttribute')->willReturnOnConsecutiveCalls($expectedGroups, [], [], [], []); $this->access->expects($this->any()) ->method('dn2groupname') ->willReturnArgument(0); @@ -797,7 +782,7 @@ class Group_LDAPTest extends TestCase { $this->assertSame(2, count($groups)); } - public function testGetUserGroupsMemberOfDisabled() { + public function testGetUserGroupsMemberOfDisabled(): void { $this->access->connection->expects($this->any()) ->method('__get') ->willReturnCallback(function ($name) { @@ -856,7 +841,7 @@ class Group_LDAPTest extends TestCase { $this->groupBackend->getUserGroups('userX'); } - public function testGetUserGroupsOfflineUser() { + public function testGetUserGroupsOfflineUser(): void { $this->enableGroups(); $offlineUser = $this->createMock(OfflineUser::class); @@ -874,15 +859,15 @@ class Group_LDAPTest extends TestCase { $this->initBackend(); $returnedGroups = $this->groupBackend->getUserGroups('userX'); $this->assertCount(2, $returnedGroups); - $this->assertTrue(in_array('groupB', $returnedGroups)); - $this->assertTrue(in_array('groupF', $returnedGroups)); + $this->assertContains('groupB', $returnedGroups); + $this->assertContains('groupF', $returnedGroups); } /** * regression tests against a case where a json object was stored instead of expected list * @see https://github.com/nextcloud/server/issues/42374 */ - public function testGetUserGroupsOfflineUserUnexpectedJson() { + public function testGetUserGroupsOfflineUserUnexpectedJson(): void { $this->enableGroups(); $offlineUser = $this->createMock(OfflineUser::class); @@ -901,11 +886,11 @@ class Group_LDAPTest extends TestCase { $this->initBackend(); $returnedGroups = $this->groupBackend->getUserGroups('userX'); $this->assertCount(2, $returnedGroups); - $this->assertTrue(in_array('groupB', $returnedGroups)); - $this->assertTrue(in_array('groupF', $returnedGroups)); + $this->assertContains('groupB', $returnedGroups); + $this->assertContains('groupF', $returnedGroups); } - public function testGetUserGroupsUnrecognizedOfflineUser() { + public function testGetUserGroupsUnrecognizedOfflineUser(): void { $this->enableGroups(); $dn = 'cn=userX,dc=foobar'; @@ -944,21 +929,19 @@ class Group_LDAPTest extends TestCase { $this->initBackend(); $returnedGroups = $this->groupBackend->getUserGroups('userX'); $this->assertCount(2, $returnedGroups); - $this->assertTrue(in_array('groupB', $returnedGroups)); - $this->assertTrue(in_array('groupF', $returnedGroups)); + $this->assertContains('groupB', $returnedGroups); + $this->assertContains('groupF', $returnedGroups); } - public function nestedGroupsProvider(): array { + public static function nestedGroupsProvider(): array { return [ [true], [false], ]; } - /** - * @dataProvider nestedGroupsProvider - */ - public function testGetGroupsByMember(bool $nestedGroups) { + #[\PHPUnit\Framework\Attributes\DataProvider('nestedGroupsProvider')] + public function testGetGroupsByMember(bool $nestedGroups): void { $groupFilter = '(&(objectclass=nextcloudGroup)(nextcloudEnabled=TRUE))'; $this->access->connection->expects($this->any()) ->method('__get') @@ -1033,10 +1016,10 @@ class Group_LDAPTest extends TestCase { $this->assertTrue(str_contains($filter, $groupFilter)); } [$memberFilter] = explode('&', $filter); - if ($memberFilter === 'member='.$dn) { + if ($memberFilter === 'member=' . $dn) { return [$group1, $group2]; return []; - } elseif ($memberFilter === 'member='.$group2['dn'][0]) { + } elseif ($memberFilter === 'member=' . $group2['dn'][0]) { return [$group3]; } else { return []; @@ -1072,9 +1055,9 @@ class Group_LDAPTest extends TestCase { $this->assertEquals($expectedGroupsNames, $groupsAgain); } - public function testCreateGroupWithPlugin() { + public function testCreateGroupWithPlugin(): void { $this->pluginManager = $this->getMockBuilder(GroupPluginManager::class) - ->setMethods(['implementsActions', 'createGroup']) + ->onlyMethods(['implementsActions', 'createGroup']) ->getMock(); $this->pluginManager->expects($this->once()) @@ -1088,15 +1071,15 @@ class Group_LDAPTest extends TestCase { ->willReturn('result'); $this->initBackend(); - $this->assertEquals($this->groupBackend->createGroup('gid'), true); + $this->assertTrue($this->groupBackend->createGroup('gid')); } - public function testCreateGroupFailing() { + public function testCreateGroupFailing(): void { $this->expectException(\Exception::class); $this->pluginManager = $this->getMockBuilder(GroupPluginManager::class) - ->setMethods(['implementsActions', 'createGroup']) + ->onlyMethods(['implementsActions', 'createGroup']) ->getMock(); $this->pluginManager->expects($this->once()) @@ -1108,9 +1091,9 @@ class Group_LDAPTest extends TestCase { $this->groupBackend->createGroup('gid'); } - public function testDeleteGroupWithPlugin() { + public function testDeleteGroupWithPlugin(): void { $this->pluginManager = $this->getMockBuilder(GroupPluginManager::class) - ->setMethods(['implementsActions', 'deleteGroup']) + ->onlyMethods(['implementsActions', 'deleteGroup']) ->getMock(); $this->pluginManager->expects($this->once()) @@ -1124,7 +1107,7 @@ class Group_LDAPTest extends TestCase { ->willReturn(true); $mapper = $this->getMockBuilder(GroupMapping::class) - ->setMethods(['unmap']) + ->onlyMethods(['unmap']) ->disableOriginalConstructor() ->getMock(); @@ -1137,11 +1120,11 @@ class Group_LDAPTest extends TestCase { } - public function testDeleteGroupFailing() { + public function testDeleteGroupFailing(): void { $this->expectException(\Exception::class); $this->pluginManager = $this->getMockBuilder(GroupPluginManager::class) - ->setMethods(['implementsActions', 'deleteGroup']) + ->onlyMethods(['implementsActions', 'deleteGroup']) ->getMock(); $this->pluginManager->expects($this->once()) @@ -1153,9 +1136,9 @@ class Group_LDAPTest extends TestCase { $this->groupBackend->deleteGroup('gid'); } - public function testAddToGroupWithPlugin() { + public function testAddToGroupWithPlugin(): void { $this->pluginManager = $this->getMockBuilder(GroupPluginManager::class) - ->setMethods(['implementsActions', 'addToGroup']) + ->onlyMethods(['implementsActions', 'addToGroup']) ->getMock(); $this->pluginManager->expects($this->once()) @@ -1169,15 +1152,15 @@ class Group_LDAPTest extends TestCase { ->willReturn('result'); $this->initBackend(); - $this->assertEquals($this->groupBackend->addToGroup('uid', 'gid'), 'result'); + $this->assertEquals('result', $this->groupBackend->addToGroup('uid', 'gid')); } - public function testAddToGroupFailing() { + public function testAddToGroupFailing(): void { $this->expectException(\Exception::class); $this->pluginManager = $this->getMockBuilder(GroupPluginManager::class) - ->setMethods(['implementsActions', 'addToGroup']) + ->onlyMethods(['implementsActions', 'addToGroup']) ->getMock(); $this->pluginManager->expects($this->once()) @@ -1189,9 +1172,9 @@ class Group_LDAPTest extends TestCase { $this->groupBackend->addToGroup('uid', 'gid'); } - public function testRemoveFromGroupWithPlugin() { + public function testRemoveFromGroupWithPlugin(): void { $this->pluginManager = $this->getMockBuilder(GroupPluginManager::class) - ->setMethods(['implementsActions', 'removeFromGroup']) + ->onlyMethods(['implementsActions', 'removeFromGroup']) ->getMock(); $this->pluginManager->expects($this->once()) @@ -1205,15 +1188,15 @@ class Group_LDAPTest extends TestCase { ->willReturn('result'); $this->initBackend(); - $this->assertEquals($this->groupBackend->removeFromGroup('uid', 'gid'), 'result'); + $this->assertEquals('result', $this->groupBackend->removeFromGroup('uid', 'gid')); } - public function testRemoveFromGroupFailing() { + public function testRemoveFromGroupFailing(): void { $this->expectException(\Exception::class); $this->pluginManager = $this->getMockBuilder(GroupPluginManager::class) - ->setMethods(['implementsActions', 'removeFromGroup']) + ->onlyMethods(['implementsActions', 'removeFromGroup']) ->getMock(); $this->pluginManager->expects($this->once()) @@ -1225,10 +1208,10 @@ class Group_LDAPTest extends TestCase { $this->groupBackend->removeFromGroup('uid', 'gid'); } - public function testGetGroupDetailsWithPlugin() { + public function testGetGroupDetailsWithPlugin(): void { /** @var GroupPluginManager|MockObject $pluginManager */ $this->pluginManager = $this->getMockBuilder(GroupPluginManager::class) - ->setMethods(['implementsActions', 'getGroupDetails']) + ->onlyMethods(['implementsActions', 'getGroupDetails']) ->getMock(); $this->pluginManager->expects($this->once()) @@ -1242,14 +1225,14 @@ class Group_LDAPTest extends TestCase { ->willReturn('result'); $this->initBackend(); - $this->assertEquals($this->groupBackend->getGroupDetails('gid'), 'result'); + $this->assertEquals('result', $this->groupBackend->getGroupDetails('gid')); } - public function testGetGroupDetailsFailing() { + public function testGetGroupDetailsFailing(): void { $this->expectException(\Exception::class); $this->pluginManager = $this->getMockBuilder(GroupPluginManager::class) - ->setMethods(['implementsActions', 'getGroupDetails']) + ->onlyMethods(['implementsActions', 'getGroupDetails']) ->getMock(); $this->pluginManager->expects($this->once()) @@ -1261,7 +1244,7 @@ class Group_LDAPTest extends TestCase { $this->groupBackend->getGroupDetails('gid'); } - public function groupMemberProvider() { + public static function groupMemberProvider(): array { $base = 'dc=species,dc=earth'; $birdsDn = [ @@ -1329,11 +1312,8 @@ class Group_LDAPTest extends TestCase { ]; } - /** - * @param string[] $expectedMembers - * @dataProvider groupMemberProvider - */ - public function testGroupMembers(array $expectedResult, ?array $groupsInfo = null) { + #[\PHPUnit\Framework\Attributes\DataProvider('groupMemberProvider')] + public function testGroupMembers(array $expectedResult, array $groupsInfo): void { $this->access->expects($this->any()) ->method('readAttribute') ->willReturnCallback(function ($group) use ($groupsInfo) { @@ -1358,21 +1338,21 @@ class Group_LDAPTest extends TestCase { foreach ($expectedResult as $groupDN => $expectedMembers) { $resultingMembers = $this->invokePrivate($this->groupBackend, '_groupMembers', [$groupDN]); - $this->assertEqualsCanonicalizing($expectedMembers, $resultingMembers); + sort($expectedMembers); + sort($resultingMembers); + $this->assertEquals($expectedMembers, $resultingMembers); } } - public function displayNameProvider() { + public static function displayNameProvider(): array { return [ ['Graphic Novelists', ['Graphic Novelists']], ['', false], ]; } - /** - * @dataProvider displayNameProvider - */ - public function testGetDisplayName(string $expected, $ldapResult) { + #[\PHPUnit\Framework\Attributes\DataProvider('displayNameProvider')] + public function testGetDisplayName(string $expected, bool|array $ldapResult): void { $gid = 'graphic_novelists'; $this->access->expects($this->atLeastOnce()) |