Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/notifications.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2018-02-16 14:51:10 +0300
committerJoas Schilling <coding@schilljs.com>2018-02-16 14:51:10 +0300
commite80405d97b6cac87ee785580307db9436f30ee2f (patch)
treee7700ef7998241cfcb2f0c9ae0e58d9a398e2751 /tests
parentfffb9ac5ca0d4c9d4a69c97f191be839c3682167 (diff)
Merge admin notifications app
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/AppInfo/RoutesTest.php2
-rw-r--r--tests/Unit/Command/GenerateTest.php174
-rw-r--r--tests/Unit/Controller/APIControllerTest.php168
-rw-r--r--tests/Unit/Notifier/NotifierTest.php162
4 files changed, 505 insertions, 1 deletions
diff --git a/tests/Unit/AppInfo/RoutesTest.php b/tests/Unit/AppInfo/RoutesTest.php
index d472265..4712c78 100644
--- a/tests/Unit/AppInfo/RoutesTest.php
+++ b/tests/Unit/AppInfo/RoutesTest.php
@@ -37,6 +37,6 @@ class RoutesTest extends TestCase {
$this->assertCount(1, $routes);
$this->assertArrayHasKey('ocs', $routes);
$this->assertInternalType('array', $routes['ocs']);
- $this->assertCount(6, $routes['ocs']);
+ $this->assertCount(7, $routes['ocs']);
}
}
diff --git a/tests/Unit/Command/GenerateTest.php b/tests/Unit/Command/GenerateTest.php
new file mode 100644
index 0000000..093a345
--- /dev/null
+++ b/tests/Unit/Command/GenerateTest.php
@@ -0,0 +1,174 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @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\Notifications\Tests\Unit\Command;
+
+use OCA\Notifications\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\Notifications\Tests\Unit\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('admin_notifications')
+ ->willReturnSelf();
+ $n->expects($this->once())
+ ->method('setUser')
+ ->with($user)
+ ->willReturnSelf();
+ $n->expects($this->once())
+ ->method('setDateTime')
+ ->willReturnSelf();
+ $n->expects($this->once())
+ ->method('setObject')
+ ->with('admin_notifications', $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/Unit/Controller/APIControllerTest.php b/tests/Unit/Controller/APIControllerTest.php
new file mode 100644
index 0000000..0d90441
--- /dev/null
+++ b/tests/Unit/Controller/APIControllerTest.php
@@ -0,0 +1,168 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @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\Notifications\Tests\Unit\Controller;
+
+use OCA\Notifications\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\Notifications\Tests\Unit\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(
+ 'notifications',
+ $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('admin_notifications')
+ ->willReturnSelf();
+ $n->expects($this->once())
+ ->method('setUser')
+ ->with($user)
+ ->willReturnSelf();
+ $n->expects($this->once())
+ ->method('setDateTime')
+ ->willReturnSelf();
+ $n->expects($this->once())
+ ->method('setObject')
+ ->with('admin_notifications', $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/Unit/Notifier/NotifierTest.php b/tests/Unit/Notifier/NotifierTest.php
new file mode 100644
index 0000000..f45a6a6
--- /dev/null
+++ b/tests/Unit/Notifier/NotifierTest.php
@@ -0,0 +1,162 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @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\Notifications\Tests\Unit\Notifier;
+
+use OCA\Notifications\Notifier\AdminNotifications;
+use OCP\IL10N;
+use OCP\IURLGenerator;
+use OCP\L10N\IFactory;
+use OCP\Notification\INotification;
+
+class NotifierTest extends \Test\TestCase {
+ /** @var AdminNotifications */
+ 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 AdminNotifications(
+ $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('admin_notifications');
+ $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('admin_notifications');
+ $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('notifications', 'notifications-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);
+ }
+}