From e10dffaa7fa153681c7c70a8b044af5a0094698e Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Wed, 8 Jul 2026 15:59:23 +0200 Subject: [PATCH] feat: Fetch groups in batch in getUserGroups Signed-off-by: Carl Schwan --- lib/private/Group/Manager.php | 14 ++------------ tests/lib/Group/ManagerTest.php | 28 ++++++++++++++++------------ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/lib/private/Group/Manager.php b/lib/private/Group/Manager.php index 028dffd227391..c7bbf147fc05b 100644 --- a/lib/private/Group/Manager.php +++ b/lib/private/Group/Manager.php @@ -302,18 +302,8 @@ public function getUserGroups(?IUser $user = null): array { * @return array */ public function getUserIdGroups(string $uid): array { - $groups = []; - - foreach ($this->getUserIdGroupIds($uid) as $groupId) { - $aGroup = $this->get($groupId); - if ($aGroup instanceof IGroup) { - $groups[$groupId] = $aGroup; - } else { - $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']); - } - } - - return $groups; + $groupIds = $this->getUserIdGroupIds($uid); + return $this->getGroupsObjects($groupIds); } /** diff --git a/tests/lib/Group/ManagerTest.php b/tests/lib/Group/ManagerTest.php index cd4eed5180149..6d369be102b11 100644 --- a/tests/lib/Group/ManagerTest.php +++ b/tests/lib/Group/ManagerTest.php @@ -428,9 +428,9 @@ public function testGetUserGroups(): void { ->with('user1') ->willReturn(['group1']); $backend->expects($this->any()) - ->method('groupExists') + ->method('getGroupDetails') ->with('group1') - ->willReturn(true); + ->willReturn(['displayName' => 'group1']); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); $manager->addBackend($backend); @@ -476,9 +476,9 @@ public function testGetUserGroupsWithDeletedGroup(): void { ->with('user1') ->willReturn(['group1']); $backend->expects($this->any()) - ->method('groupExists') - ->with('group1') - ->willReturn(false); + ->method('getGroupsDetails') + ->with(['group1']) + ->willReturn(['group1' => []]); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); $manager->addBackend($backend); @@ -560,8 +560,8 @@ public function testGetUserGroupsMultipleBackends(): void { ->with('user1') ->willReturn(['group1']); $backend1->expects($this->any()) - ->method('groupExists') - ->willReturn(true); + ->method('getGroupDetails') + ->willReturnCallback(fn ($gid) => $gid === 'group1' ? ['displayName' => 'group1'] : []); /** * @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2 @@ -571,9 +571,9 @@ public function testGetUserGroupsMultipleBackends(): void { ->method('getUserGroups') ->with('user1') ->willReturn(['group1', 'group2']); - $backend1->expects($this->any()) - ->method('groupExists') - ->willReturn(true); + $backend2->expects($this->any()) + ->method('getGroupDetails') + ->willReturnCallback(fn ($gid) => ['displayName' => $gid]); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); $manager->addBackend($backend1); @@ -870,6 +870,10 @@ public function testGetUserGroupsWithAddUser(): void { ->method('groupExists') ->with('group1') ->willReturn(true); + $backend->expects($this->any()) + ->method('getGroupDetails') + ->with('group1') + ->willReturn(['displayName' => 'group1']); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger, $this->cache, $this->remoteIpAddress); $manager->addBackend($backend); @@ -904,9 +908,9 @@ public function testGetUserGroupsWithRemoveUser(): void { return $expectedGroups; }); $backend->expects($this->any()) - ->method('groupExists') + ->method('getGroupDetails') ->with('group1') - ->willReturn(true); + ->willReturn(['displayName' => 'group1']); $backend->expects($this->once()) ->method('inGroup') ->willReturn(true);