-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI command to remove notificitions from an announcement
Signed-off-by: Marvin Winkens <m.winkens@fz-juelich.de>
- Loading branch information
Showing
3 changed files
with
193 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2024 Marvin Winkens <m.winkens@fz-juelich.de> | ||
* | ||
* @author Marvin Winkens <m.winkens@fz-juelich.de> | ||
* | ||
* @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 <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
namespace OCA\AnnouncementCenter\Command; | ||
|
||
use InvalidArgumentException; | ||
use OCA\AnnouncementCenter\Manager; | ||
use OCA\AnnouncementCenter\Model\AnnouncementDoesNotExistException; | ||
use Psr\Log\LoggerInterface; | ||
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 RemoveNotifications extends Command | ||
{ | ||
protected Manager $manager; | ||
protected LoggerInterface $logger; | ||
public function __construct(Manager $manager, LoggerInterface $logger) { | ||
parent::__construct(); | ||
$this->manager = $manager; | ||
$this->logger = $logger; | ||
} | ||
|
||
protected function configure(): void { | ||
$this | ||
->setName('announcementcenter:remove-notifications') # others use minus sign as well | ||
->setDescription('Remove notifications of announcement by id') | ||
->addArgument( | ||
'id', | ||
InputArgument::REQUIRED, | ||
'Id of announcement to remove notifications from', | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int { | ||
try { | ||
$removeNotId = $this->parseId($input->getArgument('id')); | ||
$this->manager->getAnnouncement($removeNotId, true); # check if announcement exists | ||
$this->manager->removeNotifications($removeNotId); | ||
} catch (AnnouncementDoesNotExistException) { | ||
$output->writeln('Announcement with #' . $removeNotId . ' does not exist!'); | ||
return 1; | ||
} catch (InvalidArgumentException $e) { | ||
$output->writeln($e->getMessage()); | ||
return 1; | ||
} | ||
$output->writeln('Successfully removed notifications from accouncement #' . $removeNotId); | ||
$this->logger->info('Admin removed notifications from announcement #' . $removeNotId . ' over CLI'); | ||
return 0; | ||
} | ||
|
||
private function parseId(mixed $value) { | ||
if (is_numeric($value)) { | ||
return intval($value); | ||
} | ||
throw new InvalidArgumentException('Id "' . $value . '" is not an integer'); | ||
} | ||
} |
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,114 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2024 Marvin Winkens <m.winkens@fz-juelich.de> | ||
* | ||
* @author Marvin Winkens <m.winkens@fz-juelich.de> | ||
* | ||
* @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 <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
namespace OCA\AnnouncementCenter\Tests\Command; | ||
|
||
use OCA\AnnouncementCenter\Command\RemoveNotifications; | ||
use OCA\AnnouncementCenter\Manager; | ||
use OCA\AnnouncementCenter\Model\AnnouncementDoesNotExistException; | ||
use OCA\AnnouncementCenter\Tests\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class RemoveNotificationsTest extends TestCase { | ||
protected Manager|MockObject $manager; | ||
protected LoggerInterface|MockObject $logger; | ||
protected Command $removeNotifictionsCommand; | ||
protected InputInterface|MockObject $input; | ||
protected OutputInterface|MockObject $output; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->manager = $this->createMock(Manager::class); | ||
$this->logger = $this->createMock(LoggerInterface::class); | ||
|
||
$this->input = $this->getMockBuilder(InputInterface::class) | ||
->setMethods([ | ||
'getArgument', | ||
'getOption', | ||
'getFirstArgument', | ||
'hasParameterOption', | ||
'getParameterOption', | ||
'bind', | ||
'validate', | ||
'getArguments', | ||
'setArgument', | ||
'hasArgument', | ||
'getOptions', | ||
'isInteractive', | ||
'hasOption', | ||
'setOption', | ||
'setInteractive', | ||
]) | ||
->getMock(); | ||
$this->output = $this->createMock(OutputInterface::class); | ||
|
||
$this->removeNotifictionsCommand = new RemoveNotifications( | ||
$this->manager, | ||
$this->logger, | ||
); | ||
} | ||
|
||
public function testRemoveNotificationsSuccessfully() { | ||
$this->input->expects($this->once()) | ||
->method('getArgument') | ||
->with('id') | ||
->willReturn(42); | ||
$this->manager->expects($this->once()) | ||
->method('removeNotifications'); | ||
$this->manager->expects($this->once()) | ||
->method('getAnnouncement'); | ||
$this->output->expects($this->atLeastOnce()) | ||
->method('writeln'); | ||
$this->logger->expects($this->atLeastOnce()) | ||
->method('info'); | ||
$result = self::invokePrivate($this->removeNotifictionsCommand, 'execute', [$this->input, $this->output]); | ||
self::assertEquals(0, $result); | ||
} | ||
|
||
public function testRemoveNotificationsInvalidId() { | ||
$this->input->expects($this->once()) | ||
->method('getArgument') | ||
->with('id') | ||
->willReturn('invalid'); | ||
$result = self::invokePrivate($this->removeNotifictionsCommand, 'execute', [$this->input, $this->output]); | ||
self::assertEquals(true, $result > 0); | ||
} | ||
|
||
public function testRemoveNotificationsDoesNotExist() { | ||
$this->input->expects($this->once()) | ||
->method('getArgument') | ||
->with('id') | ||
->willReturn(42); | ||
$this->manager->expects($this->once()) | ||
->method('getAnnoucement') | ||
->willThrowException(new AnnouncementDoesNotExistException('message')); | ||
$this->output->expects($this->atLeastOnce()) | ||
->method('writeln'); | ||
$result = self::invokePrivate($this->removeNotifictionsCommand, 'execute', [$this->input, $this->output]); | ||
self::assertEquals(true, $result > 0); | ||
} | ||
} |