Skip to content
Prev Previous commit
Next Next commit
Fix some tests
Signed-off-by: Carl Schwan <[email protected]>
  • Loading branch information
CarlSchwan committed Jun 13, 2022
commit 62f6eed42082947c181c866ac7ca07f2c688a63c
4 changes: 2 additions & 2 deletions lib/private/Group/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,10 @@ public function searchInGroup(string $gid, string $search = '', int $limit = -1,
$displayNameCache = \OC::$server->get(DisplayNameCache::class);
while ($row = $result->fetch()) {
if (isset($row['displayname'])) {
$users[] = new LazyUser($row['uid'], $displayNameCache, $userManager, $row['displayname']);
$users[$row['uid']] = new LazyUser($row['uid'], $displayNameCache, $userManager, $row['displayname']);
} else {
// TODO maybe also fetch the displayname directly here
$users[] = new LazyUser($row['uid'], $displayNameCache, $userManager);
$users[$row['uid']] = new LazyUser($row['uid'], $displayNameCache, $userManager);
}
}
$result->closeCursor();
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Group/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function removeUser(IUser $user): void {
public function searchUsers(string $search, ?int $limit = null, ?int $offset = null): array {
$users = [];
foreach ($this->backends as $backend) {
$users += $backend->searchInGroup($this->gid, $search, $limit, $offset);
$users = array_merge($users, $backend->searchInGroup($this->gid, $search, $limit ? $limit : -1, $offset ? $offset : 0));
if (!is_null($limit) and $limit <= 0) {
return $users;
}
Expand Down
7 changes: 5 additions & 2 deletions lib/public/Group/Backend/ABackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public function searchInGroup(string $gid, string $search = '', int $limit = -1,
// Default implementation for compatibility reasons
$displayNameCache = \OC::$server->get(DisplayNameCache::class);
$userManager = \OC::$server->get(IUserManager::class);
return array_map(fn($userId) => new LazyUser($userId, $displayNameCache, $userManager),
$this->usersInGroup($gid, $search, $limit, $offset));
$users = [];
foreach ($this->usersInGroup($gid, $search, $limit, $offset) as $userId) {
$users[$userId] = new LazyUser($userId, $displayNameCache, $userManager);
}
return $users;
}
}
48 changes: 28 additions & 20 deletions tests/lib/Group/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ public function testSearchUsers() {
$group = new \OC\Group\Group('group1', [$backend], $this->dispatcher, $userManager);

$backend->expects($this->once())
->method('usersInGroup')
->method('searchInGroup')
->with('group1', '2')
->willReturn(['user2']);
->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]);

$users = $group->searchUsers('2');

Expand All @@ -326,13 +326,13 @@ public function testSearchUsersMultipleBackends() {
$group = new \OC\Group\Group('group1', [$backend1, $backend2], $this->dispatcher, $userManager);

$backend1->expects($this->once())
->method('usersInGroup')
->method('searchInGroup')
->with('group1', '2')
->willReturn(['user2']);
->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]);
$backend2->expects($this->once())
->method('usersInGroup')
->method('searchInGroup')
->with('group1', '2')
->willReturn(['user2']);
->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]);

$users = $group->searchUsers('2');

Expand All @@ -349,9 +349,9 @@ public function testSearchUsersLimitAndOffset() {
$group = new \OC\Group\Group('group1', [$backend], $this->dispatcher, $userManager);

$backend->expects($this->once())
->method('usersInGroup')
->method('searchInGroup')
->with('group1', 'user', 1, 1)
->willReturn(['user2']);
->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]);

$users = $group->searchUsers('user', 1, 1);

