Skip to content

Commit bb580f5

Browse files
committed
occ: new command dav:delete-calendar
Add occ command 'dav:delete-calendar' to delete a user's calendar. Signed-off-by: Mattia Narducci <mattianarducci1@gmail.com>
1 parent 4fc002a commit bb580f5

File tree

5 files changed

+358
-0
lines changed

5 files changed

+358
-0
lines changed

apps/dav/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<commands>
4848
<command>OCA\DAV\Command\CreateAddressBook</command>
4949
<command>OCA\DAV\Command\CreateCalendar</command>
50+
<command>OCA\DAV\Command\DeleteCalendar</command>
5051
<command>OCA\DAV\Command\MoveCalendar</command>
5152
<command>OCA\DAV\Command\ListCalendars</command>
5253
<command>OCA\DAV\Command\RetentionCleanupCommand</command>

apps/dav/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
'OCA\\DAV\\CardDAV\\Xml\\Groups' => $baseDir . '/../lib/CardDAV/Xml/Groups.php',
119119
'OCA\\DAV\\Command\\CreateAddressBook' => $baseDir . '/../lib/Command/CreateAddressBook.php',
120120
'OCA\\DAV\\Command\\CreateCalendar' => $baseDir . '/../lib/Command/CreateCalendar.php',
121+
'OCA\\DAV\\Command\\DeleteCalendar' => $baseDir . '/../lib/Command/DeleteCalendar.php',
121122
'OCA\\DAV\\Command\\ListCalendars' => $baseDir . '/../lib/Command/ListCalendars.php',
122123
'OCA\\DAV\\Command\\MoveCalendar' => $baseDir . '/../lib/Command/MoveCalendar.php',
123124
'OCA\\DAV\\Command\\RemoveInvalidShares' => $baseDir . '/../lib/Command/RemoveInvalidShares.php',

