123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace OCA\DAV\Tests\Command;
- use InvalidArgumentException;
- use OCA\DAV\CalDAV\CalDavBackend;
- use OCA\DAV\Command\MoveCalendar;
- use OCP\IConfig;
- 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;
- /**
- * Class MoveCalendarTest
- *
- * @package OCA\DAV\Tests\Command
- */
- class MoveCalendarTest extends TestCase {
- /** @var IUserManager|MockObject $userManager */
- private $userManager;
- /** @var 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;
- protected function setUp(): void {
- parent::setUp();
- $this->userManager = $this->createMock(IUserManager::class);
- $this->groupManager = $this->createMock(IGroupManager::class);
- $this->shareManager = $this->createMock(IManager::class);
- $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,
- $this->groupManager,
- $this->shareManager,
- $this->config,
- $this->l10n,
- $this->calDav,
- $this->logger
- );
- }
- public function dataExecute() {
- return [
- [false, true],
- [true, false]
- ];
- }
- /**
- * @dataProvider dataExecute
- *
- * @param $userOriginExists
- * @param $userDestinationExists
- */
- public function testWithBadUserOrigin($userOriginExists, $userDestinationExists): void {
- $this->expectException(\InvalidArgumentException::class);
- $this->userManager->expects($this->exactly($userOriginExists ? 2 : 1))
- ->method('userExists')
- ->withConsecutive(
- ['user'],
- ['user2'],
- )
- ->willReturnOnConsecutiveCalls(
- $userOriginExists,
- $userDestinationExists,
- );
- $commandTester = new CommandTester($this->command);
- $commandTester->execute([
- 'name' => $this->command->getName(),
- 'sourceuid' => 'user',
- 'destinationuid' => 'user2',
- ]);
- }
- 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->exactly(2))
- ->method('userExists')
- ->withConsecutive(
- ['user'],
- ['user2'],
- )
- ->willReturn(true);
- $this->calDav->expects($this->once())->method('getCalendarByUri')
- ->with('principals/users/user', 'personal')
- ->willReturn(null);
- $commandTester = new CommandTester($this->command);
- $commandTester->execute([
- 'name' => 'personal',
- 'sourceuid' => 'user',
- 'destinationuid' => 'user2',
- ]);
- }
- public function testMoveWithExistingDestinationCalendar(): void {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('User <user2> already has a calendar named <personal>.');
- $this->userManager->expects($this->exactly(2))
- ->method('userExists')
- ->withConsecutive(
- ['user'],
- ['user2'],
- )
- ->willReturn(true);
- $this->calDav->expects($this->exactly(2))
- ->method('getCalendarByUri')
- ->withConsecutive(
- ['principals/users/user', 'personal'],
- ['principals/users/user2', 'personal'],
- )
- ->willReturn([
- 'id' => 1234,
- ]);
- $commandTester = new CommandTester($this->command);
- $commandTester->execute([
- 'name' => 'personal',
- 'sourceuid' => 'user',
- 'destinationuid' => 'user2',
- ]);
- }
- public function testMove(): void {
- $this->userManager->expects($this->exactly(2))
- ->method('userExists')
- ->withConsecutive(
- ['user'],
- ['user2'],
- )
- ->willReturn(true);
- $this->calDav->expects($this->exactly(2))
- ->method('getCalendarByUri')
- ->withConsecutive(
- ['principals/users/user', 'personal'],
- ['principals/users/user2', 'personal'],
- )
- ->willReturnOnConsecutiveCalls(
- [
- 'id' => 1234,
- ],
- null,
- );
- $this->calDav->expects($this->once())->method('getShares')
- ->with(1234)
- ->willReturn([]);
- $commandTester = new CommandTester($this->command);
- $commandTester->execute([
- 'name' => 'personal',
- 'sourceuid' => 'user',
- 'destinationuid' => 'user2',
- ]);
- $this->assertStringContainsString('[OK] Calendar <personal> was moved from user <user> to <user2>', $commandTester->getDisplay());
- }
- public function dataTestMoveWithDestinationNotPartOfGroup(): array {
- return [
- [true],
- [false]
- ];
- }
- /**
- * @dataProvider dataTestMoveWithDestinationNotPartOfGroup
- */
- public function testMoveWithDestinationNotPartOfGroup(bool $shareWithGroupMembersOnly): void {
- $this->userManager->expects($this->exactly(2))
- ->method('userExists')
- ->withConsecutive(
- ['user'],
- ['user2'],
- )
- ->willReturn(true);
- $this->calDav->expects($this->exactly(2))
- ->method('getCalendarByUri')
- ->withConsecutive(
- ['principals/users/user', 'personal'],
- ['principals/users/user2', 'personal'],
- )
- ->willReturnOnConsecutiveCalls(
- [
- 'id' => 1234,
- 'uri' => 'personal',
- ],
- null,
- );
- $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly')
- ->willReturn($shareWithGroupMembersOnly);
- $this->calDav->expects($this->once())->method('getShares')
- ->with(1234)
- ->willReturn([
- ['href' => 'principal:principals/groups/nextclouders']
- ]);
- 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.');
- }
- $commandTester = new CommandTester($this->command);
- $commandTester->execute([
- 'name' => 'personal',
- 'sourceuid' => 'user',
- 'destinationuid' => 'user2',
- ]);
- }
- public function testMoveWithDestinationPartOfGroup(): void {
- $this->userManager->expects($this->exactly(2))
- ->method('userExists')
- ->withConsecutive(
- ['user'],
- ['user2'],
- )
- ->willReturn(true);
- $this->calDav->expects($this->exactly(2))
- ->method('getCalendarByUri')
- ->withConsecutive(
- ['principals/users/user', 'personal'],
- ['principals/users/user2', 'personal'],
- )
- ->willReturnOnConsecutiveCalls(
- [
- 'id' => 1234,
- 'uri' => 'personal',
- ],
- null,
- );
- $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly')
- ->willReturn(true);
- $this->calDav->expects($this->once())->method('getShares')
- ->with(1234)
- ->willReturn([
- ['href' => 'principal:principals/groups/nextclouders']
- ]);
- $this->groupManager->expects($this->once())->method('isInGroup')
- ->with('user2', 'nextclouders')
- ->willReturn(true);
- $commandTester = new CommandTester($this->command);
- $commandTester->execute([
- 'name' => 'personal',
- 'sourceuid' => 'user',
- 'destinationuid' => 'user2',
- ]);
- $this->assertStringContainsString('[OK] Calendar <personal> was moved from user <user> to <user2>', $commandTester->getDisplay());
- }
- public function testMoveWithDestinationNotPartOfGroupAndForce(): void {
- $this->userManager->expects($this->exactly(2))
- ->method('userExists')
- ->withConsecutive(
- ['user'],
- ['user2'],
- )
- ->willReturn(true);
- $this->calDav->expects($this->exactly(2))
- ->method('getCalendarByUri')
- ->withConsecutive(
- ['principals/users/user', 'personal'],
- ['principals/users/user2', 'personal'],
- )
- ->willReturnOnConsecutiveCalls(
- [
- 'id' => 1234,
- 'uri' => 'personal',
- '{DAV:}displayname' => 'Personal'
- ],
- null,
- );
- $this->shareManager->expects($this->once())->method('shareWithGroupMembersOnly')
- ->willReturn(true);
- $this->calDav->expects($this->once())->method('getShares')
- ->with(1234)
- ->willReturn([
- [
- 'href' => 'principal:principals/groups/nextclouders',
- '{DAV:}displayname' => 'Personal'
- ]
- ]);
- $this->calDav->expects($this->once())->method('updateShares');
- $commandTester = new CommandTester($this->command);
- $commandTester->execute([
- 'name' => 'personal',
- 'sourceuid' => 'user',
- 'destinationuid' => 'user2',
- '--force' => true
- ]);
- $this->assertStringContainsString('[OK] Calendar <personal> was moved from user <user> to <user2>', $commandTester->getDisplay());
- }
- public function dataTestMoveWithCalendarAlreadySharedToDestination(): array {
- return [
- [true],
- [false]
- ];
- }
- /**
- * @dataProvider dataTestMoveWithCalendarAlreadySharedToDestination
- */
- public function testMoveWithCalendarAlreadySharedToDestination(bool $force): void {
- $this->userManager->expects($this->exactly(2))
- ->method('userExists')
- ->withConsecutive(
- ['user'],
- ['user2'],
- )
- ->willReturn(true);
- $this->calDav->expects($this->exactly(2))
- ->method('getCalendarByUri')
- ->withConsecutive(
- ['principals/users/user', 'personal'],
- ['principals/users/user2', 'personal'],
- )
- ->willReturnOnConsecutiveCalls(
- [
- 'id' => 1234,
- 'uri' => 'personal',
- '{DAV:}displayname' => 'Personal'
- ],
- null,
- );
- $this->calDav->expects($this->once())->method('getShares')
- ->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.');
- } else {
- $this->calDav->expects($this->once())->method('updateShares');
- }
- $commandTester = new CommandTester($this->command);
- $commandTester->execute([
- 'name' => 'personal',
- 'sourceuid' => 'user',
- 'destinationuid' => 'user2',
- '--force' => $force,
- ]);
- }
- }
|