Expand All @@ -371,13 +371,13 @@ public function testSearchUsersMultipleBackendsLimitAndOffset() {
$group = new \OC\Group\Group('group1', [$backend1, $backend2], $this->dispatcher, $userManager);

$backend1->expects($this->once())
->method('usersInGroup')
->method('searchInGroup')
->with('group1', 'user', 2, 1)
->willReturn(['user2']);
->willReturn(['user2' => new \OC\User\User('user2', null, $this->dispatcher)]);
$backend2->expects($this->once())
->method('usersInGroup')
->method('searchInGroup')
->with('group1', 'user', 2, 1)
->willReturn(['user1']);
->willReturn(['user1' => new \OC\User\User('user1', null, $this->dispatcher)]);

$users = $group->searchUsers('user', 2, 1);

Expand Down Expand Up @@ -441,17 +441,28 @@ public function testCountUsersMultipleBackends() {
}

public function testCountUsersNoMethod() {
$backend1 = $this->getMockBuilder('OC\Group\Database')
->disableOriginalConstructor()
$backend1 = $this->getMockBuilder('OCP\Group\GroupInterface')
->disableOriginalConstructor()
->setMethods([
'getGroupDetails',
'implementsActions',
'getUserGroups',
'inGroup',
'getGroups',
'groupExists',
'usersInGroup',
'createGroup',
'addToGroup',
'removeFromGroup',
'searchInGroup',
'countUsersInGroup'
])
->getMock();
$userManager = $this->getUserManager();
$group = new \OC\Group\Group('group1', [$backend1], $this->dispatcher, $userManager);

$backend1->expects($this->never())
->method('countUsersInGroup');
$backend1->expects($this->any())
->method('implementsActions')
->willReturn(false);

$users = $group->count('2');

Expand All @@ -468,9 +479,6 @@ public function testDelete() {
$backend->expects($this->once())
->method('deleteGroup')
->with('group1');
$backend->expects($this->any())
->method('implementsActions')
->willReturn(true);

$group->delete();
}
Expand Down
28 changes: 14 additions & 14 deletions tests/lib/Group/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private function getTestBackend($implementedActions = null) {
'createGroup',
'addToGroup',
'removeFromGroup',
'searchInGroup',
])
->getMock();
$backend->expects($this->any())
Expand Down Expand Up @@ -352,9 +353,13 @@ public function testSearchResultExistsButGroupDoesNot() {
->with('1')
->willReturn(['group1']);
$backend->expects($this->once())
->method('getGroupDetails')
->with('group1')
->willReturn([]);
$backend->expects($this->any())
->method('groupExists')
->with('group1')
->willReturn(false);
->willReturn(true);

/** @var \OC\User\Manager $userManager */
$userManager = $this->createMock(Manager::class);
Expand All @@ -379,6 +384,10 @@ public function testGetUserGroups() {
->method('groupExists')
->with('group1')
->willReturn(true);
$backend->expects($this->any())
->method('getGroupDetails')
->with('group1')
->willReturn([]);

$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend);
Expand Down Expand Up @@ -544,19 +553,10 @@ public function testDisplayNamesInGroupWithOneUserBackend() {
->method('groupExists')
->with('testgroup')
->willReturn(true);

$backend->expects($this->any())
->method('inGroup')
->willReturnCallback(function ($uid, $gid) {
switch ($uid) {
case 'user1': return false;
case 'user2': return true;
case 'user3': return false;
case 'user33': return true;
default:
return null;
}
});
$backend->expects($this->once())
->method('getGroupDetails')
->with('testgroup')
->willReturn([]);

$this->userManager->expects($this->any())
->method('searchDisplayName')
Expand Down
19 changes: 12 additions & 7 deletions tests/lib/Util/Group/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@
namespace Test\Util\Group;

use OCP\Group\Backend\ABackend;
use OCP\Group\Backend\IDeleteGroupBackend;
use OCP\Group\Backend\IAddToGroupBackend;
use OCP\Group\Backend\IRemoveFromGroupBackend;
use OCP\Group\Backend\ICreateGroupBackend;
use OCP\Group\Backend\ICountUsersBackend;
use OCP\IUser;

/**
* dummy group backend, does not keep state, only for testing use
* Dummy group backend, does not keep state, only for testing use
*/
class Dummy extends ABackend {
class Dummy extends ABackend implements ICreateGroupBackend, IDeleteGroupBackend, IAddToGroupBackend, IRemoveFromGroupBackend, ICountUsersBackend {
private $groups = [];
/**
* Try to create a new group
Expand All @@ -45,7 +50,7 @@ class Dummy extends ABackend {
* Tries to create a new group. If the group name already exists, false will
* be returned.
*/
public function createGroup($gid) {
public function createGroup(string $gid): bool {
if (!isset($this->groups[$gid])) {
$this->groups[$gid] = [];
return true;
Expand All @@ -61,7 +66,7 @@ public function createGroup($gid) {
*
* Deletes a group and removes it from the group_user-table
*/
public function deleteGroup($gid) {
public function deleteGroup(string $gid): bool {
if (isset($this->groups[$gid])) {
unset($this->groups[$gid]);
return true;
Expand Down Expand Up @@ -94,7 +99,7 @@ public function inGroup($uid, $gid) {
*
* Adds a user to a group.
*/
public function addToGroup($uid, $gid) {
public function addToGroup(string $uid, string $gid): bool {
if (isset($this->groups[$gid])) {
if (array_search($uid, $this->groups[$gid]) === false) {
$this->groups[$gid][] = $uid;
Expand All @@ -115,7 +120,7 @@ public function addToGroup($uid, $gid) {
*
* removes the user from a group.
*/
public function removeFromGroup($uid, $gid) {
public function removeFromGroup(string $uid, string $gid): bool {
if (isset($this->groups[$gid])) {
if (($index = array_search($uid, $this->groups[$gid])) !== false) {
unset($this->groups[$gid][$index]);
Expand Down Expand Up @@ -201,7 +206,7 @@ public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
* @param int $offset
* @return int
*/
public function countUsersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
public function countUsersInGroup(string $gid, string $search = ''): int {
if (isset($this->groups[$gid])) {
if (empty($search)) {
return count($this->groups[$gid]);
Expand Down