Skip to content

Commit

Permalink
Add CLI command to remove notificitions from an announcement
Browse files Browse the repository at this point in the history
Signed-off-by: Marvin Winkens <m.winkens@fz-juelich.de>
  • Loading branch information
mwinkens committed Sep 20, 2024
1 parent 42f4f41 commit 2fdaa89
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<command>OCA\AnnouncementCenter\Command\Announce</command>
<command>OCA\AnnouncementCenter\Command\AnnouncementList</command>
<command>OCA\AnnouncementCenter\Command\AnnouncementDelete</command>
<command>OCA\AnnouncementCenter\Command\RemoveNotifications</command>
</commands>

<settings>
Expand Down
78 changes: 78 additions & 0 deletions lib/Command/RemoveNotifications.php
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');
}
}
114 changes: 114 additions & 0 deletions tests/Command/RemoveNotificationsTest.php
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);
}
}

0 comments on commit 2fdaa89

Please sign in to comment.