diff --git a/README.md b/README.md index 9740ccb..63e1b78 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ +#⚠️ App "admin_notifications" is obsoleted with Nextcloud 14 ⚠️ +The functionality of the "admin_notifications" app has been merged into the default notifications app for Nextcloud 14. You can savely uninstall and delete the "admin_notifications" app, because it does not do anything anymore. + +The new documentation is in the [notifications repository](https://github.com/nextcloud/notifications/blob/master/docs/admin-notifications.md). + + # 🔧🔔 Admin notifications Allows admins to generate notifications for users via the console or an HTTP endpoint diff --git a/appinfo/info.xml b/appinfo/info.xml index f08424b..300dd28 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -2,10 +2,12 @@ admin_notifications Admin notifications - + AGPL Joas Schilling - 1.0.1 + 1.0.2 AdminNotifications organization @@ -17,14 +19,10 @@ https://github.com/nextcloud/admin_notifications.git - + - - - OCA\AdminNotifications\Command\Generate - diff --git a/appinfo/routes.php b/appinfo/routes.php deleted file mode 100644 index 7fcd45f..0000000 --- a/appinfo/routes.php +++ /dev/null @@ -1,28 +0,0 @@ - - * - * @author Joas Schilling - * - * @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 . - * - */ - -return [ - 'ocs' => [ - ['name' => 'API#generateNotification', 'url' => '/api/v1/notifications/{userId}', 'verb' => 'POST'], - ], -]; diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 3c08909..4f0f64b 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -34,19 +34,36 @@ public function __construct() { } public function register() { - $this->registerNotifier(); - } + $config = $this->getContainer()->getServer()->getConfig(); + + if ($config->getAppValue(self::APP_ID, 'published-deprecation-notification', 'no') === 'yes') { + return; + } + + $notificationManager = $this->getContainer()->getServer()->getNotificationManager(); + $groupManager = $this->getContainer()->getServer()->getGroupManager(); + + $notification = $notificationManager->createNotification(); + $time = time(); + $datetime = new \DateTime(); + $datetime->setTimestamp($time); + + try { + $notification->setApp(Application::APP_ID) + ->setDateTime($datetime) + ->setObject(Application::APP_ID, dechex($time)) + ->setSubject('cli', ['App "admin_notifications" is obsolete']) + ->setMessage('cli', ['The functionality of the "admin_notifications" app has been merged into the default notifications app for Nextcloud 14. You can safely uninstall and delete the "admin_notifications" app, because it does not do anything anymore.']); + $admins = $groupManager->get('admin'); + foreach ($admins->getUsers() as $admin) { + $notification->setUser($admin->getUID()); + $notificationManager->notify($notification); + } + } catch (\InvalidArgumentException $e) { + return; + } - protected function registerNotifier() { - $this->getContainer()->getServer()->getNotificationManager()->registerNotifier(function() { - return $this->getContainer()->query(Notifier::class); - }, function() { - $l = $this->getContainer()->getServer()->getL10NFactory()->get(self::APP_ID); - return [ - 'id' => self::APP_ID, - 'name' => $l->t('Admin notifications'), - ]; - }); + $config->setAppValue(self::APP_ID, 'published-deprecation-notification', 'yes'); } } diff --git a/lib/Controller/APIController.php b/lib/Controller/APIController.php deleted file mode 100644 index 31387c7..0000000 --- a/lib/Controller/APIController.php +++ /dev/null @@ -1,109 +0,0 @@ - - * - * @author Joas Schilling - * - * @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 . - * - */ - -namespace OCA\AdminNotifications\Controller; - -use OCA\AdminNotifications\AppInfo\Application; -use OCP\AppFramework\Http; -use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\OCSController; -use OCP\AppFramework\Utility\ITimeFactory; -use OCP\IRequest; -use OCP\IUser; -use OCP\IUserManager; -use OCP\Notification\IManager; - -class APIController extends OCSController { - - /** @var ITimeFactory */ - protected $timeFactory; - - /** @var IUserManager */ - protected $userManager; - - /** @var IManager */ - protected $notificationManager; - - /** - * @param string $appName - * @param IRequest $request - * @param ITimeFactory $timeFactory - * @param IUserManager $userManager - * @param IManager $notificationManager - */ - public function __construct($appName, IRequest $request, ITimeFactory $timeFactory, IUserManager $userManager, IManager $notificationManager) { - parent::__construct($appName, $request); - - $this->timeFactory = $timeFactory; - $this->userManager = $userManager; - $this->notificationManager = $notificationManager; - } - - /** - * @param string $userId - * @param string $shortMessage - * @param string $longMessage - * @return DataResponse - */ - public function generateNotification($userId, $shortMessage, $longMessage) { - $shortMessage = (string) $shortMessage; - $longMessage = (string) $longMessage; - - $user = $this->userManager->get($userId); - - if (!$user instanceof IUser) { - return new DataResponse(null, Http::STATUS_NOT_FOUND); - } - - if ($shortMessage === '' || strlen($shortMessage) > 255) { - return new DataResponse(null, Http::STATUS_BAD_REQUEST); - } - - if ($longMessage !== '' && strlen($longMessage) > 4000) { - return new DataResponse(null, Http::STATUS_BAD_REQUEST); - } - - $notification = $this->notificationManager->createNotification(); - $time = $this->timeFactory->getTime(); - $datetime = new \DateTime(); - $datetime->setTimestamp($time); - - try { - $notification->setApp(Application::APP_ID) - ->setUser($user->getUID()) - ->setDateTime($datetime) - ->setObject(Application::APP_ID, dechex($time)) - ->setSubject('ocs', [$shortMessage]); - - if ($longMessage !== '') { - $notification->setMessage('ocs', [$longMessage]); - } - - $this->notificationManager->notify($notification); - } catch (\InvalidArgumentException $e) { - return new DataResponse(null, Http::STATUS_INTERNAL_SERVER_ERROR); - } - - return new DataResponse(); - } -} diff --git a/lib/Notification/Notifier.php b/lib/Notification/Notifier.php deleted file mode 100644 index 3c7c3f2..0000000 --- a/lib/Notification/Notifier.php +++ /dev/null @@ -1,79 +0,0 @@ - - * - * @author Joas Schilling - * - * @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 . - * - */ - -namespace OCA\AdminNotifications\Notification; - - -use OCA\AdminNotifications\AppInfo\Application; -use OCP\IURLGenerator; -use OCP\L10N\IFactory; -use OCP\Notification\INotification; -use OCP\Notification\INotifier; - -class Notifier implements INotifier { - - /** @var IFactory */ - protected $l10nFactory; - - /** @var IURLGenerator */ - protected $urlGenerator; - - /** - * @param IFactory $l10nFactory - * @param IURLGenerator $urlGenerator - */ - public function __construct(IFactory $l10nFactory, IURLGenerator $urlGenerator) { - $this->l10nFactory = $l10nFactory; - $this->urlGenerator = $urlGenerator; - } - - /** - * @param INotification $notification - * @param string $languageCode The code of the language that should be used to prepare the notification - * @return INotification - * @throws \InvalidArgumentException When the notification was not prepared by a notifier - */ - public function prepare(INotification $notification, $languageCode) { - if ($notification->getApp() !== Application::APP_ID) { - throw new \InvalidArgumentException('Unknown app'); - } - - switch ($notification->getSubject()) { - // Deal with known subjects - case 'cli': - case 'ocs': - $subjectParams = $notification->getSubjectParameters(); - $notification->setParsedSubject($subjectParams[0]); - $messageParams = $notification->getMessageParameters(); - if (isset($messageParams[0]) && $messageParams[0] !== '') { - $notification->setParsedMessage($messageParams[0]); - } - - $notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg'))); - return $notification; - - default: - throw new \InvalidArgumentException('Unknown subject'); - } - } -} diff --git a/tests/AppInfo/AppTest.php b/tests/AppInfo/AppTest.php deleted file mode 100644 index 9106661..0000000 --- a/tests/AppInfo/AppTest.php +++ /dev/null @@ -1,94 +0,0 @@ - - * - * @author Joas Schilling - * - * @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 . - * - */ - -namespace OCA\AdminNotifications\Tests\AppInfo; - -use OCA\AdminNotifications\AppInfo\Application; -use OCA\AdminNotifications\Notification\Notifier; -use OCP\IL10N; -use OCP\L10N\IFactory; -use OCP\Notification\IManager; -use Test\TestCase; - -/** - * Class AppTest - * - * @package OCA\AdminNotifications\Tests - * @group DB - */ -class AppTest extends TestCase { - /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ - protected $language; - /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $languageFactory; - /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $notificationManager; - - protected function setUp() { - parent::setUp(); - - $this->languageFactory = $this->createMock(IFactory::class); - $this->notificationManager = $this->createMock(IManager::class); - $this->language = $this->createMock(IL10N::class); - $this->language->expects($this->any()) - ->method('t') - ->willReturnCallback(function($string, $args) { - return vsprintf($string, $args); - }); - - $this->overwriteService('NotificationManager', $this->notificationManager); - $this->overwriteService('L10NFactory', $this->languageFactory); - } - - protected function tearDown() { - $this->restoreService('L10NFactory'); - $this->restoreService('NotificationManager'); - - parent::tearDown(); - } - - public function testAppNotification() { - $this->languageFactory->expects($this->once()) - ->method('get') - ->with(Application::APP_ID) - ->willReturn($this->language); - - $this->notificationManager->expects($this->once()) - ->method('registerNotifier') - ->willReturnCallback(function($closureNotifier, $closureInfo) { - $this->assertInstanceOf(\Closure::class, $closureNotifier); - $notifier = $closureNotifier(); - $this->assertInstanceOf(Notifier::class, $notifier); - - $this->assertInstanceOf(\Closure::class, $closureInfo); - $info = $closureInfo(); - $this->assertInternalType('array', $info); - $this->assertArrayHasKey('id', $info); - $this->assertInternalType('string', $info['id']); - $this->assertArrayHasKey('name', $info); - $this->assertInternalType('string', $info['name']); - }); - - include __DIR__ . '/../../appinfo/app.php'; - } -} diff --git a/tests/AppInfo/ApplicationTest.php b/tests/AppInfo/ApplicationTest.php index 30761d9..5ccffe2 100644 --- a/tests/AppInfo/ApplicationTest.php +++ b/tests/AppInfo/ApplicationTest.php @@ -62,12 +62,6 @@ public function testContainerAppName() { public function dataContainerQuery() { return [ [Application::class, App::class], - - [APIController::class, Controller::class], - [APIController::class, OCSController::class], - - [Generate::class, Command::class], - [Notifier::class, INotifier::class], ]; } diff --git a/tests/AppInfo/RoutesTest.php b/tests/AppInfo/RoutesTest.php deleted file mode 100644 index a58d41c..0000000 --- a/tests/AppInfo/RoutesTest.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * @author Joas Schilling - * - * @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 . - * - */ - -namespace OCA\AdminNotifications\Tests\AppInfo; - -use Test\TestCase; - -class RoutesTest extends TestCase { - public function testRoutes() { - $routes = include __DIR__ . '/../../appinfo/routes.php'; - $this->assertInternalType('array', $routes); - $this->assertCount(1, $routes); - $this->assertArrayHasKey('ocs', $routes); - $this->assertInternalType('array', $routes['ocs']); - $this->assertCount(1, $routes['ocs']); - } -} diff --git a/tests/Command/GenerateTest.php b/tests/Command/GenerateTest.php deleted file mode 100644 index fc29c6d..0000000 --- a/tests/Command/GenerateTest.php +++ /dev/null @@ -1,175 +0,0 @@ - - * - * @author Joas Schilling - * - * @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 . - * - */ - -namespace OCA\AdminNotifications\Tests\Command; - -use OCA\AdminNotifications\AppInfo\Application; -use OCA\AdminNotifications\Command\Generate; -use OCP\AppFramework\Utility\ITimeFactory; -use OCP\IUser; -use OCP\IUserManager; -use OCP\Notification\IManager; -use OCP\Notification\INotification; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; - -/** - * Class GenerateTest - * - * @package OCA\AdminNotifications\Tests\Command - * @group DB - */ -class GenerateTest extends \Test\TestCase { - /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $timeFactory; - /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $userManager; - /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $notificationManager; - /** @var Generate */ - protected $command; - - protected function setUp() { - parent::setUp(); - - $this->timeFactory = $this->createMock(ITimeFactory::class); - $this->userManager = $this->createMock(IUserManager::class); - $this->notificationManager = $this->createMock(IManager::class); - - $this->command = new Generate( - $this->timeFactory, - $this->userManager, - $this->notificationManager - ); - } - - public function dataExecute() { - return [ - ['user', '', '', false, null, false, null, 123, null, 1], - ['user', '', '', false, null, false, 'user', 123, null, 1], - ['user', str_repeat('a', 256), '', false, null, false, 'user', 123, null, 1], - ['user', 'short', '', true, false, false, 'user', 123, '7b', 0], - ['user', 'short', str_repeat('a', 4001), false, null, false, 'user', 123, null, 1], - ['user', 'short', str_repeat('a', 4000), true, false, true, 'user', 123, '7b', 0], - ['user', 'short', 'long', true, true, true, 'user', 123, '7b', 1], - ]; - } - - /** - * @dataProvider dataExecute - * @param string $userId - * @param string $short - * @param string $long - * @param bool $createNotification - * @param bool $notifyThrows - * @param bool $validLong - * @param string|null $user - * @param int $time - * @param string|null $hexTime - * @param int $exitCode - */ - public function testExecute($userId, $short, $long, $createNotification, $notifyThrows, $validLong, $user, $time, $hexTime, $exitCode) { - if ($user !== null) { - $u = $this->createMock(IUser::class); - $u->expects($createNotification ? $this->once() : $this->never()) - ->method('getUID') - ->willReturn($user); - } else { - $u = null; - } - $this->userManager->expects($this->any()) - ->method('get') - ->with($userId) - ->willReturn($u); - - $this->timeFactory->expects($hexTime === null ? $this->never() : $this->once()) - ->method('getTime') - ->willReturn($time); - - if ($createNotification) { - $n = $this->createMock(INotification::class); - $n->expects($this->once()) - ->method('setApp') - ->with(Application::APP_ID) - ->willReturnSelf(); - $n->expects($this->once()) - ->method('setUser') - ->with($user) - ->willReturnSelf(); - $n->expects($this->once()) - ->method('setDateTime') - ->willReturnSelf(); - $n->expects($this->once()) - ->method('setObject') - ->with(Application::APP_ID, $hexTime) - ->willReturnSelf(); - $n->expects($this->once()) - ->method('setSubject') - ->with('cli', [$short]) - ->willReturnSelf(); - if ($validLong) { - $n->expects($this->once()) - ->method('setMessage') - ->with('cli', [$long]) - ->willReturnSelf(); - } else { - $n->expects($this->never()) - ->method('setMessage'); - } - - $this->notificationManager->expects($this->once()) - ->method('createNotification') - ->willReturn($n); - - if ($notifyThrows === null) { - $this->notificationManager->expects($this->never()) - ->method('notify'); - } else if ($notifyThrows === false) { - $this->notificationManager->expects($this->once()) - ->method('notify') - ->with($n); - } else if ($notifyThrows === true) { - $this->notificationManager->expects($this->once()) - ->method('notify') - ->willThrowException(new \InvalidArgumentException()); - } - } - - $input = $this->createMock(InputInterface::class); - $input->expects($this->exactly(2)) - ->method('getArgument') - ->willReturnMap([ - ['user-id', $userId], - ['short-message', $short], - ]); - $input->expects($this->exactly(1)) - ->method('getOption') - ->willReturnMap([ - ['long-message', $long], - ]); - $output = $this->createMock(OutputInterface::class); - - $return = self::invokePrivate($this->command, 'execute', [$input, $output]); - $this->assertSame($exitCode, $return); - } -} diff --git a/tests/Controller/APIControllerTest.php b/tests/Controller/APIControllerTest.php deleted file mode 100644 index 1191803..0000000 --- a/tests/Controller/APIControllerTest.php +++ /dev/null @@ -1,169 +0,0 @@ - - * - * @author Joas Schilling - * - * @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 . - * - */ - -namespace OCA\AdminNotifications\Tests\Controller; - -use OCA\AdminNotifications\AppInfo\Application; -use OCA\AdminNotifications\Controller\APIController; -use OCP\AppFramework\Http; -use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\Utility\ITimeFactory; -use OCP\IRequest; -use OCP\IUser; -use OCP\IUserManager; -use OCP\Notification\IManager; -use OCP\Notification\INotification; - -/** - * Class APIControllerTest - * - * @package OCA\AdminNotifications\Tests\Controller - * @group DB - */ -class APIControllerTest extends \Test\TestCase { - /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $timeFactory; - /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $userManager; - /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ - protected $notificationManager; - /** @var APIController */ - protected $controller; - - protected function setUp() { - parent::setUp(); - - /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject $request */ - $request = $this->createMock(IRequest::class); - $this->timeFactory = $this->createMock(ITimeFactory::class); - $this->userManager = $this->createMock(IUserManager::class); - $this->notificationManager = $this->createMock(IManager::class); - - $this->controller = new APIController( - Application::APP_ID, - $request, - $this->timeFactory, - $this->userManager, - $this->notificationManager - ); - } - - public function dataGenerateNotification() { - return [ - ['user', '', '', false, null, false, null, 123, null, Http::STATUS_NOT_FOUND], - ['user', '', '', false, null, false, 'user', 123, null, Http::STATUS_BAD_REQUEST], - ['user', null, '', false, null, false, 'user', 123, null, Http::STATUS_BAD_REQUEST], - ['user', str_repeat('a', 256), '', false, null, false, 'user', 123, null, Http::STATUS_BAD_REQUEST], - ['user', 'short', '', true, false, false, 'user', 123, '7b', Http::STATUS_OK], - ['user', 'short', str_repeat('a', 4001), false, null, false, 'user', 123, null, Http::STATUS_BAD_REQUEST], - ['user', 'short', str_repeat('a', 4000), true, false, true, 'user', 123, '7b', Http::STATUS_OK], - ['user', 'short', 'long', true, true, true, 'user', 123, '7b', Http::STATUS_INTERNAL_SERVER_ERROR], - ]; - } - - /** - * @dataProvider dataGenerateNotification - * @param string $userId - * @param string $short - * @param string $long - * @param bool $createNotification - * @param bool $notifyThrows - * @param bool $validLong - * @param string|null $user - * @param int $time - * @param string|null $hexTime - * @param int $statusCode - */ - public function testGenerateNotification($userId, $short, $long, $createNotification, $notifyThrows, $validLong, $user, $time, $hexTime, $statusCode) { - if ($user !== null) { - $u = $this->createMock(IUser::class); - $u->expects($createNotification ? $this->once() : $this->never()) - ->method('getUID') - ->willReturn($user); - } else { - $u = null; - } - $this->userManager->expects($this->any()) - ->method('get') - ->with($userId) - ->willReturn($u); - - $this->timeFactory->expects($hexTime === null ? $this->never() : $this->once()) - ->method('getTime') - ->willReturn($time); - - if ($createNotification) { - $n = $this->createMock(INotification::class); - $n->expects($this->once()) - ->method('setApp') - ->with(Application::APP_ID) - ->willReturnSelf(); - $n->expects($this->once()) - ->method('setUser') - ->with($user) - ->willReturnSelf(); - $n->expects($this->once()) - ->method('setDateTime') - ->willReturnSelf(); - $n->expects($this->once()) - ->method('setObject') - ->with(Application::APP_ID, $hexTime) - ->willReturnSelf(); - $n->expects($this->once()) - ->method('setSubject') - ->with('ocs', [$short]) - ->willReturnSelf(); - if ($validLong) { - $n->expects($this->once()) - ->method('setMessage') - ->with('ocs', [$long]) - ->willReturnSelf(); - } else { - $n->expects($this->never()) - ->method('setMessage'); - } - - $this->notificationManager->expects($this->once()) - ->method('createNotification') - ->willReturn($n); - - if ($notifyThrows === null) { - $this->notificationManager->expects($this->never()) - ->method('notify'); - } else if ($notifyThrows === false) { - $this->notificationManager->expects($this->once()) - ->method('notify') - ->with($n); - } else if ($notifyThrows === true) { - $this->notificationManager->expects($this->once()) - ->method('notify') - ->willThrowException(new \InvalidArgumentException()); - } - } - - $response = $this->controller->generateNotification($userId, $short, $long); - - $this->assertInstanceOf(DataResponse::class, $response); - $this->assertSame($statusCode, $response->getStatus()); - } -} diff --git a/tests/Notification/NotifierTest.php b/tests/Notification/NotifierTest.php deleted file mode 100644 index 81c8f71..0000000 --- a/tests/Notification/NotifierTest.php +++ /dev/null @@ -1,163 +0,0 @@ - - * - * @author Joas Schilling - * - * @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 . - * - */ - -namespace OCA\AdminNotifications\Tests\Notification; - -use OCA\AdminNotifications\AppInfo\Application; -use OCA\AdminNotifications\Notification\Notifier; -use OCP\IL10N; -use OCP\IURLGenerator; -use OCP\L10N\IFactory; -use OCP\Notification\INotification; - -class NotifierTest extends \Test\TestCase { - /** @var Notifier */ - protected $notifier; - - /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */ - protected $factory; - /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ - protected $urlGenerator; - /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */ - protected $l; - - protected function setUp() { - parent::setUp(); - - $this->urlGenerator = $this->createMock(IURLGenerator::class); - $this->l = $this->createMock(IL10N::class); - $this->l->expects($this->any()) - ->method('t') - ->willReturnCallback(function($string, $args) { - return vsprintf($string, $args); - }); - $this->factory = $this->createMock(IFactory::class); - $this->factory->expects($this->any()) - ->method('get') - ->willReturn($this->l); - - $this->notifier = new Notifier( - $this->factory, - $this->urlGenerator - ); - } - - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Unknown app - */ - public function testPrepareWrongApp() { - /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ - $notification = $this->createMock(INotification::class); - - $notification->expects($this->once()) - ->method('getApp') - ->willReturn('notifications'); - $notification->expects($this->never()) - ->method('getSubject'); - - $this->notifier->prepare($notification, 'en'); - } - - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage Unknown subject - */ - public function testPrepareWrongSubject() { - /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ - $notification = $this->createMock(INotification::class); - - $notification->expects($this->once()) - ->method('getApp') - ->willReturn(Application::APP_ID); - $notification->expects($this->once()) - ->method('getSubject') - ->willReturn('wrong subject'); - - $this->notifier->prepare($notification, 'en'); - } - - public function dataPrepare() { - return [ - ['ocs', ['subject'], ['message'], true], - ]; - } - - /** - * @dataProvider dataPrepare - * - * @param string $subject - * @param array $subjectParams - * @param array $messageParams - * @param bool $setMessage - */ - public function testPrepare($subject, $subjectParams, $messageParams, $setMessage) { - /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */ - $notification = $this->createMock(INotification::class); - - $notification->expects($this->once()) - ->method('getApp') - ->willReturn(Application::APP_ID); - $notification->expects($this->once()) - ->method('getSubject') - ->willReturn($subject); - $notification->expects($this->once()) - ->method('getSubjectParameters') - ->willReturn($subjectParams); - $notification->expects($this->once()) - ->method('getMessageParameters') - ->willReturn($messageParams); - - $notification->expects($this->once()) - ->method('setParsedSubject') - ->with($subjectParams[0]) - ->willReturnSelf(); - - if ($setMessage) { - $notification->expects($this->once()) - ->method('setParsedMessage') - ->with($messageParams[0]) - ->willReturnSelf(); - } else { - $notification->expects($this->never()) - ->method('setParsedMessage'); - } - - $this->urlGenerator->expects($this->once()) - ->method('imagePath') - ->with(Application::APP_ID, 'app-dark.svg') - ->willReturn('icon-url'); - $this->urlGenerator->expects($this->once()) - ->method('getAbsoluteURL') - ->with('icon-url') - ->willReturn('absolute-icon-url'); - $notification->expects($this->once()) - ->method('setIcon') - ->with('absolute-icon-url') - ->willReturnSelf(); - - $return = $this->notifier->prepare($notification, 'en'); - - $this->assertEquals($notification, $return); - } -}