diff options
Diffstat (limited to 'apps/dav/tests/unit/Command/MoveCalendarTest.php')
-rw-r--r-- | apps/dav/tests/unit/Command/MoveCalendarTest.php | 332 |
1 files changed, 128 insertions, 204 deletions
diff --git a/apps/dav/tests/unit/Command/MoveCalendarTest.php b/apps/dav/tests/unit/Command/MoveCalendarTest.php index 5a858e140ac..e9f016961f2 100644 --- a/apps/dav/tests/unit/Command/MoveCalendarTest.php +++ b/apps/dav/tests/unit/Command/MoveCalendarTest.php @@ -1,30 +1,11 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016 Thomas Citharel <nextcloud@tcit.fr> - * - * @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; @@ -45,30 +26,14 @@ use Test\TestCase; * @package OCA\DAV\Tests\Command */ class MoveCalendarTest extends TestCase { - - /** @var \OCP\IUserManager|MockObject $userManager */ - private $userManager; - - /** @var \OCP\IGroupManager|MockObject $groupManager */ - private $groupManager; - - /** @var \OCP\Share\IManager|MockObject $shareManager */ - private $shareManager; - - /** @var IConfig|MockObject $l10n */ - private $config; - - /** @var IL10N|MockObject $l10n */ - private $l10n; - - /** @var CalDavBackend|MockObject $l10n */ - private $calDav; - - /** @var MoveCalendar */ - private $command; - - /** @var LoggerInterface|MockObject */ - private $logger; + 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(); @@ -92,33 +57,23 @@ class MoveCalendarTest extends TestCase { ); } - 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([ @@ -129,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)) + $this->userManager->expects($this->exactly(2)) ->method('userExists') - ->with('user') - ->willReturn(true); - - $this->userManager->expects($this->at(1)) - ->method('userExists') - ->with('user2') - ->willReturn(true); + ->willReturnMap([ + ['user', true], + ['user2', true], + ]); $this->calDav->expects($this->once())->method('getCalendarByUri') ->with('principals/users/user', 'personal') @@ -156,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); @@ -190,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) @@ -222,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); @@ -267,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); @@ -278,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); @@ -320,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)) - ->method('userExists') - ->with('user') - ->willReturn(true); - - $this->userManager->expects($this->at(1)) + public function testMoveWithDestinationNotPartOfGroupAndForce(): 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', - '{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); @@ -367,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)) - ->method('userExists') - ->with('user') - ->willReturn(true); - - $this->userManager->expects($this->at(1)) + #[\PHPUnit\Framework\Attributes\DataProvider('dataTestMoveWithCalendarAlreadySharedToDestination')] + public function testMoveWithCalendarAlreadySharedToDestination(bool $force): 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', - '{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'); } |