Skip to content

Commit

Permalink
Merge pull request #3143 from nextcloud/backport/3068/stable25
Browse files Browse the repository at this point in the history
[stable25] fix: mention search issues
  • Loading branch information
juliusknorr authored Oct 11, 2022
2 parents f3cd0fb + 30c68a4 commit 524dd33
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 9 deletions.
16 changes: 7 additions & 9 deletions lib/Controller/UserApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -43,24 +40,25 @@ 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;
}
}
}

$currentSession = $this->sessionService->getSession($documentId, $sessionId, $sessionToken);
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);
Expand Down
116 changes: 116 additions & 0 deletions tests/unit/Controller/UserApiControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace OCA\Text\Controller;

use OCA\Text\Service\SessionService;
use OCP\Collaboration\Collaborators\ISearch;
use OCP\AppFramework\Http\DataResponse;
use \OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
use OCA\Text\Db\Session;
use Test\TestCase;

class UserApiControllerTest extends TestCase {
/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $collaboratorSearch;
/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $userSession;
/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $userManager;
/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $sessionService;
/**
* @var UserApiController
*/
private $userApiController;
/**
* @var IRequest|\PHPUnit\Framework\MockObject\MockObject
*/
private $request;

protected function setUp(): void {
parent::setUp();
$this->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'],
];
}
}

0 comments on commit 524dd33

Please sign in to comment.