12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094 |
- <?php
- declare(strict_types=1);
- /**
- * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
- * @copyright 2017 Lukas Reschke <lukas@statuscode.ch>
- *
- * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author 2017 Lukas Reschke <lukas@statuscode.ch>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- namespace Tests\Contacts\ContactsMenu;
- use OC\Contacts\ContactsMenu\ContactsStore;
- use OC\KnownUser\KnownUserService;
- use OC\Profile\ProfileManager;
- use OCA\UserStatus\Db\UserStatus;
- use OCA\UserStatus\Service\StatusService;
- use OCP\Contacts\IManager;
- use OCP\IConfig;
- use OCP\IGroupManager;
- use OCP\IURLGenerator;
- use OCP\IUser;
- use OCP\IUserManager;
- use OCP\L10N\IFactory as IL10NFactory;
- use PHPUnit\Framework\MockObject\MockObject;
- use Test\TestCase;
- class ContactsStoreTest extends TestCase {
- private ContactsStore $contactsStore;
- private StatusService|MockObject $statusService;
- /** @var IManager|MockObject */
- private $contactsManager;
- /** @var ProfileManager */
- private $profileManager;
- /** @var IUserManager|MockObject */
- private $userManager;
- /** @var IURLGenerator */
- private $urlGenerator;
- /** @var IGroupManager|MockObject */
- private $groupManager;
- /** @var IConfig|MockObject */
- private $config;
- /** @var KnownUserService|MockObject */
- private $knownUserService;
- /** @var IL10NFactory */
- private $l10nFactory;
- protected function setUp(): void {
- parent::setUp();
- $this->contactsManager = $this->createMock(IManager::class);
- $this->statusService = $this->createMock(StatusService::class);
- $this->userManager = $this->createMock(IUserManager::class);
- $this->profileManager = $this->createMock(ProfileManager::class);
- $this->urlGenerator = $this->createMock(IURLGenerator::class);
- $this->groupManager = $this->createMock(IGroupManager::class);
- $this->config = $this->createMock(IConfig::class);
- $this->knownUserService = $this->createMock(KnownUserService::class);
- $this->l10nFactory = $this->createMock(IL10NFactory::class);
- $this->contactsStore = new ContactsStore(
- $this->contactsManager,
- $this->statusService,
- $this->config,
- $this->profileManager,
- $this->userManager,
- $this->urlGenerator,
- $this->groupManager,
- $this->knownUserService,
- $this->l10nFactory,
- );
- }
- public function testGetContactsWithoutFilter() {
- /** @var IUser|MockObject $user */
- $user = $this->createMock(IUser::class);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
- ->willReturn([
- [
- 'UID' => 123,
- ],
- [
- 'UID' => 567,
- 'FN' => 'Darren Roner',
- 'EMAIL' => [
- 'darren@roner.au'
- ],
- ],
- ]);
- $user->expects($this->exactly(2))
- ->method('getUID')
- ->willReturn('user123');
- $entries = $this->contactsStore->getContacts($user, '');
- $this->assertCount(2, $entries);
- $this->assertEquals([
- 'darren@roner.au'
- ], $entries[1]->getEMailAddresses());
- }
- public function testGetContactsHidesOwnEntry() {
- /** @var IUser|MockObject $user */
- $user = $this->createMock(IUser::class);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
- ->willReturn([
- [
- 'UID' => 'user123',
- ],
- [
- 'UID' => 567,
- 'FN' => 'Darren Roner',
- 'EMAIL' => [
- 'darren@roner.au'
- ],
- ],
- ]);
- $user->expects($this->exactly(2))
- ->method('getUID')
- ->willReturn('user123');
- $entries = $this->contactsStore->getContacts($user, '');
- $this->assertCount(1, $entries);
- }
- public function testGetContactsWithoutBinaryImage() {
- /** @var IUser|MockObject $user */
- $user = $this->createMock(IUser::class);
- $this->urlGenerator->expects($this->any())
- ->method('linkToRouteAbsolute')
- ->with('core.GuestAvatar.getAvatar', $this->anything())
- ->willReturn('https://urlToNcAvatar.test');
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
- ->willReturn([
- [
- 'UID' => 123,
- ],
- [
- 'UID' => 567,
- 'FN' => 'Darren Roner',
- 'EMAIL' => [
- 'darren@roner.au'
- ],
- 'PHOTO' => base64_encode('photophotophoto'),
- ],
- ]);
- $user->expects($this->exactly(2))
- ->method('getUID')
- ->willReturn('user123');
- $entries = $this->contactsStore->getContacts($user, '');
- $this->assertCount(2, $entries);
- $this->assertSame('https://urlToNcAvatar.test', $entries[1]->getAvatar());
- }
- public function testGetContactsWithoutAvatarURI() {
- /** @var IUser|MockObject $user */
- $user = $this->createMock(IUser::class);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
- ->willReturn([
- [
- 'UID' => 123,
- ],
- [
- 'UID' => 567,
- 'FN' => 'Darren Roner',
- 'EMAIL' => [
- 'darren@roner.au'
- ],
- 'PHOTO' => 'VALUE=uri:https://photo',
- ],
- ]);
- $user->expects($this->exactly(2))
- ->method('getUID')
- ->willReturn('user123');
- $entries = $this->contactsStore->getContacts($user, '');
- $this->assertCount(2, $entries);
- $this->assertEquals('https://photo', $entries[1]->getAvatar());
- }
- public function testGetContactsWhenUserIsInExcludeGroups() {
- $this->config
- ->method('getAppValue')
- ->willReturnMap([
- ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
- ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
- ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
- ['core', 'shareapi_exclude_groups', 'no', 'yes'],
- ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
- ['core', 'shareapi_exclude_groups_list', '', '["group1", "group5", "group6"]'],
- ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
- ]);
- /** @var IUser|MockObject $currentUser */
- $currentUser = $this->createMock(IUser::class);
- $currentUser->expects($this->exactly(2))
- ->method('getUID')
- ->willReturn('user001');
- $this->groupManager->expects($this->once())
- ->method('getUserGroupIds')
- ->with($this->equalTo($currentUser))
- ->willReturn(['group1', 'group2', 'group3']);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
- ->willReturn([
- [
- 'UID' => 'user123',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user12345',
- 'isLocalSystemBook' => true
- ],
- ]);
- $entries = $this->contactsStore->getContacts($currentUser, '');
- $this->assertCount(0, $entries);
- }
- public function testGetContactsOnlyShareIfInTheSameGroup() {
- $this->config
- ->method('getAppValue')
- ->willReturnMap([
- ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
- ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
- ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
- ['core', 'shareapi_exclude_groups', 'no', 'no'],
- ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
- ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
- ]);
- /** @var IUser|MockObject $currentUser */
- $currentUser = $this->createMock(IUser::class);
- $currentUser->expects($this->exactly(2))
- ->method('getUID')
- ->willReturn('user001');
- $user1 = $this->createMock(IUser::class);
- $user2 = $this->createMock(IUser::class);
- $user3 = $this->createMock(IUser::class);
- $this->groupManager->expects($this->exactly(4))
- ->method('getUserGroupIds')
- ->withConsecutive(
- [$this->equalTo($currentUser)],
- [$this->equalTo($user1)],
- [$this->equalTo($user2)],
- [$this->equalTo($user3)]
- )
- ->willReturnOnConsecutiveCalls(
- ['group1', 'group2', 'group3'],
- ['group1'],
- ['group2', 'group3'],
- ['group8', 'group9']
- );
- $this->userManager->expects($this->exactly(3))
- ->method('get')
- ->withConsecutive(
- ['user1'],
- ['user2'],
- ['user3']
- )
- ->willReturnOnConsecutiveCalls($user1, $user2, $user3);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
- ->willReturn([
- [
- 'UID' => 'user1',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user2',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user3',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'contact',
- ],
- ]);
- $entries = $this->contactsStore->getContacts($currentUser, '');
- $this->assertCount(3, $entries);
- $this->assertEquals('user1', $entries[0]->getProperty('UID'));
- $this->assertEquals('user2', $entries[1]->getProperty('UID'));
- $this->assertEquals('contact', $entries[2]->getProperty('UID'));
- }
- public function testGetContactsOnlyEnumerateIfInTheSameGroup() {
- $this->config
- ->method('getAppValue')
- ->willReturnMap([
- ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
- ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'yes'],
- ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
- ['core', 'shareapi_exclude_groups', 'no', 'no'],
- ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
- ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
- ]);
- /** @var IUser|MockObject $currentUser */
- $currentUser = $this->createMock(IUser::class);
- $currentUser->expects($this->exactly(2))
- ->method('getUID')
- ->willReturn('user001');
- $user1 = $this->createMock(IUser::class);
- $user2 = $this->createMock(IUser::class);
- $user3 = $this->createMock(IUser::class);
- $this->groupManager->expects($this->exactly(4))
- ->method('getUserGroupIds')
- ->withConsecutive(
- [$this->equalTo($currentUser)],
- [$this->equalTo($user1)],
- [$this->equalTo($user2)],
- [$this->equalTo($user3)]
- )
- ->willReturnOnConsecutiveCalls(
- ['group1', 'group2', 'group3'],
- ['group1'],
- ['group2', 'group3'],
- ['group8', 'group9']
- );
- $this->userManager->expects($this->exactly(3))
- ->method('get')
- ->withConsecutive(
- ['user1'],
- ['user2'],
- ['user3']
- )
- ->willReturn($user1, $user2, $user3);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
- ->willReturn([
- [
- 'UID' => 'user1',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user2',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user3',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'contact',
- ],
- ]);
- $entries = $this->contactsStore->getContacts($currentUser, '');
- $this->assertCount(3, $entries);
- $this->assertEquals('user1', $entries[0]->getProperty('UID'));
- $this->assertEquals('user2', $entries[1]->getProperty('UID'));
- $this->assertEquals('contact', $entries[2]->getProperty('UID'));
- }
- public function testGetContactsOnlyEnumerateIfPhoneBookMatch() {
- $this->config
- ->method('getAppValue')
- ->willReturnMap([
- ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
- ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
- ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'yes'],
- ['core', 'shareapi_exclude_groups', 'no', 'no'],
- ['core', 'shareapi_only_share_with_group_members', 'no', 'no'],
- ]);
- /** @var IUser|MockObject $currentUser */
- $currentUser = $this->createMock(IUser::class);
- $currentUser->expects($this->exactly(2))
- ->method('getUID')
- ->willReturn('user001');
- $this->groupManager->expects($this->once())
- ->method('getUserGroupIds')
- ->with($this->equalTo($currentUser))
- ->willReturn(['group1', 'group2', 'group3']);
- $this->knownUserService->method('isKnownToUser')
- ->willReturnMap([
- ['user001', 'user1', true],
- ['user001', 'user2', true],
- ['user001', 'user3', false],
- ]);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
- ->willReturn([
- [
- 'UID' => 'user1',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user2',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user3',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'contact',
- ],
- ]);
- $entries = $this->contactsStore->getContacts($currentUser, '');
- $this->assertCount(3, $entries);
- $this->assertEquals('user1', $entries[0]->getProperty('UID'));
- $this->assertEquals('user2', $entries[1]->getProperty('UID'));
- $this->assertEquals('contact', $entries[2]->getProperty('UID'));
- }
- public function testGetContactsOnlyEnumerateIfPhoneBookMatchWithOwnGroupsOnly() {
- $this->config
- ->method('getAppValue')
- ->willReturnMap([
- ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
- ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
- ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'yes'],
- ['core', 'shareapi_exclude_groups', 'no', 'no'],
- ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
- ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
- ]);
- /** @var IUser|MockObject $currentUser */
- $currentUser = $this->createMock(IUser::class);
- $currentUser->expects($this->exactly(2))
- ->method('getUID')
- ->willReturn('user001');
- $user1 = $this->createMock(IUser::class);
- $user2 = $this->createMock(IUser::class);
- $user3 = $this->createMock(IUser::class);
- $this->groupManager->expects($this->exactly(4))
- ->method('getUserGroupIds')
- ->withConsecutive(
- [$this->equalTo($currentUser)],
- [$this->equalTo($user1)],
- [$this->equalTo($user2)],
- [$this->equalTo($user3)]
- )
- ->willReturnOnConsecutiveCalls(
- ['group1', 'group2', 'group3'],
- ['group1'],
- ['group2', 'group3'],
- ['group8', 'group9']
- );
- $this->userManager->expects($this->exactly(3))
- ->method('get')
- ->withConsecutive(
- ['user1'],
- ['user2'],
- ['user3']
- )
- ->willReturnOnConsecutiveCalls($user1, $user2, $user3);
- $this->knownUserService->method('isKnownToUser')
- ->willReturnMap([
- ['user001', 'user1', true],
- ['user001', 'user2', true],
- ['user001', 'user3', true],
- ]);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
- ->willReturn([
- [
- 'UID' => 'user1',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user2',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user3',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'contact',
- ],
- ]);
- $entries = $this->contactsStore->getContacts($currentUser, '');
- $this->assertCount(3, $entries);
- $this->assertEquals('user1', $entries[0]->getProperty('UID'));
- $this->assertEquals('user2', $entries[1]->getProperty('UID'));
- $this->assertEquals('contact', $entries[2]->getProperty('UID'));
- }
- public function testGetContactsOnlyEnumerateIfPhoneBookOrSameGroup() {
- $this->config
- ->method('getAppValue')
- ->willReturnMap([
- ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
- ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'yes'],
- ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'yes'],
- ['core', 'shareapi_exclude_groups', 'no', 'no'],
- ['core', 'shareapi_only_share_with_group_members', 'no', 'no'],
- ]);
- /** @var IUser|MockObject $currentUser */
- $currentUser = $this->createMock(IUser::class);
- $currentUser->expects($this->exactly(2))
- ->method('getUID')
- ->willReturn('user001');
- $user1 = $this->createMock(IUser::class);
- $this->groupManager->expects($this->exactly(2))
- ->method('getUserGroupIds')
- ->withConsecutive(
- [$this->equalTo($currentUser)],
- [$this->equalTo($user1)]
- )
- ->willReturnOnConsecutiveCalls(
- ['group1', 'group2', 'group3'],
- ['group1']
- );
- $this->userManager->expects($this->once())
- ->method('get')
- ->with('user1')
- ->willReturn($user1);
- $this->knownUserService->method('isKnownToUser')
- ->willReturnMap([
- ['user001', 'user1', false],
- ['user001', 'user2', true],
- ['user001', 'user3', true],
- ]);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
- ->willReturn([
- [
- 'UID' => 'user1',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user2',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user3',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'contact',
- ],
- ]);
- $entries = $this->contactsStore->getContacts($currentUser, '');
- $this->assertCount(4, $entries);
- $this->assertEquals('user1', $entries[0]->getProperty('UID'));
- $this->assertEquals('user2', $entries[1]->getProperty('UID'));
- $this->assertEquals('user3', $entries[2]->getProperty('UID'));
- $this->assertEquals('contact', $entries[3]->getProperty('UID'));
- }
- public function testGetContactsOnlyEnumerateIfPhoneBookOrSameGroupInOwnGroupsOnly() {
- $this->config
- ->method('getAppValue')
- ->willReturnMap([
- ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
- ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'yes'],
- ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'yes'],
- ['core', 'shareapi_exclude_groups', 'no', 'no'],
- ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
- ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
- ]);
- /** @var IUser|MockObject $currentUser */
- $currentUser = $this->createMock(IUser::class);
- $currentUser->expects($this->exactly(2))
- ->method('getUID')
- ->willReturn('user001');
- $user1 = $this->createMock(IUser::class);
- $user2 = $this->createMock(IUser::class);
- $user3 = $this->createMock(IUser::class);
- $this->groupManager->expects($this->exactly(4))
- ->method('getUserGroupIds')
- ->withConsecutive(
- [$this->equalTo($currentUser)],
- [$this->equalTo($user1)],
- [$this->equalTo($user2)],
- [$this->equalTo($user3)]
- )
- ->willReturnOnConsecutiveCalls(
- ['group1', 'group2', 'group3'],
- ['group1'],
- ['group2', 'group3'],
- ['group8', 'group9']
- );
- $this->userManager->expects($this->exactly(3))
- ->method('get')
- ->withConsecutive(
- ['user1'],
- ['user2'],
- ['user3']
- )
- ->willReturnOnConsecutiveCalls($user1, $user2, $user3);
- $this->knownUserService->method('isKnownToUser')
- ->willReturnMap([
- ['user001', 'user1', false],
- ['user001', 'user2', true],
- ['user001', 'user3', true],
- ]);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo(''), $this->equalTo(['FN', 'EMAIL']))
- ->willReturn([
- [
- 'UID' => 'user1',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user2',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'user3',
- 'isLocalSystemBook' => true
- ],
- [
- 'UID' => 'contact',
- ],
- ]);
- $entries = $this->contactsStore->getContacts($currentUser, '');
- $this->assertCount(3, $entries);
- $this->assertEquals('user1', $entries[0]->getProperty('UID'));
- $this->assertEquals('user2', $entries[1]->getProperty('UID'));
- $this->assertEquals('contact', $entries[2]->getProperty('UID'));
- }
- public function testGetContactsWithFilter() {
- $this->config
- ->method('getAppValue')
- ->willReturnMap([
- ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'no'],
- ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'yes'],
- ]);
- /** @var IUser|MockObject $user */
- $user = $this->createMock(IUser::class);
- $this->contactsManager->expects($this->any())
- ->method('search')
- ->willReturn([
- [
- 'UID' => 'a567',
- 'FN' => 'Darren Roner',
- 'EMAIL' => [
- 'darren@roner.au',
- ],
- 'isLocalSystemBook' => true,
- ],
- [
- 'UID' => 'john',
- 'FN' => 'John Doe',
- 'EMAIL' => [
- 'john@example.com',
- ],
- 'isLocalSystemBook' => true,
- ],
- [
- 'FN' => 'Anne D',
- 'EMAIL' => [
- 'anne@example.com',
- ],
- 'isLocalSystemBook' => false,
- ],
- ]);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user123');
- // Complete match on UID should match
- $entry = $this->contactsStore->getContacts($user, 'a567');
- $this->assertSame(2, count($entry));
- $this->assertEquals([
- 'darren@roner.au'
- ], $entry[0]->getEMailAddresses());
- // Partial match on UID should not match
- $entry = $this->contactsStore->getContacts($user, 'a56');
- $this->assertSame(1, count($entry));
- $this->assertEquals([
- 'anne@example.com'
- ], $entry[0]->getEMailAddresses());
- // Complete match on email should match
- $entry = $this->contactsStore->getContacts($user, 'john@example.com');
- $this->assertSame(2, count($entry));
- $this->assertEquals([
- 'john@example.com'
- ], $entry[0]->getEMailAddresses());
- $this->assertEquals([
- 'anne@example.com'
- ], $entry[1]->getEMailAddresses());
- // Partial match on email should not match
- $entry = $this->contactsStore->getContacts($user, 'john@example.co');
- $this->assertSame(1, count($entry));
- $this->assertEquals([
- 'anne@example.com'
- ], $entry[0]->getEMailAddresses());
- // Match on FN should not match
- $entry = $this->contactsStore->getContacts($user, 'Darren Roner');
- $this->assertSame(1, count($entry));
- $this->assertEquals([
- 'anne@example.com'
- ], $entry[0]->getEMailAddresses());
- // Don't filter users in local addressbook
- $entry = $this->contactsStore->getContacts($user, 'Anne D');
- $this->assertSame(1, count($entry));
- $this->assertEquals([
- 'anne@example.com'
- ], $entry[0]->getEMailAddresses());
- }
- public function testGetContactsWithFilterWithoutFullMatch() {
- $this->config
- ->method('getAppValue')
- ->willReturnMap([
- ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'no'],
- ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'no'],
- ]);
- /** @var IUser|MockObject $user */
- $user = $this->createMock(IUser::class);
- $this->contactsManager->expects($this->any())
- ->method('search')
- ->willReturn([
- [
- 'UID' => 'a567',
- 'FN' => 'Darren Roner',
- 'EMAIL' => [
- 'darren@roner.au',
- ],
- 'isLocalSystemBook' => true,
- ],
- [
- 'UID' => 'john',
- 'FN' => 'John Doe',
- 'EMAIL' => [
- 'john@example.com',
- ],
- 'isLocalSystemBook' => true,
- ],
- [
- 'FN' => 'Anne D',
- 'EMAIL' => [
- 'anne@example.com',
- ],
- 'isLocalSystemBook' => false,
- ],
- ]);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user123');
- // Complete match on UID should not match
- $entry = $this->contactsStore->getContacts($user, 'a567');
- $this->assertSame(1, count($entry));
- $this->assertEquals([
- 'anne@example.com'
- ], $entry[0]->getEMailAddresses());
- // Partial match on UID should not match
- $entry = $this->contactsStore->getContacts($user, 'a56');
- $this->assertSame(1, count($entry));
- $this->assertEquals([
- 'anne@example.com'
- ], $entry[0]->getEMailAddresses());
- // Complete match on email should not match
- $entry = $this->contactsStore->getContacts($user, 'john@example.com');
- $this->assertSame(1, count($entry));
- $this->assertEquals([
- 'anne@example.com'
- ], $entry[0]->getEMailAddresses());
- // Partial match on email should not match
- $entry = $this->contactsStore->getContacts($user, 'john@example.co');
- $this->assertSame(1, count($entry));
- $this->assertEquals([
- 'anne@example.com'
- ], $entry[0]->getEMailAddresses());
- // Match on FN should not match
- $entry = $this->contactsStore->getContacts($user, 'Darren Roner');
- $this->assertSame(1, count($entry));
- $this->assertEquals([
- 'anne@example.com'
- ], $entry[0]->getEMailAddresses());
- // Don't filter users in local addressbook
- $entry = $this->contactsStore->getContacts($user, 'Anne D');
- $this->assertSame(1, count($entry));
- $this->assertEquals([
- 'anne@example.com'
- ], $entry[0]->getEMailAddresses());
- }
- public function testFindOneUser() {
- $this->config
- ->method('getAppValue')
- ->willReturnMap([
- ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
- ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
- ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
- ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'yes'],
- ['core', 'shareapi_exclude_groups', 'no', 'yes'],
- ['core', 'shareapi_exclude_groups_list', '', ''],
- ['core', 'shareapi_only_share_with_group_members', 'no', 'no'],
- ]);
- /** @var IUser|MockObject $user */
- $user = $this->createMock(IUser::class);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo('a567'), $this->equalTo(['UID']))
- ->willReturn([
- [
- 'UID' => 123,
- 'isLocalSystemBook' => false
- ],
- [
- 'UID' => 'a567',
- 'FN' => 'Darren Roner',
- 'EMAIL' => [
- 'darren@roner.au'
- ],
- 'isLocalSystemBook' => true
- ],
- ]);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user123');
- $entry = $this->contactsStore->findOne($user, 0, 'a567');
- $this->assertEquals([
- 'darren@roner.au'
- ], $entry->getEMailAddresses());
- }
- public function testFindOneEMail() {
- /** @var IUser|MockObject $user */
- $user = $this->createMock(IUser::class);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo('darren@roner.au'), $this->equalTo(['EMAIL']))
- ->willReturn([
- [
- 'UID' => 123,
- 'isLocalSystemBook' => false
- ],
- [
- 'UID' => 'a567',
- 'FN' => 'Darren Roner',
- 'EMAIL' => [
- 'darren@roner.au'
- ],
- 'isLocalSystemBook' => false
- ],
- ]);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user123');
- $entry = $this->contactsStore->findOne($user, 4, 'darren@roner.au');
- $this->assertEquals([
- 'darren@roner.au'
- ], $entry->getEMailAddresses());
- }
- public function testFindOneNotSupportedType() {
- /** @var IUser|MockObject $user */
- $user = $this->createMock(IUser::class);
- $entry = $this->contactsStore->findOne($user, 42, 'darren@roner.au');
- $this->assertEquals(null, $entry);
- }
- public function testFindOneNoMatches() {
- /** @var IUser|MockObject $user */
- $user = $this->createMock(IUser::class);
- $this->contactsManager->expects($this->once())
- ->method('search')
- ->with($this->equalTo('a567'), $this->equalTo(['UID']))
- ->willReturn([
- [
- 'UID' => 123,
- 'isLocalSystemBook' => false
- ],
- [
- 'UID' => 'a567',
- 'FN' => 'Darren Roner',
- 'EMAIL' => [
- 'darren@roner.au123'
- ],
- 'isLocalSystemBook' => false
- ],
- ]);
- $user->expects($this->never())
- ->method('getUID');
- $entry = $this->contactsStore->findOne($user, 0, 'a567');
- $this->assertEquals(null, $entry);
- }
- public function testGetRecentStatusFirst(): void {
- $user = $this->createMock(IUser::class);
- $status1 = new UserStatus();
- $status1->setUserId('user1');
- $status2 = new UserStatus();
- $status2->setUserId('user2');
- $this->statusService->expects(self::once())
- ->method('findAllRecentStatusChanges')
- ->willReturn([
- $status1,
- $status2,
- ]);
- $user1 = $this->createMock(IUser::class);
- $user1->method('getCloudId')->willReturn('user1@localcloud');
- $user2 = $this->createMock(IUser::class);
- $user2->method('getCloudId')->willReturn('user2@localcloud');
- $this->userManager->expects(self::exactly(2))
- ->method('get')
- ->willReturnCallback(function ($uid) use ($user1, $user2) {
- return match ($uid) {
- 'user1' => $user1,
- 'user2' => $user2,
- };
- });
- $this->contactsManager
- ->expects(self::exactly(3))
- ->method('search')
- ->willReturnCallback(function ($uid, $searchProps, $options) {
- return match ([$uid, $options['limit'] ?? null]) {
- ['user1@localcloud', 1] => [
- [
- 'UID' => 'user1',
- 'URI' => 'user1.vcf',
- ],
- ],
- ['user2@localcloud' => [], 1], // Simulate not found
- ['', 4] => [
- [
- 'UID' => 'contact1',
- 'URI' => 'contact1.vcf',
- ],
- [
- 'UID' => 'contact2',
- 'URI' => 'contact2.vcf',
- ],
- ],
- default => [],
- };
- });
- $contacts = $this->contactsStore->getContacts(
- $user,
- null,
- 5,
- );
- self::assertCount(3, $contacts);
- self::assertEquals('user1', $contacts[0]->getProperty('UID'));
- self::assertEquals('contact1', $contacts[1]->getProperty('UID'));
- self::assertEquals('contact2', $contacts[2]->getProperty('UID'));
- }
- public function testPaginateRecentStatus(): void {
- $user = $this->createMock(IUser::class);
- $status1 = new UserStatus();
- $status1->setUserId('user1');
- $status2 = new UserStatus();
- $status2->setUserId('user2');
- $status3 = new UserStatus();
- $status3->setUserId('user3');
- $this->statusService->expects(self::never())
- ->method('findAllRecentStatusChanges');
- $this->contactsManager
- ->expects(self::exactly(2))
- ->method('search')
- ->willReturnCallback(function ($uid, $searchProps, $options) {
- return match ([$uid, $options['limit'] ?? null, $options['offset'] ?? null]) {
- ['', 2, 0] => [
- [
- 'UID' => 'contact1',
- 'URI' => 'contact1.vcf',
- ],
- [
- 'UID' => 'contact2',
- 'URI' => 'contact2.vcf',
- ],
- ],
- ['', 2, 3] => [
- [
- 'UID' => 'contact3',
- 'URI' => 'contact3.vcf',
- ],
- ],
- default => [],
- };
- });
- $page1 = $this->contactsStore->getContacts(
- $user,
- null,
- 2,
- 0,
- );
- $page2 = $this->contactsStore->getContacts(
- $user,
- null,
- 2,
- 3,
- );
- self::assertCount(2, $page1);
- self::assertCount(1, $page2);
- }
- }
|