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
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@owncloud.com>2016-05-02 17:42:21 +0300
committerJoas Schilling <nickvergessen@owncloud.com>2016-05-03 09:55:18 +0300
commitb5eba36d7ca8590c649ae01bdee8359dc9abc779 (patch)
treea1f8f55ec4454f3afdc84aa63ac73d53a963666f /tests/Unit
parentd68f691eeacc5a13d6b63c575fd457f344ca56f5 (diff)
PSR-4 for the Notifications app
Diffstat (limited to 'tests/Unit')
-rw-r--r--tests/Unit/AppInfo/AppTest.php126
-rw-r--r--tests/Unit/AppInfo/ApplicationTest.php97
-rw-r--r--tests/Unit/AppInfo/RoutesTest.php39
-rw-r--r--tests/Unit/AppTest.php78
-rw-r--r--tests/Unit/CapabilitiesTest.php42
-rw-r--r--tests/Unit/Controller/EndpointControllerTest.php532
-rw-r--r--tests/Unit/HandlerTest.php240
-rw-r--r--tests/Unit/TestCase.php65
-rw-r--r--tests/Unit/bootstrap.php39
-rw-r--r--tests/Unit/phpunit.xml24
10 files changed, 1282 insertions, 0 deletions
diff --git a/tests/Unit/AppInfo/AppTest.php b/tests/Unit/AppInfo/AppTest.php
new file mode 100644
index 0000000..a2fe57b
--- /dev/null
+++ b/tests/Unit/AppInfo/AppTest.php
@@ -0,0 +1,126 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Notifications\Tests\Unit\AppInfo;
+
+use OCA\Notifications\Tests\Unit\TestCase;
+use OCP\IUser;
+
+/**
+ * Class AppTest
+ *
+ * @group DB
+ * @package OCA\Notifications\Tests\AppInfo
+ */
+class AppTest extends TestCase {
+ /** @var \OCP\Notification\IManager|\PHPUnit_Framework_MockObject_MockObject */
+ protected $manager;
+ /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
+ protected $request;
+ /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */
+ protected $session;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->manager = $this->getMockBuilder('OCP\Notification\IManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->request = $this->getMockBuilder('OCP\IRequest')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->session = $this->getMockBuilder('OC\User\Session')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->overwriteService('NotificationManager', $this->manager);
+ $this->overwriteService('Request', $this->request);
+ $this->overwriteService('UserSession', $this->session);
+ }
+
+ protected function tearDown() {
+ $this->restoreService('NotificationManager');
+ $this->restoreService('Request');
+ $this->restoreService('UserSession');
+
+ parent::tearDown();
+ }
+
+ public function testRegisterApp() {
+ $this->manager->expects($this->once())
+ ->method('registerApp')
+ ->willReturnCallback(function($closure) {
+ $this->assertInstanceOf('\Closure', $closure);
+ $navigation = $closure();
+ $this->assertInstanceOf('\OCA\Notifications\App', $navigation);
+ });
+
+ include(__DIR__ . '/../../../appinfo/app.php');
+ }
+
+ public function dataLoadingJSAndCSS() {
+ $user = $this->getMockBuilder('OCP\IUser')
+ ->disableOriginalClone()
+ ->getMock();
+
+ return [
+ ['/index.php', '/apps/files', $user, true],
+ ['/index.php', '/apps/files', null, false],
+ ['/remote.php', '/webdav', $user, false],
+ ['/index.php', '/s/1234567890123', $user, false],
+ ];
+ }
+
+ /**
+ * @dataProvider dataLoadingJSAndCSS
+ * @param string $scriptName
+ * @param string $pathInfo
+ * @param IUser|null $user
+ * @param bool $scriptsAdded
+ */
+ public function testLoadingJSAndCSS($scriptName, $pathInfo, $user, $scriptsAdded) {
+ $this->request->expects($this->any())
+ ->method('getScriptName')
+ ->willReturn($scriptName);
+ $this->request->expects($this->any())
+ ->method('getPathInfo')
+ ->willReturn($pathInfo);
+ $this->session->expects($this->once())
+ ->method('getUser')
+ ->willReturn($user);
+
+ \OC_Util::$scripts = [];
+ \OC_Util::$styles = [];
+
+ include(__DIR__ . '/../../../appinfo/app.php');
+
+ if ($scriptsAdded) {
+ $this->assertNotEmpty(\OC_Util::$scripts);
+ $this->assertNotEmpty(\OC_Util::$styles);
+ } else {
+ $this->assertEmpty(\OC_Util::$scripts);
+ $this->assertEmpty(\OC_Util::$styles);
+ }
+ }
+}
diff --git a/tests/Unit/AppInfo/ApplicationTest.php b/tests/Unit/AppInfo/ApplicationTest.php
new file mode 100644
index 0000000..c3f4ad1
--- /dev/null
+++ b/tests/Unit/AppInfo/ApplicationTest.php
@@ -0,0 +1,97 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Notifications\Tests\Unit\AppInfo;
+
+use OCA\Notifications\AppInfo\Application;
+use OCA\Notifications\Tests\Unit\TestCase;
+
+/**
+ * Class ApplicationTest
+ *
+ * @group DB
+ * @package OCA\Notifications\Tests\AppInfo
+ */
+class ApplicationTest extends TestCase {
+ /** @var \OCA\Notifications\AppInfo\Application */
+ protected $app;
+
+ /** @var \OCP\AppFramework\IAppContainer */
+ protected $container;
+
+ protected function setUp() {
+ parent::setUp();
+ $this->app = new Application();
+ $this->container = $this->app->getContainer();
+ }
+
+ public function testContainerAppName() {
+ $this->app = new Application();
+ $this->assertEquals('notifications', $this->container->getAppName());
+ }
+
+ public function dataContainerQuery() {
+ return array(
+ array('EndpointController', 'OCA\Notifications\Controller\EndpointController'),
+ array('Capabilities', 'OCA\Notifications\Capabilities'),
+ );
+ }
+
+ /**
+ * @dataProvider dataContainerQuery
+ * @param string $service
+ * @param string $expected
+ */
+ public function testContainerQuery($service, $expected) {
+ $this->assertTrue($this->container->query($service) instanceof $expected);
+ }
+
+ public function dataGetCurrentUser() {
+ $user = $this->getMockBuilder('OCP\IUser')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $user->expects($this->any())
+ ->method('getUID')
+ ->willReturn('uid');
+
+ return [
+ [$user, 'uid'],
+ [null, ''],
+ ];
+ }
+
+ /**
+ * @dataProvider dataGetCurrentUser
+ * @param mixed $user
+ * @param string $expected
+ */
+ public function testGetCurrentUser($user, $expected) {
+ $session = $this->getMockBuilder('OCP\IUserSession')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $session->expects($this->any())
+ ->method('getUser')
+ ->willReturn($user);
+
+ $this->assertSame($expected, $this->invokePrivate($this->app, 'getCurrentUser', [$session]));
+ }
+}
diff --git a/tests/Unit/AppInfo/RoutesTest.php b/tests/Unit/AppInfo/RoutesTest.php
new file mode 100644
index 0000000..60e969f
--- /dev/null
+++ b/tests/Unit/AppInfo/RoutesTest.php
@@ -0,0 +1,39 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Notifications\Tests\Unit\AppInfo;
+
+use OCA\Notifications\Tests\Unit\TestCase;
+
+/**
+ * Class RoutesTest
+ *
+ * @group DB
+ * @package OCA\Notifications\Tests\AppInfo
+ */
+class RoutesTest extends TestCase {
+ public function testRoutes() {
+ // Execute so we know that no error occurred
+ include(__DIR__ . '/../../../appinfo/routes.php');
+ $this->assertTrue(true);
+ }
+}
diff --git a/tests/Unit/AppTest.php b/tests/Unit/AppTest.php
new file mode 100644
index 0000000..e3cd99c
--- /dev/null
+++ b/tests/Unit/AppTest.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Notifications\Tests\Unit;
+
+
+use OCA\Notifications\App;
+
+class AppTest extends TestCase {
+ /** @var \OCA\Notifications\Handler|\PHPUnit_Framework_MockObject_MockObject */
+ protected $handler;
+
+ /** @var \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject */
+ protected $notification;
+
+ /** @var \OCA\Notifications\App */
+ protected $app;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->handler = $this->getMockBuilder('OCA\Notifications\Handler')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->app = new App(
+ $this->handler
+ );
+ }
+
+ public function testNotify() {
+ $this->handler->expects($this->once())
+ ->method('add')
+ ->with($this->notification);
+
+ $this->app->notify($this->notification);
+ }
+
+ public function testGetCount() {
+ $this->handler->expects($this->once())
+ ->method('count')
+ ->with($this->notification)
+ ->willReturn(42);
+
+ $this->assertSame(42, $this->app->getCount($this->notification));
+ }
+
+ public function testMarkProcessed() {
+ $this->handler->expects($this->once())
+ ->method('delete')
+ ->with($this->notification);
+
+ $this->app->markProcessed($this->notification);
+ }
+}
diff --git a/tests/Unit/CapabilitiesTest.php b/tests/Unit/CapabilitiesTest.php
new file mode 100644
index 0000000..f1f82ba
--- /dev/null
+++ b/tests/Unit/CapabilitiesTest.php
@@ -0,0 +1,42 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Notifications\Tests\Unit;
+
+use OCA\Notifications\Capabilities;
+
+class CapabilitiesTest extends TestCase {
+
+ public function testGetCapabilities() {
+ $capabilities = new Capabilities();
+
+ $this->assertSame([
+ 'notifications' => [
+ 'ocs-endpoints' => [
+ 'list',
+ 'get',
+ 'delete',
+ ],
+ ],
+ ], $capabilities->getCapabilities());
+ }
+}
diff --git a/tests/Unit/Controller/EndpointControllerTest.php b/tests/Unit/Controller/EndpointControllerTest.php
new file mode 100644
index 0000000..872364e
--- /dev/null
+++ b/tests/Unit/Controller/EndpointControllerTest.php
@@ -0,0 +1,532 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Notifications\Tests\Unit\Controller;
+
+use OCA\Notifications\Controller\EndpointController;
+use OCA\Notifications\Tests\Unit\TestCase;
+use OCP\AppFramework\Http;
+
+class EndpointControllerTest extends TestCase {
+ /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
+ protected $request;
+
+ /** @var \OCA\Notifications\Handler|\PHPUnit_Framework_MockObject_MockObject */
+ protected $handler;
+
+ /** @var \OCP\Notification\IManager|\PHPUnit_Framework_MockObject_MockObject */
+ protected $manager;
+
+ /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
+ protected $config;
+
+ /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */
+ protected $session;
+
+ /** @var EndpointController */
+ protected $controller;
+
+ /** @var \OCP\IUser|\PHPUnit_Framework_MockObject_MockObject */
+ protected $user;
+
+ protected function setUp() {
+ parent::setUp();
+
+ /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
+ $this->request = $this->getMockBuilder('OCP\IRequest')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ /** @var \OCA\Notifications\Handler|\PHPUnit_Framework_MockObject_MockObject */
+ $this->handler = $this->getMockBuilder('OCA\Notifications\Handler')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ /** @var \OCP\Notification\IManager|\PHPUnit_Framework_MockObject_MockObject */
+ $this->manager = $this->getMockBuilder('OCP\Notification\IManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
+ $this->config = $this->getMockBuilder('OCP\IConfig')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */
+ $this->session = $this->getMockBuilder('OCP\IUserSession')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ /** @var \OCP\IUser|\PHPUnit_Framework_MockObject_MockObject */
+ $this->user = $this->getMockBuilder('OCP\IUser')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->session->expects($this->any())
+ ->method('getUser')
+ ->willReturn($this->user);
+ }
+
+ protected function getController(array $methods = [], $username = 'username') {
+ $this->user->expects($this->any())
+ ->method('getUID')
+ ->willReturn($username);
+
+ if (empty($methods)) {
+ return new EndpointController(
+ 'notifications',
+ $this->request,
+ $this->handler,
+ $this->manager,
+ $this->config,
+ $this->session
+ );
+ } else {
+ return $this->getMockBuilder('OCA\Notifications\Controller\EndpointController')
+ ->setConstructorArgs([
+ 'notifications',
+ $this->request,
+ $this->handler,
+ $this->manager,
+ $this->config,
+ $this->session
+ ])
+ ->setMethods($methods)
+ ->getMock();
+ }
+ }
+
+ public function dataListNotifications() {
+ return [
+ [
+ [], md5(json_encode([])), [],
+ ],
+ [
+ [
+ 1 => $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock(),
+ 3 => $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock(),
+ ],
+ md5(json_encode([1, 3])),
+ ['$notification', '$notification'],
+ ],
+ [
+ [
+ 42 => $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock(),
+ ],
+ md5(json_encode([42])),
+ ['$notification'],
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider dataListNotifications
+ * @param array $notifications
+ * @param string $expectedETag
+ * @param array $expectedData
+ */
+ public function testListNotifications(array $notifications, $expectedETag, array $expectedData) {
+ $controller = $this->getController([
+ 'notificationToArray',
+ ]);
+ $controller->expects($this->exactly(sizeof($notifications)))
+ ->method('notificationToArray')
+ ->willReturn('$notification');
+
+ $filter = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $filter->expects($this->once())
+ ->method('setUser')
+ ->willReturn('username');
+
+ $this->manager->expects($this->once())
+ ->method('hasNotifiers')
+ ->willReturn(true);
+ $this->manager->expects($this->once())
+ ->method('createNotification')
+ ->willReturn($filter);
+ $this->manager->expects($this->exactly(sizeof($notifications)))
+ ->method('prepare')
+ ->willReturnArgument(0);
+
+ $this->handler->expects($this->once())
+ ->method('get')
+ ->with($filter)
+ ->willReturn($notifications);
+
+ $response = $controller->listNotifications();
+ $this->assertInstanceOf('OC_OCS_Result', $response);
+
+ $headers = $response->getHeaders();
+ $this->assertArrayHasKey('ETag', $headers);
+ $this->assertSame($expectedETag, $headers['ETag']);
+ $this->assertSame($expectedData, $response->getData());
+ }
+
+ public function dataListNotificationsThrows() {
+ return [
+ [
+ [
+ 1 => $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock(),
+ 3 => $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock(),
+ ],
+ md5(json_encode([3])),
+ ['$notification'],
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider dataListNotificationsThrows
+ * @param array $notifications
+ * @param string $expectedETag
+ * @param array $expectedData
+ */
+ public function testListNotificationsThrows(array $notifications, $expectedETag, array $expectedData) {
+ $controller = $this->getController([
+ 'notificationToArray',
+ ]);
+ $controller->expects($this->exactly(1))
+ ->method('notificationToArray')
+ ->willReturn('$notification');
+
+ $filter = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $filter->expects($this->once())
+ ->method('setUser')
+ ->willReturn('username');
+
+ $this->manager->expects($this->once())
+ ->method('hasNotifiers')
+ ->willReturn(true);
+ $this->manager->expects($this->once())
+ ->method('createNotification')
+ ->willReturn($filter);
+ $this->manager->expects($this->at(2))
+ ->method('prepare')
+ ->willThrowException(new \InvalidArgumentException());
+ $this->manager->expects($this->at(3))
+ ->method('prepare')
+ ->willReturnArgument(0);
+
+ $this->handler->expects($this->once())
+ ->method('get')
+ ->with($filter)
+ ->willReturn($notifications);
+
+ $response = $controller->listNotifications();
+ $this->assertInstanceOf('OC_OCS_Result', $response);
+
+ $headers = $response->getHeaders();
+ $this->assertArrayHasKey('ETag', $headers);
+ $this->assertSame($expectedETag, $headers['ETag']);
+ $this->assertSame($expectedData, $response->getData());
+ }
+
+ public function testListNotificationsNoNotifiers() {
+ $controller = $this->getController();
+ $this->manager->expects($this->once())
+ ->method('hasNotifiers')
+ ->willReturn(false);
+
+ $response = $controller->listNotifications();
+ $this->assertInstanceOf('OC_OCS_Result', $response);
+
+ $this->assertSame(Http::STATUS_NO_CONTENT, $response->getStatusCode());
+ }
+
+ public function dataGetNotification() {
+ return [
+ [42, 'username1', ['$notification']],
+ [21, 'username2', ['$notification']],
+ ];
+ }
+
+ /**
+ * @dataProvider dataGetNotification
+ * @param int $id
+ * @param string $username
+ */
+ public function testGetNotification($id, $username) {
+ $controller = $this->getController([
+ 'notificationToArray',
+ ], $username);
+
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->manager->expects($this->once())
+ ->method('hasNotifiers')
+ ->willReturn(true);
+ $this->manager->expects($this->once() )
+ ->method('prepare')
+ ->with($notification)
+ ->willReturn($notification);
+
+ $this->handler->expects($this->once())
+ ->method('getById')
+ ->with($id, $username)
+ ->willReturn($notification);
+
+ $controller->expects($this->exactly(1))
+ ->method('notificationToArray')
+ ->with($id, $notification)
+ ->willReturn('$notification');
+
+ $response = $controller->getNotification(['id' => (string) $id]);
+ $this->assertInstanceOf('OC_OCS_Result', $response);
+
+ $this->assertSame(100, $response->getStatusCode());
+ }
+
+ public function dataGetNotificationNoId() {
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ return [
+ [false, [], false, null], // No notifiers
+ [true, [], false, null], // No id
+ [true, ['id' => 42], true, null], // Not found in database
+ [true, ['id' => 42], true, $notification], // Not handled on prepare
+ ];
+ }
+
+ /**
+ * @dataProvider dataGetNotificationNoId
+ * @param bool $hasNotifiers
+ * @param array $parameters
+ * @param bool $called
+ * @param null|\OCP\Notification\INotification $notification
+ */
+ public function testGetNotificationNoId($hasNotifiers, array $parameters, $called, $notification) {
+ $controller = $this->getController();
+
+ $this->manager->expects($this->once())
+ ->method('hasNotifiers')
+ ->willReturn($hasNotifiers);
+
+ $this->handler->expects($called ? $this->once() : $this->never())
+ ->method('getById')
+ ->willReturn($notification);
+
+ $this->manager->expects($called && $notification ? $this->once() : $this->never())
+ ->method('prepare')
+ ->willThrowException(new \InvalidArgumentException());
+
+ $response = $controller->getNotification($parameters);
+ $this->assertInstanceOf('OC_OCS_Result', $response);
+
+ $this->assertSame(404, $response->getStatusCode());
+ }
+
+ public function dataDeleteNotification() {
+ return [
+ [42, 'username1'],
+ [21, 'username2'],
+ ];
+ }
+
+ /**
+ * @dataProvider dataDeleteNotification
+ * @param int $id
+ * @param string $username
+ */
+ public function testDeleteNotification($id, $username) {
+ $controller = $this->getController([], $username);
+
+ $this->handler->expects($this->once())
+ ->method('deleteById')
+ ->with($id, $username);
+
+ $response = $controller->deleteNotification(['id' => (string) $id]);
+ $this->assertInstanceOf('OC_OCS_Result', $response);
+
+ $this->assertSame(100, $response->getStatusCode());
+ }
+
+ public function testDeleteNotificationNoId() {
+ $controller = $this->getController();
+
+ $this->handler->expects($this->never())
+ ->method('deleteById');
+
+ $response = $controller->deleteNotification([]);
+ $this->assertInstanceOf('OC_OCS_Result', $response);
+
+ $this->assertSame(404, $response->getStatusCode());
+ }
+
+ public function dataNotificationToArray() {
+ return [
+ [42, 'app1', 'user1', 1234, 'type1', 42, 'subject1', 'message1', 'link1', [], []],
+ [1337, 'app2', 'user2', 1337, 'type2', 21, 'subject2', 'message2', 'link2', [
+ $this->getMockBuilder('OCP\Notification\IAction')
+ ->disableOriginalConstructor()
+ ->getMock(),
+ $this->getMockBuilder('OCP\Notification\IAction')
+ ->disableOriginalConstructor()
+ ->getMock(),
+ ], ['action', 'action']],
+ ];
+ }
+
+ /**
+ * @dataProvider dataNotificationToArray
+ *
+ * @param int $id
+ * @param string $app
+ * @param string $user
+ * @param int $timestamp
+ * @param string $objectType
+ * @param int $objectId
+ * @param string $subject
+ * @param string $message
+ * @param string $link
+ * @param array $actions
+ * @param array $actionsExpected
+ */
+ public function testNotificationToArray($id, $app, $user, $timestamp, $objectType, $objectId, $subject, $message, $link, array $actions, array $actionsExpected) {
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $notification->expects($this->once())
+ ->method('getApp')
+ ->willReturn($app);
+
+ $notification->expects($this->once())
+ ->method('getUser')
+ ->willReturn($user);
+
+ $dateTime = new \DateTime();
+ $dateTime->setTimestamp($timestamp);
+ $notification->expects($this->once())
+ ->method('getDateTime')
+ ->willReturn($dateTime);
+
+ $notification->expects($this->once())
+ ->method('getObjectType')
+ ->willReturn($objectType);
+
+ $notification->expects($this->once())
+ ->method('getObjectId')
+ ->willReturn($objectId);
+
+ $notification->expects($this->once())
+ ->method('getParsedSubject')
+ ->willReturn($subject);
+
+ $notification->expects($this->once())
+ ->method('getParsedMessage')
+ ->willReturn($message);
+
+ $notification->expects($this->once())
+ ->method('getLink')
+ ->willReturn($link);
+
+ $notification->expects($this->once())
+ ->method('getParsedActions')
+ ->willReturn($actions);
+
+ $controller = $this->getController([
+ 'actionToArray'
+ ]);
+ $controller->expects($this->exactly(sizeof($actions)))
+ ->method('actionToArray')
+ ->willReturn('action');
+
+ $this->assertEquals([
+ 'notification_id' => $id,
+ 'app' => $app,
+ 'user' => $user,
+ 'datetime' => date('c', $timestamp),
+ 'object_type' => $objectType,
+ 'object_id' => $objectId,
+ 'subject' => $subject,
+ 'message' => $message,
+ 'link' => $link,
+ 'actions' => $actionsExpected,
+ ],
+ $this->invokePrivate($controller, 'notificationToArray', [$id, $notification])
+ );
+ }
+
+ public function dataActionToArray() {
+ return [
+ ['label1', 'link1', 'GET', false],
+ ['label2', 'link2', 'POST', true],
+ ];
+ }
+
+ /**
+ * @dataProvider dataActionToArray
+ *
+ * @param string $label
+ * @param string $link
+ * @param string $requestType
+ * @param bool $isPrimary
+ */
+ public function testActionToArray($label, $link, $requestType, $isPrimary) {
+ $action = $this->getMockBuilder('OCP\Notification\IAction')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $action->expects($this->once())
+ ->method('getParsedLabel')
+ ->willReturn($label);
+
+ $action->expects($this->once())
+ ->method('getLink')
+ ->willReturn($link);
+
+ $action->expects($this->once())
+ ->method('getRequestType')
+ ->willReturn($requestType);
+
+ $action->expects($this->once())
+ ->method('isPrimary')
+ ->willReturn($isPrimary);
+
+ $this->assertEquals([
+ 'label' => $label,
+ 'link' => $link,
+ 'type' => $requestType,
+ 'primary' => $isPrimary,
+ ],
+ $this->invokePrivate($this->getController(), 'actionToArray', [$action])
+ );
+ }
+}
diff --git a/tests/Unit/HandlerTest.php b/tests/Unit/HandlerTest.php
new file mode 100644
index 0000000..9f9ff8b
--- /dev/null
+++ b/tests/Unit/HandlerTest.php
@@ -0,0 +1,240 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Notifications\Tests\Unit;
+
+
+use OCA\Notifications\Handler;
+
+/**
+ * Class HandlerTest
+ *
+ * @group DB
+ * @package OCA\Notifications\Tests\Lib
+ */
+class HandlerTest extends TestCase {
+ /** @var \OCA\Notifications\Handler */
+ protected $handler;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->handler = new Handler(
+ \OC::$server->getDatabaseConnection(),
+ \OC::$server->getNotificationManager()
+ );
+
+ $this->handler->delete($this->getNotification([
+ 'getApp' => 'testing_notifications',
+ ]));
+ }
+
+ public function testFull() {
+ $notification = $this->getNotification([
+ 'getApp' => 'testing_notifications',
+ 'getUser' => 'test_user1',
+ 'getDateTime' => new \DateTime(),
+ 'getObjectType' => 'notification',
+ 'getObjectId' => '1337',
+ 'getSubject' => 'subject',
+ 'getSubjectParameters' => [],
+ 'getMessage' => 'message',
+ 'getMessageParameters' => [],
+ 'getLink' => 'link',
+ 'getActions' => [
+ [
+ 'getLabel' => 'action_label',
+ 'getLink' => 'action_link',
+ 'getRequestType' => 'GET',
+ 'isPrimary' => false,
+ ]
+ ],
+ ]);
+ $limitedNotification1 = $this->getNotification([
+ 'getApp' => 'testing_notifications',
+ 'getUser' => 'test_user1',
+ ]);
+ $limitedNotification2 = $this->getNotification([
+ 'getApp' => 'testing_notifications',
+ 'getUser' => 'test_user2',
+ ]);
+
+ // Make sure there is no notification
+ $this->assertSame(0, $this->handler->count($limitedNotification1), 'Wrong notification count for user1 before adding');
+ $notifications = $this->handler->get($limitedNotification1);
+ $this->assertCount(0, $notifications, 'Wrong notification count for user1 before beginning');
+ $this->assertSame(0, $this->handler->count($limitedNotification2), 'Wrong notification count for user2 before adding');
+ $notifications = $this->handler->get($limitedNotification2);
+ $this->assertCount(0, $notifications, 'Wrong notification count for user2 before beginning');
+
+ // Add and count
+ $this->handler->add($notification);
+ $this->assertSame(1, $this->handler->count($limitedNotification1), 'Wrong notification count for user1 after adding');
+ $this->assertSame(0, $this->handler->count($limitedNotification2), 'Wrong notification count for user2 after adding');
+
+ // Get and count
+ $notifications = $this->handler->get($limitedNotification1);
+ $this->assertCount(1, $notifications, 'Wrong notification get for user1 after adding');
+ $notifications = $this->handler->get($limitedNotification2);
+ $this->assertCount(0, $notifications, 'Wrong notification get for user2 after adding');
+
+ // Delete and count again
+ $this->handler->delete($notification);
+ $this->assertSame(0, $this->handler->count($limitedNotification1), 'Wrong notification count for user1 after deleting');
+ $this->assertSame(0, $this->handler->count($limitedNotification2), 'Wrong notification count for user2 after deleting');
+ }
+
+ public function testDeleteById() {
+ $notification = $this->getNotification([
+ 'getApp' => 'testing_notifications',
+ 'getUser' => 'test_user1',
+ 'getDateTime' => new \DateTime(),
+ 'getObjectType' => 'notification',
+ 'getObjectId' => '1337',
+ 'getSubject' => 'subject',
+ 'getSubjectParameters' => [],
+ 'getMessage' => 'message',
+ 'getMessageParameters' => [],
+ 'getLink' => 'link',
+ 'getActions' => [
+ [
+ 'getLabel' => 'action_label',
+ 'getLink' => 'action_link',
+ 'getRequestType' => 'GET',
+ 'isPrimary' => true,
+ ]
+ ],
+ ]);
+ $limitedNotification = $this->getNotification([
+ 'getApp' => 'testing_notifications',
+ 'getUser' => 'test_user1',
+ ]);
+
+ // Make sure there is no notification
+ $this->assertSame(0, $this->handler->count($limitedNotification));
+ $notifications = $this->handler->get($limitedNotification);
+ $this->assertCount(0, $notifications);
+
+ // Add and count
+ $this->handler->add($notification);
+ $this->assertSame(1, $this->handler->count($limitedNotification));
+
+ // Get and count
+ $notifications = $this->handler->get($limitedNotification);
+ $this->assertCount(1, $notifications);
+ reset($notifications);
+ $notificationId = key($notifications);
+
+ // Get with wrong user
+ $getNotification = $this->handler->getById($notificationId, 'test_user2');
+ $this->assertSame(null, $getNotification);
+
+ // Delete with wrong user
+ $this->handler->deleteById($notificationId, 'test_user2');
+ $this->assertSame(1, $this->handler->count($limitedNotification), 'Wrong notification count for user1 after trying to delete for user2');
+
+ // Get with correct user
+ $getNotification = $this->handler->getById($notificationId, 'test_user1');
+ $this->assertInstanceOf('OCP\Notification\INotification', $getNotification);
+
+ // Delete and count
+ $this->handler->deleteById($notificationId, 'test_user1');
+ $this->assertSame(0, $this->handler->count($limitedNotification), 'Wrong notification count for user1 after deleting');
+ }
+
+ /**
+ * @param array $values
+ * @return \OCP\Notification\INotification|\PHPUnit_Framework_MockObject_MockObject
+ */
+ protected function getNotification(array $values = []) {
+ $notification = $this->getMockBuilder('OCP\Notification\INotification')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ foreach ($values as $method => $returnValue) {
+ if ($method === 'getActions') {
+ $actions = [];
+ foreach ($returnValue as $actionData) {
+ $action = $this->getMockBuilder('OCP\Notification\IAction')
+ ->disableOriginalConstructor()
+ ->getMock();
+ foreach ($actionData as $actionMethod => $actionValue) {
+ $action->expects($this->any())
+ ->method($actionMethod)
+ ->willReturn($actionValue);
+ }
+ $actions[] = $action;
+ }
+ $notification->expects($this->any())
+ ->method($method)
+ ->willReturn($actions);
+ } else {
+ $notification->expects($this->any())
+ ->method($method)
+ ->willReturn($returnValue);
+ }
+ }
+
+ $defaultDateTime = new \DateTime();
+ $defaultDateTime->setTimestamp(0);
+ $defaultValues = [
+ 'getApp' => '',
+ 'getUser' => '',
+ 'getDateTime' => $defaultDateTime,
+ 'getObjectType' => '',
+ 'getObjectId' => '',
+ 'getSubject' => '',
+ 'getSubjectParameters' => [],
+ 'getMessage' => '',
+ 'getMessageParameters' => [],
+ 'getLink' => '',
+ 'getActions' => [],
+ ];
+ foreach ($defaultValues as $method => $returnValue) {
+ if (isset($values[$method])) {
+ continue;
+ }
+
+ $notification->expects($this->any())
+ ->method($method)
+ ->willReturn($returnValue);
+ }
+
+ $defaultValues = [
+ 'setApp',
+ 'setUser',
+ 'setDateTime',
+ 'setObject',
+ 'setSubject',
+ 'setMessage',
+ 'setLink',
+ 'addAction',
+ ];
+ foreach ($defaultValues as $method) {
+ $notification->expects($this->any())
+ ->method($method)
+ ->willReturnSelf();
+ }
+
+ return $notification;
+ }
+}
diff --git a/tests/Unit/TestCase.php b/tests/Unit/TestCase.php
new file mode 100644
index 0000000..a60b3b8
--- /dev/null
+++ b/tests/Unit/TestCase.php
@@ -0,0 +1,65 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\Notifications\Tests\Unit;
+
+abstract class TestCase extends \Test\TestCase {
+ /** @var array */
+ protected $services = [];
+
+ /**
+ * @param string $name
+ * @param mixed $newService
+ * @return bool
+ */
+ public function overwriteService($name, $newService) {
+ if (isset($this->services[$name])) {
+ return false;
+ }
+
+ $this->services[$name] = \OC::$server->query($name);
+ \OC::$server->registerService($name, function () use ($newService) {
+ return $newService;
+ });
+
+ return true;
+ }
+
+ /**
+ * @param string $name
+ * @return bool
+ */
+ public function restoreService($name) {
+ if (isset($this->services[$name])) {
+ $oldService = $this->services[$name];
+ \OC::$server->registerService($name, function () use ($oldService) {
+ return $oldService;
+ });
+
+
+ unset($this->services[$name]);
+ return true;
+ }
+
+ return false;
+ }
+}
diff --git a/tests/Unit/bootstrap.php b/tests/Unit/bootstrap.php
new file mode 100644
index 0000000..fb650cb
--- /dev/null
+++ b/tests/Unit/bootstrap.php
@@ -0,0 +1,39 @@
+<?php
+/**
+ * @author Joas Schilling <nickvergessen@owncloud.com>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+if (!defined('PHPUNIT_RUN')) {
+ define('PHPUNIT_RUN', 1);
+}
+
+require_once __DIR__ . '/../../../../lib/base.php';
+
+// Fix for "Autoload path not allowed: .../tests/lib/testcase.php"
+\OC::$loader->addValidRoot(OC::$SERVERROOT . '/tests');
+
+// Fix for "Autoload path not allowed: .../notifications/tests/testcase.php"
+\OC_App::loadApp('notifications');
+
+if(!class_exists('PHPUnit_Framework_TestCase')) {
+ require_once('PHPUnit/Autoload.php');
+}
+
+OC_Hook::clear();
diff --git a/tests/Unit/phpunit.xml b/tests/Unit/phpunit.xml
new file mode 100644
index 0000000..b531a90
--- /dev/null
+++ b/tests/Unit/phpunit.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<phpunit bootstrap="bootstrap.php"
+ strict="true"
+ verbose="true"
+ timeoutForSmallTests="900"
+ timeoutForMediumTests="900"
+ timeoutForLargeTests="900"
+ >
+ <testsuite name='ownCloud - Notifications App Tests'>
+ <directory suffix='Test.php'>.</directory>
+ </testsuite>
+ <!-- filters for code coverage -->
+ <filter>
+ <whitelist>
+ <directory suffix=".php">../../../notifications/lib</directory>
+ <directory suffix=".php">../../../notifications/appinfo</directory>
+ </whitelist>
+ </filter>
+ <logging>
+ <!-- and this is where your report will be written -->
+ <log type="coverage-clover" target="./clover.xml"/>
+ </logging>
+</phpunit>
+