diff --git a/lib/Controller/UserApiController.php b/lib/Controller/UserApiController.php index a5ef90a475e..ddb406cb530 100644 --- a/lib/Controller/UserApiController.php +++ b/lib/Controller/UserApiController.php @@ -8,21 +8,18 @@ use OCP\Collaboration\Collaborators\ISearch; use \OCP\IRequest; use OCP\IUserManager; -use OCP\IUserSession; use OCP\Share\IShare; class UserApiController extends ApiController { private ISearch $collaboratorSearch; - private IUserSession $userSession; private IUserManager $userManager; private SessionService $sessionService; - public function __construct($appName, IRequest $request, SessionService $sessionService, ISearch $ISearch, IUserManager $userManager, IUserSession $userSession) { + public function __construct($appName, IRequest $request, SessionService $sessionService, ISearch $ISearch, IUserManager $userManager) { parent::__construct($appName, $request); $this->sessionService = $sessionService; $this->collaboratorSearch = $ISearch; - $this->userSession = $userSession; $this->userManager = $userManager; } @@ -43,7 +40,10 @@ public function index(int $documentId, int $sessionId, string $sessionToken, str foreach ($sessions as $session) { $sessionUserId = $session['userId']; if ($sessionUserId !== null && !isset($users[$sessionUserId])) { - $users[$sessionUserId] = $this->userManager->getDisplayName($sessionUserId); + $displayName = $this->userManager->getDisplayName($sessionUserId); + if (stripos($displayName, $filter) !== false) { + $users[$sessionUserId] = $displayName; + } } } @@ -51,16 +51,14 @@ public function index(int $documentId, int $sessionId, string $sessionToken, str if ($currentSession->getUserId() !== null) { // Add other users to the autocomplete list [$result] = $this->collaboratorSearch->search($filter, [IShare::TYPE_USER], false, $limit, 0); + $userSearch = array_merge($result['users'], $result['exact']['users']); - foreach ($result['users'] as ['label' => $label, 'value' => $value]) { + foreach ($userSearch as ['label' => $label, 'value' => $value]) { if (isset($value['shareWith'])) { $id = $value['shareWith']; $users[$id] = $label; } } - - $user = $this->userSession->getUser(); - $users[$user->getUID()] = $user->getDisplayName(); } return new DataResponse($users); diff --git a/tests/unit/Controller/UserApiControllerTest.php b/tests/unit/Controller/UserApiControllerTest.php new file mode 100644 index 00000000000..96462edc824 --- /dev/null +++ b/tests/unit/Controller/UserApiControllerTest.php @@ -0,0 +1,116 @@ +request = $this->createMock(IRequest::class); + $this->collaboratorSearch = $this->createMock(ISearch::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->sessionService = $this->createMock(SessionService::class); + $this->userApiController = new UserApiController( + 'text', + $this->request, + $this->sessionService, + $this->collaboratorSearch, + $this->userManager, + $this->userSession + ); + } + + /** + * @dataProvider dataTestUsersIndex + */ + public function testUsersIndex(int $documentId, int $sessionId, string $sessionToken, string $filter) { + $session = new Session(); + $session->setUserId('admin'); + $this->sessionService + ->expects($this->once()) + ->method('isValidSession')->willReturn(true); + $this->sessionService + ->expects($this->once()) + ->method('getAllSessions')->willReturn([[ + 'userId' => 'admin', + 'displayName' => 'admin', + ]]); + $this->sessionService + ->expects($this->once()) + ->method('getSession')->willReturn($session); + $this->collaboratorSearch + ->expects($this->once()) + ->method('search')->willReturn([ + [ + 'exact' => [ + 'users' => [] + ], + 'users' => [ + [ + 'label' => 'admin', + 'subline' => '', + 'icon' => 'icon-user', + 'value' => [ + 'shareType' => 0, + 'shareWith' => 'admin' + ], + 'shareWithDisplayNameUnique' => 'admin', + 'status' => [] + ], + ] + ] + ]); + + $response = $this->userApiController->index( + $documentId, + $sessionId, + $sessionToken, + $filter + ); + $this->assertInstanceOf(DataResponse::class, $response); + $this->assertIsArray($response->getData()); + $this->assertSame(['admin' => 'admin'], $response->getData()); + } + + public function dataTestUsersIndex() { + return [ + [103, 44, 'token1', 'admin'], + [103, 44, 'token2', 'admin'], + [103, 44, 'token3', 'admin'], + ]; + } +}