summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2019-01-28 17:35:13 +0100
committerGitHub <noreply@github.com>2019-01-28 17:35:13 +0100
commit554c78c9b1641351626f0bbcceb44826dfe854a5 (patch)
tree2fa333c23daca2037240b3cf59dca78a219da5be /apps
parentcfdece7833ed407cfee63b73c9ae20fde848ffc3 (diff)
parent3180ddd2023c54c984f6999ade489de3d9b4fb72 (diff)
downloadnextcloud-server-554c78c9b1641351626f0bbcceb44826dfe854a5.tar.gz
nextcloud-server-554c78c9b1641351626f0bbcceb44826dfe854a5.zip
Merge pull request #5881 from nextcloud/command-move-calendar
Add command to move a calendar from an user to another
Diffstat (limited to 'apps')
-rw-r--r--apps/dav/appinfo/info.xml2
-rw-r--r--apps/dav/composer/composer/autoload_classmap.php2
-rw-r--r--apps/dav/composer/composer/autoload_static.php2
-rw-r--r--apps/dav/lib/CalDAV/CalDavBackend.php17
-rw-r--r--apps/dav/lib/Command/ListCalendars.php108
-rw-r--r--apps/dav/lib/Command/MoveCalendar.php185
-rw-r--r--apps/dav/tests/unit/CalDAV/CalDavBackendTest.php17
-rw-r--r--apps/dav/tests/unit/Command/ListCalendarsTest.php139
-rw-r--r--apps/dav/tests/unit/Command/MoveCalendarTest.php427
9 files changed, 899 insertions, 0 deletions
diff --git a/apps/dav/appinfo/info.xml b/apps/dav/appinfo/info.xml
index d9418456d8b..5cc374a566d 100644
--- a/apps/dav/appinfo/info.xml
+++ b/apps/dav/appinfo/info.xml
@@ -42,6 +42,8 @@
<commands>
<command>OCA\DAV\Command\CreateAddressBook</command>
<command>OCA\DAV\Command\CreateCalendar</command>
+ <command>OCA\DAV\Command\MoveCalendar</command>
+ <command>OCA\DAV\Command\ListCalendars</command>
<command>OCA\DAV\Command\SyncBirthdayCalendar</command>
<command>OCA\DAV\Command\SyncSystemAddressBook</command>
<command>OCA\DAV\Command\RemoveInvalidShares</command>
diff --git a/apps/dav/composer/composer/autoload_classmap.php b/apps/dav/composer/composer/autoload_classmap.php
index d2bb79495f7..53382dc8fb6 100644
--- a/apps/dav/composer/composer/autoload_classmap.php
+++ b/apps/dav/composer/composer/autoload_classmap.php
@@ -79,6 +79,8 @@ return array(
'OCA\\DAV\\CardDAV\\Xml\\Groups' => $baseDir . '/../lib/CardDAV/Xml/Groups.php',
'OCA\\DAV\\Command\\CreateAddressBook' => $baseDir . '/../lib/Command/CreateAddressBook.php',
'OCA\\DAV\\Command\\CreateCalendar' => $baseDir . '/../lib/Command/CreateCalendar.php',
+ 'OCA\\DAV\\Command\\ListCalendars' => $baseDir . '/../lib/Command/ListCalendars.php',
+ 'OCA\\DAV\\Command\\MoveCalendar' => $baseDir . '/../lib/Command/MoveCalendar.php',
'OCA\\DAV\\Command\\RemoveInvalidShares' => $baseDir . '/../lib/Command/RemoveInvalidShares.php',
'OCA\\DAV\\Command\\SyncBirthdayCalendar' => $baseDir . '/../lib/Command/SyncBirthdayCalendar.php',
'OCA\\DAV\\Command\\SyncSystemAddressBook' => $baseDir . '/../lib/Command/SyncSystemAddressBook.php',
diff --git a/apps/dav/composer/composer/autoload_static.php b/apps/dav/composer/composer/autoload_static.php
index 48667db5e36..f4b608fd1f5 100644
--- a/apps/dav/composer/composer/autoload_static.php
+++ b/apps/dav/composer/composer/autoload_static.php
@@ -94,6 +94,8 @@ class ComposerStaticInitDAV
'OCA\\DAV\\CardDAV\\Xml\\Groups' => __DIR__ . '/..' . '/../lib/CardDAV/Xml/Groups.php',
'OCA\\DAV\\Command\\CreateAddressBook' => __DIR__ . '/..' . '/../lib/Command/CreateAddressBook.php',
'OCA\\DAV\\Command\\CreateCalendar' => __DIR__ . '/..' . '/../lib/Command/CreateCalendar.php',
+ 'OCA\\DAV\\Command\\ListCalendars' => __DIR__ . '/..' . '/../lib/Command/ListCalendars.php',
+ 'OCA\\DAV\\Command\\MoveCalendar' => __DIR__ . '/..' . '/../lib/Command/MoveCalendar.php',
'OCA\\DAV\\Command\\RemoveInvalidShares' => __DIR__ . '/..' . '/../lib/Command/RemoveInvalidShares.php',
'OCA\\DAV\\Command\\SyncBirthdayCalendar' => __DIR__ . '/..' . '/../lib/Command/SyncBirthdayCalendar.php',
'OCA\\DAV\\Command\\SyncSystemAddressBook' => __DIR__ . '/..' . '/../lib/Command/SyncSystemAddressBook.php',
diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php
index 187ba4ecdcf..88ee778e82c 100644
--- a/apps/dav/lib/CalDAV/CalDavBackend.php
+++ b/apps/dav/lib/CalDAV/CalDavBackend.php
@@ -2522,6 +2522,23 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
}
/**
+ * Move a calendar from one user to another
+ *
+ * @param string $uriName
+ * @param string $uriOrigin
+ * @param string $uriDestination
+ */
+ public function moveCalendar($uriName, $uriOrigin, $uriDestination)
+ {
+ $query = $this->db->getQueryBuilder();
+ $query->update('calendars')
+ ->set('principaluri', $query->createNamedParameter($uriDestination))
+ ->where($query->expr()->eq('principaluri', $query->createNamedParameter($uriOrigin)))
+ ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($uriName)))
+ ->execute();
+ }
+
+ /**
* read VCalendar data into a VCalendar object
*
* @param string $objectData
diff --git a/apps/dav/lib/Command/ListCalendars.php b/apps/dav/lib/Command/ListCalendars.php
new file mode 100644
index 00000000000..6c2f5bdb506
--- /dev/null
+++ b/apps/dav/lib/Command/ListCalendars.php
@@ -0,0 +1,108 @@
+<?php
+/**
+ * @copyright Copyright (c) 2018, Georg Ehrke
+ *
+ * @author Georg Ehrke <oc.list@georgehrke.com>
+ * @author Thomas Citharel <tcit@tcit.fr>
+ *
+ * @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/>
+ *
+ */
+namespace OCA\DAV\Command;
+
+use OCA\DAV\CalDAV\BirthdayService;
+use OCA\DAV\CalDAV\CalDavBackend;
+use OCA\DAV\Connector\Sabre\Principal;
+use OCP\IConfig;
+use OCP\IDBConnection;
+use OCP\IGroupManager;
+use OCP\IUserManager;
+use OCP\IUserSession;
+use OCP\Share\IManager;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Helper\Table;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class ListCalendars extends Command {
+
+ /** @var IUserManager */
+ protected $userManager;
+
+ /** @var CalDavBackend */
+ private $caldav;
+
+ /**
+ * @param IUserManager $userManager
+ * @param CalDavBackend $caldav
+ */
+ function __construct(IUserManager $userManager, CalDavBackend $caldav) {
+ parent::__construct();
+ $this->userManager = $userManager;
+ $this->caldav = $caldav;
+ }
+
+ protected function configure() {
+ $this
+ ->setName('dav:list-calendars')
+ ->setDescription('List all calendars of a user')
+ ->addArgument('uid',
+ InputArgument::REQUIRED,
+ 'User for whom all calendars will be listed');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output) {
+ $user = $input->getArgument('uid');
+ if (!$this->userManager->userExists($user)) {
+ throw new \InvalidArgumentException("User <$user> is unknown.");
+ }
+
+ $calendars = $this->caldav->getCalendarsForUser("principals/users/$user");
+
+ $calendarTableData = [];
+ foreach($calendars as $calendar) {
+ // skip birthday calendar
+ if ($calendar['uri'] === BirthdayService::BIRTHDAY_CALENDAR_URI) {
+ continue;
+ }
+
+ $readOnly = false;
+ $readOnlyIndex = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only';
+ if (isset($calendar[$readOnlyIndex])) {
+ $readOnly = $calendar[$readOnlyIndex];
+ }
+
+ $calendarTableData[] = [
+ $calendar['uri'],
+ $calendar['{DAV:}displayname'],
+ $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'],
+ $calendar['{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'],
+ $readOnly ? ' x ' : ' ✓ ',
+ ];
+ }
+
+ if (count($calendarTableData) > 0) {
+ $table = new Table($output);
+ $table->setHeaders(['uri', 'displayname', 'owner\'s userid', 'owner\'s displayname', 'writable'])
+ ->setRows($calendarTableData);
+
+ $table->render();
+ } else {
+ $output->writeln("<info>User <$user> has no calendars</info>");
+ }
+ }
+
+}
diff --git a/apps/dav/lib/Command/MoveCalendar.php b/apps/dav/lib/Command/MoveCalendar.php
new file mode 100644
index 00000000000..a2c7ca8c4d8
--- /dev/null
+++ b/apps/dav/lib/Command/MoveCalendar.php
@@ -0,0 +1,185 @@
+<?php
+/**
+ * @author Thomas Citharel <tcit@tcit.fr>
+ *
+ * @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/>
+ *
+ */
+namespace OCA\DAV\Command;
+
+use OCA\DAV\CalDAV\CalDavBackend;
+use OCA\DAV\CalDAV\Calendar;
+use OCA\DAV\Connector\Sabre\Principal;
+use OCP\IConfig;
+use OCP\IDBConnection;
+use OCP\IGroupManager;
+use OCP\IL10N;
+use OCP\IUserManager;
+use OCP\IUserSession;
+use OCP\Share\IManager as IShareManager;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+
+class MoveCalendar extends Command {
+
+ /** @var IUserManager */
+ private $userManager;
+
+ /** @var IGroupManager */
+ private $groupManager;
+
+ /** @var IShareManager */
+ private $shareManager;
+
+ /** @var IConfig $config */
+ private $config;
+
+ /** @var IL10N */
+ private $l10n;
+
+ /** @var SymfonyStyle */
+ private $io;
+
+ /** @var CalDavBackend */
+ private $calDav;
+
+ const URI_USERS = 'principals/users/';
+
+ /**
+ * @param IUserManager $userManager
+ * @param IGroupManager $groupManager
+ * @param IShareManager $shareManager
+ * @param IConfig $config
+ * @param IL10N $l10n
+ * @param CalDavBackend $calDav
+ */
+ function __construct(
+ IUserManager $userManager,
+ IGroupManager $groupManager,
+ IShareManager $shareManager,
+ IConfig $config,
+ IL10N $l10n,
+ CalDavBackend $calDav
+ ) {
+ parent::__construct();
+ $this->userManager = $userManager;
+ $this->groupManager = $groupManager;
+ $this->shareManager = $shareManager;
+ $this->config = $config;
+ $this->l10n = $l10n;
+ $this->calDav = $calDav;
+ }
+
+ protected function configure() {
+ $this
+ ->setName('dav:move-calendar')
+ ->setDescription('Move a calendar from an user to another')
+ ->addArgument('name',
+ InputArgument::REQUIRED,
+ 'Name of the calendar to move')
+ ->addArgument('sourceuid',
+ InputArgument::REQUIRED,
+ 'User who currently owns the calendar')
+ ->addArgument('destinationuid',
+ InputArgument::REQUIRED,
+ 'User who will receive the calendar')
+ ->addOption('force', 'f', InputOption::VALUE_NONE, "Force the migration by removing existing shares");
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output) {
+ $userOrigin = $input->getArgument('sourceuid');
+ $userDestination = $input->getArgument('destinationuid');
+
+ $this->io = new SymfonyStyle($input, $output);
+
+ if (!$this->userManager->userExists($userOrigin)) {
+ throw new \InvalidArgumentException("User <$userOrigin> is unknown.");
+ }
+
+ if (!$this->userManager->userExists($userDestination)) {
+ throw new \InvalidArgumentException("User <$userDestination> is unknown.");
+ }
+
+ $name = $input->getArgument('name');
+
+ $calendar = $this->calDav->getCalendarByUri(self::URI_USERS . $userOrigin, $name);
+
+ if (null === $calendar) {
+ throw new \InvalidArgumentException("User <$userOrigin> has no calendar named <$name>. You can run occ dav:list-calendars to list calendars URIs for this user.");
+ }
+
+ if (null !== $this->calDav->getCalendarByUri(self::URI_USERS . $userDestination, $name)) {
+ throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>.");
+ }
+
+ $this->checkShares($calendar, $userOrigin, $userDestination, $input->getOption('force'));
+
+ $this->calDav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination);
+
+ $this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination>");
+ }
+
+ /**
+ * Check that moving the calendar won't break shares
+ *
+ * @param array $calendar
+ * @param string $userOrigin
+ * @param string $userDestination
+ * @param bool $force
+ */
+ private function checkShares(array $calendar, string $userOrigin, string $userDestination, bool $force = false)
+ {
+ $shares = $this->calDav->getShares($calendar['id']);
+ foreach ($shares as $share) {
+ list(, $prefix, $userOrGroup) = explode('/', $share['href'], 3);
+
+ /**
+ * Check that user destination is member of the groups which whom the calendar was shared
+ * If we ask to force the migration, the share with the group is dropped
+ */
+ if ($this->shareManager->shareWithGroupMembersOnly() === true && 'groups' === $prefix && !$this->groupManager->isInGroup($userDestination, $userOrGroup)) {
+ if ($force) {
+ $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config), [], ['href' => 'principal:principals/groups/' . $userOrGroup]);
+ } else {
+ throw new \InvalidArgumentException("User <$userDestination> is not part of the group <$userOrGroup> with whom the calendar <" . $calendar['uri'] . "> was shared. You may use -f to move the calendar while deleting this share.");
+ }
+ }
+
+ /**
+ * Check that calendar isn't already shared with user destination
+ */
+ if ($userOrGroup === $userDestination) {
+ if ($force) {
+ $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config), [], ['href' => 'principal:principals/users/' . $userOrGroup]);
+ } else {
+ throw new \InvalidArgumentException("The calendar <" . $calendar['uri'] . "> is already shared to user <$userDestination>.You may use -f to move the calendar while deleting this share.");
+ }
+ }
+ }
+ /**
+ * Warn that share links have changed if there are shares
+ */
+ if (count($shares) > 0) {
+ $this->io->note([
+ "Please note that moving calendar " . $calendar['uri'] . " from user <$userOrigin> to <$userDestination> has caused share links to change.",
+ "Sharees will need to change \"example.com/remote.php/dav/calendars/uid/" . $calendar['uri'] . "_shared_by_$userOrigin\" to \"example.com/remote.php/dav/calendars/uid/" . $calendar['uri'] . "_shared_by_$userDestination\""
+ ]);
+ }
+ }
+}
diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php
index 44609f2ca6c..1b2169b6675 100644
--- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php
+++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php
@@ -983,4 +983,21 @@ EOD;
$this->assertEquals(null, $this->backend->getCalendarObject($subscriptionId, $uri, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION));
}
+
+ public function testCalendarMovement()
+ {
+ $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
+
+ $this->assertCount(1, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER));
+
+ $calendarInfoUser = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
+
+ $this->backend->moveCalendar('Example', self::UNIT_TEST_USER, self::UNIT_TEST_USER1);
+ $this->assertCount(0, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER));
+ $this->assertCount(1, $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1));
+
+ $calendarInfoUser1 = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER1)[0];
+ $this->assertEquals($calendarInfoUser['id'], $calendarInfoUser1['id']);
+ $this->assertEquals($calendarInfoUser['uri'], $calendarInfoUser1['uri']);
+ }
}
diff --git a/apps/dav/tests/unit/Command/ListCalendarsTest.php b/apps/dav/tests/unit/Command/ListCalendarsTest.php
new file mode 100644
index 00000000000..1a0ca8c7954
--- /dev/null
+++ b/apps/dav/tests/unit/Command/ListCalendarsTest.php
@@ -0,0 +1,139 @@
+<?php
+/**
+ * @author Thomas Citharel <tcit@tcit.fr>
+ *
+ * @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/>
+ *
+ */
+
+namespace OCA\DAV\Tests\Command;
+
+use InvalidArgumentException;
+use OCA\DAV\CalDAV\BirthdayService;
+use OCA\DAV\CalDAV\CalDavBackend;
+use OCA\DAV\Command\ListCalendars;
+use OCP\IUserManager;
+use Symfony\Component\Console\Tester\CommandTester;
+use Test\TestCase;
+
+
+/**
+ * Class ListCalendarsTest
+ *
+ * @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;
+
+ const USERNAME = 'username';
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->userManager = $this->createMock(IUserManager::class);
+ $this->calDav = $this->createMock(CalDavBackend::class);
+
+ $this->command = new ListCalendars(
+ $this->userManager,
+ $this->calDav
+ );
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testWithBadUser()
+ {
+ $this->userManager->expects($this->once())
+ ->method('userExists')
+ ->with(self::USERNAME)
+ ->willReturn(false);
+
+ $commandTester = new CommandTester($this->command);
+ $commandTester->execute([
+ 'uid' => self::USERNAME,
+ ]);
+ $this->assertContains("User <" . self::USERNAME . "> in unknown", $commandTester->getDisplay());
+ }
+
+ public function testWithCorrectUserWithNoCalendars()
+ {
+ $this->userManager->expects($this->once())
+ ->method('userExists')
+ ->with(self::USERNAME)
+ ->willReturn(true);
+
+ $this->calDav->expects($this->once())
+ ->method('getCalendarsForUser')
+ ->with('principals/users/' . self::USERNAME)
+ ->willReturn([]);
+
+ $commandTester = new CommandTester($this->command);
+ $commandTester->execute([
+ 'uid' => self::USERNAME,
+ ]);
+ $this->assertContains("User <" . self::USERNAME . "> has no calendars\n", $commandTester->getDisplay());
+ }
+
+ public function dataExecute()
+ {
+ return [
+ [false, '✓'],
+ [true, 'x']
+ ];
+ }
+
+ /**
+ * @dataProvider dataExecute
+ */
+ public function testWithCorrectUser(bool $readOnly, string $output)
+ {
+ $this->userManager->expects($this->once())
+ ->method('userExists')
+ ->with(self::USERNAME)
+ ->willReturn(true);
+
+ $this->calDav->expects($this->once())
+ ->method('getCalendarsForUser')
+ ->with('principals/users/' . self::USERNAME)
+ ->willReturn([
+ [
+ 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI,
+ ],
+ [
+ '{' . \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->assertContains($output, $commandTester->getDisplay());
+ $this->assertNotContains(BirthdayService::BIRTHDAY_CALENDAR_URI, $commandTester->getDisplay());
+ }
+}
diff --git a/apps/dav/tests/unit/Command/MoveCalendarTest.php b/apps/dav/tests/unit/Command/MoveCalendarTest.php
new file mode 100644
index 00000000000..07d789dbfb2
--- /dev/null
+++ b/apps/dav/tests/unit/Command/MoveCalendarTest.php
@@ -0,0 +1,427 @@
+<?php
+/**
+ * @author Thomas Citharel <tcit@tcit.fr>
+ *
+ * @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/>
+ *
+ */
+
+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 Symfony\Component\Console\Tester\CommandTester;
+use Test\TestCase;
+
+
+/**
+ * Class MoveCalendarTest
+ *
+ * @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;
+
+ protected function setUp() {
+ 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->command = new MoveCalendar(
+ $this->userManager,
+ $this->groupManager,
+ $this->shareManager,
+ $this->config,
+ $this->l10n,
+ $this->calDav
+ );
+ }
+
+ public function dataExecute() {
+ return [
+ [false, true],
+ [true, false]
+ ];
+ }
+
+ /**
+ * @dataProvider dataExecute
+ *
+ * @expectedException InvalidArgumentException
+ * @param $userOriginExists
+ * @param $userDestinationExists
+ */
+ public function testWithBadUserOrigin($userOriginExists, $userDestinationExists)
+ {
+ $this->userManager->expects($this->at(0))
+ ->method('userExists')
+ ->with('user')
+ ->willReturn($userOriginExists);
+
+ if (!$userDestinationExists) {
+ $this->userManager->expects($this->at(1))
+ ->method('userExists')
+ ->with('user2')
+ ->willReturn($userDestinationExists);
+ }
+
+ $commandTester = new CommandTester($this->command);
+ $commandTester->execute([
+ 'name' => $this->command->getName(),
+ 'sourceuid' => 'user',
+ 'destinationuid' => 'user2',
+ ]);
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ * @expectedExceptionMessage User <user> has no calendar named <personal>. You can run occ dav:list-calendars to list calendars URIs for this user.
+ */
+ public function testMoveWithInexistantCalendar()
+ {
+ $this->userManager->expects($this->at(0))
+ ->method('userExists')
+ ->with('user')
+ ->willReturn(true);
+
+ $this->userManager->expects($this->at(1))
+ ->method('userExists')
+ ->with('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',
+ ]);
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ * @expectedExceptionMessage User <user2> already has a calendar named <personal>.
+ */
+ public function testMoveWithExistingDestinationCalendar()
+ {
+ $this->userManager->expects($this->at(0))
+ ->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,
+ ]);
+
+ $this->calDav->expects($this->at(1))->method('getCalendarByUri')
+ ->with('principals/users/user2', 'personal')
+ ->willReturn([
+ 'id' => 1234,
+ ]);
+
+ $commandTester = new CommandTester($this->command);
+ $commandTester->execute([
+ 'name' => 'personal',
+ 'sourceuid' => 'user',
+ 'destinationuid' => 'user2',
+ ]);
+ }
+
+ public function testMove()
+ {
+ $this->userManager->expects($this->at(0))
+ ->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,
+ ]);
+
+ $this->calDav->expects($this->at(1))->method('getCalendarByUri')
+ ->with('principals/users/user2', 'personal')
+ ->willReturn(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->assertContains("[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)
+ {
+ $this->userManager->expects($this->at(0))
+ ->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'
+ ]);
+
+ $this->calDav->expects($this->at(1))->method('getCalendarByUri')
+ ->with('principals/users/user2', 'personal')
+ ->willReturn(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()
+ {
+ $this->userManager->expects($this->at(0))
+ ->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'
+ ]);
+
+ $this->calDav->expects($this->at(1))->method('getCalendarByUri')
+ ->with('principals/users/user2', 'personal')
+ ->willReturn(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->assertContains("[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))
+ ->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'
+ ]);
+
+ $this->calDav->expects($this->at(1))->method('getCalendarByUri')
+ ->with('principals/users/user2', 'personal')
+ ->willReturn(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->assertContains("[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)
+ {
+ $this->userManager->expects($this->at(0))
+ ->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',
+ ]);
+
+ $this->calDav->expects($this->at(1))->method('getCalendarByUri')
+ ->with('principals/users/user2', 'personal')
+ ->willReturn(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,
+ ]);
+ }
+}