Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI command to remove notifications from an announcement #840

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
79 changes: 79 additions & 0 deletions lib/Command/RemoveNotifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

/**
* @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 {
public function __construct(
protected Manager $manager,
protected LoggerInterface $logger,
) {
parent::__construct();
}

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);
$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 (int)$value;
}
throw new InvalidArgumentException('Id "' . $value . '" is not an integer');
}
}
116 changes: 116 additions & 0 deletions tests/Command/RemoveNotificationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);
/**
* @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 $removeNotificationsCommand;
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->removeNotificationsCommand = 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->removeNotificationsCommand, '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->removeNotificationsCommand, '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('getAnnouncement')
->willThrowException(new AnnouncementDoesNotExistException('message'));
$this->output->expects($this->atLeastOnce())
->method('writeln');
$result = self::invokePrivate($this->removeNotificationsCommand, 'execute', [$this->input, $this->output]);
self::assertEquals(true, $result > 0);
}
}
Loading