-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
occ: new command dav:delete-birthday-calendar
Add occ command 'dav:delete-birthday-calendar' to delete a user's birthday calendar. Signed-off-by: Mattia Narducci <[email protected]>
- Loading branch information
Showing
5 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* | ||
* @copyright Copyright (c) 2021, Mattia Narducci ([email protected]) | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* 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 | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
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\Output\OutputInterface; | ||
|
||
class DeleteBirthdayCalendar 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() { | ||
$this | ||
->setName('dav:delete-birthday-calendar') | ||
->setDescription('Remove a birthday calendar') | ||
->addArgument('uid', | ||
InputArgument::REQUIRED, | ||
'User who owns the calendar'); | ||
} | ||
|
||
protected function execute( | ||
InputInterface $input, | ||
OutputInterface $output | ||
) : int { | ||
$user = $input->getArgument('uid'); | ||
if (!$this->userManager->userExists($user)) { | ||
throw new \InvalidArgumentException( | ||
'User <' . $user . '> is unknown.'); | ||
} | ||
|
||
$calendar = $this->calDav->getCalendarByUri( | ||
'principals/users/' . $user, | ||
BirthdayService::BIRTHDAY_CALENDAR_URI); | ||
if ($calendar === null) { | ||
throw new \InvalidArgumentException( | ||
'User <' . $user . '> does not have a birthday calendar'); | ||
} | ||
$calendar = new Calendar( | ||
$this->calDav, | ||
$calendar, | ||
$this->l10n, | ||
$this->config); | ||
$calendar->delete(); | ||
return 0; | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
apps/dav/tests/unit/Command/DeleteBirthdayCalendarTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* | ||
* @copyright Copyright (c) 2021, Mattia Narducci ([email protected]) | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* 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 | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\DAV\Tests\Command; | ||
|
||
use OCA\DAV\CalDAV\BirthdayService; | ||
use OCA\DAV\CalDav\CalDavBackend; | ||
use OCA\DAV\Command\DeleteBirthdayCalendar; | ||
use OCP\IConfig; | ||
use OCP\IL10N; | ||
use OCP\IUserManager; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Test\TestCase; | ||
|
||
/** | ||
* Class DeleteBirthdayCalendarTest | ||
* | ||
* @package OCA\DAV\Tests\Command | ||
*/ | ||
class DeleteBirthdayCalendarTest extends TestCase { | ||
public const USER = 'user'; | ||
|
||
/** @var CalDavBackend|MockObject */ | ||
private $calDav; | ||
|
||
/** @var IConfig|MockObject */ | ||
private $config; | ||
|
||
/** @var IL10N|MockObject */ | ||
private $l10n; | ||
|
||
/** @var IUserManager|MockObject */ | ||
private $userManager; | ||
|
||
/** @var DeleteBirthdayCalendar */ | ||
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 DeleteBirthdayCalendar( | ||
$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, | ||
]); | ||
} | ||
|
||
public function testNoCalendar() { | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage( | ||
'User <' . self::USER . '> does not have a birthday calendar'); | ||
|
||
$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(null); | ||
|
||
$commandTester = new CommandTester($this->command); | ||
$commandTester->execute([ | ||
'uid' => self::USER, | ||
]); | ||
} | ||
|
||
public function testDelete() { | ||
$id = 1234; | ||
|
||
$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([ | ||
'id' => $id, | ||
'principaluri' => | ||
'principals/users/' . self::USER, | ||
'uri' => BirthdayService::BIRTHDAY_CALENDAR_URI | ||
]); | ||
$this->calDav->expects($this->once()) | ||
->method('deleteCalendar') | ||
->with($id); | ||
|
||
$commandTester = new CommandTester($this->command); | ||
$commandTester->execute([ | ||
'uid' => self::USER, | ||
]); | ||
} | ||
} |