apps/dav/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class ComposerStaticInitDAV
133133
'OCA\\DAV\\CardDAV\\Xml\\Groups' => __DIR__ . '/..' . '/../lib/CardDAV/Xml/Groups.php',
134134
'OCA\\DAV\\Command\\CreateAddressBook' => __DIR__ . '/..' . '/../lib/Command/CreateAddressBook.php',
135135
'OCA\\DAV\\Command\\CreateCalendar' => __DIR__ . '/..' . '/../lib/Command/CreateCalendar.php',
136+
'OCA\\DAV\\Command\\DeleteCalendar' => __DIR__ . '/..' . '/../lib/Command/DeleteCalendar.php',
136137
'OCA\\DAV\\Command\\ListCalendars' => __DIR__ . '/..' . '/../lib/Command/ListCalendars.php',
137138
'OCA\\DAV\\Command\\MoveCalendar' => __DIR__ . '/..' . '/../lib/Command/MoveCalendar.php',
138139
'OCA\\DAV\\Command\\RemoveInvalidShares' => __DIR__ . '/..' . '/../lib/Command/RemoveInvalidShares.php',
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
*
6+
* @copyright Copyright (c) 2021, Mattia Narducci (mattianarducci1@gmail.com)
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OCA\DAV\Command;
26+
27+
use OCA\DAV\CalDAV\BirthdayService;
28+
use OCA\DAV\CalDAV\CalDavBackend;
29+
use OCP\IConfig;
30+
use OCP\IUserManager;
31+
use Symfony\Component\Console\Command\Command;
32+
use Symfony\Component\Console\Input\InputArgument;
33+
use Symfony\Component\Console\Input\InputInterface;
34+
use Symfony\Component\Console\Input\InputOption;
35+
use Symfony\Component\Console\Output\OutputInterface;
36+
37+
class DeleteCalendar extends Command {
38+
/** @var CalDavBackend */
39+
private $calDav;
40+
41+
/** @var IConfig */
42+
private $config;
43+
44+
/** @var IUserManager */
45+
private $userManager;
46+
47+
/**
48+
* @param CalDavBackend $calDav
49+
* @param IConfig $config
50+
* @param IUserManager $userManager
51+
*/
52+
public function __construct(
53+
CalDavBackend $calDav,
54+
IConfig $config,
55+
IUserManager $userManager
56+
) {
57+
parent::__construct();
58+
$this->calDav = $calDav;
59+
$this->config = $config;
60+
$this->userManager = $userManager;
61+
}
62+
63+
protected function configure(): void {
64+
$this
65+
->setName('dav:delete-calendar')
66+
->setDescription('Delete a dav calendar')
67+
->addArgument('uid',
68+
InputArgument::REQUIRED,
69+
'User who owns the calendar')
70+
->addArgument('name',
71+
InputArgument::OPTIONAL,
72+
'Name of the calendar to delete')
73+
->addOption('birthday',
74+
null,
75+
InputOption::VALUE_NONE,
76+
'Delete the birthday calendar')
77+
->addOption('force',
78+
'f',
79+
InputOption::VALUE_NONE,
80+
'Force delete skipping trashbin');
81+
}
82+
83+
protected function execute(
84+
InputInterface $input,
85+
OutputInterface $output
86+
): int {
87+
/** @var string $user **/
88+
$user = $input->getArgument('uid');
89+
if (!$this->userManager->userExists($user)) {
90+
throw new \InvalidArgumentException(
91+
'User <' . $user . '> is unknown.');
92+
}
93+
94+
$birthday = $input->getOption('birthday');
95+
if ($birthday !== false) {
96+
$name = BirthdayService::BIRTHDAY_CALENDAR_URI;
97+
} else {
98+
/** @var string $name **/
99+
$name = $input->getArgument('name');
100+
if (!$name) {
101+
throw new \InvalidArgumentException(
102+
'Please specify a calendar name or --birthday');
103+
}
104+
}
105+
106+
$calendar = $this->calDav->getCalendarByUri(
107+
'principals/users/' . $user,
108+
$name);
109+
if ($calendar === null) {
110+
throw new \InvalidArgumentException(
111+
'User <' . $user . '> has no calendar named <' . $name . '>. You can run occ dav:list-calendars to list calendars URIs for this user.');
112+
}
113+
114+
if ($name === BirthdayService::BIRTHDAY_CALENDAR_URI) {
115+
$this->config->setUserValue($user,
116+
'dav', 'generateBirthdayCalendar', 'no');
117+
}
118+
119+
$force = $input->getOption('force');
120+
121+
$this->calDav->deleteCalendar($calendar['id'], $force !== false);
122+
123+
return 0;
124+
}
125+
}
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
*
6+
* @copyright Copyright (c) 2021, Mattia Narducci (mattianarducci1@gmail.com)
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OCA\DAV\Tests\Command;
26+
27+
use OCA\DAV\CalDAV\BirthdayService;
28+
use OCA\DAV\CalDav\CalDavBackend;
29+
use OCA\DAV\Command\DeleteCalendar;
30+
use OCP\IConfig;
31+
use OCP\IUserManager;
32+
use PHPUnit\Framework\MockObject\MockObject;
33+
use Symfony\Component\Console\Tester\CommandTester;
34+
use Test\TestCase;
35+
36+
/**
37+
* Class DeleteCalendarTest
38+
*
39+
* @package OCA\DAV\Tests\Command
40+
*/
41+
class DeleteCalendarTest extends TestCase {
42+
public const USER = 'user';
43+
public const NAME = 'calendar';
44+
45+
/** @var CalDavBackend|MockObject */
46+
private $calDav;
47+
48+
/** @var IConfig|MockObject */
49+
private $config;
50+
51+
/** @var IUserManager|MockObject */
52+
private $userManager;
53+
54+
/** @var DeleteCalendar */
55+
private $command;
56+
57+
protected function setUp(): void {
58+
parent::setUp();
59+
60+
$this->calDav = $this->createMock(CalDavBackend::class);
61+
$this->config = $this->createMock(IConfig::class);
62+
$this->userManager = $this->createMock(IUserManager::class);
63+
64+
$this->command = new DeleteCalendar(
65+
$this->calDav,
66+
$this->config,
67+
$this->userManager,
68+
);
69+
}
70+
71+
public function testInvalidUser() {
72+
$this->expectException(\InvalidArgumentException::class);
73+
$this->expectExceptionMessage(
74+
'User <' . self::USER . '> is unknown.');
75+
76+
$this->userManager->expects($this->once())
77+
->method('userExists')
78+
->with(self::USER)
79+
->willReturn(false);
80+
81+
$commandTester = new CommandTester($this->command);
82+
$commandTester->execute([
83+
'uid' => self::USER,
84+
'name' => self::NAME,
85+
]);
86+
}
87+
88+
public function testNoCalendarName() {
89+
$this->expectException(\InvalidArgumentException::class);
90+
$this->expectExceptionMessage(
91+
'Please specify a calendar name or --birthday');
92+
93+
$this->userManager->expects($this->once())
94+
->method('userExists')
95+
->with(self::USER)
96+
->willReturn(true);
97+
98+
$commandTester = new CommandTester($this->command);
99+
$commandTester->execute([
100+
'uid' => self::USER,
101+
]);
102+
}
103+
104+
public function testInvalidCalendar() {
105+
$this->expectException(\InvalidArgumentException::class);
106+
$this->expectExceptionMessage(
107+
'User <' . self::USER . '> has no calendar named <' . self::NAME . '>.');
108+
109+
$this->userManager->expects($this->once())
110+
->method('userExists')
111+
->with(self::USER)
112+
->willReturn(true);
113+
$this->calDav->expects($this->once())
114+
->method('getCalendarByUri')
115+
->with(
116+
'principals/users/' . self::USER,
117+
self::NAME
118+
)
119+
->willReturn(null);
120+
121+
$commandTester = new CommandTester($this->command);
122+
$commandTester->execute([
123+
'uid' => self::USER,
124+
'name' => self::NAME,
125+
]);
126+
}
127+
128+
public function testDelete() {
129+
$id = 1234;
130+
131+
$this->userManager->expects($this->once())
132+
->method('userExists')
133+
->with(self::USER)
134+
->willReturn(true);
135+
$this->calDav->expects($this->once())
136+
->method('getCalendarByUri')
137+
->with(
138+
'principals/users/' . self::USER,
139+
self::NAME
140+
)
141+
->willReturn(['id' => $id]);
142+
$this->calDav->expects($this->once())
143+
->method('deleteCalendar')
144+
->with($id, false);
145+
146+
$commandTester = new CommandTester($this->command);
147+
$commandTester->execute([
148+
'uid' => self::USER,
149+
'name' => self::NAME,
150+
]);
151+
}
152+
153+
public function testForceDelete() {
154+
$id = 1234;
155+
156+
$this->userManager->expects($this->once())
157+
->method('userExists')
158+
->with(self::USER)
159+
->willReturn(true);
160+
$this->calDav->expects($this->once())
161+
->method('getCalendarByUri')
162+
->with(
163+
'principals/users/' . self::USER,
164+
self::NAME
165+
)
166+
->willReturn(['id' => $id]);
167+
$this->calDav->expects($this->once())
168+
->method('deleteCalendar')
169+
->with($id, true);
170+
171+
$commandTester = new CommandTester($this->command);
172+
$commandTester->execute([
173+
'uid' => self::USER,
174+
'name' => self::NAME,
175+
'-f' => true
176+
]);
177+
}
178+
179+
public function testDeleteBirthday() {
180+
$id = 1234;
181+
182+
$this->userManager->expects($this->once())
183+
->method('userExists')
184+
->with(self::USER)
185+
->willReturn(true);
186+
$this->calDav->expects($this->once())
187+
->method('getCalendarByUri')
188+
->with(
189+
'principals/users/' . self::USER,
190+
BirthdayService::BIRTHDAY_CALENDAR_URI
191+
)
192+
->willReturn(['id' => $id]);
193+
$this->config->expects($this->once())
194+
->method('setUserValue')
195+
->with(
196+
self::USER,
197+
'dav', 'generateBirthdayCalendar', 'no'
198+
);
199+
$this->calDav->expects($this->once())
200+
->method('deleteCalendar')
201+
->with($id);
202+
203+
$commandTester = new CommandTester($this->command);
204+
$commandTester->execute([
205+
'uid' => self::USER,
206+
'--birthday' => true,
207+
]);
208+
}
209+
210+
public function testBirthdayHasPrecedence() {
211+
$this->userManager->expects($this->once())
212+
->method('userExists')
213+
->with(self::USER)
214+
->willReturn(true);
215+
$this->calDav->expects($this->once())
216+
->method('getCalendarByUri')
217+
->with(
218+
'principals/users/' . self::USER,
219+
BirthdayService::BIRTHDAY_CALENDAR_URI
220+
)
221+
->willReturn(['id' => 1234]);
222+
223+
$commandTester = new CommandTester($this->command);
224+
$commandTester->execute([
225+
'uid' => self::USER,
226+
'name' => self::NAME,
227+
'--birthday' => true,
228+
]);
229+
}
230+
}

0 commit comments

Comments
 (0)