From 4850dae1df586d107c18f4f249dca445ad2798f3 Mon Sep 17 00:00:00 2001 From: Mattia Narducci Date: Fri, 4 Jun 2021 00:49:32 +0200 Subject: [PATCH] occ: new command dav:delete-calendar Add occ command 'dav:delete-calendar' to delete a user's calendar. Signed-off-by: Mattia Narducci --- apps/dav/appinfo/info.xml | 1 + .../composer/composer/autoload_classmap.php | 1 + .../dav/composer/composer/autoload_static.php | 1 + apps/dav/lib/Command/DeleteCalendar.php | 137 ++++++++++ .../tests/unit/Command/DeleteCalendarTest.php | 250 ++++++++++++++++++ 5 files changed, 390 insertions(+) create mode 100644 apps/dav/lib/Command/DeleteCalendar.php create mode 100644 apps/dav/tests/unit/Command/DeleteCalendarTest.php diff --git a/apps/dav/appinfo/info.xml b/apps/dav/appinfo/info.xml index c99d82a798d73..0cfa2e968ea74 100644 --- a/apps/dav/appinfo/info.xml +++ b/apps/dav/appinfo/info.xml @@ -47,6 +47,7 @@ OCA\DAV\Command\CreateAddressBook OCA\DAV\Command\CreateCalendar + OCA\DAV\Command\DeleteCalendar OCA\DAV\Command\MoveCalendar OCA\DAV\Command\ListCalendars OCA\DAV\Command\RetentionCleanupCommand diff --git a/apps/dav/composer/composer/autoload_classmap.php b/apps/dav/composer/composer/autoload_classmap.php index 19c0e2549f65a..13e78cee71645 100644 --- a/apps/dav/composer/composer/autoload_classmap.php +++ b/apps/dav/composer/composer/autoload_classmap.php @@ -118,6 +118,7 @@ '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\\DeleteCalendar' => $baseDir . '/../lib/Command/DeleteCalendar.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', diff --git a/apps/dav/composer/composer/autoload_static.php b/apps/dav/composer/composer/autoload_static.php index 6895064e39df7..42ba59e0582e2 100644 --- a/apps/dav/composer/composer/autoload_static.php +++ b/apps/dav/composer/composer/autoload_static.php @@ -133,6 +133,7 @@ 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\\DeleteCalendar' => __DIR__ . '/..' . '/../lib/Command/DeleteCalendar.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', diff --git a/apps/dav/lib/Command/DeleteCalendar.php b/apps/dav/lib/Command/DeleteCalendar.php new file mode 100644 index 0000000000000..42aa0e3278f52 --- /dev/null +++ b/apps/dav/lib/Command/DeleteCalendar.php @@ -0,0 +1,137 @@ +. + * + */ + +namespace OCA\DAV\Command; + +use OCA\DAV\CalDAV\BirthdayService; +use OCA\DAV\CalDAV\CalDavBackend; +use OCA\DAV\CalDAV\Calendar; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IUserManager; +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; + +class DeleteCalendar extends Command { + /** @var CalDavBackend */ + private $calDav; + + /** @var IConfig */ + private $config; + + /** @var IL10N */ + private $l10n; + + /** @var IUserManager */ + private $userManager; + + /** + * @param CalDavBackend $calDav + * @param IConfig $config + * @param IL10N $l10n + * @param IUserManager $userManager + */ + public function __construct( + CalDavBackend $calDav, + IConfig $config, + IL10N $l10n, + IUserManager $userManager + ) { + parent::__construct(); + $this->calDav = $calDav; + $this->config = $config; + $this->l10n = $l10n; + $this->userManager = $userManager; + } + + protected function configure(): void { + $this + ->setName('dav:delete-calendar') + ->setDescription('Delete a dav calendar') + ->addArgument('uid', + InputArgument::REQUIRED, + 'User who owns the calendar') + ->addArgument('name', + InputArgument::OPTIONAL, + 'Name of the calendar to delete') + ->addOption('birthday', + null, + InputOption::VALUE_NONE, + 'Delete the birthday calendar') + ->addOption('force', + 'f', + InputOption::VALUE_NONE, + 'Force delete skipping trashbin'); + } + + protected function execute( + InputInterface $input, + OutputInterface $output + ): int { + /** @var string $user **/ + $user = $input->getArgument('uid'); + if (!$this->userManager->userExists($user)) { + throw new \InvalidArgumentException( + 'User <' . $user . '> is unknown.'); + } + + $birthday = $input->getOption('birthday'); + if ($birthday !== false) { + $name = BirthdayService::BIRTHDAY_CALENDAR_URI; + } else { + /** @var string $name **/ + $name = $input->getArgument('name'); + if (!$name) { + throw new \InvalidArgumentException( + 'Please specify a calendar name or --birthday'); + } + } + + $calendarInfo = $this->calDav->getCalendarByUri( + 'principals/users/' . $user, + $name); + if ($calendarInfo === null) { + throw new \InvalidArgumentException( + 'User <' . $user . '> has no calendar named <' . $name . '>. You can run occ dav:list-calendars to list calendars URIs for this user.'); + } + + $calendar = new Calendar( + $this->calDav, + $calendarInfo, + $this->l10n, + $this->config); + + $force = $input->getOption('force'); + if ($force) { + $calendar->disableTrashbin(); + } + + $calendar->delete(); + + return 0; + } +} diff --git a/apps/dav/tests/unit/Command/DeleteCalendarTest.php b/apps/dav/tests/unit/Command/DeleteCalendarTest.php new file mode 100644 index 0000000000000..ac4e8f07413bd --- /dev/null +++ b/apps/dav/tests/unit/Command/DeleteCalendarTest.php @@ -0,0 +1,250 @@ +. + * + */ + +namespace OCA\DAV\Tests\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 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'; + + /** @var CalDavBackend|MockObject */ + private $calDav; + + /** @var IConfig|MockObject */ + private $config; + + /** @var IL10N|MockObject */ + private $l10n; + + /** @var IUserManager|MockObject */ + private $userManager; + + /** @var DeleteCalendar */ + private $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->command = new DeleteCalendar( + $this->calDav, + $this->config, + $this->l10n, + $this->userManager, + ); + } + + public function testInvalidUser() { + $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() { + $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() { + $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() { + $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() { + $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() { + $id = 1234; + $calendar = [ + 'id' => $id, + 'principaluri' => 'principals/users/' . self::USER, + 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI + ]; + + $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() { + $calendar = [ + 'id' => 1234, + 'principaluri' => 'principals/users/' . self::USER, + 'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI + ]; + $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, + ]); + } +}