diff options
Diffstat (limited to 'apps/dav/tests/unit/Command')
-rw-r--r-- | apps/dav/tests/unit/Command/DeleteCalendarTest.php | 231 | ||||
-rw-r--r-- | apps/dav/tests/unit/Command/ListAddressbooksTest.php | 107 | ||||
-rw-r--r-- | apps/dav/tests/unit/Command/ListCalendarSharesTest.php | 172 | ||||
-rw-r--r-- | apps/dav/tests/unit/Command/ListCalendarsTest.php | 59 | ||||
-rw-r--r-- | apps/dav/tests/unit/Command/MoveCalendarTest.php | 336 | ||||
-rw-r--r-- | apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php | 46 |
6 files changed, 674 insertions, 277 deletions
diff --git a/apps/dav/tests/unit/Command/DeleteCalendarTest.php b/apps/dav/tests/unit/Command/DeleteCalendarTest.php new file mode 100644 index 00000000000..2bd269de6dc --- /dev/null +++ b/apps/dav/tests/unit/Command/DeleteCalendarTest.php @@ -0,0 +1,231 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\DAV\Tests\unit\Command; + +use OCA\DAV\CalDAV\BirthdayService; +use OCA\DAV\CalDAV\CalDavBackend; +use OCA\DAV\Command\DeleteCalendar; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; +use Symfony\Component\Console\Tester\CommandTester; +use Test\TestCase; + +/** + * Class DeleteCalendarTest + * + * @package OCA\DAV\Tests\Command + */ +class DeleteCalendarTest extends TestCase { + public const USER = 'user'; + public const NAME = 'calendar'; + + private CalDavBackend&MockObject $calDav; + private IConfig&MockObject $config; + private IL10N&MockObject $l10n; + private IUserManager&MockObject $userManager; + private LoggerInterface&MockObject $logger; + private DeleteCalendar $command; + + protected function setUp(): void { + parent::setUp(); + + $this->calDav = $this->createMock(CalDavBackend::class); + $this->config = $this->createMock(IConfig::class); + $this->l10n = $this->createMock(IL10N::class); + $this->userManager = $this->createMock(IUserManager::class); + $this->logger = $this->createMock(LoggerInterface::class); + + $this->command = new DeleteCalendar( + $this->calDav, + $this->config, + $this->l10n, + $this->userManager, + $this->logger + ); + } + + public function testInvalidUser(): void { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage( + 'User <' . self::USER . '> is unknown.'); + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USER) + ->willReturn(false); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => self::USER, + 'name' => self::NAME, + ]); + } + + public function testNoCalendarName(): void { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage( + 'Please specify a calendar name or --birthday'); + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USER) + ->willReturn(true); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => self::USER, + ]); + } + + public function testInvalidCalendar(): void { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage( + 'User <' . self::USER . '> has no calendar named <' . self::NAME . '>.'); + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USER) + ->willReturn(true); + $this->calDav->expects($this->once()) + ->method('getCalendarByUri') + ->with( + 'principals/users/' . self::USER, + self::NAME + ) + ->willReturn(null); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => self::USER, + 'name' => self::NAME, + ]); + } + + public function testDelete(): void { + $id = 1234; + $calendar = [ + 'id' => $id, + 'principaluri' => 'principals/users/' . self::USER, + 'uri' => self::NAME, + ]; + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USER) + ->willReturn(true); + $this->calDav->expects($this->once()) + ->method('getCalendarByUri') + ->with( + 'principals/users/' . self::USER, + self::NAME + ) + ->willReturn($calendar); + $this->calDav->expects($this->once()) + ->method('deleteCalendar') + ->with($id, false); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => self::USER, + 'name' => self::NAME, + ]); + } + + public function testForceDelete(): void { + $id = 1234; + $calendar = [ + 'id' => $id, + 'principaluri' => 'principals/users/' . self::USER, + 'uri' => self::NAME + ]; + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USER) + ->willReturn(true); + $this->calDav->expects($this->once()) + ->method('getCalendarByUri') + ->with( + 'principals/users/' . self::USER, + self::NAME + ) + ->willReturn($calendar); + $this->calDav->expects($this->once()) + ->method('deleteCalendar') + ->with($id, true); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => self::USER, + 'name' => self::NAME, + '-f' => true + ]); + } + + public function testDeleteBirthday(): void { + $id = 1234; + $calendar = [ + 'id' => $id, + 'principaluri' => 'principals/users/' . self::USER, + 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI, + '{DAV:}displayname' => 'Test', + ]; + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USER) + ->willReturn(true); + $this->calDav->expects($this->once()) + ->method('getCalendarByUri') + ->with( + 'principals/users/' . self::USER, + BirthdayService::BIRTHDAY_CALENDAR_URI + ) + ->willReturn($calendar); + $this->calDav->expects($this->once()) + ->method('deleteCalendar') + ->with($id); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => self::USER, + '--birthday' => true, + ]); + } + + public function testBirthdayHasPrecedence(): void { + $calendar = [ + 'id' => 1234, + 'principaluri' => 'principals/users/' . self::USER, + 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI, + '{DAV:}displayname' => 'Test', + ]; + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USER) + ->willReturn(true); + $this->calDav->expects($this->once()) + ->method('getCalendarByUri') + ->with( + 'principals/users/' . self::USER, + BirthdayService::BIRTHDAY_CALENDAR_URI + ) + ->willReturn($calendar); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => self::USER, + 'name' => self::NAME, + '--birthday' => true, + ]); + } +} diff --git a/apps/dav/tests/unit/Command/ListAddressbooksTest.php b/apps/dav/tests/unit/Command/ListAddressbooksTest.php new file mode 100644 index 00000000000..2768ed576c3 --- /dev/null +++ b/apps/dav/tests/unit/Command/ListAddressbooksTest.php @@ -0,0 +1,107 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\DAV\Tests\unit\Command; + +use OCA\DAV\CardDAV\CardDavBackend; +use OCA\DAV\Command\ListAddressbooks; +use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\Console\Tester\CommandTester; +use Test\TestCase; + +/** + * Class ListCalendarsTest + * + * @package OCA\DAV\Tests\Command + */ +class ListAddressbooksTest extends TestCase { + private IUserManager&MockObject $userManager; + private CardDavBackend&MockObject $cardDavBackend; + private ListAddressbooks $command; + + public const USERNAME = 'username'; + + protected function setUp(): void { + parent::setUp(); + + $this->userManager = $this->createMock(IUserManager::class); + $this->cardDavBackend = $this->createMock(CardDavBackend::class); + + $this->command = new ListAddressbooks( + $this->userManager, + $this->cardDavBackend + ); + } + + public function testWithBadUser(): void { + $this->expectException(\InvalidArgumentException::class); + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USERNAME) + ->willReturn(false); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => self::USERNAME, + ]); + $this->assertStringContainsString('User <' . self::USERNAME . '> in unknown', $commandTester->getDisplay()); + } + + public function testWithCorrectUserWithNoCalendars(): void { + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USERNAME) + ->willReturn(true); + + $this->cardDavBackend->expects($this->once()) + ->method('getAddressBooksForUser') + ->with('principals/users/' . self::USERNAME) + ->willReturn([]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => self::USERNAME, + ]); + $this->assertStringContainsString('User <' . self::USERNAME . "> has no addressbooks\n", $commandTester->getDisplay()); + } + + public static function dataExecute(): array { + return [ + [false, '✓'], + [true, 'x'] + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecute')] + public function testWithCorrectUser(bool $readOnly, string $output): void { + $this->userManager->expects($this->once()) + ->method('userExists') + ->with(self::USERNAME) + ->willReturn(true); + + $this->cardDavBackend->expects($this->once()) + ->method('getAddressBooksForUser') + ->with('principals/users/' . self::USERNAME) + ->willReturn([ + [ + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => $readOnly, + 'uri' => 'test', + '{DAV:}displayname' => 'dp', + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => 'owner-principal', + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname' => 'owner-dp', + ] + ]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => self::USERNAME, + ]); + $this->assertStringContainsString($output, $commandTester->getDisplay()); + } +} diff --git a/apps/dav/tests/unit/Command/ListCalendarSharesTest.php b/apps/dav/tests/unit/Command/ListCalendarSharesTest.php new file mode 100644 index 00000000000..e5d4251cbf9 --- /dev/null +++ b/apps/dav/tests/unit/Command/ListCalendarSharesTest.php @@ -0,0 +1,172 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\DAV\Tests\unit\Command; + +use OCA\DAV\CalDAV\CalDavBackend; +use OCA\DAV\Command\ListCalendarShares; +use OCA\DAV\Connector\Sabre\Principal; +use OCA\DAV\DAV\Sharing\SharingMapper; +use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\Console\Tester\CommandTester; +use Test\TestCase; + +class ListCalendarSharesTest extends TestCase { + + private IUserManager&MockObject $userManager; + private Principal&MockObject $principal; + private CalDavBackend&MockObject $caldav; + private SharingMapper $sharingMapper; + private ListCalendarShares $command; + + protected function setUp(): void { + parent::setUp(); + + $this->userManager = $this->createMock(IUserManager::class); + $this->principal = $this->createMock(Principal::class); + $this->caldav = $this->createMock(CalDavBackend::class); + $this->sharingMapper = $this->createMock(SharingMapper::class); + + $this->command = new ListCalendarShares( + $this->userManager, + $this->principal, + $this->caldav, + $this->sharingMapper, + ); + } + + public function testUserUnknown(): void { + $user = 'bob'; + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage("User $user is unknown"); + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with($user) + ->willReturn(false); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => $user, + ]); + } + + public function testPrincipalNotFound(): void { + $user = 'bob'; + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage("Unable to fetch principal for user $user"); + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with($user) + ->willReturn(true); + + $this->principal->expects($this->once()) + ->method('getPrincipalByPath') + ->with('principals/users/' . $user) + ->willReturn(null); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => $user, + ]); + } + + public function testNoCalendarShares(): void { + $user = 'bob'; + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with($user) + ->willReturn(true); + + $this->principal->expects($this->once()) + ->method('getPrincipalByPath') + ->with('principals/users/' . $user) + ->willReturn([ + 'uri' => 'principals/users/' . $user, + ]); + + $this->principal->expects($this->once()) + ->method('getGroupMembership') + ->willReturn([]); + $this->principal->expects($this->once()) + ->method('getCircleMembership') + ->willReturn([]); + + $this->sharingMapper->expects($this->once()) + ->method('getSharesByPrincipals') + ->willReturn([]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => $user, + ]); + + $this->assertStringContainsString( + "User $user has no calendar shares", + $commandTester->getDisplay() + ); + } + + public function testFilterByCalendarId(): void { + $user = 'bob'; + + $this->userManager->expects($this->once()) + ->method('userExists') + ->with($user) + ->willReturn(true); + + $this->principal->expects($this->once()) + ->method('getPrincipalByPath') + ->with('principals/users/' . $user) + ->willReturn([ + 'uri' => 'principals/users/' . $user, + ]); + + $this->principal->expects($this->once()) + ->method('getGroupMembership') + ->willReturn([]); + $this->principal->expects($this->once()) + ->method('getCircleMembership') + ->willReturn([]); + + $this->sharingMapper->expects($this->once()) + ->method('getSharesByPrincipals') + ->willReturn([ + [ + 'id' => 1000, + 'principaluri' => 'principals/users/bob', + 'type' => 'calendar', + 'access' => 2, + 'resourceid' => 10 + ], + [ + 'id' => 1001, + 'principaluri' => 'principals/users/bob', + 'type' => 'calendar', + 'access' => 3, + 'resourceid' => 11 + ], + ]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'uid' => $user, + '--calendar-id' => 10, + ]); + + $this->assertStringNotContainsString( + '1001', + $commandTester->getDisplay() + ); + } +} diff --git a/apps/dav/tests/unit/Command/ListCalendarsTest.php b/apps/dav/tests/unit/Command/ListCalendarsTest.php index c98365e25fb..d398a7c772f 100644 --- a/apps/dav/tests/unit/Command/ListCalendarsTest.php +++ b/apps/dav/tests/unit/Command/ListCalendarsTest.php @@ -1,36 +1,17 @@ <?php + +declare(strict_types=1); /** - * - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Georg Ehrke <oc.list@georgehrke.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Citharel <nextcloud@tcit.fr> - * - * @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: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - -namespace OCA\DAV\Tests\Command; +namespace OCA\DAV\Tests\unit\Command; use OCA\DAV\CalDAV\BirthdayService; use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\Command\ListCalendars; use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\Console\Tester\CommandTester; use Test\TestCase; @@ -40,15 +21,9 @@ use Test\TestCase; * @package OCA\DAV\Tests\Command */ class ListCalendarsTest extends TestCase { - - /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject $userManager */ - private $userManager; - - /** @var CalDavBackend|\PHPUnit\Framework\MockObject\MockObject $l10n */ - private $calDav; - - /** @var ListCalendars */ - private $command; + private IUserManager&MockObject $userManager; + private CalDavBackend&MockObject $calDav; + private ListCalendars $command; public const USERNAME = 'username'; @@ -64,7 +39,7 @@ class ListCalendarsTest extends TestCase { ); } - public function testWithBadUser() { + public function testWithBadUser(): void { $this->expectException(\InvalidArgumentException::class); $this->userManager->expects($this->once()) @@ -76,10 +51,10 @@ class ListCalendarsTest extends TestCase { $commandTester->execute([ 'uid' => self::USERNAME, ]); - $this->assertStringContainsString("User <" . self::USERNAME . "> in unknown", $commandTester->getDisplay()); + $this->assertStringContainsString('User <' . self::USERNAME . '> in unknown', $commandTester->getDisplay()); } - public function testWithCorrectUserWithNoCalendars() { + public function testWithCorrectUserWithNoCalendars(): void { $this->userManager->expects($this->once()) ->method('userExists') ->with(self::USERNAME) @@ -94,20 +69,18 @@ class ListCalendarsTest extends TestCase { $commandTester->execute([ 'uid' => self::USERNAME, ]); - $this->assertStringContainsString("User <" . self::USERNAME . "> has no calendars\n", $commandTester->getDisplay()); + $this->assertStringContainsString('User <' . self::USERNAME . "> has no calendars\n", $commandTester->getDisplay()); } - public function dataExecute() { + public static function dataExecute(): array { return [ [false, '✓'], [true, 'x'] ]; } - /** - * @dataProvider dataExecute - */ - public function testWithCorrectUser(bool $readOnly, string $output) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecute')] + public function testWithCorrectUser(bool $readOnly, string $output): void { $this->userManager->expects($this->once()) ->method('userExists') ->with(self::USERNAME) diff --git a/apps/dav/tests/unit/Command/MoveCalendarTest.php b/apps/dav/tests/unit/Command/MoveCalendarTest.php index 73443eacb7c..e9f016961f2 100644 --- a/apps/dav/tests/unit/Command/MoveCalendarTest.php +++ b/apps/dav/tests/unit/Command/MoveCalendarTest.php @@ -1,31 +1,11 @@ <?php + +declare(strict_types=1); /** - * - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Georg Ehrke <oc.list@georgehrke.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Citharel <nextcloud@tcit.fr> - * - * @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: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - -namespace OCA\DAV\Tests\Command; +namespace OCA\DAV\Tests\unit\Command; use InvalidArgumentException; use OCA\DAV\CalDAV\CalDavBackend; @@ -35,6 +15,8 @@ use OCP\IGroupManager; use OCP\IL10N; use OCP\IUserManager; use OCP\Share\IManager; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Symfony\Component\Console\Tester\CommandTester; use Test\TestCase; @@ -44,27 +26,14 @@ use Test\TestCase; * @package OCA\DAV\Tests\Command */ class MoveCalendarTest extends TestCase { - - /** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject $userManager */ - private $userManager; - - /** @var \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject $groupManager */ - private $groupManager; - - /** @var \OCP\Share\IManager|\PHPUnit\Framework\MockObject\MockObject $shareManager */ - private $shareManager; - - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject $l10n */ - private $config; - - /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject $l10n */ - private $l10n; - - /** @var CalDavBackend|\PHPUnit\Framework\MockObject\MockObject $l10n */ - private $calDav; - - /** @var MoveCalendar */ - private $command; + private IUserManager&MockObject $userManager; + private IGroupManager&MockObject $groupManager; + private \OCP\Share\IManager&MockObject $shareManager; + private IConfig&MockObject $config; + private IL10N&MockObject $l10n; + private CalDavBackend&MockObject $calDav; + private LoggerInterface&MockObject $logger; + private MoveCalendar $command; protected function setUp(): void { parent::setUp(); @@ -75,6 +44,7 @@ class MoveCalendarTest extends TestCase { $this->config = $this->createMock(IConfig::class); $this->l10n = $this->createMock(IL10N::class); $this->calDav = $this->createMock(CalDavBackend::class); + $this->logger = $this->createMock(LoggerInterface::class); $this->command = new MoveCalendar( $this->userManager, @@ -82,37 +52,28 @@ class MoveCalendarTest extends TestCase { $this->shareManager, $this->config, $this->l10n, - $this->calDav + $this->calDav, + $this->logger ); } - public function dataExecute() { + public static function dataExecute(): array { return [ [false, true], [true, false] ]; } - /** - * @dataProvider dataExecute - * - * @param $userOriginExists - * @param $userDestinationExists - */ - public function testWithBadUserOrigin($userOriginExists, $userDestinationExists) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataExecute')] + public function testWithBadUserOrigin(bool $userOriginExists, bool $userDestinationExists): void { $this->expectException(\InvalidArgumentException::class); - $this->userManager->expects($this->at(0)) + $this->userManager->expects($this->exactly($userOriginExists ? 2 : 1)) ->method('userExists') - ->with('user') - ->willReturn($userOriginExists); - - if (!$userDestinationExists) { - $this->userManager->expects($this->at(1)) - ->method('userExists') - ->with('user2') - ->willReturn($userDestinationExists); - } + ->willReturnMap([ + ['user', $userOriginExists], + ['user2', $userDestinationExists], + ]); $commandTester = new CommandTester($this->command); $commandTester->execute([ @@ -123,19 +84,16 @@ class MoveCalendarTest extends TestCase { } - public function testMoveWithInexistantCalendar() { + public function testMoveWithInexistantCalendar(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('User <user> has no calendar named <personal>. You can run occ dav:list-calendars to list calendars URIs for this user.'); - $this->userManager->expects($this->at(0)) - ->method('userExists') - ->with('user') - ->willReturn(true); - - $this->userManager->expects($this->at(1)) + $this->userManager->expects($this->exactly(2)) ->method('userExists') - ->with('user2') - ->willReturn(true); + ->willReturnMap([ + ['user', true], + ['user2', true], + ]); $this->calDav->expects($this->once())->method('getCalendarByUri') ->with('principals/users/user', 'personal') @@ -150,30 +108,26 @@ class MoveCalendarTest extends TestCase { } - public function testMoveWithExistingDestinationCalendar() { + public function testMoveWithExistingDestinationCalendar(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('User <user2> already has a calendar named <personal>.'); - $this->userManager->expects($this->at(0)) - ->method('userExists') - ->with('user') - ->willReturn(true); - - $this->userManager->expects($this->at(1)) + $this->userManager->expects($this->exactly(2)) ->method('userExists') - ->with('user2') - ->willReturn(true); - - $this->calDav->expects($this->at(0))->method('getCalendarByUri') - ->with('principals/users/user', 'personal') - ->willReturn([ - 'id' => 1234, + ->willReturnMap([ + ['user', true], + ['user2', true], ]); - $this->calDav->expects($this->at(1))->method('getCalendarByUri') - ->with('principals/users/user2', 'personal') - ->willReturn([ - 'id' => 1234, + $this->calDav->expects($this->exactly(2)) + ->method('getCalendarByUri') + ->willReturnMap([ + ['principals/users/user', 'personal', [ + 'id' => 1234, + ]], + ['principals/users/user2', 'personal', [ + 'id' => 1234, + ]], ]); $commandTester = new CommandTester($this->command); @@ -184,26 +138,22 @@ class MoveCalendarTest extends TestCase { ]); } - public function testMove() { - $this->userManager->expects($this->at(0)) + public function testMove(): void { + $this->userManager->expects($this->exactly(2)) ->method('userExists') - ->with('user') - ->willReturn(true); - - $this->userManager->expects($this->at(1)) - ->method('userExists') - ->with('user2') - ->willReturn(true); - - $this->calDav->expects($this->at(0))->method('getCalendarByUri') - ->with('principals/users/user', 'personal') - ->willReturn([ - 'id' => 1234, + ->willReturnMap([ + ['user', true], + ['user2', true], ]); - $this->calDav->expects($this->at(1))->method('getCalendarByUri') - ->with('principals/users/user2', 'personal') - ->willReturn(null); + $this->calDav->expects($this->exactly(2)) + ->method('getCalendarByUri') + ->willReturnMap([ + ['principals/users/user', 'personal', [ + 'id' => 1234, + ]], + ['principals/users/user2', 'personal', null], + ]); $this->calDav->expects($this->once())->method('getShares') ->with(1234) @@ -216,40 +166,34 @@ class MoveCalendarTest extends TestCase { 'destinationuid' => 'user2', ]); - $this->assertStringContainsString("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay()); + $this->assertStringContainsString('[OK] Calendar <personal> was moved from user <user> to <user2>', $commandTester->getDisplay()); } - public function dataTestMoveWithDestinationNotPartOfGroup(): array { + public static function dataTestMoveWithDestinationNotPartOfGroup(): array { return [ [true], [false] ]; } - /** - * @dataProvider dataTestMoveWithDestinationNotPartOfGroup - */ - public function testMoveWithDestinationNotPartOfGroup(bool $shareWithGroupMembersOnly) { - $this->userManager->expects($this->at(0)) - ->method('userExists') - ->with('user') - ->willReturn(true); - - $this->userManager->expects($this->at(1)) + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestMoveWithDestinationNotPartOfGroup')] + public function testMoveWithDestinationNotPartOfGroup(bool $shareWithGroupMembersOnly): void { + $this->userManager->expects($this->exactly(2)) ->method('userExists') - ->with('user2') - ->willReturn(true); - - $this->calDav->expects($this->at(0))->method('getCalendarByUri') - ->with('principals/users/user', 'personal') - ->willReturn([ - 'id' => 1234, - 'uri' => 'personal' + ->willReturnMap([ + ['user', true], + ['user2', true], ]); - $this->calDav->expects($this->at(1))->method('getCalendarByUri') - ->with('principals/users/user2', 'personal') - ->willReturn(null); + $this->calDav->expects($this->exactly(2)) + ->method('getCalendarByUri') + ->willReturnMap([ + ['principals/users/user', 'personal', [ + 'id' => 1234, + 'uri' => 'personal', + ]], + ['principals/users/user2', 'personal', null], + ]); $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly') ->willReturn($shareWithGroupMembersOnly); @@ -261,7 +205,7 @@ class MoveCalendarTest extends TestCase { ]); if ($shareWithGroupMembersOnly === true) { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("User <user2> is not part of the group <nextclouders> with whom the calendar <personal> was shared. You may use -f to move the calendar while deleting this share."); + $this->expectExceptionMessage('User <user2> is not part of the group <nextclouders> with whom the calendar <personal> was shared. You may use -f to move the calendar while deleting this share.'); } $commandTester = new CommandTester($this->command); @@ -272,27 +216,23 @@ class MoveCalendarTest extends TestCase { ]); } - public function testMoveWithDestinationPartOfGroup() { - $this->userManager->expects($this->at(0)) + public function testMoveWithDestinationPartOfGroup(): void { + $this->userManager->expects($this->exactly(2)) ->method('userExists') - ->with('user') - ->willReturn(true); - - $this->userManager->expects($this->at(1)) - ->method('userExists') - ->with('user2') - ->willReturn(true); - - $this->calDav->expects($this->at(0))->method('getCalendarByUri') - ->with('principals/users/user', 'personal') - ->willReturn([ - 'id' => 1234, - 'uri' => 'personal' + ->willReturnMap([ + ['user', true], + ['user2', true], ]); - $this->calDav->expects($this->at(1))->method('getCalendarByUri') - ->with('principals/users/user2', 'personal') - ->willReturn(null); + $this->calDav->expects($this->exactly(2)) + ->method('getCalendarByUri') + ->willReturnMap([ + ['principals/users/user', 'personal', [ + 'id' => 1234, + 'uri' => 'personal', + ]], + ['principals/users/user2', 'personal', null], + ]); $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly') ->willReturn(true); @@ -314,31 +254,27 @@ class MoveCalendarTest extends TestCase { 'destinationuid' => 'user2', ]); - $this->assertStringContainsString("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay()); + $this->assertStringContainsString('[OK] Calendar <personal> was moved from user <user> to <user2>', $commandTester->getDisplay()); } - public function testMoveWithDestinationNotPartOfGroupAndForce() { - $this->userManager->expects($this->at(0)) + public function testMoveWithDestinationNotPartOfGroupAndForce(): void { + $this->userManager->expects($this->exactly(2)) ->method('userExists') - ->with('user') - ->willReturn(true); - - $this->userManager->expects($this->at(1)) - ->method('userExists') - ->with('user2') - ->willReturn(true); - - $this->calDav->expects($this->at(0))->method('getCalendarByUri') - ->with('principals/users/user', 'personal') - ->willReturn([ - 'id' => 1234, - 'uri' => 'personal', - '{DAV:}displayname' => 'Personal' + ->willReturnMap([ + ['user', true], + ['user2', true], ]); - $this->calDav->expects($this->at(1))->method('getCalendarByUri') - ->with('principals/users/user2', 'personal') - ->willReturn(null); + $this->calDav->expects($this->exactly(2)) + ->method('getCalendarByUri') + ->willReturnMap([ + ['principals/users/user', 'personal', [ + 'id' => 1234, + 'uri' => 'personal', + '{DAV:}displayname' => 'Personal' + ]], + ['principals/users/user2', 'personal', null], + ]); $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly') ->willReturn(true); @@ -361,54 +297,48 @@ class MoveCalendarTest extends TestCase { '--force' => true ]); - $this->assertStringContainsString("[OK] Calendar <personal> was moved from user <user> to <user2>", $commandTester->getDisplay()); + $this->assertStringContainsString('[OK] Calendar <personal> was moved from user <user> to <user2>', $commandTester->getDisplay()); } - public function dataTestMoveWithCalendarAlreadySharedToDestination(): array { + public static function dataTestMoveWithCalendarAlreadySharedToDestination(): array { return [ [true], [false] ]; } - /** - * @dataProvider dataTestMoveWithCalendarAlreadySharedToDestination - */ - public function testMoveWithCalendarAlreadySharedToDestination(bool $force) { - $this->userManager->expects($this->at(0)) + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestMoveWithCalendarAlreadySharedToDestination')] + public function testMoveWithCalendarAlreadySharedToDestination(bool $force): void { + $this->userManager->expects($this->exactly(2)) ->method('userExists') - ->with('user') - ->willReturn(true); - - $this->userManager->expects($this->at(1)) - ->method('userExists') - ->with('user2') - ->willReturn(true); - - $this->calDav->expects($this->at(0))->method('getCalendarByUri') - ->with('principals/users/user', 'personal') - ->willReturn([ - 'id' => 1234, - 'uri' => 'personal', - '{DAV:}displayname' => 'Personal', + ->willReturnMap([ + ['user', true], + ['user2', true], ]); - $this->calDav->expects($this->at(1))->method('getCalendarByUri') - ->with('principals/users/user2', 'personal') - ->willReturn(null); + $this->calDav->expects($this->exactly(2)) + ->method('getCalendarByUri') + ->willReturnMap([ + ['principals/users/user', 'personal', [ + 'id' => 1234, + 'uri' => 'personal', + '{DAV:}displayname' => 'Personal' + ]], + ['principals/users/user2', 'personal', null], + ]); $this->calDav->expects($this->once())->method('getShares') - ->with(1234) - ->willReturn([ - [ - 'href' => 'principal:principals/users/user2', - '{DAV:}displayname' => 'Personal' - ] - ]); + ->with(1234) + ->willReturn([ + [ + 'href' => 'principal:principals/users/user2', + '{DAV:}displayname' => 'Personal' + ] + ]); if ($force === false) { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("The calendar <personal> is already shared to user <user2>.You may use -f to move the calendar while deleting this share."); + $this->expectExceptionMessage('The calendar <personal> is already shared to user <user2>.You may use -f to move the calendar while deleting this share.'); } else { $this->calDav->expects($this->once())->method('updateShares'); } diff --git a/apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php b/apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php index 0cf7d0d75d1..ec56aa64eb2 100644 --- a/apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php +++ b/apps/dav/tests/unit/Command/RemoveInvalidSharesTest.php @@ -1,31 +1,17 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2018, ownCloud GmbH - * - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2018 ownCloud GmbH + * SPDX-License-Identifier: AGPL-3.0-only */ - -namespace OCA\DAV\Tests\Unit\Command; +namespace OCA\DAV\Tests\unit\Command; use OCA\DAV\Command\RemoveInvalidShares; use OCA\DAV\Connector\Sabre\Principal; -use OCP\Migration\IOutput; +use OCP\IDBConnection; +use OCP\Server; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; @@ -39,7 +25,7 @@ use Test\TestCase; class RemoveInvalidSharesTest extends TestCase { protected function setUp(): void { parent::setUp(); - $db = \OC::$server->getDatabaseConnection(); + $db = Server::get(IDBConnection::class); $db->insertIfNotExist('*PREFIX*dav_shares', [ 'principaluri' => 'principal:unknown', @@ -49,20 +35,18 @@ class RemoveInvalidSharesTest extends TestCase { ]); } - public function test() { - $db = \OC::$server->getDatabaseConnection(); - /** @var Principal | \PHPUnit\Framework\MockObject\MockObject $principal */ + public function test(): void { + $db = Server::get(IDBConnection::class); $principal = $this->createMock(Principal::class); - /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $output */ - $output = $this->createMock(IOutput::class); - $repair = new RemoveInvalidShares($db, $principal); $this->invokePrivate($repair, 'run', [$this->createMock(InputInterface::class), $this->createMock(OutputInterface::class)]); $query = $db->getQueryBuilder(); - $result = $query->select('*')->from('dav_shares') - ->where($query->expr()->eq('principaluri', $query->createNamedParameter('principal:unknown')))->execute(); + $query->select('*') + ->from('dav_shares') + ->where($query->expr()->eq('principaluri', $query->createNamedParameter('principal:unknown'))); + $result = $query->executeQuery(); $data = $result->fetchAll(); $result->closeCursor(); $this->assertEquals(0, count($data)); |