diff options
Diffstat (limited to 'tests/lib/Contacts/ContactsMenu')
8 files changed, 585 insertions, 399 deletions
diff --git a/tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php b/tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php index d858eab4d11..09e0e11bd5e 100644 --- a/tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php +++ b/tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php @@ -1,25 +1,8 @@ <?php /** - * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @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; @@ -29,9 +12,7 @@ use OCP\Contacts\ContactsMenu\IAction; use Test\TestCase; class ActionFactoryTest extends TestCase { - - /** @var ActionFactory */ - private $actionFactory; + private ActionFactory $actionFactory; protected function setUp(): void { parent::setUp(); @@ -39,7 +20,7 @@ class ActionFactoryTest extends TestCase { $this->actionFactory = new ActionFactory(); } - public function testNewLinkAction() { + public function testNewLinkAction(): void { $icon = 'icon-test'; $name = 'Test'; $href = 'some/url'; @@ -51,7 +32,7 @@ class ActionFactoryTest extends TestCase { $this->assertEquals(10, $action->getPriority()); } - public function testNewEMailAction() { + public function testNewEMailAction(): void { $icon = 'icon-test'; $name = 'Test'; $href = 'user@example.com'; diff --git a/tests/lib/Contacts/ContactsMenu/ActionProviderStoreTest.php b/tests/lib/Contacts/ContactsMenu/ActionProviderStoreTest.php index a3557d7cda6..84de6ec88e2 100644 --- a/tests/lib/Contacts/ContactsMenu/ActionProviderStoreTest.php +++ b/tests/lib/Contacts/ContactsMenu/ActionProviderStoreTest.php @@ -1,25 +1,8 @@ <?php /** - * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @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; @@ -27,44 +10,42 @@ namespace Tests\Contacts\ContactsMenu; use OC\App\AppManager; use OC\Contacts\ContactsMenu\ActionProviderStore; use OC\Contacts\ContactsMenu\Providers\EMailProvider; +use OC\Contacts\ContactsMenu\Providers\LocalTimeProvider; use OC\Contacts\ContactsMenu\Providers\ProfileProvider; use OCP\App\IAppManager; use OCP\AppFramework\QueryException; use OCP\Contacts\ContactsMenu\IProvider; use OCP\IServerContainer; use OCP\IUser; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; class ActionProviderStoreTest extends TestCase { - - /** @var IServerContainer|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IServerContainer|MockObject */ private $serverContainer; - /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IAppManager|MockObject */ private $appManager; - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ - private $logger; - - /** @var ActionProviderStore */ - private $actionProviderStore; + private ActionProviderStore $actionProviderStore; protected function setUp(): void { parent::setUp(); $this->serverContainer = $this->createMock(IServerContainer::class); $this->appManager = $this->createMock(AppManager::class); - $this->logger = $this->createMock(LoggerInterface::class); + $logger = $this->createMock(LoggerInterface::class); - $this->actionProviderStore = new ActionProviderStore($this->serverContainer, $this->appManager, $this->logger); + $this->actionProviderStore = new ActionProviderStore($this->serverContainer, $this->appManager, $logger); } - public function testGetProviders() { + public function testGetProviders(): void { $user = $this->createMock(IUser::class); $provider1 = $this->createMock(ProfileProvider::class); - $provider2 = $this->createMock(EMailProvider::class); - $provider3 = $this->createMock(IProvider::class); + $provider2 = $this->createMock(LocalTimeProvider::class); + $provider3 = $this->createMock(EMailProvider::class); + $provider4 = $this->createMock(IProvider::class); $this->appManager->expects($this->once()) ->method('getEnabledAppsForUser') @@ -78,25 +59,28 @@ class ActionProviderStoreTest extends TestCase { 'OCA\Contacts\Provider1', ], ]); - $this->serverContainer->expects($this->exactly(3)) - ->method('query') + $this->serverContainer->expects($this->exactly(4)) + ->method('get') ->willReturnMap([ - [ProfileProvider::class, true, $provider1], - [EMailProvider::class, true, $provider2], - ['OCA\Contacts\Provider1', true, $provider3] + [ProfileProvider::class, $provider1], + [LocalTimeProvider::class, $provider2], + [EMailProvider::class, $provider3], + ['OCA\Contacts\Provider1', $provider4] ]); $providers = $this->actionProviderStore->getProviders($user); - $this->assertCount(3, $providers); + $this->assertCount(4, $providers); $this->assertInstanceOf(ProfileProvider::class, $providers[0]); - $this->assertInstanceOf(EMailProvider::class, $providers[1]); + $this->assertInstanceOf(LocalTimeProvider::class, $providers[1]); + $this->assertInstanceOf(EMailProvider::class, $providers[2]); } - public function testGetProvidersOfAppWithIncompleInfo() { + public function testGetProvidersOfAppWithIncompleInfo(): void { $user = $this->createMock(IUser::class); $provider1 = $this->createMock(ProfileProvider::class); - $provider2 = $this->createMock(EMailProvider::class); + $provider2 = $this->createMock(LocalTimeProvider::class); + $provider3 = $this->createMock(EMailProvider::class); $this->appManager->expects($this->once()) ->method('getEnabledAppsForUser') @@ -106,22 +90,24 @@ class ActionProviderStoreTest extends TestCase { ->method('getAppInfo') ->with('contacts') ->willReturn([/* Empty info.xml */]); - $this->serverContainer->expects($this->exactly(2)) - ->method('query') + $this->serverContainer->expects($this->exactly(3)) + ->method('get') ->willReturnMap([ - [ProfileProvider::class, true, $provider1], - [EMailProvider::class, true, $provider2], + [ProfileProvider::class, $provider1], + [LocalTimeProvider::class, $provider2], + [EMailProvider::class, $provider3], ]); $providers = $this->actionProviderStore->getProviders($user); - $this->assertCount(2, $providers); + $this->assertCount(3, $providers); $this->assertInstanceOf(ProfileProvider::class, $providers[0]); - $this->assertInstanceOf(EMailProvider::class, $providers[1]); + $this->assertInstanceOf(LocalTimeProvider::class, $providers[1]); + $this->assertInstanceOf(EMailProvider::class, $providers[2]); } - public function testGetProvidersWithQueryException() { + public function testGetProvidersWithQueryException(): void { $this->expectException(\Exception::class); $user = $this->createMock(IUser::class); @@ -130,7 +116,7 @@ class ActionProviderStoreTest extends TestCase { ->with($user) ->willReturn([]); $this->serverContainer->expects($this->once()) - ->method('query') + ->method('get') ->willThrowException(new QueryException()); $this->actionProviderStore->getProviders($user); diff --git a/tests/lib/Contacts/ContactsMenu/Actions/LinkActionTest.php b/tests/lib/Contacts/ContactsMenu/Actions/LinkActionTest.php index 1f5d37e7483..5e2b416a66b 100644 --- a/tests/lib/Contacts/ContactsMenu/Actions/LinkActionTest.php +++ b/tests/lib/Contacts/ContactsMenu/Actions/LinkActionTest.php @@ -1,25 +1,8 @@ <?php /** - * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @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\Actions; @@ -28,7 +11,7 @@ use OC\Contacts\ContactsMenu\Actions\LinkAction; use Test\TestCase; class LinkActionTest extends TestCase { - private $action; + private LinkAction $action; protected function setUp(): void { parent::setUp(); @@ -36,7 +19,7 @@ class LinkActionTest extends TestCase { $this->action = new LinkAction(); } - public function testSetIcon() { + public function testSetIcon(): void { $icon = 'icon-test'; $this->action->setIcon($icon); @@ -46,15 +29,15 @@ class LinkActionTest extends TestCase { $this->assertEquals($json['icon'], $icon); } - public function testGetSetName() { + public function testGetSetName(): void { $name = 'Jane Doe'; - $this->assertNull($this->action->getName()); + $this->assertEmpty($this->action->getName()); $this->action->setName($name); $this->assertEquals($name, $this->action->getName()); } - public function testGetSetPriority() { + public function testGetSetPriority(): void { $prio = 50; $this->assertEquals(10, $this->action->getPriority()); @@ -62,15 +45,15 @@ class LinkActionTest extends TestCase { $this->assertEquals($prio, $this->action->getPriority()); } - public function testSetHref() { + public function testSetHref(): void { $this->action->setHref('/some/url'); $json = $this->action->jsonSerialize(); $this->assertArrayHasKey('hyperlink', $json); - $this->assertEquals($json['hyperlink'], '/some/url'); + $this->assertEquals('/some/url', $json['hyperlink']); } - public function testJsonSerialize() { + public function testJsonSerialize(): void { $this->action->setIcon('icon-contacts'); $this->action->setName('Nickie Works'); $this->action->setPriority(33); @@ -88,7 +71,7 @@ class LinkActionTest extends TestCase { $this->assertEquals($expected, $json); } - public function testJsonSerializeNoAppName() { + public function testJsonSerializeNoAppName(): void { $this->action->setIcon('icon-contacts'); $this->action->setName('Nickie Works'); $this->action->setPriority(33); diff --git a/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php b/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php index bd82c203ff5..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; @@ -39,19 +25,19 @@ use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class ContactsStoreTest extends TestCase { - /** @var ContactsStore */ - private $contactsStore; - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ + private ContactsStore $contactsStore; + private StatusService|MockObject $statusService; + /** @var IManager|MockObject */ private $contactsManager; /** @var ProfileManager */ private $profileManager; - /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IUserManager|MockObject */ private $userManager; /** @var IURLGenerator */ private $urlGenerator; - /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IGroupManager|MockObject */ private $groupManager; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IConfig|MockObject */ private $config; /** @var KnownUserService|MockObject */ private $knownUserService; @@ -62,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); @@ -71,18 +58,19 @@ 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() { - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ + public function testGetContactsWithoutFilter(): void { + /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->once()) ->method('search') @@ -111,8 +99,8 @@ class ContactsStoreTest extends TestCase { ], $entries[1]->getEMailAddresses()); } - public function testGetContactsHidesOwnEntry() { - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ + public function testGetContactsHidesOwnEntry(): void { + /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->once()) ->method('search') @@ -138,9 +126,13 @@ class ContactsStoreTest extends TestCase { $this->assertCount(1, $entries); } - public function testGetContactsWithoutBinaryImage() { - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ + public function testGetContactsWithoutBinaryImage(): void { + /** @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'])) @@ -164,11 +156,11 @@ class ContactsStoreTest extends TestCase { $entries = $this->contactsStore->getContacts($user, ''); $this->assertCount(2, $entries); - $this->assertNull($entries[1]->getAvatar()); + $this->assertSame('https://urlToNcAvatar.test', $entries[1]->getAvatar()); } - public function testGetContactsWithoutAvatarURI() { - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ + public function testGetContactsWithoutAvatarURI(): void { + /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->once()) ->method('search') @@ -196,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([ @@ -206,9 +198,10 @@ 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|\PHPUnit\Framework\MockObject\MockObject $currentUser */ + /** @var IUser|MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); $currentUser->expects($this->exactly(2)) ->method('getUID') @@ -240,7 +233,7 @@ class ContactsStoreTest extends TestCase { $this->assertCount(0, $entries); } - public function testGetContactsOnlyShareIfInTheSameGroup() { + public function testGetContactsOnlyShareIfInTheSameGroup(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -249,46 +242,40 @@ 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|\PHPUnit\Framework\MockObject\MockObject $currentUser */ + /** @var IUser|MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); - $this->groupManager->expects($this->at(0)) - ->method('getUserGroupIds') - ->with($this->equalTo($currentUser)) - ->willReturn(['group1', 'group2', 'group3']); - $user1 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(0)) - ->method('get') - ->with('user1') - ->willReturn($user1); - $this->groupManager->expects($this->at(1)) - ->method('getUserGroupIds') - ->with($this->equalTo($user1)) - ->willReturn(['group1']); $user2 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(1)) - ->method('get') - ->with('user2') - ->willReturn($user2); - $this->groupManager->expects($this->at(2)) - ->method('getUserGroupIds') - ->with($this->equalTo($user2)) - ->willReturn(['group2', 'group3']); $user3 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(2)) - ->method('get') - ->with('user3') - ->willReturn($user3); - $this->groupManager->expects($this->at(3)) + + $calls = [ + [[$currentUser], ['group1', 'group2', 'group3']], + [[$user1], ['group1']], + [[$user2], ['group2', 'group3']], + [[$user3], ['group8', 'group9']], + ]; + $this->groupManager->expects($this->exactly(4)) ->method('getUserGroupIds') - ->with($this->equalTo($user3)) - ->willReturn(['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') + ->willReturnMap([ + ['user1', $user1], + ['user2', $user2], + ['user3', $user3], + ]); $this->contactsManager->expects($this->once()) ->method('search') @@ -319,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([ @@ -328,46 +315,40 @@ 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|\PHPUnit\Framework\MockObject\MockObject $currentUser */ + /** @var IUser|MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); - $this->groupManager->expects($this->at(0)) - ->method('getUserGroupIds') - ->with($this->equalTo($currentUser)) - ->willReturn(['group1', 'group2', 'group3']); - $user1 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(0)) - ->method('get') - ->with('user1') - ->willReturn($user1); - $this->groupManager->expects($this->at(1)) - ->method('getUserGroupIds') - ->with($this->equalTo($user1)) - ->willReturn(['group1']); $user2 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(1)) - ->method('get') - ->with('user2') - ->willReturn($user2); - $this->groupManager->expects($this->at(2)) - ->method('getUserGroupIds') - ->with($this->equalTo($user2)) - ->willReturn(['group2', 'group3']); $user3 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(2)) - ->method('get') - ->with('user3') - ->willReturn($user3); - $this->groupManager->expects($this->at(3)) + + $calls = [ + [[$currentUser], ['group1', 'group2', 'group3']], + [[$user1], ['group1']], + [[$user2], ['group2', 'group3']], + [[$user3], ['group8', 'group9']], + ]; + $this->groupManager->expects($this->exactly(4)) ->method('getUserGroupIds') - ->with($this->equalTo($user3)) - ->willReturn(['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') + ->willReturnMap([ + ['user1', $user1], + ['user2', $user2], + ['user3', $user3], + ]); $this->contactsManager->expects($this->once()) ->method('search') @@ -398,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([ @@ -409,13 +390,13 @@ class ContactsStoreTest extends TestCase { ['core', 'shareapi_only_share_with_group_members', 'no', 'no'], ]); - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */ + /** @var IUser|MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); - $this->groupManager->expects($this->at(0)) + $this->groupManager->expects($this->once()) ->method('getUserGroupIds') ->with($this->equalTo($currentUser)) ->willReturn(['group1', 'group2', 'group3']); @@ -456,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([ @@ -465,46 +446,40 @@ 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|\PHPUnit\Framework\MockObject\MockObject $currentUser */ + /** @var IUser|MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); - $this->groupManager->expects($this->at(0)) - ->method('getUserGroupIds') - ->with($this->equalTo($currentUser)) - ->willReturn(['group1', 'group2', 'group3']); - $user1 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(0)) - ->method('get') - ->with('user1') - ->willReturn($user1); - $this->groupManager->expects($this->at(1)) - ->method('getUserGroupIds') - ->with($this->equalTo($user1)) - ->willReturn(['group1']); $user2 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(1)) - ->method('get') - ->with('user2') - ->willReturn($user2); - $this->groupManager->expects($this->at(2)) - ->method('getUserGroupIds') - ->with($this->equalTo($user2)) - ->willReturn(['group2', 'group3']); $user3 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(2)) - ->method('get') - ->with('user3') - ->willReturn($user3); - $this->groupManager->expects($this->at(3)) + + $calls = [ + [[$currentUser], ['group1', 'group2', 'group3']], + [[$user1], ['group1']], + [[$user2], ['group2', 'group3']], + [[$user3], ['group8', 'group9']], + ]; + $this->groupManager->expects($this->exactly(4)) ->method('getUserGroupIds') - ->with($this->equalTo($user3)) - ->willReturn(['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') + ->willReturnMap([ + ['user1', $user1], + ['user2', $user2], + ['user3', $user3], + ]); $this->knownUserService->method('isKnownToUser') ->willReturnMap([ @@ -542,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([ @@ -553,26 +528,30 @@ class ContactsStoreTest extends TestCase { ['core', 'shareapi_only_share_with_group_members', 'no', 'no'], ]); - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $currentUser */ + /** @var IUser|MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); - $this->groupManager->expects($this->at(0)) + $user1 = $this->createMock(IUser::class); + + $calls = [ + [[$currentUser], ['group1', 'group2', 'group3']], + [[$user1], ['group1']], + ]; + $this->groupManager->expects($this->exactly(2)) ->method('getUserGroupIds') - ->with($this->equalTo($currentUser)) - ->willReturn(['group1', 'group2', 'group3']); + ->willReturnCallback(function () use (&$calls): array { + $expected = array_shift($calls); + $this->assertEquals($expected[0], func_get_args()); + return $expected[1]; + }); - $user1 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(0)) + $this->userManager->expects($this->once()) ->method('get') ->with('user1') ->willReturn($user1); - $this->groupManager->expects($this->at(1)) - ->method('getUserGroupIds') - ->with($this->equalTo($user1)) - ->willReturn(['group1']); $this->knownUserService->method('isKnownToUser') ->willReturnMap([ @@ -611,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([ @@ -620,46 +599,40 @@ 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|\PHPUnit\Framework\MockObject\MockObject $currentUser */ + /** @var IUser|MockObject $currentUser */ $currentUser = $this->createMock(IUser::class); $currentUser->expects($this->exactly(2)) ->method('getUID') ->willReturn('user001'); - $this->groupManager->expects($this->at(0)) - ->method('getUserGroupIds') - ->with($this->equalTo($currentUser)) - ->willReturn(['group1', 'group2', 'group3']); - $user1 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(0)) - ->method('get') - ->with('user1') - ->willReturn($user1); - $this->groupManager->expects($this->at(1)) - ->method('getUserGroupIds') - ->with($this->equalTo($user1)) - ->willReturn(['group1']); $user2 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(1)) - ->method('get') - ->with('user2') - ->willReturn($user2); - $this->groupManager->expects($this->at(2)) - ->method('getUserGroupIds') - ->with($this->equalTo($user2)) - ->willReturn(['group2', 'group3']); $user3 = $this->createMock(IUser::class); - $this->userManager->expects($this->at(2)) - ->method('get') - ->with('user3') - ->willReturn($user3); - $this->groupManager->expects($this->at(3)) + + $calls = [ + [[$currentUser], ['group1', 'group2', 'group3']], + [[$user1], ['group1']], + [[$user2], ['group2', 'group3']], + [[$user3], ['group8', 'group9']], + ]; + $this->groupManager->expects($this->exactly(4)) ->method('getUserGroupIds') - ->with($this->equalTo($user3)) - ->willReturn(['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') + ->willReturnMap([ + ['user1', $user1], + ['user2', $user2], + ['user3', $user3], + ]); $this->knownUserService->method('isKnownToUser') ->willReturnMap([ @@ -697,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([ @@ -705,7 +678,7 @@ class ContactsStoreTest extends TestCase { ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'yes'], ]); - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ + /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->any()) ->method('search') @@ -784,7 +757,7 @@ class ContactsStoreTest extends TestCase { ], $entry[0]->getEMailAddresses()); } - public function testGetContactsWithFilterWithoutFullMatch() { + public function testGetContactsWithFilterWithoutFullMatch(): void { $this->config ->method('getAppValue') ->willReturnMap([ @@ -792,7 +765,7 @@ class ContactsStoreTest extends TestCase { ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'no'], ]); - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ + /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->any()) ->method('search') @@ -868,12 +841,20 @@ class ContactsStoreTest extends TestCase { ], $entry[0]->getEMailAddresses()); } - public function testFindOneUser() { - $this->config->expects($this->at(0))->method('getAppValue') - ->with($this->equalTo('core'), $this->equalTo('shareapi_allow_share_dialog_user_enumeration'), $this->equalTo('yes')) - ->willReturn('yes'); + public function testFindOneUser(): void { + $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|\PHPUnit\Framework\MockObject\MockObject $user */ + /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->once()) ->method('search') @@ -903,8 +884,8 @@ class ContactsStoreTest extends TestCase { ], $entry->getEMailAddresses()); } - public function testFindOneEMail() { - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ + public function testFindOneEMail(): void { + /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->once()) ->method('search') @@ -934,8 +915,8 @@ class ContactsStoreTest extends TestCase { ], $entry->getEMailAddresses()); } - public function testFindOneNotSupportedType() { - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ + public function testFindOneNotSupportedType(): void { + /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $entry = $this->contactsStore->findOne($user, 42, 'darren@roner.au'); @@ -943,8 +924,8 @@ class ContactsStoreTest extends TestCase { $this->assertEquals(null, $entry); } - public function testFindOneNoMatches() { - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ + public function testFindOneNoMatches(): void { + /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $this->contactsManager->expects($this->once()) ->method('search') @@ -970,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); + } } diff --git a/tests/lib/Contacts/ContactsMenu/EntryTest.php b/tests/lib/Contacts/ContactsMenu/EntryTest.php index 561afcf5dde..15f5b60b948 100644 --- a/tests/lib/Contacts/ContactsMenu/EntryTest.php +++ b/tests/lib/Contacts/ContactsMenu/EntryTest.php @@ -1,25 +1,8 @@ <?php /** - * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @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; @@ -29,9 +12,7 @@ use OC\Contacts\ContactsMenu\Entry; use Test\TestCase; class EntryTest extends TestCase { - - /** @var Entry */ - private $entry; + private Entry $entry; protected function setUp(): void { parent::setUp(); @@ -39,25 +20,25 @@ class EntryTest extends TestCase { $this->entry = new Entry(); } - public function testSetId() { + public function testSetId(): void { $this->entry->setId(123); $this->addToAssertionCount(1); } - public function testSetGetFullName() { + public function testSetGetFullName(): void { $fn = 'Danette Chaille'; $this->assertEquals('', $this->entry->getFullName()); $this->entry->setFullName($fn); $this->assertEquals($fn, $this->entry->getFullName()); } - public function testAddGetEMailAddresses() { + public function testAddGetEMailAddresses(): void { $this->assertEmpty($this->entry->getEMailAddresses()); $this->entry->addEMailAddress('user@example.com'); $this->assertEquals(['user@example.com'], $this->entry->getEMailAddresses()); } - public function testAddAndSortAction() { + public function testAddAndSortAction(): void { // Three actions, two with equal priority $action1 = new LinkAction(); $action2 = new LinkAction(); @@ -81,7 +62,7 @@ class EntryTest extends TestCase { $this->assertSame($action2, $sorted[2]); } - public function testSetGetProperties() { + public function testSetGetProperties(): void { $props = [ 'prop1' => 123, 'prop2' => 'string', @@ -94,7 +75,7 @@ class EntryTest extends TestCase { $this->assertEquals('string', $this->entry->getProperty('prop2')); } - public function testJsonSerialize() { + public function testJsonSerialize(): void { $expectedJson = [ 'id' => '123', 'fullName' => 'Guadalupe Frisbey', @@ -105,6 +86,12 @@ class EntryTest extends TestCase { 'emailAddresses' => ['user@example.com'], 'profileTitle' => null, 'profileUrl' => null, + 'status' => null, + 'statusMessage' => null, + 'statusMessageTimestamp' => null, + 'statusIcon' => null, + 'isUser' => false, + 'uid' => null, ]; $this->entry->setId(123); diff --git a/tests/lib/Contacts/ContactsMenu/ManagerTest.php b/tests/lib/Contacts/ContactsMenu/ManagerTest.php index 2f5acf61644..dd6c634c740 100644 --- a/tests/lib/Contacts/ContactsMenu/ManagerTest.php +++ b/tests/lib/Contacts/ContactsMenu/ManagerTest.php @@ -1,56 +1,38 @@ <?php /** - * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @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; use OC\Contacts\ContactsMenu\ActionProviderStore; use OC\Contacts\ContactsMenu\ContactsStore; +use OC\Contacts\ContactsMenu\Entry; use OC\Contacts\ContactsMenu\Manager; use OCP\App\IAppManager; use OCP\Constants; -use OCP\Contacts\ContactsMenu\IEntry; use OCP\Contacts\ContactsMenu\IProvider; use OCP\IConfig; use OCP\IUser; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class ManagerTest extends TestCase { - - /** @var ContactsStore|\PHPUnit\Framework\MockObject\MockObject */ + /** @var ContactsStore|MockObject */ private $contactsStore; - /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IAppManager|MockObject */ private $appManager; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IConfig|MockObject */ private $config; - /** @var ActionProviderStore|\PHPUnit\Framework\MockObject\MockObject */ + /** @var ActionProviderStore|MockObject */ private $actionProviderStore; - /** @var Manager */ - private $manager; + private Manager $manager; protected function setUp(): void { parent::setUp(); @@ -63,10 +45,10 @@ class ManagerTest extends TestCase { $this->manager = new Manager($this->contactsStore, $this->actionProviderStore, $this->appManager, $this->config); } - private function generateTestEntries() { + private function generateTestEntries(): array { $entries = []; foreach (range('Z', 'A') as $char) { - $entry = $this->createMock(IEntry::class); + $entry = $this->createMock(Entry::class); $entry->expects($this->any()) ->method('getFullName') ->willReturn('Contact ' . $char); @@ -75,20 +57,18 @@ class ManagerTest extends TestCase { return $entries; } - public function testGetFilteredEntries() { + public function testGetFilteredEntries(): void { $filter = 'con'; $user = $this->createMock(IUser::class); $entries = $this->generateTestEntries(); $provider = $this->createMock(IProvider::class); - $this->config->expects($this->at(0)) - ->method('getSystemValueInt') - ->with('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT) - ->willReturn(25); - $this->config->expects($this->at(1)) + $this->config->expects($this->exactly(2)) ->method('getSystemValueInt') - ->with('sharing.minSearchStringLength', 0) - ->willReturn(0); + ->willReturnMap([ + ['sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT, 25], + ['sharing.minSearchStringLength', 0, 0], + ]); $this->contactsStore->expects($this->once()) ->method('getContacts') ->with($user, $filter) @@ -113,20 +93,18 @@ class ManagerTest extends TestCase { $this->assertEquals($expected, $data); } - public function testGetFilteredEntriesLimit() { + public function testGetFilteredEntriesLimit(): void { $filter = 'con'; $user = $this->createMock(IUser::class); $entries = $this->generateTestEntries(); $provider = $this->createMock(IProvider::class); - $this->config->expects($this->at(0)) + $this->config->expects($this->exactly(2)) ->method('getSystemValueInt') - ->with('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT) - ->willReturn(3); - $this->config->expects($this->at(1)) - ->method('getSystemValueInt') - ->with('sharing.minSearchStringLength', 0) - ->willReturn(0); + ->willReturnMap([ + ['sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT, 3], + ['sharing.minSearchStringLength', 0, 0], + ]); $this->contactsStore->expects($this->once()) ->method('getContacts') ->with($user, $filter) @@ -151,19 +129,17 @@ class ManagerTest extends TestCase { $this->assertEquals($expected, $data); } - public function testGetFilteredEntriesMinSearchStringLength() { + public function testGetFilteredEntriesMinSearchStringLength(): void { $filter = 'con'; $user = $this->createMock(IUser::class); $provider = $this->createMock(IProvider::class); - $this->config->expects($this->at(0)) - ->method('getSystemValueInt') - ->with('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT) - ->willReturn(3); - $this->config->expects($this->at(1)) + $this->config->expects($this->exactly(2)) ->method('getSystemValueInt') - ->with('sharing.minSearchStringLength', 0) - ->willReturn(4); + ->willReturnMap([ + ['sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT, 3], + ['sharing.minSearchStringLength', 0, 4], + ]); $this->appManager->expects($this->once()) ->method('isEnabledForUser') ->with($this->equalTo('contacts'), $user) @@ -178,7 +154,7 @@ class ManagerTest extends TestCase { $this->assertEquals($expected, $data); } - public function testFindOne() { + public function testFindOne(): void { $shareTypeFilter = 42; $shareWithFilter = 'foobar'; @@ -201,7 +177,7 @@ class ManagerTest extends TestCase { $this->assertEquals($entry, $data); } - public function testFindOne404() { + public function testFindOne404(): void { $shareTypeFilter = 42; $shareWithFilter = 'foobar'; diff --git a/tests/lib/Contacts/ContactsMenu/Providers/EMailproviderTest.php b/tests/lib/Contacts/ContactsMenu/Providers/EMailproviderTest.php index c0052469aba..648351c2ca9 100644 --- a/tests/lib/Contacts/ContactsMenu/Providers/EMailproviderTest.php +++ b/tests/lib/Contacts/ContactsMenu/Providers/EMailproviderTest.php @@ -1,25 +1,8 @@ <?php /** - * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @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\Providers; @@ -29,18 +12,17 @@ use OCP\Contacts\ContactsMenu\IActionFactory; use OCP\Contacts\ContactsMenu\IEntry; use OCP\Contacts\ContactsMenu\ILinkAction; use OCP\IURLGenerator; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class EMailproviderTest extends TestCase { - - /** @var IActionFactory|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IActionFactory|MockObject */ private $actionFactory; - /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IURLGenerator|MockObject */ private $urlGenerator; - /** @var EMailProvider */ - private $provider; + private EMailProvider $provider; protected function setUp(): void { parent::setUp(); @@ -51,7 +33,7 @@ class EMailproviderTest extends TestCase { $this->provider = new EMailProvider($this->actionFactory, $this->urlGenerator); } - public function testProcess() { + public function testProcess(): void { $entry = $this->createMock(IEntry::class); $action = $this->createMock(ILinkAction::class); $iconUrl = 'https://example.com/img/actions/icon.svg'; @@ -78,9 +60,8 @@ class EMailproviderTest extends TestCase { $this->provider->process($entry); } - public function testProcessEmptyAddress() { + public function testProcessEmptyAddress(): void { $entry = $this->createMock(IEntry::class); - $action = $this->createMock(ILinkAction::class); $iconUrl = 'https://example.com/img/actions/icon.svg'; $this->urlGenerator->expects($this->once()) ->method('imagePath') diff --git a/tests/lib/Contacts/ContactsMenu/Providers/LocalTimeProviderTest.php b/tests/lib/Contacts/ContactsMenu/Providers/LocalTimeProviderTest.php new file mode 100644 index 00000000000..cc53c0bcfcf --- /dev/null +++ b/tests/lib/Contacts/ContactsMenu/Providers/LocalTimeProviderTest.php @@ -0,0 +1,197 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace lib\Contacts\ContactsMenu\Providers; + +use OC\Contacts\ContactsMenu\Providers\LocalTimeProvider; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Contacts\ContactsMenu\IActionFactory; +use OCP\Contacts\ContactsMenu\IEntry; +use OCP\Contacts\ContactsMenu\ILinkAction; +use OCP\IConfig; +use OCP\IDateTimeFormatter; +use OCP\IL10N; +use OCP\IURLGenerator; +use OCP\IUser; +use OCP\IUserManager; +use OCP\IUserSession; +use OCP\L10N\IFactory as IL10NFactory; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class LocalTimeProviderTest extends TestCase { + + private IActionFactory&MockObject $actionFactory; + private IL10N&MockObject $l; + private IL10NFactory&MockObject $l10nFactory; + private IURLGenerator&MockObject $urlGenerator; + private IUserManager&MockObject $userManager; + private ITimeFactory&MockObject $timeFactory; + private IUserSession&MockObject $userSession; + private IDateTimeFormatter&MockObject $dateTimeFormatter; + private IConfig&MockObject $config; + + private LocalTimeProvider $provider; + + protected function setUp(): void { + parent::setUp(); + + $this->actionFactory = $this->createMock(IActionFactory::class); + $this->l10nFactory = $this->createMock(IL10NFactory::class); + $this->l = $this->createMock(IL10N::class); + $this->l->expects($this->any()) + ->method('t') + ->willReturnCallback(function ($text, $parameters = []) { + return vsprintf($text, $parameters); + }); + $this->l->expects($this->any()) + ->method('n') + ->willReturnCallback(function ($text, $textPlural, $n, $parameters = []) { + $formatted = str_replace('%n', (string)$n, $n === 1 ? $text : $textPlural); + return vsprintf($formatted, $parameters); + }); + $this->urlGenerator = $this->createMock(IURLGenerator::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class); + $this->config = $this->createMock(IConfig::class); + $this->userSession = $this->createMock(IUserSession::class); + + $this->provider = new LocalTimeProvider( + $this->actionFactory, + $this->l10nFactory, + $this->urlGenerator, + $this->userManager, + $this->timeFactory, + $this->dateTimeFormatter, + $this->config, + $this->userSession, + ); + } + + public static function dataTestProcess(): array { + return [ + 'no current user' => [ + false, + null, + null, + 'Local time: 10:24', + ], + 'both UTC' => [ + true, + null, + null, + '10:24 • same time', + ], + 'both same time zone' => [ + true, + 'Europe/Berlin', + 'Europe/Berlin', + '11:24 • same time', + ], + '1h behind' => [ + true, + 'Europe/Berlin', + 'Europe/London', + '10:24 • 1h behind', + ], + '4:45h ahead' => [ + true, + 'Europe/Berlin', + 'Asia/Kathmandu', + '16:09 • 4h45m ahead', + ], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestProcess')] + public function testProcess(bool $hasCurrentUser, ?string $currentUserTZ, ?string $targetUserTZ, string $expected): void { + $entry = $this->createMock(IEntry::class); + $entry->expects($this->once()) + ->method('getProperty') + ->with('UID') + ->willReturn('user1'); + + $user = $this->createMock(IUser::class); + $user->method('getUID') + ->willReturn('user1'); + $this->userManager->expects($this->once()) + ->method('get') + ->with('user1') + ->willReturn($user); + + $this->l10nFactory->method('get') + ->with('lib') + ->willReturn($this->l); + + $this->config->method('getSystemValueString') + ->with('default_timezone', 'UTC') + ->willReturn('UTC'); + $this->config + ->method('getUserValue') + ->willReturnMap([ + ['user1', 'core', 'timezone', '', $targetUserTZ], + ['currentUser', 'core', 'timezone', '', $currentUserTZ], + ]); + + if ($hasCurrentUser) { + $currentUser = $this->createMock(IUser::class); + $currentUser->method('getUID') + ->willReturn('currentUser'); + $this->userSession->method('getUser') + ->willReturn($currentUser); + } + + $this->timeFactory->method('getDateTime') + ->willReturnCallback(fn ($time, $tz) => (new \DateTime('2023-01-04 10:24:43', new \DateTimeZone('UTC')))->setTimezone($tz)); + + $this->dateTimeFormatter->method('formatTime') + ->willReturnCallback(fn (\DateTime $time) => $time->format('H:i')); + + $this->urlGenerator->method('imagePath') + ->willReturn('actions/recent.svg'); + $this->urlGenerator->method('getAbsoluteURL') + ->with('actions/recent.svg') + ->willReturn('https://localhost/actions/recent.svg'); + + $action = $this->createMock(ILinkAction::class); + $this->actionFactory->expects($this->once()) + ->method('newLinkAction') + ->with( + 'https://localhost/actions/recent.svg', + $expected, + '#', + 'timezone' + ) + ->willReturn($action); + + $entry->expects($this->once()) + ->method('addAction') + ->with($action); + + $this->provider->process($entry); + } + + public function testProcessNoUser(): void { + $entry = $this->createMock(IEntry::class); + $entry->expects($this->once()) + ->method('getProperty') + ->with('UID') + ->willReturn('user1'); + + $user = $this->createMock(IUser::class); + $user->method('getUID') + ->willReturn(null); + + $entry->expects($this->never()) + ->method('addAction'); + + $this->provider->process($entry); + } +} |