diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2015-09-01 17:57:43 +0300 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2015-09-01 17:57:43 +0300 |
commit | b3d8280eb4345d045080a430601ce3676a9e48d2 (patch) | |
tree | 3781c82d6553f2481323240167358c0594ffffa5 /tests | |
parent | bf26921a4caffff92bbc209d34cf5b452511ded8 (diff) |
Add an endpoint
Diffstat (limited to 'tests')
-rw-r--r-- | tests/appinfo/AppTest.php | 25 | ||||
-rw-r--r-- | tests/appinfo/ApplicationTest.php | 88 | ||||
-rw-r--r-- | tests/appinfo/RoutesTest.php | 33 | ||||
-rw-r--r-- | tests/bootstrap.php | 19 | ||||
-rw-r--r-- | tests/controller/EndpointControllerTest.php | 363 | ||||
-rw-r--r-- | tests/lib/HandlerTest.php | 58 | ||||
-rw-r--r-- | tests/testcase.php | 25 |
7 files changed, 584 insertions, 27 deletions
diff --git a/tests/appinfo/AppTest.php b/tests/appinfo/AppTest.php index e484739..5beda66 100644 --- a/tests/appinfo/AppTest.php +++ b/tests/appinfo/AppTest.php @@ -1,23 +1,22 @@ <?php - /** - * ownCloud - Notification + * @author Joas Schilling <nickvergessen@owncloud.com> * - * @author Joas Schilling - * @copyright 2014 Joas Schilling nickvergessen@owncloud.com + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE - * License as published by the Free Software Foundation; either - * version 3 of the License, or any later version. + * 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 library is distributed in the hope that it will be useful, + * 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. + * 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/> * - * You should have received a copy of the GNU Affero General Public - * License along with this library. If not, see <http://www.gnu.org/licenses/>. */ namespace OCA\Notification\Tests; diff --git a/tests/appinfo/ApplicationTest.php b/tests/appinfo/ApplicationTest.php new file mode 100644 index 0000000..1149cba --- /dev/null +++ b/tests/appinfo/ApplicationTest.php @@ -0,0 +1,88 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, 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; + +use OCA\Notifications\AppInfo\Application; + +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'), + ); + } + + /** + * @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/appinfo/RoutesTest.php b/tests/appinfo/RoutesTest.php new file mode 100644 index 0000000..cd521f8 --- /dev/null +++ b/tests/appinfo/RoutesTest.php @@ -0,0 +1,33 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, 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; + +class RoutesTest extends TestCase { + public function testRoutes() { + $routes = include(__DIR__ . '/../../appinfo/routes.php'); + $this->assertInternalType('array', $routes); + $this->assertCount(1, $routes); + $this->assertArrayHasKey('routes', $routes); + $this->assertInternalType('array', $routes['routes']); + $this->assertGreaterThanOrEqual(1, sizeof($routes['routes'])); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 86bfd7d..c43c825 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,4 +1,23 @@ <?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, 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); diff --git a/tests/controller/EndpointControllerTest.php b/tests/controller/EndpointControllerTest.php new file mode 100644 index 0000000..8b8369e --- /dev/null +++ b/tests/controller/EndpointControllerTest.php @@ -0,0 +1,363 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, 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\Controller; + +use OCA\Notifications\Controller\EndpointController; +use OCA\Notifications\Handler; +use OCA\Notifications\Tests\TestCase; +use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Controller; +use OCP\IConfig; +use OCP\IRequest; +use OCP\Notification\IAction; +use OCP\Notification\IManager; +use OCP\Notification\INotification; + +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 EndpointController */ + protected $controller; + + 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(); + } + + protected function getController(array $methods = []) { + if (empty($methods)) { + return new EndpointController( + 'notifications', + $this->request, + $this->handler, + $this->manager, + $this->config, + 'username' + ); + } else { + return $this->getMockBuilder('OCA\Notifications\Controller\EndpointController') + ->setConstructorArgs([ + 'notifications', + $this->request, + $this->handler, + $this->manager, + $this->config, + 'username' + ]) + ->setMethods($methods) + ->getMock(); + } + } + + public function dataGet() { + return [ + [ + [], [], + ], + [ + [ + $this->getMockBuilder('OCP\Notification\INotification') + ->disableOriginalConstructor() + ->getMock(), + $this->getMockBuilder('OCP\Notification\INotification') + ->disableOriginalConstructor() + ->getMock(), + ], + ['$notification', '$notification'], + ], + [ + [ + $this->getMockBuilder('OCP\Notification\INotification') + ->disableOriginalConstructor() + ->getMock(), + ], + ['$notification'], + ], + ]; + } + + /** + * @dataProvider dataGet + * @param array $notifications + * @param array $expectedData + */ + public function testGet(array $notifications, 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('createNotification') + ->willReturn($filter); + + $this->handler->expects($this->once()) + ->method('get') + ->with($filter) + ->willReturn($notifications); + + $response = $controller->get(); + $this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response); + + $this->assertSame($expectedData, $response->getData()); + } + + public function dataNotificationToArray() { + return [ + ['app1', 'user1', 1234, 'type1', 42, 'subject1', 'message1', 'link1', 'icon1', [], []], + ['app2', 'user2', 1337, 'type2', 21, 'subject2', 'message2', 'link2', 'icon2', [ + $this->getMockBuilder('OCP\Notification\IAction') + ->disableOriginalConstructor() + ->getMock(), + $this->getMockBuilder('OCP\Notification\IAction') + ->disableOriginalConstructor() + ->getMock(), + ], ['action', 'action']], + ]; + } + + /** + * @dataProvider dataNotificationToArray + * + * @param string $app + * @param string $user + * @param int $timestamp + * @param string $type + * @param int $id + * @param string $subject + * @param string $message + * @param string $link + * @param string $icon + * @param array $actions + */ + public function testNotificationToArray($app, $user, $timestamp, $type, $id, $subject, $message, $link, $icon, 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); + + $notification->expects($this->once()) + ->method('getTimestamp') + ->willReturn($timestamp); + + $notification->expects($this->once()) + ->method('getObjectType') + ->willReturn($type); + + $notification->expects($this->once()) + ->method('getObjectId') + ->willReturn($id); + + $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('getIcon') + ->willReturn($icon); + + $notification->expects($this->once()) + ->method('getParsedActions') + ->willReturn($actions); + + $controller = $this->getController([ + 'actionToArray' + ]); + $controller->expects($this->exactly(sizeof($actions))) + ->method('actionToArray') + ->willReturn('action'); + + $this->assertEquals([ + 'app' => $app, + 'user' => $user, + 'timestamp' => $timestamp, + 'object_type' => $type, + 'object_id' => $id, + 'subject' => $subject, + 'message' => $message, + 'link' => $link, + 'icon' => $icon, + 'actions' => $actionsExpected, + ], + $this->invokePrivate($controller, 'notificationToArray', [$notification]) + ); + } + + public function dataActionToArray() { + return [ + ['label1', 'link1', 'GET', 'icon1'], + ['label2', 'link2', 'POST', 'icon2'], + ]; + } + + /** + * @dataProvider dataActionToArray + * + * @param string $label + * @param string $link + * @param string $requestType + * @param string $icon + */ + public function testActionToArray($label, $link, $requestType, $icon) { + $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('getIcon') + ->willReturn($icon); + + $this->assertEquals([ + 'label' => $label, + 'icon' => $icon, + 'link' => $link, + 'type' => $requestType, + ], + $this->invokePrivate($this->getController(), 'actionToArray', [$action]) + ); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * @return JSONResponse + */ + public function get() { + $filter = $this->manager->createNotification(); + $filter->setUser($this->user); + $language = $this->config->getUserValue($this->user, 'core', 'lang', null); + + $notifications = $this->handler->get($filter); + + $data = []; + foreach ($notifications as $notification) { + $this->manager->prepare($notification, $language); + $data[] = $this->notificationToArray($notification); + } + + return new JSONResponse($data); + } + + /** + * @param INotification $notification + * @return array + */ + protected function notificationToArray(INotification $notification) { + $data = [ + 'app' => $notification->getApp(), + 'user' => $notification->getUser(), + 'timestamp' => $notification->getTimestamp(), + 'object_type' => $notification->getObjectType(), + 'object_id' => $notification->getObjectId(), + 'subject' => $notification->getParsedSubject(), + 'message' => $notification->getParsedMessage(), + 'link' => $notification->getLink(), + 'icon' => $notification->getIcon(), + 'actions' => [], + ]; + + foreach ($notification->getParsedActions() as $action) { + $data['actions'][] = $this->actionToArray($action); + } + + return $data; + } + + /** + * @param IAction $action + * @return array + */ + protected function actionToArray(IAction $action) { + return [ + 'label' => $action->getParsedLabel(), + 'icon' => $action->getIcon(), + 'link' => $action->getLink(), + 'type' => $action->getRequestType(), + ]; + } +} diff --git a/tests/lib/HandlerTest.php b/tests/lib/HandlerTest.php index 4a400ef..0aad099 100644 --- a/tests/lib/HandlerTest.php +++ b/tests/lib/HandlerTest.php @@ -33,8 +33,13 @@ class HandlerTest extends TestCase { parent::setUp(); $this->handler = new Handler( - \OC::$server->getDatabaseConnection() + \OC::$server->getDatabaseConnection(), + \OC::$server->getNotificationManager() ); + + $this->handler->delete($this->getNotification([ + 'getApp' => 'testing_notifications', + ])); } public function testFull() { @@ -59,14 +64,24 @@ class HandlerTest extends TestCase { ] ], ]); + $limitedNotification = $this->getNotification([ + 'getApp' => 'testing_notifications', + 'getUser' => 'test_user', + ]); // Make sure there is no notification $this->assertSame(0, $this->handler->count($notification)); + $notifications = $this->handler->get($limitedNotification); + $this->assertCount(0, $notifications); // Add and count $this->handler->add($notification); $this->assertSame(1, $this->handler->count($notification)); + // Get and count + $notifications = $this->handler->get($limitedNotification); + $this->assertCount(1, $notifications); + // Delete and count again $this->handler->delete($notification); $this->assertSame(0, $this->handler->count($notification)); @@ -105,6 +120,47 @@ class HandlerTest extends TestCase { } } + $defaultValues = [ + 'getApp' => '', + 'getUser' => '', + 'getTimestamp' => 0, + 'getObjectType' => '', + 'getObjectId' => 0, + 'getSubject' => '', + 'getSubjectParameters' => [], + 'getMessage' => '', + 'getMessageParameters' => [], + 'getLink' => '', + 'getIcon' => '', + 'getActions' => [], + ]; + foreach ($defaultValues as $method => $returnValue) { + if (isset($values[$method])) { + continue; + } + + $notification->expects($this->any()) + ->method($method) + ->willReturn($returnValue); + } + + $defaultValues = [ + 'setApp', + 'setUser', + 'setTimestamp', + 'setObject', + 'setSubject', + 'setMessage', + 'setLink', + 'setIcon', + 'addAction', + ]; + foreach ($defaultValues as $method) { + $notification->expects($this->any()) + ->method($method) + ->willReturnSelf(); + } + return $notification; } } diff --git a/tests/testcase.php b/tests/testcase.php index 7a10d8a..5ebb65c 100644 --- a/tests/testcase.php +++ b/tests/testcase.php @@ -1,23 +1,22 @@ <?php - /** - * ownCloud - Notifications + * @author Joas Schilling <nickvergessen@owncloud.com> * - * @author Joas Schilling - * @copyright 2015 Joas Schilling nickvergessen@owncloud.com + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE - * License as published by the Free Software Foundation; either - * version 3 of the License, or any later version. + * 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 library is distributed in the hope that it will be useful, + * 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. + * 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/> * - * You should have received a copy of the GNU Affero General Public - * License along with this library. If not, see <http://www.gnu.org/licenses/>. */ namespace OCA\Notifications\Tests; |