123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-only
- */
- namespace OCA\DAV\Tests\unit\Connector\Sabre;
- use OC\KnownUser\KnownUserService;
- use OC\User\User;
- use OCA\DAV\CalDAV\Proxy\Proxy;
- use OCA\DAV\CalDAV\Proxy\ProxyMapper;
- use OCA\DAV\Connector\Sabre\Principal;
- use OCP\Accounts\IAccount;
- use OCP\Accounts\IAccountManager;
- use OCP\Accounts\IAccountProperty;
- use OCP\Accounts\IAccountPropertyCollection;
- use OCP\App\IAppManager;
- use OCP\IConfig;
- use OCP\IGroup;
- use OCP\IGroupManager;
- use OCP\IUser;
- use OCP\IUserManager;
- use OCP\IUserSession;
- use OCP\L10N\IFactory;
- use OCP\Share\IManager;
- use PHPUnit\Framework\MockObject\MockObject;
- use Sabre\DAV\Exception;
- use Sabre\DAV\PropPatch;
- use Test\TestCase;
- class PrincipalTest extends TestCase {
- /** @var IUserManager | MockObject */
- private $userManager;
- /** @var Principal */
- private $connector;
- /** @var IGroupManager | MockObject */
- private $groupManager;
- /** @var IAccountManager|MockObject */
- private $accountManager;
- /** @var IManager | MockObject */
- private $shareManager;
- /** @var IUserSession | MockObject */
- private $userSession;
- /** @var IAppManager | MockObject */
- private $appManager;
- /** @var ProxyMapper | MockObject */
- private $proxyMapper;
- /** @var KnownUserService|MockObject */
- private $knownUserService;
- /** @var IConfig | MockObject */
- private $config;
- /** @var IFactory|MockObject */
- private $languageFactory;
- protected function setUp(): void {
- $this->userManager = $this->createMock(IUserManager::class);
- $this->groupManager = $this->createMock(IGroupManager::class);
- $this->accountManager = $this->createMock(IAccountManager::class);
- $this->shareManager = $this->createMock(IManager::class);
- $this->userSession = $this->createMock(IUserSession::class);
- $this->appManager = $this->createMock(IAppManager::class);
- $this->proxyMapper = $this->createMock(ProxyMapper::class);
- $this->knownUserService = $this->createMock(KnownUserService::class);
- $this->config = $this->createMock(IConfig::class);
- $this->languageFactory = $this->createMock(IFactory::class);
- $this->connector = new Principal(
- $this->userManager,
- $this->groupManager,
- $this->accountManager,
- $this->shareManager,
- $this->userSession,
- $this->appManager,
- $this->proxyMapper,
- $this->knownUserService,
- $this->config,
- $this->languageFactory
- );
- parent::setUp();
- }
- public function testGetPrincipalsByPrefixWithoutPrefix(): void {
- $response = $this->connector->getPrincipalsByPrefix('');
- $this->assertSame([], $response);
- }
- public function testGetPrincipalsByPrefixWithUsers(): void {
- $fooUser = $this->createMock(User::class);
- $fooUser
- ->expects($this->once())
- ->method('getUID')
- ->willReturn('foo');
- $fooUser
- ->expects($this->once())
- ->method('getDisplayName')
- ->willReturn('Dr. Foo-Bar');
- $fooUser
- ->expects($this->once())
- ->method('getSystemEMailAddress')
- ->willReturn('');
- $barUser = $this->createMock(User::class);
- $barUser
- ->expects($this->once())
- ->method('getUID')
- ->willReturn('bar');
- $barUser
- ->expects($this->once())
- ->method('getSystemEMailAddress')
- ->willReturn('bar@nextcloud.com');
- $this->userManager
- ->expects($this->once())
- ->method('search')
- ->with('')
- ->willReturn([$fooUser, $barUser]);
- $this->languageFactory
- ->expects($this->exactly(2))
- ->method('getUserLanguage')
- ->withConsecutive([$fooUser], [$barUser])
- ->willReturnOnConsecutiveCalls('de', 'en');
- $fooAccountPropertyCollection = $this->createMock(IAccountPropertyCollection::class);
- $fooAccountPropertyCollection->expects($this->once())
- ->method('getProperties')
- ->with()
- ->willReturn([]);
- $fooAccount = $this->createMock(IAccount::class);
- $fooAccount->expects($this->once())
- ->method('getPropertyCollection')
- ->with(IAccountManager::COLLECTION_EMAIL)
- ->willReturn($fooAccountPropertyCollection);
- $emailPropertyOne = $this->createMock(IAccountProperty::class);
- $emailPropertyOne->expects($this->once())
- ->method('getValue')
- ->with()
- ->willReturn('alias@nextcloud.com');
- $emailPropertyTwo = $this->createMock(IAccountProperty::class);
- $emailPropertyTwo->expects($this->once())
- ->method('getValue')
- ->with()
- ->willReturn('alias2@nextcloud.com');
- $barAccountPropertyCollection = $this->createMock(IAccountPropertyCollection::class);
- $barAccountPropertyCollection->expects($this->once())
- ->method('getProperties')
- ->with()
- ->willReturn([$emailPropertyOne, $emailPropertyTwo]);
- $barAccount = $this->createMock(IAccount::class);
- $barAccount->expects($this->once())
- ->method('getPropertyCollection')
- ->with(IAccountManager::COLLECTION_EMAIL)
- ->willReturn($barAccountPropertyCollection);
- $this->accountManager
- ->expects($this->exactly(2))
- ->method('getAccount')
- ->withConsecutive([$fooUser], [$barUser])
- ->willReturnOnConsecutiveCalls($fooAccount, $barAccount);
- $expectedResponse = [
- 0 => [
- 'uri' => 'principals/users/foo',
- '{DAV:}displayname' => 'Dr. Foo-Bar',
- '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
- '{http://nextcloud.com/ns}language' => 'de',
- ],
- 1 => [
- 'uri' => 'principals/users/bar',
- '{DAV:}displayname' => 'bar',
- '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
- '{http://nextcloud.com/ns}language' => 'en',
- '{http://sabredav.org/ns}email-address' => 'bar@nextcloud.com',
- '{DAV:}alternate-URI-set' => ['mailto:alias@nextcloud.com', 'mailto:alias2@nextcloud.com']
- ]
- ];
- $response = $this->connector->getPrincipalsByPrefix('principals/users');
- $this->assertSame($expectedResponse, $response);
- }
- public function testGetPrincipalsByPrefixEmpty(): void {
- $this->userManager
- ->expects($this->once())
- ->method('search')
- ->with('')
- ->willReturn([]);
- $response = $this->connector->getPrincipalsByPrefix('principals/users');
- $this->assertSame([], $response);
- }
- public function testGetPrincipalsByPathWithoutMail(): void {
- $fooUser = $this->createMock(User::class);
- $fooUser
- ->expects($this->once())
- ->method('getUID')
- ->willReturn('foo');
- $this->userManager
- ->expects($this->once())
- ->method('get')
- ->with('foo')
- ->willReturn($fooUser);
- $this->languageFactory
- ->expects($this->once())
- ->method('getUserLanguage')
- ->with($fooUser)
- ->willReturn('de');
- $expectedResponse = [
- 'uri' => 'principals/users/foo',
- '{DAV:}displayname' => 'foo',
- '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
- '{http://nextcloud.com/ns}language' => 'de'
- ];
- $response = $this->connector->getPrincipalByPath('principals/users/foo');
- $this->assertSame($expectedResponse, $response);
- }
- public function testGetPrincipalsByPathWithMail(): void {
- $fooUser = $this->createMock(User::class);
- $fooUser
- ->expects($this->once())
- ->method('getSystemEMailAddress')
- ->willReturn('foo@nextcloud.com');
- $fooUser
- ->expects($this->once())
- ->method('getUID')
- ->willReturn('foo');
- $this->userManager
- ->expects($this->once())
- ->method('get')
- ->with('foo')
- ->willReturn($fooUser);
- $this->languageFactory
- ->expects($this->once())
- ->method('getUserLanguage')
- ->with($fooUser)
- ->willReturn('de');
- $expectedResponse = [
- 'uri' => 'principals/users/foo',
- '{DAV:}displayname' => 'foo',
- '{urn:ietf:params:xml:ns:caldav}calendar-user-type' => 'INDIVIDUAL',
- '{http://nextcloud.com/ns}language' => 'de',
- '{http://sabredav.org/ns}email-address' => 'foo@nextcloud.com',
- ];
- $response = $this->connector->getPrincipalByPath('principals/users/foo');
- $this->assertSame($expectedResponse, $response);
- }
- public function testGetPrincipalsByPathEmpty(): void {
- $this->userManager
- ->expects($this->once())
- ->method('get')
- ->with('foo')
- ->willReturn(null);
- $response = $this->connector->getPrincipalByPath('principals/users/foo');
- $this->assertNull($response);
- }
- public function testGetGroupMemberSet(): void {
- $response = $this->connector->getGroupMemberSet('principals/users/foo');
- $this->assertSame([], $response);
- }
- public function testGetGroupMemberSetEmpty(): void {
- $this->expectException(Exception::class);
- $this->expectExceptionMessage('Principal not found');
- $this->userManager
- ->expects($this->once())
- ->method('get')
- ->with('foo')
- ->willReturn(null);
- $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-read');
- }
- public function testGetGroupMemberSetProxyRead(): void {
- $fooUser = $this->createMock(User::class);
- $fooUser
- ->expects($this->once())
- ->method('getUID')
- ->willReturn('foo');
- $this->userManager
- ->expects($this->once())
- ->method('get')
- ->with('foo')
- ->willReturn($fooUser);
- $proxy1 = new Proxy();
- $proxy1->setProxyId('proxyId1');
- $proxy1->setPermissions(1);
- $proxy2 = new Proxy();
- $proxy2->setProxyId('proxyId2');
- $proxy2->setPermissions(3);
- $proxy3 = new Proxy();
- $proxy3->setProxyId('proxyId3');
- $proxy3->setPermissions(3);
- $this->proxyMapper->expects($this->once())
- ->method('getProxiesOf')
- ->with('principals/users/foo')
- ->willReturn([$proxy1, $proxy2, $proxy3]);
- $this->assertEquals(['proxyId1'], $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-read'));
- }
- public function testGetGroupMemberSetProxyWrite(): void {
- $fooUser = $this->createMock(User::class);
- $fooUser
- ->expects($this->once())
- ->method('getUID')
- ->willReturn('foo');
- $this->userManager
- ->expects($this->once())
- ->method('get')
- ->with('foo')
- ->willReturn($fooUser);
- $proxy1 = new Proxy();
- $proxy1->setProxyId('proxyId1');
- $proxy1->setPermissions(1);
- $proxy2 = new Proxy();
- $proxy2->setProxyId('proxyId2');
- $proxy2->setPermissions(3);
- $proxy3 = new Proxy();
- $proxy3->setProxyId('proxyId3');
- $proxy3->setPermissions(3);
- $this->proxyMapper->expects($this->once())
- ->method('getProxiesOf')
- ->with('principals/users/foo')
- ->willReturn([$proxy1, $proxy2, $proxy3]);
- $this->assertEquals(['proxyId2', 'proxyId3'], $this->connector->getGroupMemberSet('principals/users/foo/calendar-proxy-write'));
- }
- public function testGetGroupMembership(): void {
- $fooUser = $this->createMock(User::class);
- $group1 = $this->createMock(IGroup::class);
- $group1->expects($this->once())
- ->method('getGID')
- ->willReturn('group1');
- $group2 = $this->createMock(IGroup::class);
- $group2->expects($this->once())
- ->method('getGID')
- ->willReturn('foo/bar');
- $this->userManager
- ->expects($this->exactly(2))
- ->method('get')
- ->with('foo')
- ->willReturn($fooUser);
- $this->groupManager
- ->expects($this->once())
- ->method('getUserGroups')
- ->with($fooUser)
- ->willReturn([
- $group1,
- $group2,
- ]);
- $proxy1 = new Proxy();
- $proxy1->setOwnerId('proxyId1');
- $proxy1->setPermissions(1);
- $proxy2 = new Proxy();
- $proxy2->setOwnerId('proxyId2');
- $proxy2->setPermissions(3);
- $this->proxyMapper->expects($this->once())
- ->method('getProxiesFor')
- ->with('principals/users/foo')
- ->willReturn([$proxy1, $proxy2]);
- $expectedResponse = [
- 'principals/groups/group1',
- 'principals/groups/foo%2Fbar',
- 'proxyId1/calendar-proxy-read',
- 'proxyId2/calendar-proxy-write',
- ];
- $response = $this->connector->getGroupMembership('principals/users/foo');
- $this->assertSame($expectedResponse, $response);
- }
- public function testGetGroupMembershipEmpty(): void {
- $this->expectException(Exception::class);
- $this->expectExceptionMessage('Principal not found');
- $this->userManager
- ->expects($this->once())
- ->method('get')
- ->with('foo')
- ->willReturn(null);
- $this->connector->getGroupMembership('principals/users/foo');
- }
- public function testSetGroupMembership(): void {
- $this->expectException(Exception::class);
- $this->expectExceptionMessage('Setting members of the group is not supported yet');
- $this->connector->setGroupMemberSet('principals/users/foo', ['foo']);
- }
- public function testSetGroupMembershipProxy(): void {
- $fooUser = $this->createMock(User::class);
- $fooUser
- ->expects($this->once())
- ->method('getUID')
- ->willReturn('foo');
- $barUser = $this->createMock(User::class);
- $barUser
- ->expects($this->once())
- ->method('getUID')
- ->willReturn('bar');
- $this->userManager
- ->expects($this->exactly(2))
- ->method('get')
- ->willReturnMap([
- ['foo', $fooUser],
- ['bar', $barUser],
- ]);
- $this->proxyMapper->expects($this->once())
- ->method('getProxiesOf')
- ->with('principals/users/foo')
- ->willReturn([]);
- $this->proxyMapper->expects($this->once())
- ->method('insert')
- ->with($this->callback(function ($proxy) {
- /** @var Proxy $proxy */
- if ($proxy->getOwnerId() !== 'principals/users/foo') {
- return false;
- }
- if ($proxy->getProxyId() !== 'principals/users/bar') {
- return false;
- }
- if ($proxy->getPermissions() !== 3) {
- return false;
- }
- return true;
- }));
- $this->connector->setGroupMemberSet('principals/users/foo/calendar-proxy-write', ['principals/users/bar']);
- }
- public function testUpdatePrincipal(): void {
- $this->assertSame(0, $this->connector->updatePrincipal('foo', new PropPatch([])));
- }
- public function testSearchPrincipalsWithEmptySearchProperties(): void {
- $this->assertSame([], $this->connector->searchPrincipals('principals/users', []));
- }
- public function testSearchPrincipalsWithWrongPrefixPath(): void {
- $this->assertSame([], $this->connector->searchPrincipals('principals/groups',
- ['{http://sabredav.org/ns}email-address' => 'foo']));
- }
- /**
- * @dataProvider searchPrincipalsDataProvider
- */
- public function testSearchPrincipals($sharingEnabled, $groupsOnly, $test, $result): void {
- $this->shareManager->expects($this->once())
- ->method('shareAPIEnabled')
- ->willReturn($sharingEnabled);
- $getUserGroupIdsReturnMap = [];
- if ($sharingEnabled) {
- $this->shareManager->expects($this->once())
- ->method('allowEnumeration')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('shareWithGroupMembersOnly')
- ->willReturn($groupsOnly);
- if ($groupsOnly) {
- $user = $this->createMock(IUser::class);
- $this->userSession->expects($this->atLeastOnce())
- ->method('getUser')
- ->willReturn($user);
- $getUserGroupIdsReturnMap[] = [$user, ['group1', 'group2', 'group5']];
- }
- } else {
- $this->config->expects($this->never())
- ->method('getAppValue');
- $this->shareManager->expects($this->never())
- ->method('shareWithGroupMembersOnly');
- $this->groupManager->expects($this->never())
- ->method($this->anything());
- }
- $user2 = $this->createMock(IUser::class);
- $user2->method('getUID')->willReturn('user2');
- $user3 = $this->createMock(IUser::class);
- $user3->method('getUID')->willReturn('user3');
- $user4 = $this->createMock(IUser::class);
- $user4->method('getUID')->willReturn('user4');
- if ($sharingEnabled) {
- $this->userManager->expects($this->once())
- ->method('getByEmail')
- ->with('user@example.com')
- ->willReturn([$user2, $user3]);
- $this->userManager->expects($this->once())
- ->method('searchDisplayName')
- ->with('User 12')
- ->willReturn([$user3, $user4]);
- } else {
- $this->userManager->expects($this->never())
- ->method('getByEmail');
- $this->userManager->expects($this->never())
- ->method('searchDisplayName');
- }
- if ($sharingEnabled && $groupsOnly) {
- $getUserGroupIdsReturnMap[] = [$user2, ['group1', 'group3']];
- $getUserGroupIdsReturnMap[] = [$user3, ['group3', 'group4']];
- $getUserGroupIdsReturnMap[] = [$user4, ['group4', 'group5']];
- }
- $this->groupManager->expects($this->any())
- ->method('getUserGroupIds')
- ->willReturnMap($getUserGroupIdsReturnMap);
- $this->assertEquals($result, $this->connector->searchPrincipals('principals/users',
- ['{http://sabredav.org/ns}email-address' => 'user@example.com',
- '{DAV:}displayname' => 'User 12'], $test));
- }
- public function searchPrincipalsDataProvider(): array {
- return [
- [true, false, 'allof', ['principals/users/user3']],
- [true, false, 'anyof', ['principals/users/user2', 'principals/users/user3', 'principals/users/user4']],
- [true, true, 'allof', []],
- [true, true, 'anyof', ['principals/users/user2', 'principals/users/user4']],
- [false, false, 'allof', []],
- [false, false, 'anyof', []],
- ];
- }
- public function testSearchPrincipalByCalendarUserAddressSet(): void {
- $this->shareManager->expects($this->exactly(2))
- ->method('shareAPIEnabled')
- ->willReturn(true);
- $this->shareManager->expects($this->exactly(2))
- ->method('allowEnumeration')
- ->willReturn(true);
- $this->shareManager->expects($this->exactly(2))
- ->method('shareWithGroupMembersOnly')
- ->willReturn(false);
- $user2 = $this->createMock(IUser::class);
- $user2->method('getUID')->willReturn('user2');
- $user3 = $this->createMock(IUser::class);
- $user3->method('getUID')->willReturn('user3');
- $this->userManager->expects($this->once())
- ->method('getByEmail')
- ->with('user@example.com')
- ->willReturn([$user2, $user3]);
- $this->assertEquals([
- 'principals/users/user2',
- 'principals/users/user3',
- ], $this->connector->searchPrincipals('principals/users',
- ['{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => 'user@example.com']));
- }
- public function testSearchPrincipalWithEnumerationDisabledDisplayname(): void {
- $this->shareManager->expects($this->once())
- ->method('shareAPIEnabled')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('allowEnumeration')
- ->willReturn(false);
- $this->shareManager->expects($this->once())
- ->method('shareWithGroupMembersOnly')
- ->willReturn(false);
- $this->shareManager->expects($this->once())
- ->method('allowEnumerationFullMatch')
- ->willReturn(true);
- $user2 = $this->createMock(IUser::class);
- $user2->method('getUID')->willReturn('user2');
- $user2->method('getDisplayName')->willReturn('User 2');
- $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar');
- $user3 = $this->createMock(IUser::class);
- $user3->method('getUID')->willReturn('user3');
- $user3->method('getDisplayName')->willReturn('User 22');
- $user3->method('getSystemEMailAddress')->willReturn('user2@foo.bar123');
- $user4 = $this->createMock(IUser::class);
- $user4->method('getUID')->willReturn('user4');
- $user4->method('getDisplayName')->willReturn('User 222');
- $user4->method('getSystemEMailAddress')->willReturn('user2@foo.bar456');
- $this->userManager->expects($this->once())
- ->method('searchDisplayName')
- ->with('User 2')
- ->willReturn([$user2, $user3, $user4]);
- $this->assertEquals(['principals/users/user2'], $this->connector->searchPrincipals('principals/users',
- ['{DAV:}displayname' => 'User 2']));
- }
- public function testSearchPrincipalWithEnumerationDisabledDisplaynameOnFullMatch(): void {
- $this->shareManager->expects($this->once())
- ->method('shareAPIEnabled')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('allowEnumeration')
- ->willReturn(false);
- $this->shareManager->expects($this->once())
- ->method('shareWithGroupMembersOnly')
- ->willReturn(false);
- $this->shareManager->expects($this->once())
- ->method('allowEnumerationFullMatch')
- ->willReturn(false);
- $this->assertEquals([], $this->connector->searchPrincipals('principals/users',
- ['{DAV:}displayname' => 'User 2']));
- }
- public function testSearchPrincipalWithEnumerationDisabledEmail(): void {
- $this->shareManager->expects($this->once())
- ->method('shareAPIEnabled')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('allowEnumeration')
- ->willReturn(false);
- $this->shareManager->expects($this->once())
- ->method('shareWithGroupMembersOnly')
- ->willReturn(false);
- $this->shareManager->expects($this->once())
- ->method('allowEnumerationFullMatch')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('matchEmail')
- ->willReturn(true);
- $user2 = $this->createMock(IUser::class);
- $user2->method('getUID')->willReturn('user2');
- $user2->method('getDisplayName')->willReturn('User 2');
- $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar');
- $user3 = $this->createMock(IUser::class);
- $user3->method('getUID')->willReturn('user3');
- $user2->method('getDisplayName')->willReturn('User 22');
- $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar123');
- $user4 = $this->createMock(IUser::class);
- $user4->method('getUID')->willReturn('user4');
- $user2->method('getDisplayName')->willReturn('User 222');
- $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar456');
- $this->userManager->expects($this->once())
- ->method('getByEmail')
- ->with('user2@foo.bar')
- ->willReturn([$user2]);
- $this->assertEquals(['principals/users/user2'], $this->connector->searchPrincipals('principals/users',
- ['{http://sabredav.org/ns}email-address' => 'user2@foo.bar']));
- }
- public function testSearchPrincipalWithEnumerationDisabledEmailOnFullMatch(): void {
- $this->shareManager->expects($this->once())
- ->method('shareAPIEnabled')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('allowEnumeration')
- ->willReturn(false);
- $this->shareManager->expects($this->once())
- ->method('shareWithGroupMembersOnly')
- ->willReturn(false);
- $this->shareManager->expects($this->once())
- ->method('allowEnumerationFullMatch')
- ->willReturn(false);
- $this->assertEquals([], $this->connector->searchPrincipals('principals/users',
- ['{http://sabredav.org/ns}email-address' => 'user2@foo.bar']));
- }
- public function testSearchPrincipalWithEnumerationLimitedDisplayname(): void {
- $this->shareManager->expects($this->once())
- ->method('shareAPIEnabled')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('allowEnumeration')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('limitEnumerationToGroups')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('shareWithGroupMembersOnly')
- ->willReturn(false);
- $user2 = $this->createMock(IUser::class);
- $user2->method('getUID')->willReturn('user2');
- $user2->method('getDisplayName')->willReturn('User 2');
- $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar');
- $user3 = $this->createMock(IUser::class);
- $user3->method('getUID')->willReturn('user3');
- $user3->method('getDisplayName')->willReturn('User 22');
- $user3->method('getSystemEMailAddress')->willReturn('user2@foo.bar123');
- $user4 = $this->createMock(IUser::class);
- $user4->method('getUID')->willReturn('user4');
- $user4->method('getDisplayName')->willReturn('User 222');
- $user4->method('getSystemEMailAddress')->willReturn('user2@foo.bar456');
- $this->userSession->expects($this->once())
- ->method('getUser')
- ->willReturn($user2);
- $this->groupManager->expects($this->exactly(4))
- ->method('getUserGroupIds')
- ->willReturnMap([
- [$user2, ['group1']],
- [$user3, ['group1']],
- [$user4, ['group2']],
- ]);
- $this->userManager->expects($this->once())
- ->method('searchDisplayName')
- ->with('User')
- ->willReturn([$user2, $user3, $user4]);
- $this->assertEquals([
- 'principals/users/user2',
- 'principals/users/user3',
- ], $this->connector->searchPrincipals('principals/users',
- ['{DAV:}displayname' => 'User']));
- }
- public function testSearchPrincipalWithEnumerationLimitedMail(): void {
- $this->shareManager->expects($this->once())
- ->method('shareAPIEnabled')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('allowEnumeration')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('limitEnumerationToGroups')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('shareWithGroupMembersOnly')
- ->willReturn(false);
- $user2 = $this->createMock(IUser::class);
- $user2->method('getUID')->willReturn('user2');
- $user2->method('getDisplayName')->willReturn('User 2');
- $user2->method('getSystemEMailAddress')->willReturn('user2@foo.bar');
- $user3 = $this->createMock(IUser::class);
- $user3->method('getUID')->willReturn('user3');
- $user3->method('getDisplayName')->willReturn('User 22');
- $user3->method('getSystemEMailAddress')->willReturn('user2@foo.bar123');
- $user4 = $this->createMock(IUser::class);
- $user4->method('getUID')->willReturn('user4');
- $user4->method('getDisplayName')->willReturn('User 222');
- $user4->method('getSystemEMailAddress')->willReturn('user2@foo.bar456');
- $this->userSession->expects($this->once())
- ->method('getUser')
- ->willReturn($user2);
- $this->groupManager->expects($this->exactly(4))
- ->method('getUserGroupIds')
- ->willReturnMap([
- [$user2, ['group1']],
- [$user3, ['group1']],
- [$user4, ['group2']],
- ]);
- $this->userManager->expects($this->once())
- ->method('getByEmail')
- ->with('user')
- ->willReturn([$user2, $user3, $user4]);
- $this->assertEquals([
- 'principals/users/user2',
- 'principals/users/user3'
- ], $this->connector->searchPrincipals('principals/users',
- ['{http://sabredav.org/ns}email-address' => 'user']));
- }
- public function testFindByUriSharingApiDisabled(): void {
- $this->shareManager->expects($this->once())
- ->method('shareApiEnabled')
- ->willReturn(false);
- $this->assertEquals(null, $this->connector->findByUri('mailto:user@foo.com', 'principals/users'));
- }
- /**
- * @dataProvider findByUriWithGroupRestrictionDataProvider
- */
- public function testFindByUriWithGroupRestriction($uri, $email, $expects): void {
- $this->shareManager->expects($this->once())
- ->method('shareApiEnabled')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('shareWithGroupMembersOnly')
- ->willReturn(true);
- $user = $this->createMock(IUser::class);
- $this->userSession->expects($this->once())
- ->method('getUser')
- ->willReturn($user);
- $user2 = $this->createMock(IUser::class);
- $user2->method('getUID')->willReturn('user2');
- $user3 = $this->createMock(IUser::class);
- $user3->method('getUID')->willReturn('user3');
- $this->userManager->expects($this->once())
- ->method('getByEmail')
- ->with($email)
- ->willReturn([$email === 'user2@foo.bar' ? $user2 : $user3]);
- if ($email === 'user2@foo.bar') {
- $this->groupManager->expects($this->exactly(2))
- ->method('getUserGroupIds')
- ->withConsecutive(
- [$user],
- [$user2],
- )
- ->willReturnOnConsecutiveCalls(
- ['group1', 'group2'],
- ['group1', 'group3'],
- );
- } else {
- $this->groupManager->expects($this->exactly(2))
- ->method('getUserGroupIds')
- ->withConsecutive(
- [$user],
- [$user3],
- )
- ->willReturnOnConsecutiveCalls(
- ['group1', 'group2'],
- ['group3', 'group3'],
- );
- }
- $this->assertEquals($expects, $this->connector->findByUri($uri, 'principals/users'));
- }
- public function findByUriWithGroupRestrictionDataProvider(): array {
- return [
- ['mailto:user2@foo.bar', 'user2@foo.bar', 'principals/users/user2'],
- ['mailto:user3@foo.bar', 'user3@foo.bar', null],
- ];
- }
- /**
- * @dataProvider findByUriWithoutGroupRestrictionDataProvider
- */
- public function testFindByUriWithoutGroupRestriction($uri, $email, $expects): void {
- $this->shareManager->expects($this->once())
- ->method('shareApiEnabled')
- ->willReturn(true);
- $this->shareManager->expects($this->once())
- ->method('shareWithGroupMembersOnly')
- ->willReturn(false);
- $user2 = $this->createMock(IUser::class);
- $user2->method('getUID')->willReturn('user2');
- $user3 = $this->createMock(IUser::class);
- $user3->method('getUID')->willReturn('user3');
- $this->userManager->expects($this->once())
- ->method('getByEmail')
- ->with($email)
- ->willReturn([$email === 'user2@foo.bar' ? $user2 : $user3]);
- $this->assertEquals($expects, $this->connector->findByUri($uri, 'principals/users'));
- }
- public function findByUriWithoutGroupRestrictionDataProvider(): array {
- return [
- ['mailto:user2@foo.bar', 'user2@foo.bar', 'principals/users/user2'],
- ['mailto:user3@foo.bar', 'user3@foo.bar', 'principals/users/user3'],
- ];
- }
- public function testGetEmailAddressesOfPrincipal(): void {
- $principal = [
- '{http://sabredav.org/ns}email-address' => 'bar@company.org',
- '{DAV:}alternate-URI-set' => [
- '/some/url',
- 'mailto:foo@bar.com',
- 'mailto:duplicate@example.com',
- ],
- '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => [
- 'mailto:bernard@example.com',
- 'mailto:bernard.desruisseaux@example.com',
- ],
- '{http://calendarserver.org/ns/}email-address-set' => [
- 'mailto:duplicate@example.com',
- 'mailto:user@some.org',
- ],
- ];
- $expected = [
- 'bar@company.org',
- 'foo@bar.com',
- 'duplicate@example.com',
- 'bernard@example.com',
- 'bernard.desruisseaux@example.com',
- 'user@some.org',
- ];
- $actual = $this->connector->getEmailAddressesOfPrincipal($principal);
- $this->assertEquals($expected, $actual);
- }
- }
|