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

github.com/nextcloud/admin_notifications.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2017-02-13 14:02:40 +0300
committerGitHub <noreply@github.com>2017-02-13 14:02:40 +0300
commit1d3adbdb54b036d2dc6afab021cfd311ffc9cdad (patch)
treea707ec4aed6c95e76491e2c6bafe393fb6f0b976
parentc619d51466bb2489b6ddf1b9d82d01e1c4be1343 (diff)
parent8a141bb1b46484daf9c872b18459e55595cbd51c (diff)
Merge pull request #2 from nextcloud/more-tests
Bring the coverage to 100%
-rw-r--r--lib/Command/Generate.php6
-rw-r--r--tests/Command/GenerateTest.php175
-rw-r--r--tests/Controller/APIControllerTest.php168
3 files changed, 346 insertions, 3 deletions
diff --git a/lib/Command/Generate.php b/lib/Command/Generate.php
index 27c4863..17ace7f 100644
--- a/lib/Command/Generate.php
+++ b/lib/Command/Generate.php
@@ -90,20 +90,20 @@ class Generate extends Command {
protected function execute(InputInterface $input, OutputInterface $output) {
$userId = $input->getArgument('user-id');
- $user = $this->userManager->get($userId);
+ $subject = $input->getArgument('short-message');
+ $message = $input->getOption('long-message');
+ $user = $this->userManager->get($userId);
if (!$user instanceof IUser) {
$output->writeln('Unknown user');
return 1;
}
- $subject = $input->getArgument('short-message');
if ($subject === '' || strlen($subject) > 255) {
$output->writeln('Too long or empty short-message');
return 1;
}
- $message = $input->getOption('long-message');
if ($message !== '' && strlen($message) > 4000) {
$output->writeln('Too long long-message');
return 1;
diff --git a/tests/Command/GenerateTest.php b/tests/Command/GenerateTest.php
new file mode 100644
index 0000000..fc29c6d
--- /dev/null
+++ b/tests/Command/GenerateTest.php
@@ -0,0 +1,175 @@
+<?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\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
new file mode 100644
index 0000000..2dbf726
--- /dev/null
+++ b/tests/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\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', 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());
+ }
+}