Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable25] fix: mention search issues #3143

Merged
merged 4 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'],
];
}
}