123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace Test\Collaboration\Collaborators;
- use OC\Collaboration\Collaborators\SearchResult;
- use OC\Collaboration\Collaborators\UserPlugin;
- use OC\KnownUser\KnownUserService;
- use OCP\Collaboration\Collaborators\ISearchResult;
- use OCP\IConfig;
- use OCP\IGroup;
- use OCP\IGroupManager;
- use OCP\IUser;
- use OCP\IUserManager;
- use OCP\IUserSession;
- use OCP\Share\IShare;
- use OCP\UserStatus\IManager as IUserStatusManager;
- use PHPUnit\Framework\MockObject\MockObject;
- use Test\TestCase;
- class UserPluginTest extends TestCase {
- /** @var IConfig|MockObject */
- protected $config;
- /** @var IUserManager|MockObject */
- protected $userManager;
- /** @var IGroupManager|MockObject */
- protected $groupManager;
- /** @var IUserSession|MockObject */
- protected $session;
- /** @var KnownUserService|MockObject */
- protected $knownUserService;
- /** @var IUserStatusManager|MockObject */
- protected $userStatusManager;
- /** @var UserPlugin */
- protected $plugin;
- /** @var ISearchResult */
- protected $searchResult;
- protected int $limit = 2;
- protected int $offset = 0;
- /** @var IUser|MockObject */
- protected $user;
- protected function setUp(): void {
- parent::setUp();
- $this->config = $this->createMock(IConfig::class);
- $this->userManager = $this->createMock(IUserManager::class);
- $this->groupManager = $this->createMock(IGroupManager::class);
- $this->session = $this->createMock(IUserSession::class);
- $this->knownUserService = $this->createMock(KnownUserService::class);
- $this->userStatusManager = $this->createMock(IUserStatusManager::class);
- $this->searchResult = new SearchResult();
- $this->user = $this->getUserMock('admin', 'Administrator');
- }
- public function instantiatePlugin() {
- // cannot be done within setUp, because dependent mocks needs to be set
- // up with configuration etc. first
- $this->plugin = new UserPlugin(
- $this->config,
- $this->userManager,
- $this->groupManager,
- $this->session,
- $this->knownUserService,
- $this->userStatusManager
- );
- }
- public function mockConfig($mockedSettings) {
- $this->config->expects($this->any())
- ->method('getAppValue')
- ->willReturnCallback(
- function ($appName, $key, $default) use ($mockedSettings) {
- return $mockedSettings[$appName][$key] ?? $default;
- }
- );
- }
- public function getUserMock($uid, $displayName, $enabled = true, $groups = []) {
- $user = $this->createMock(IUser::class);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn($uid);
- $user->expects($this->any())
- ->method('getDisplayName')
- ->willReturn($displayName);
- $user->expects($this->any())
- ->method('isEnabled')
- ->willReturn($enabled);
- return $user;
- }
- public function getGroupMock($gid) {
- $group = $this->createMock(IGroup::class);
- $group->expects($this->any())
- ->method('getGID')
- ->willReturn($gid);
- return $group;
- }
- public function dataGetUsers() {
- return [
- ['test', false, true, [], [], [], [], true, false],
- ['test', false, false, [], [], [], [], true, false],
- ['test', true, true, [], [], [], [], true, false],
- ['test', true, false, [], [], [], [], true, false],
- [
- 'test', false, true, [], [],
- [
- ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
- ], [], true, $this->getUserMock('test', 'Test'),
- ],
- [
- 'test', false, false, [], [],
- [
- ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
- ], [], true, $this->getUserMock('test', 'Test'),
- ],
- [
- 'test', true, true, [], [],
- [], [], true, $this->getUserMock('test', 'Test'),
- ],
- [
- 'test', true, false, [], [],
- [], [], true, $this->getUserMock('test', 'Test'),
- ],
- [
- 'test', true, true, ['test-group'], [['test-group', 'test', 2, 0, []]],
- [
- ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
- ], [], true, $this->getUserMock('test', 'Test'),
- ],
- [
- 'test', true, false, ['test-group'], [['test-group', 'test', 2, 0, []]],
- [
- ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
- ], [], true, $this->getUserMock('test', 'Test'),
- ],
- [
- 'test',
- false,
- true,
- [],
- [
- $this->getUserMock('test1', 'Test One'),
- ],
- [],
- [
- ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
- ],
- true,
- false,
- ],
- [
- 'test',
- false,
- false,
- [],
- [
- $this->getUserMock('test1', 'Test One'),
- ],
- [],
- [],
- true,
- false,
- ],
- [
- 'test',
- false,
- true,
- [],
- [
- $this->getUserMock('test1', 'Test One'),
- $this->getUserMock('test2', 'Test Two'),
- ],
- [],
- [
- ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
- ['label' => 'Test Two', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test2'],
- ],
- false,
- false,
- ],
- [
- 'test',
- false,
- false,
- [],
- [
- $this->getUserMock('test1', 'Test One'),
- $this->getUserMock('test2', 'Test Two'),
- ],
- [],
- [],
- true,
- false,
- ],
- [
- 'test',
- false,
- true,
- [],
- [
- $this->getUserMock('test0', 'Test'),
- $this->getUserMock('test1', 'Test One'),
- $this->getUserMock('test2', 'Test Two'),
- ],
- [
- ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
- ],
- [
- ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
- ['label' => 'Test Two', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test2'],
- ],
- false,
- false,
- ],
- [
- 'test',
- false,
- true,
- [],
- [
- $this->getUserMock('test0', 'Test'),
- $this->getUserMock('test1', 'Test One'),
- $this->getUserMock('test2', 'Test Two'),
- ],
- [
- ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
- ],
- [
- ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
- ['label' => 'Test Two', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test2'],
- ],
- false,
- false,
- [],
- true,
- ],
- [
- 'test',
- false,
- false,
- [],
- [
- $this->getUserMock('test0', 'Test'),
- $this->getUserMock('test1', 'Test One'),
- $this->getUserMock('test2', 'Test Two'),
- ],
- [
- ['label' => 'Test', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test0'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test0'],
- ],
- [],
- true,
- false,
- ],
- [
- 'test',
- true,
- true,
- ['abc', 'xyz'],
- [
- ['abc', 'test', 2, 0, ['test1' => 'Test One']],
- ['xyz', 'test', 2, 0, []],
- ],
- [],
- [
- ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
- ],
- true,
- false,
- [['test1', $this->getUserMock('test1', 'Test One')]],
- ],
- [
- 'test',
- true,
- false,
- ['abc', 'xyz'],
- [
- ['abc', 'test', 2, 0, ['test1' => 'Test One']],
- ['xyz', 'test', 2, 0, []],
- ],
- [],
- [],
- true,
- false,
- [['test1', $this->getUserMock('test1', 'Test One')]],
- ],
- [
- 'test',
- true,
- true,
- ['abc', 'xyz'],
- [
- ['abc', 'test', 2, 0, [
- 'test1' => 'Test One',
- 'test2' => 'Test Two',
- ]],
- ['xyz', 'test', 2, 0, [
- 'test1' => 'Test One',
- 'test2' => 'Test Two',
- ]],
- ],
- [],
- [
- ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test1'],
- ['label' => 'Test Two', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test2'],
- ],
- true,
- false,
- [
- ['test1', $this->getUserMock('test1', 'Test One')],
- ['test2', $this->getUserMock('test2', 'Test Two')],
- ],
- ],
- [
- 'test',
- true,
- false,
- ['abc', 'xyz'],
- [
- ['abc', 'test', 2, 0, [
- 'test1' => 'Test One',
- 'test2' => 'Test Two',
- ]],
- ['xyz', 'test', 2, 0, [
- 'test1' => 'Test One',
- 'test2' => 'Test Two',
- ]],
- ],
- [],
- [],
- true,
- false,
- [
- ['test1', $this->getUserMock('test1', 'Test One')],
- ['test2', $this->getUserMock('test2', 'Test Two')],
- ],
- ],
- [
- 'test',
- true,
- true,
- ['abc', 'xyz'],
- [
- ['abc', 'test', 2, 0, [
- 'test' => 'Test One',
- ]],
- ['xyz', 'test', 2, 0, [
- 'test2' => 'Test Two',
- ]],
- ],
- [
- ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
- ],
- [
- ['label' => 'Test Two', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test2'],
- ],
- false,
- false,
- [
- ['test', $this->getUserMock('test', 'Test One')],
- ['test2', $this->getUserMock('test2', 'Test Two')],
- ],
- ],
- [
- 'test',
- true,
- false,
- ['abc', 'xyz'],
- [
- ['abc', 'test', 2, 0, [
- 'test' => 'Test One',
- ]],
- ['xyz', 'test', 2, 0, [
- 'test2' => 'Test Two',
- ]],
- ],
- [
- ['label' => 'Test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test'], 'icon' => 'icon-user', 'subline' => null, 'status' => [], 'shareWithDisplayNameUnique' => 'test'],
- ],
- [],
- true,
- false,
- [
- ['test', $this->getUserMock('test', 'Test One')],
- ['test2', $this->getUserMock('test2', 'Test Two')],
- ],
- ],
- ];
- }
- /**
- * @dataProvider dataGetUsers
- *
- * @param string $searchTerm
- * @param bool $shareWithGroupOnly
- * @param bool $shareeEnumeration
- * @param array $groupResponse
- * @param array $userResponse
- * @param array $exactExpected
- * @param array $expected
- * @param bool $reachedEnd
- * @param bool|IUser $singleUser
- * @param array $users
- */
- public function testSearch(
- $searchTerm,
- $shareWithGroupOnly,
- $shareeEnumeration,
- array $groupResponse,
- array $userResponse,
- array $exactExpected,
- array $expected,
- $reachedEnd,
- $singleUser,
- array $users = [],
- $shareeEnumerationPhone = false,
- ): void {
- $this->mockConfig(['core' => [
- 'shareapi_only_share_with_group_members' => $shareWithGroupOnly ? 'yes' : 'no',
- 'shareapi_allow_share_dialog_user_enumeration' => $shareeEnumeration? 'yes' : 'no',
- 'shareapi_restrict_user_enumeration_to_group' => false ? 'yes' : 'no',
- 'shareapi_restrict_user_enumeration_to_phone' => $shareeEnumerationPhone ? 'yes' : 'no',
- ]]);
- $this->instantiatePlugin();
- $this->session->expects($this->any())
- ->method('getUser')
- ->willReturn($this->user);
- if (!$shareWithGroupOnly) {
- if ($shareeEnumerationPhone) {
- $this->userManager->expects($this->once())
- ->method('searchKnownUsersByDisplayName')
- ->with($this->user->getUID(), $searchTerm, $this->limit, $this->offset)
- ->willReturn($userResponse);
- $this->knownUserService->method('isKnownToUser')
- ->willReturnMap([
- [$this->user->getUID(), 'test0', true],
- [$this->user->getUID(), 'test1', true],
- [$this->user->getUID(), 'test2', true],
- ]);
- } else {
- $this->userManager->expects($this->once())
- ->method('searchDisplayName')
- ->with($searchTerm, $this->limit, $this->offset)
- ->willReturn($userResponse);
- }
- } else {
- $this->groupManager->method('getUserGroupIds')
- ->with($this->user)
- ->willReturn($groupResponse);
- if ($singleUser !== false) {
- $this->groupManager->method('getUserGroupIds')
- ->with($singleUser)
- ->willReturn($groupResponse);
- }
- $this->groupManager->method('displayNamesInGroup')
- ->willReturnMap($userResponse);
- }
- if ($singleUser !== false) {
- $users[] = [$searchTerm, $singleUser];
- }
- if (!empty($users)) {
- $this->userManager->expects($this->atLeastOnce())
- ->method('get')
- ->willReturnMap($users);
- }
- $moreResults = $this->plugin->search($searchTerm, $this->limit, $this->offset, $this->searchResult);
- $result = $this->searchResult->asArray();
- $this->assertEquals($exactExpected, $result['exact']['users']);
- $this->assertEquals($expected, $result['users']);
- $this->assertSame($reachedEnd, $moreResults);
- }
- public function takeOutCurrentUserProvider() {
- $inputUsers = [
- 'alice' => 'Alice',
- 'bob' => 'Bob',
- 'carol' => 'Carol',
- ];
- return [
- [
- $inputUsers,
- ['alice', 'carol'],
- 'bob',
- ],
- [
- $inputUsers,
- ['alice', 'bob', 'carol'],
- 'dave',
- ],
- [
- $inputUsers,
- ['alice', 'bob', 'carol'],
- null,
- ],
- ];
- }
- /**
- * @dataProvider takeOutCurrentUserProvider
- * @param array $users
- * @param array $expectedUIDs
- * @param $currentUserId
- */
- public function testTakeOutCurrentUser(array $users, array $expectedUIDs, $currentUserId): void {
- $this->instantiatePlugin();
- $this->session->expects($this->once())
- ->method('getUser')
- ->willReturnCallback(function () use ($currentUserId) {
- if ($currentUserId !== null) {
- return $this->getUserMock($currentUserId, $currentUserId);
- }
- return null;
- });
- $this->plugin->takeOutCurrentUser($users);
- $this->assertSame($expectedUIDs, array_keys($users));
- }
- public function dataSearchEnumeration() {
- return [
- [
- 'test',
- ['groupA'],
- [
- ['uid' => 'test1', 'groups' => ['groupA']],
- ['uid' => 'test2', 'groups' => ['groupB']],
- ],
- ['exact' => [], 'wide' => ['test1']],
- ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
- ],
- [
- 'test',
- ['groupA'],
- [
- ['uid' => 'test1', 'displayName' => 'Test user 1', 'groups' => ['groupA']],
- ['uid' => 'test2', 'displayName' => 'Test user 2', 'groups' => ['groupA']],
- ],
- ['exact' => [], 'wide' => []],
- ['core' => ['shareapi_allow_share_dialog_user_enumeration' => 'no']],
- ],
- [
- 'test1',
- ['groupA'],
- [
- ['uid' => 'test1', 'displayName' => 'Test user 1', 'groups' => ['groupA']],
- ['uid' => 'test2', 'displayName' => 'Test user 2', 'groups' => ['groupA']],
- ],
- ['exact' => ['test1'], 'wide' => []],
- ['core' => ['shareapi_allow_share_dialog_user_enumeration' => 'no']],
- ],
- [
- 'test1',
- ['groupA'],
- [
- ['uid' => 'test1', 'displayName' => 'Test user 1', 'groups' => ['groupA']],
- ['uid' => 'test2', 'displayName' => 'Test user 2', 'groups' => ['groupA']],
- ],
- ['exact' => [], 'wide' => []],
- [
- 'core' => [
- 'shareapi_allow_share_dialog_user_enumeration' => 'no',
- 'shareapi_restrict_user_enumeration_full_match_userid' => 'no',
- ],
- ]
- ],
- [
- 'Test user 1',
- ['groupA'],
- [
- ['uid' => 'test1', 'displayName' => 'Test user 1', 'groups' => ['groupA']],
- ['uid' => 'test2', 'displayName' => 'Test user 2', 'groups' => ['groupA']],
- ],
- ['exact' => ['test1'], 'wide' => []],
- [
- 'core' => [
- 'shareapi_allow_share_dialog_user_enumeration' => 'no',
- 'shareapi_restrict_user_enumeration_full_match_userid' => 'no',
- ],
- ]
- ],
- [
- 'Test user 1',
- ['groupA'],
- [
- ['uid' => 'test1', 'displayName' => 'Test user 1 (Second displayName for user 1)', 'groups' => ['groupA']],
- ['uid' => 'test2', 'displayName' => 'Test user 2 (Second displayName for user 2)', 'groups' => ['groupA']],
- ],
- ['exact' => [], 'wide' => []],
- ['core' => ['shareapi_allow_share_dialog_user_enumeration' => 'no'],
- ]
- ],
- [
- 'Test user 1',
- ['groupA'],
- [
- ['uid' => 'test1', 'displayName' => 'Test user 1 (Second displayName for user 1)', 'groups' => ['groupA']],
- ['uid' => 'test2', 'displayName' => 'Test user 2 (Second displayName for user 2)', 'groups' => ['groupA']],
- ],
- ['exact' => ['test1'], 'wide' => []],
- [
- 'core' => [
- 'shareapi_allow_share_dialog_user_enumeration' => 'no',
- 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn' => 'yes',
- ],
- ]
- ],
- [
- 'test1',
- ['groupA'],
- [
- ['uid' => 'test1', 'groups' => ['groupA']],
- ['uid' => 'test2', 'groups' => ['groupB']],
- ],
- ['exact' => ['test1'], 'wide' => []],
- ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
- ],
- [
- 'test',
- ['groupA'],
- [
- ['uid' => 'test1', 'groups' => ['groupA']],
- ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
- ],
- ['exact' => [], 'wide' => ['test1', 'test2']],
- ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
- ],
- [
- 'test',
- ['groupA'],
- [
- ['uid' => 'test1', 'groups' => ['groupA', 'groupC']],
- ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
- ],
- ['exact' => [], 'wide' => ['test1', 'test2']],
- ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
- ],
- [
- 'test',
- ['groupC', 'groupB'],
- [
- ['uid' => 'test1', 'groups' => ['groupA', 'groupC']],
- ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
- ],
- ['exact' => [], 'wide' => ['test1', 'test2']],
- ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
- ],
- [
- 'test',
- [],
- [
- ['uid' => 'test1', 'groups' => ['groupA']],
- ['uid' => 'test2', 'groups' => ['groupB', 'groupA']],
- ],
- ['exact' => [], 'wide' => []],
- ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
- ],
- [
- 'test',
- ['groupC', 'groupB'],
- [
- ['uid' => 'test1', 'groups' => []],
- ['uid' => 'test2', 'groups' => []],
- ],
- ['exact' => [], 'wide' => []],
- ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
- ],
- [
- 'test',
- ['groupC', 'groupB'],
- [
- ['uid' => 'test1', 'groups' => []],
- ['uid' => 'test2', 'groups' => []],
- ],
- ['exact' => [], 'wide' => []],
- ['core' => ['shareapi_restrict_user_enumeration_to_group' => 'yes']],
- ],
- ];
- }
- /**
- * @dataProvider dataSearchEnumeration
- */
- public function testSearchEnumerationLimit($search, $userGroups, $matchingUsers, $result, $mockedSettings): void {
- $this->mockConfig($mockedSettings);
- $userResults = [];
- foreach ($matchingUsers as $user) {
- $userResults[$user['uid']] = $user['uid'];
- }
- $usersById = [];
- foreach ($matchingUsers as $user) {
- $usersById[$user['uid']] = $user;
- }
- $mappedResultExact = array_map(function ($user) use ($usersById, $search) {
- return [
- 'label' => $search === $user ? $user : $usersById[$user]['displayName'],
- 'value' => ['shareType' => 0, 'shareWith' => $user],
- 'icon' => 'icon-user',
- 'subline' => null,
- 'status' => [],
- 'shareWithDisplayNameUnique' => $user,
- ];
- }, $result['exact']);
- $mappedResultWide = array_map(function ($user) {
- return [
- 'label' => $user,
- 'value' => ['shareType' => 0, 'shareWith' => $user],
- 'icon' => 'icon-user',
- 'subline' => null,
- 'status' => [],
- 'shareWithDisplayNameUnique' => $user,
- ];
- }, $result['wide']);
- $this->userManager
- ->method('get')
- ->willReturnCallback(function ($userId) use ($userResults) {
- if (isset($userResults[$userId])) {
- return $this->getUserMock($userId, $userId);
- }
- return null;
- });
- $this->userManager
- ->method('searchDisplayName')
- ->willReturnCallback(function ($search) use ($matchingUsers) {
- $users = array_filter(
- $matchingUsers,
- fn ($user) => str_contains(strtolower($user['displayName']), strtolower($search))
- );
- return array_map(
- fn ($user) => $this->getUserMock($user['uid'], $user['displayName']),
- $users);
- });
- $this->groupManager->method('displayNamesInGroup')
- ->willReturn($userResults);
- $this->session->expects($this->any())
- ->method('getUser')
- ->willReturn($this->getUserMock('test', 'foo'));
- $this->groupManager->expects($this->any())
- ->method('getUserGroupIds')
- ->willReturnCallback(function ($user) use ($matchingUsers, $userGroups) {
- static $firstCall = true;
- if ($firstCall) {
- $firstCall = false;
- // current user
- return $userGroups;
- }
- $neededObject = array_filter(
- $matchingUsers,
- function ($e) use ($user) {
- return $user->getUID() === $e['uid'];
- }
- );
- if (count($neededObject) > 0) {
- return array_shift($neededObject)['groups'];
- }
- return [];
- });
- $this->instantiatePlugin();
- $this->plugin->search($search, $this->limit, $this->offset, $this->searchResult);
- $result = $this->searchResult->asArray();
- $this->assertEquals($mappedResultExact, $result['exact']['users']);
- $this->assertEquals($mappedResultWide, $result['users']);
- }
- }
|