diff options
Diffstat (limited to 'tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php')
-rw-r--r-- | tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php | 341 |
1 files changed, 221 insertions, 120 deletions
diff --git a/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php b/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php index a038058069e..9097ee779d2 100644 --- a/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php +++ b/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php @@ -1,26 +1,10 @@ <?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/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Tests\Contacts\ContactsMenu; @@ -28,6 +12,8 @@ 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; @@ -40,6 +26,7 @@ use Test\TestCase; class ContactsStoreTest extends TestCase { private ContactsStore $contactsStore; + private StatusService|MockObject $statusService; /** @var IManager|MockObject */ private $contactsManager; /** @var ProfileManager */ @@ -61,6 +48,7 @@ class ContactsStoreTest extends TestCase { 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); @@ -70,17 +58,18 @@ class ContactsStoreTest extends TestCase { $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 + $this->l10nFactory, ); } - public function testGetContactsWithoutFilter() { + public function testGetContactsWithoutFilter(): void { /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->once()) @@ -110,7 +99,7 @@ class ContactsStoreTest extends TestCase { ], $entries[1]->getEMailAddresses()); } - public function testGetContactsHidesOwnEntry() { + public function testGetContactsHidesOwnEntry(): void { /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->once()) @@ -137,12 +126,12 @@ class ContactsStoreTest extends TestCase { $this->assertCount(1, $entries); } - public function testGetContactsWithoutBinaryImage() { + public function testGetContactsWithoutBinaryImage(): void { /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->urlGenerator->expects($this->any()) ->method('linkToRouteAbsolute') - ->with('core.avatar.getAvatar', $this->anything()) + ->with('core.GuestAvatar.getAvatar', $this->anything()) ->willReturn('https://urlToNcAvatar.test'); $this->contactsManager->expects($this->once()) ->method('search') @@ -170,7 +159,7 @@ class ContactsStoreTest extends TestCase { $this->assertSame('https://urlToNcAvatar.test', $entries[1]->getAvatar()); } - public function testGetContactsWithoutAvatarURI() { + public function testGetContactsWithoutAvatarURI(): void { /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->once()) @@ -199,7 +188,7 @@ class ContactsStoreTest extends TestCase { $this->assertEquals('https://photo', $entries[1]->getAvatar()); } - public function testGetContactsWhenUserIsInExcludeGroups() { + public function testGetContactsWhenUserIsInExcludeGroups(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -209,6 +198,7 @@ class ContactsStoreTest extends TestCase { ['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 */ @@ -243,7 +233,7 @@ class ContactsStoreTest extends TestCase { $this->assertCount(0, $entries); } - public function testGetContactsOnlyShareIfInTheSameGroup() { + public function testGetContactsOnlyShareIfInTheSameGroup(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -252,6 +242,7 @@ class ContactsStoreTest extends TestCase { ['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 */ @@ -264,29 +255,27 @@ class ContactsStoreTest extends TestCase { $user2 = $this->createMock(IUser::class); $user3 = $this->createMock(IUser::class); + $calls = [ + [[$currentUser], ['group1', 'group2', 'group3']], + [[$user1], ['group1']], + [[$user2], ['group2', 'group3']], + [[$user3], ['group8', 'group9']], + ]; $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'] - ); + ->willReturnCallback(function () use (&$calls): array { + $expected = array_shift($calls); + $this->assertEquals($expected[0], func_get_args()); + return $expected[1]; + }); $this->userManager->expects($this->exactly(3)) ->method('get') - ->withConsecutive( - ['user1'], - ['user2'], - ['user3'] - ) - ->willReturnOnConsecutiveCalls($user1, $user2, $user3); + ->willReturnMap([ + ['user1', $user1], + ['user2', $user2], + ['user3', $user3], + ]); $this->contactsManager->expects($this->once()) ->method('search') @@ -317,7 +306,7 @@ class ContactsStoreTest extends TestCase { $this->assertEquals('contact', $entries[2]->getProperty('UID')); } - public function testGetContactsOnlyEnumerateIfInTheSameGroup() { + public function testGetContactsOnlyEnumerateIfInTheSameGroup(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -326,6 +315,7 @@ class ContactsStoreTest extends TestCase { ['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 */ @@ -338,29 +328,27 @@ class ContactsStoreTest extends TestCase { $user2 = $this->createMock(IUser::class); $user3 = $this->createMock(IUser::class); + $calls = [ + [[$currentUser], ['group1', 'group2', 'group3']], + [[$user1], ['group1']], + [[$user2], ['group2', 'group3']], + [[$user3], ['group8', 'group9']], + ]; $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'] - ); + ->willReturnCallback(function () use (&$calls): array { + $expected = array_shift($calls); + $this->assertEquals($expected[0], func_get_args()); + return $expected[1]; + }); $this->userManager->expects($this->exactly(3)) ->method('get') - ->withConsecutive( - ['user1'], - ['user2'], - ['user3'] - ) - ->willReturn($user1, $user2, $user3); + ->willReturnMap([ + ['user1', $user1], + ['user2', $user2], + ['user3', $user3], + ]); $this->contactsManager->expects($this->once()) ->method('search') @@ -391,7 +379,7 @@ class ContactsStoreTest extends TestCase { $this->assertEquals('contact', $entries[2]->getProperty('UID')); } - public function testGetContactsOnlyEnumerateIfPhoneBookMatch() { + public function testGetContactsOnlyEnumerateIfPhoneBookMatch(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -449,7 +437,7 @@ class ContactsStoreTest extends TestCase { $this->assertEquals('contact', $entries[2]->getProperty('UID')); } - public function testGetContactsOnlyEnumerateIfPhoneBookMatchWithOwnGroupsOnly() { + public function testGetContactsOnlyEnumerateIfPhoneBookMatchWithOwnGroupsOnly(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -458,6 +446,7 @@ class ContactsStoreTest extends TestCase { ['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 */ @@ -470,29 +459,27 @@ class ContactsStoreTest extends TestCase { $user2 = $this->createMock(IUser::class); $user3 = $this->createMock(IUser::class); + $calls = [ + [[$currentUser], ['group1', 'group2', 'group3']], + [[$user1], ['group1']], + [[$user2], ['group2', 'group3']], + [[$user3], ['group8', 'group9']], + ]; $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'] - ); + ->willReturnCallback(function () use (&$calls): array { + $expected = array_shift($calls); + $this->assertEquals($expected[0], func_get_args()); + return $expected[1]; + }); $this->userManager->expects($this->exactly(3)) ->method('get') - ->withConsecutive( - ['user1'], - ['user2'], - ['user3'] - ) - ->willReturnOnConsecutiveCalls($user1, $user2, $user3); + ->willReturnMap([ + ['user1', $user1], + ['user2', $user2], + ['user3', $user3], + ]); $this->knownUserService->method('isKnownToUser') ->willReturnMap([ @@ -530,7 +517,7 @@ class ContactsStoreTest extends TestCase { $this->assertEquals('contact', $entries[2]->getProperty('UID')); } - public function testGetContactsOnlyEnumerateIfPhoneBookOrSameGroup() { + public function testGetContactsOnlyEnumerateIfPhoneBookOrSameGroup(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -549,16 +536,17 @@ class ContactsStoreTest extends TestCase { $user1 = $this->createMock(IUser::class); + $calls = [ + [[$currentUser], ['group1', 'group2', 'group3']], + [[$user1], ['group1']], + ]; $this->groupManager->expects($this->exactly(2)) ->method('getUserGroupIds') - ->withConsecutive( - [$this->equalTo($currentUser)], - [$this->equalTo($user1)] - ) - ->willReturnOnConsecutiveCalls( - ['group1', 'group2', 'group3'], - ['group1'] - ); + ->willReturnCallback(function () use (&$calls): array { + $expected = array_shift($calls); + $this->assertEquals($expected[0], func_get_args()); + return $expected[1]; + }); $this->userManager->expects($this->once()) ->method('get') @@ -602,7 +590,7 @@ class ContactsStoreTest extends TestCase { $this->assertEquals('contact', $entries[3]->getProperty('UID')); } - public function testGetContactsOnlyEnumerateIfPhoneBookOrSameGroupInOwnGroupsOnly() { + public function testGetContactsOnlyEnumerateIfPhoneBookOrSameGroupInOwnGroupsOnly(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -611,6 +599,7 @@ class ContactsStoreTest extends TestCase { ['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 */ @@ -623,29 +612,27 @@ class ContactsStoreTest extends TestCase { $user2 = $this->createMock(IUser::class); $user3 = $this->createMock(IUser::class); + $calls = [ + [[$currentUser], ['group1', 'group2', 'group3']], + [[$user1], ['group1']], + [[$user2], ['group2', 'group3']], + [[$user3], ['group8', 'group9']], + ]; $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'] - ); + ->willReturnCallback(function () use (&$calls): array { + $expected = array_shift($calls); + $this->assertEquals($expected[0], func_get_args()); + return $expected[1]; + }); $this->userManager->expects($this->exactly(3)) ->method('get') - ->withConsecutive( - ['user1'], - ['user2'], - ['user3'] - ) - ->willReturnOnConsecutiveCalls($user1, $user2, $user3); + ->willReturnMap([ + ['user1', $user1], + ['user2', $user2], + ['user3', $user3], + ]); $this->knownUserService->method('isKnownToUser') ->willReturnMap([ @@ -683,7 +670,7 @@ class ContactsStoreTest extends TestCase { $this->assertEquals('contact', $entries[2]->getProperty('UID')); } - public function testGetContactsWithFilter() { + public function testGetContactsWithFilter(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -770,7 +757,7 @@ class ContactsStoreTest extends TestCase { ], $entry[0]->getEMailAddresses()); } - public function testGetContactsWithFilterWithoutFullMatch() { + public function testGetContactsWithFilterWithoutFullMatch(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -854,7 +841,7 @@ class ContactsStoreTest extends TestCase { ], $entry[0]->getEMailAddresses()); } - public function testFindOneUser() { + public function testFindOneUser(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -897,7 +884,7 @@ class ContactsStoreTest extends TestCase { ], $entry->getEMailAddresses()); } - public function testFindOneEMail() { + public function testFindOneEMail(): void { /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->once()) @@ -928,7 +915,7 @@ class ContactsStoreTest extends TestCase { ], $entry->getEMailAddresses()); } - public function testFindOneNotSupportedType() { + public function testFindOneNotSupportedType(): void { /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); @@ -937,7 +924,7 @@ class ContactsStoreTest extends TestCase { $this->assertEquals(null, $entry); } - public function testFindOneNoMatches() { + public function testFindOneNoMatches(): void { /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->once()) @@ -964,4 +951,118 @@ class ContactsStoreTest extends TestCase { $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); + } } |