From 4a2238c75be9b0ed4be17869878c04cb812d276f Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Thu, 8 Mar 2018 14:44:02 +0100 Subject: [PATCH] add list-calendars command Signed-off-by: Georg Ehrke Rebase and delete URI suggestion part Invite to use occ dav:list-calendars instead Signed-off-by: Thomas Citharel Fix autoload Signed-off-by: Thomas Citharel Use injection & test everything And rebase Signed-off-by: Thomas Citharel Add test for ListCalendars and refactoring Signed-off-by: Thomas Citharel Fix indentation Signed-off-by: Thomas Citharel --- apps/dav/appinfo/info.xml | 1 + .../composer/composer/autoload_classmap.php | 2 + .../dav/composer/composer/autoload_static.php | 2 + apps/dav/lib/CalDAV/CalDavBackend.php | 26 -- apps/dav/lib/Command/ListCalendars.php | 108 ++++++++ apps/dav/lib/Command/MoveCalendar.php | 59 ++--- .../tests/unit/Command/ListCalendarsTest.php | 139 ++++++++++ .../tests/unit/Command/MoveCalendarTest.php | 248 +++++++++++++++++- 8 files changed, 519 insertions(+), 66 deletions(-) create mode 100644 apps/dav/lib/Command/ListCalendars.php create mode 100644 apps/dav/tests/unit/Command/ListCalendarsTest.php diff --git a/apps/dav/appinfo/info.xml b/apps/dav/appinfo/info.xml index 23892b3487..890dd7f06a 100644 --- a/apps/dav/appinfo/info.xml +++ b/apps/dav/appinfo/info.xml @@ -42,6 +42,7 @@ OCA\DAV\Command\CreateAddressBook OCA\DAV\Command\CreateCalendar OCA\DAV\Command\MoveCalendar + OCA\DAV\Command\ListCalendars OCA\DAV\Command\SyncBirthdayCalendar OCA\DAV\Command\SyncSystemAddressBook OCA\DAV\Command\RemoveInvalidShares diff --git a/apps/dav/composer/composer/autoload_classmap.php b/apps/dav/composer/composer/autoload_classmap.php index e9aaf35f64..007f061f5e 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 40fb07033f..b37b9e8f01 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 1214d11f36..88ee778e82 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -2538,32 +2538,6 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription ->execute(); } - /** - * @param string $displayName - * @param string|null $principalUri - * @return array - */ - public function findCalendarsUrisByDisplayName($displayName, $principalUri = null) - { - // Ideally $displayName would be sanitized the same way as stringUtility.js does in calendar - - $query = $this->db->getQueryBuilder(); - $query->select('uri') - ->from('calendars') - ->where($query->expr()->iLike('displayname', - $query->createNamedParameter('%'.$this->db->escapeLikeParameter($displayName).'%'))); - if ($principalUri) { - $query->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); - } - $result = $query->execute(); - - $calendarUris = []; - while($row = $result->fetch()) { - $calendarUris[] = $row['uri']; - } - return $calendarUris; - } - /** * read VCalendar data into a VCalendar object * diff --git a/apps/dav/lib/Command/ListCalendars.php b/apps/dav/lib/Command/ListCalendars.php new file mode 100644 index 0000000000..9080e9f8e2 --- /dev/null +++ b/apps/dav/lib/Command/ListCalendars.php @@ -0,0 +1,108 @@ + + * @author Thomas Citharel + * + * @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 + * + */ +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('user', + InputArgument::REQUIRED, + 'User for whom all calendars will be listed'); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + $user = $input->getArgument('user'); + 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("User <$user> has no calendars"); + } + } + +} diff --git a/apps/dav/lib/Command/MoveCalendar.php b/apps/dav/lib/Command/MoveCalendar.php index 20d04bfdd6..0c2e6daa05 100644 --- a/apps/dav/lib/Command/MoveCalendar.php +++ b/apps/dav/lib/Command/MoveCalendar.php @@ -22,10 +22,13 @@ 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; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -36,37 +39,45 @@ use Symfony\Component\Console\Style\SymfonyStyle; class MoveCalendar extends Command { /** @var IUserManager */ - protected $userManager; + private $userManager; /** @var IGroupManager $groupManager */ private $groupManager; - /** @var \OCP\IDBConnection */ - protected $dbConnection; + /** @var IConfig $config */ + private $config; /** @var IL10N */ - protected $l10n; + private $l10n; /** @var SymfonyStyle */ private $io; /** @var CalDavBackend */ - private $caldav; + private $calDav; const URI_USERS = 'principals/users/'; /** * @param IUserManager $userManager * @param IGroupManager $groupManager - * @param IDBConnection $dbConnection + * @param IConfig $config * @param IL10N $l10n + * @param CalDavBackend $calDav */ - function __construct(IUserManager $userManager, IGroupManager $groupManager, IDBConnection $dbConnection, IL10N $l10n) { + function __construct( + IUserManager $userManager, + IGroupManager $groupManager, + IConfig $config, + IL10N $l10n, + CalDavBackend $calDav + ) { parent::__construct(); $this->userManager = $userManager; $this->groupManager = $groupManager; - $this->dbConnection = $dbConnection; + $this->config = $config; $this->l10n = $l10n; + $this->calDav = $calDav; } protected function configure() { @@ -104,35 +115,21 @@ class MoveCalendar extends Command { throw new \InvalidArgumentException("User <$userDestination> is unknown."); } - $principalBackend = new Principal( - $this->userManager, - $this->groupManager - ); - $random = \OC::$server->getSecureRandom(); - $dispatcher = \OC::$server->getEventDispatcher(); - $name = $input->getArgument('name'); - $this->caldav = new CalDavBackend($this->dbConnection, $principalBackend, $this->userManager, $random, $dispatcher); - $calendar = $this->caldav->getCalendarByUri(self::URI_USERS . $userOrigin, $name); + $calendar = $this->calDav->getCalendarByUri(self::URI_USERS . $userOrigin, $name); if (null === $calendar) { - /** If we got no matching calendar with URI, let's try the display names */ - $suggestedUris = $this->caldav->findCalendarsUrisByDisplayName($name, self::URI_USERS . $userOrigin); - if (count($suggestedUris) > 0) { - $this->io->note('No calendar with this URI was found, but you may want to try with these?'); - $this->io->listing($suggestedUris); - } - throw new \InvalidArgumentException("User <$userOrigin> has no calendar named <$name>."); + 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)) { + if (null !== $this->calDav->getCalendarByUri(self::URI_USERS . $userDestination, $name)) { throw new \InvalidArgumentException("User <$userDestination> already has a calendar named <$name>."); } $this->checkShares($calendar, $userDestination, $input->getOption('force')); - $this->caldav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination); + $this->calDav->moveCalendar($name, self::URI_USERS . $userOrigin, self::URI_USERS . $userDestination); $this->io->success("Calendar <$name> was moved from user <$userOrigin> to <$userDestination>"); } @@ -147,14 +144,14 @@ class MoveCalendar extends Command { */ private function checkShares($calendar, $userDestination, $force = false) { - $shares = $this->caldav->getShares($calendar['id']); + $shares = $this->calDav->getShares($calendar['id']); foreach ($shares as $share) { - list($prefix, $group) = str_split($share['href'], 28); - if ('principal:principals/groups/' === $prefix && !$this->groupManager->isInGroup($userDestination, $group)) { + list(, $prefix, $group) = explode('/', $share['href'], 3); + if ('groups' === $prefix && !$this->groupManager->isInGroup($userDestination, $group)) { if ($force) { - $this->caldav->updateShares(new Calendar($this->caldav, $calendar, $this->l10n), [], ['href' => 'principal:principals/groups/' . $group]); + $this->calDav->updateShares(new Calendar($this->calDav, $calendar, $this->l10n, $this->config), [], ['href' => 'principal:principals/groups/' . $group]); } else { - throw new \InvalidArgumentException("User <$userDestination> is not part of the group <$group> with which the calendar <" . $calendar['uri'] . "> was shared. You may use -f to move the calendar while deleting this share."); + throw new \InvalidArgumentException("User <$userDestination> is not part of the group <$group> with whom the calendar <" . $calendar['uri'] . "> was shared. You may use -f to move the calendar while deleting this share."); } } } diff --git a/apps/dav/tests/unit/Command/ListCalendarsTest.php b/apps/dav/tests/unit/Command/ListCalendarsTest.php new file mode 100644 index 0000000000..8d19c72491 --- /dev/null +++ b/apps/dav/tests/unit/Command/ListCalendarsTest.php @@ -0,0 +1,139 @@ + + * + * @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 + * + */ + +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([ + 'user' => 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([ + 'user' => 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([ + 'user' => 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 index 12329fd4af..5e5c1f150e 100644 --- a/apps/dav/tests/unit/Command/MoveCalendarTest.php +++ b/apps/dav/tests/unit/Command/MoveCalendarTest.php @@ -21,8 +21,9 @@ namespace OCA\DAV\Tests\Command; use InvalidArgumentException; +use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\Command\MoveCalendar; -use OCP\IDBConnection; +use OCP\IConfig; use OCP\IGroupManager; use OCP\IL10N; use OCP\IUserManager; @@ -34,7 +35,6 @@ use Test\TestCase; * Class MoveCalendarTest * * @package OCA\DAV\Tests\Command - * @group DB */ class MoveCalendarTest extends TestCase { @@ -44,12 +44,15 @@ class MoveCalendarTest extends TestCase { /** @var \OCP\IGroupManager|\PHPUnit_Framework_MockObject_MockObject $groupManager */ private $groupManager; - /** @var \OCP\IDBConnection|\PHPUnit_Framework_MockObject_MockObject $dbConnection */ - private $dbConnection; + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $l10n */ + private $config; - /** @var \OCP\IL10N|\PHPUnit_Framework_MockObject_MockObject $l10n */ + /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject $l10n */ private $l10n; + /** @var CalDavBackend|\PHPUnit_Framework_MockObject_MockObject $l10n */ + private $calDav; + /** @var MoveCalendar */ private $command; @@ -58,14 +61,16 @@ class MoveCalendarTest extends TestCase { $this->userManager = $this->createMock(IUserManager::class); $this->groupManager = $this->createMock(IGroupManager::class); - $this->dbConnection = $this->createMock(IDBConnection::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->dbConnection, - $this->l10n + $this->config, + $this->l10n, + $this->calDav ); } @@ -118,4 +123,229 @@ class MoveCalendarTest extends TestCase { 'userdestination' => 'user2', ]); } -} \ No newline at end of file + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage User has no calendar named . 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', + 'userorigin' => 'user', + 'userdestination' => 'user2', + ]); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage User already has a calendar named . + */ + 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', + 'userorigin' => 'user', + 'userdestination' => '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', + 'userorigin' => 'user', + 'userdestination' => 'user2', + ]); + + $this->assertContains("[OK] Calendar was moved from user to ", $commandTester->getDisplay()); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage User is not part of the group with whom the calendar was shared. You may use -f to move the calendar while deleting this share. + */ + public function testMoveWithDestinationNotPartOfGroup() + { + $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->calDav->expects($this->once())->method('getShares') + ->with(1234) + ->willReturn([ + ['href' => 'principal:principals/groups/nextclouders'] + ]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'name' => 'personal', + 'userorigin' => 'user', + 'userdestination' => '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->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', + 'userorigin' => 'user', + 'userdestination' => 'user2', + ]); + + $this->assertContains("[OK] Calendar was moved from user to ", $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->calDav->expects($this->once())->method('getShares') + ->with(1234) + ->willReturn([ + ['href' => 'principal:principals/groups/nextclouders'] + ]); + + $commandTester = new CommandTester($this->command); + $commandTester->execute([ + 'name' => 'personal', + 'userorigin' => 'user', + 'userdestination' => 'user2', + '--force' => true + ]); + + $this->assertContains("[OK] Calendar was moved from user to ", $commandTester->getDisplay()); + } +}