diff options
author | Julius Haertl <jus@bitgrid.net> | 2016-09-04 17:33:37 +0300 |
---|---|---|
committer | Julius Haertl <jus@bitgrid.net> | 2016-09-04 17:37:25 +0300 |
commit | b2cef929bc7be1f0e7a3a88ac4d73012dc8cb029 (patch) | |
tree | 2e7af8d5d5d3f6123773d22ecab86c7f7b9e3b1a | |
parent | 5eb2388842c729a2e311807deec2e67b826d1a6d (diff) |
Fix unit tests
-rw-r--r-- | appinfo/autoload.php | 25 | ||||
-rw-r--r-- | appinfo/info.xml | 1 | ||||
-rw-r--r-- | lib/AppInfo/Application.php | 21 | ||||
-rw-r--r-- | lib/Controller/AppController.php | 15 | ||||
-rw-r--r-- | lib/Controller/SettingsController.php | 4 | ||||
-rw-r--r-- | lib/Util.php | 3 | ||||
-rw-r--r-- | tests/Integration/AppTest.php (renamed from tests/integration/AppTest.php) | 0 | ||||
-rw-r--r-- | tests/bootstrap.php | 36 | ||||
-rw-r--r-- | tests/unit/UtilTest.php | 99 | ||||
-rw-r--r-- | tests/unit/controller/AppControllerTest.php | 98 | ||||
-rw-r--r-- | tests/unit/controller/SettingsControllerTest.php | 50 |
11 files changed, 291 insertions, 61 deletions
diff --git a/appinfo/autoload.php b/appinfo/autoload.php new file mode 100644 index 0000000..737046e --- /dev/null +++ b/appinfo/autoload.php @@ -0,0 +1,25 @@ +<?php +/** + * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @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\AppOrder\AppInfo; + +use OCP\AppFramework\App; diff --git a/appinfo/info.xml b/appinfo/info.xml index 4071b1c..7942aa5 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -9,6 +9,7 @@ <dependencies> <owncloud min-version="9.0" max-version="9.2" /> </dependencies> + <namespace>AppOrder</namespace> <repository type="git">https://github.com/juliushaertl/apporder.git</repository> <ocsid>174715</ocsid> </info> diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 5b426cd..275a5df 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -1,4 +1,25 @@ <?php +/** + * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @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\AppOrder\AppInfo; diff --git a/lib/Controller/AppController.php b/lib/Controller/AppController.php index 4175d85..cb5d8fe 100644 --- a/lib/Controller/AppController.php +++ b/lib/Controller/AppController.php @@ -25,24 +25,24 @@ namespace OCA\AppOrder\Controller; use \OCP\AppFramework\Controller; use OCP\AppFramework\Http\RedirectResponse; -use \OCP\AppFramework\Http\TemplateResponse; use \OCP\IRequest; use \OCP\INavigationManager; use \OCA\AppOrder\Service\ConfigService; use OCA\AppOrder\Util; +use OCP\IURLGenerator; class AppController extends Controller { private $userId; private $appConfig; - private $navigationManager; + private $urlGenerator; private $util; - public function __construct($appName, IRequest $request, ConfigService $appConfig, INavigationManager $navigationManager, Util $util, $userId) { + public function __construct($appName, IRequest $request, ConfigService $appConfig, IURLGenerator $urlGenerator, Util $util, $userId) { parent::__construct($appName, $request); $this->userId = $userId; $this->appConfig = $appConfig; - $this->navigationManager = $navigationManager; + $this->urlGenerator = $urlGenerator; $this->util = $util; } @@ -51,7 +51,12 @@ class AppController extends Controller { * @return RedirectResponse */ public function index() { - $firstPage = json_decode($this->util->getAppOrder())[0]; + $order = json_decode($this->util->getAppOrder()); + if($order !== null && sizeof($order)>0) { + $firstPage = $order[0]; + } else { + return new RedirectResponse($this->urlGenerator->linkTo('files','')); + } return new RedirectResponse($firstPage); } diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index 3bce7b7..f4186ec 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -16,11 +16,11 @@ class SettingsController extends Controller { private $navigationManager; private $util; - public function __construct($appName, IRequest $request, ConfigService $appConfig, INavigationManager $navigationManager, Util $util, $userId) { + public function __construct($appName, IRequest $request, ConfigService $appConfig, INavigationManager $urlGenerator, Util $util, $userId) { parent::__construct($appName, $request); $this->userId = $userId; $this->appConfig = $appConfig; - $this->navigationManager = $navigationManager; + $this->navigationManager = $urlGenerator; $this->util = $util; } diff --git a/lib/Util.php b/lib/Util.php index 2a2ec5b..e9b35a4 100644 --- a/lib/Util.php +++ b/lib/Util.php @@ -23,7 +23,10 @@ namespace OCA\AppOrder; +use OCA\AppOrder\Service\ConfigService; + class Util { + private $userId; private $appConfig; diff --git a/tests/integration/AppTest.php b/tests/Integration/AppTest.php index 04c90a6..04c90a6 100644 --- a/tests/integration/AppTest.php +++ b/tests/Integration/AppTest.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 2249de2..ad27814 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,16 +1,36 @@ <?php /** - * ownCloud - foobar + * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net> * - * This file is licensed under the Affero General Public License version 3 or - * later. See the COPYING file. + * @author Julius Härtl <jus@bitgrid.net> + * + * @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/>. * - * @author jus <jus@bitgrid.net> - * @copyright jus 2016 */ -require_once __DIR__.'/../../../tests/bootstrap.php'; -require_once __DIR__.'/../appinfo/autoload.php'; +require_once __DIR__ . '/../../../tests/bootstrap.php'; +require_once __DIR__ . '/../appinfo/autoload.php'; + + +require_once __DIR__.'/../../../lib/base.php'; +if(!class_exists('PHPUnit_Framework_TestCase')) { + require_once('PHPUnit/Autoload.php'); +} -\OC::$loader->addValidRoot(OC::$SERVERROOT.'/tests'); \OC_App::loadApp('apporder'); + +OC_Hook::clear(); + diff --git a/tests/unit/UtilTest.php b/tests/unit/UtilTest.php new file mode 100644 index 0000000..123386b --- /dev/null +++ b/tests/unit/UtilTest.php @@ -0,0 +1,99 @@ +<?php +/** + * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @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\AppOrder; + +use OCA\AppOrder\Service\ConfigService; +use \OCA\AppOrder\Util; + +class UtilTest extends \PHPUnit_Framework_TestCase { + + /** + * @var ConfigService + */ + private $service; + private $userId; + private $util; + private $config; + public function setUp() { + + parent::setUp(); + + $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->service = $this->getMockBuilder('\OCA\AppOrder\Service\ConfigService') + ->disableOriginalConstructor() + ->getMock(); + $this->userId = 'admin'; + $this->util = new Util($this->service, $this->userId); + + } + + public function testMatchOrder() { + $nav = [ + ['href' => '/app/files/', 'name' => 'Files'], + ['href' => '/app/calendar/', 'name' => 'Calendar'], + ['href' => '/app/tasks/', 'name' => 'Tasks'], + ]; + $order = ['/app/calendar/', '/app/tasks/']; + $result = $this->util->matchOrder($nav, $order); + $expected = [ + '/app/calendar/' => ['href' => '/app/calendar/', 'name' => 'Calendar'], + '/app/tasks/' => ['href' => '/app/tasks/', 'name' => 'Tasks'], + '/app/files/' => ['href' => '/app/files/', 'name' => 'Files'], + ]; + $this->assertEquals($expected, $result); + } + + public function testGetAppOrder() { + $nav_system = ['/app/calendar/', '/app/tasks/']; + $nav_user = ['/app/files/', '/app/calendar/', '/app/tasks/']; + $this->service->expects($this->once()) + ->method('getAppValue') + ->with('order') + ->will($this->returnValue(json_encode($nav_system))); + $this->service->expects($this->once()) + ->method('getUserValue') + ->with('order', $this->userId) + ->will($this->returnValue(json_encode($nav_user))); + $result = $this->util->getAppOrder(); + $this->assertEquals(json_encode($nav_user), $result); + } + + public function testGetAppOrderNoUser() { + $nav_system = ['/app/calendar/', '/app/tasks/']; + $nav_user = ''; + $this->service->expects($this->once()) + ->method('getAppValue') + ->with('order') + ->will($this->returnValue(json_encode($nav_system))); + $this->service->expects($this->once()) + ->method('getUserValue') + ->with('order', $this->userId) + ->will($this->returnValue($nav_user)); + $result = $this->util->getAppOrder(); + $this->assertEquals(json_encode($nav_system), $result); + } + +} diff --git a/tests/unit/controller/AppControllerTest.php b/tests/unit/controller/AppControllerTest.php new file mode 100644 index 0000000..ed5cdb8 --- /dev/null +++ b/tests/unit/controller/AppControllerTest.php @@ -0,0 +1,98 @@ +<?php +/** + * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @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\AppOrder\Tests\Unit\Controller; + +use OCP\IRequest; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http; +use \OCA\AppOrder\Service; +use \OCA\AppOrder\AppInfo\Application; +use \OCA\AppOrder\Controller; +use OCA\AppOrder\Controller\AppController; + +class AppControllerTest extends \Test\TestCase { + + private $container; + private $request; + private $service; + private $urlGenerator; + private $userId; + private $appName; + /** + * @var AppController + */ + private $controller; + private $config; + private $util; + + public function setUp() { + + parent::setUp(); + + $app = new \OCA\AppOrder\AppInfo\Application(); + $this->container = $app->getContainer(); + $this->request = $this->getMockBuilder('OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->service = $this->getMockBuilder('\OCA\AppOrder\Service\ConfigService') + ->disableOriginalConstructor() + ->getMock(); + $this->urlGenerator = \OC::$server->getURLGenerator(); + $this->util = $this->getMockBuilder('\OCA\AppOrder\Util')->disableOriginalConstructor()->getMock(); + + $this->userId = 'admin'; + $this->appName = 'apporder'; + $this->controller = new AppController( + $this->appName, $this->request, $this->service, $this->urlGenerator, $this->util, + $this->userId + ); + + } + + public function testIndex() { + $this->util->expects($this->once()) + ->method('getAppOrder') + ->willReturn( + json_encode(['/index.php/foo/bar', '/index.php/bar/foo']) + ); + $expected = new Http\RedirectResponse('/index.php/foo/bar'); + $result = $this->controller->index(); + $this->assertEquals($expected, $result); + } + + public function testIndexEmpty() { + $this->util->expects($this->once()) + ->method('getAppOrder') + ->willReturn(""); + $result = $this->controller->index(); + + $expected = new Http\RedirectResponse('/apps/files/'); + $this->assertEquals($expected, $result); + } + + +} diff --git a/tests/unit/controller/SettingsControllerTest.php b/tests/unit/controller/SettingsControllerTest.php index 5550f19..65ba926 100644 --- a/tests/unit/controller/SettingsControllerTest.php +++ b/tests/unit/controller/SettingsControllerTest.php @@ -23,7 +23,7 @@ class SettingsControllerTest extends \Test\TestCase { parent::setUp(); - $app = new Application(); + $app = new \OCA\AppOrder\AppInfo\Application(); $this->container = $app->getContainer(); $this->request = $this->getMockBuilder('OCP\IRequest') ->disableOriginalConstructor() @@ -37,10 +37,12 @@ class SettingsControllerTest extends \Test\TestCase { $this->navigationManager = $this->getMockBuilder('\OCP\INavigationManager')->setMethods(['getAll', 'add', 'setActiveEntry']) ->disableOriginalConstructor() ->getMock(); + $this->userId = 'admin'; $this->appName = 'apporder'; + $this->util = new \OCA\AppOrder\Util($this->service, $this->userId); $this->controller = new \OCA\AppOrder\Controller\SettingsController( - $this->appName, $this->request, $this->service, $this->navigationManager, + $this->appName, $this->request, $this->service, $this->navigationManager, $this->util, $this->userId ); @@ -74,50 +76,6 @@ class SettingsControllerTest extends \Test\TestCase { $this->assertEquals($expected, $result); } - public function testMatchOrder() { - $nav = [ - ['href' => '/app/files/', 'name' => 'Files'], - ['href' => '/app/calendar/', 'name' => 'Calendar'], - ['href' => '/app/tasks/', 'name' => 'Tasks'], - ]; - $order = ['/app/calendar/', '/app/tasks/']; - $result = $this->controller->matchOrder($nav, $order); - $expected = [ - '/app/calendar/' => ['href' => '/app/calendar/', 'name' => 'Calendar'], - '/app/tasks/' => ['href' => '/app/tasks/', 'name' => 'Tasks'], - '/app/files/' => ['href' => '/app/files/', 'name' => 'Files'], - ]; - $this->assertEquals($expected, $result); - } - - public function testGetAppOrder() { - $nav_system = ['/app/calendar/', '/app/tasks/']; - $nav_user = ['/app/files/', '/app/calendar/', '/app/tasks/']; - $this->service->expects($this->once()) - ->method('getAppValue') - ->with('order') - ->will($this->returnValue(json_encode($nav_system))); - $this->service->expects($this->once()) - ->method('getUserValue') - ->with('order', $this->userId) - ->will($this->returnValue(json_encode($nav_user))); - $result = $this->controller->getAppOrder(); - $this->assertEquals(json_encode($nav_user), $result); - } - public function testGetAppOrderNoUser() { - $nav_system = ['/app/calendar/', '/app/tasks/']; - $nav_user = ''; - $this->service->expects($this->once()) - ->method('getAppValue') - ->with('order') - ->will($this->returnValue(json_encode($nav_system))); - $this->service->expects($this->once()) - ->method('getUserValue') - ->with('order', $this->userId) - ->will($this->returnValue($nav_user)); - $result = $this->controller->getAppOrder(); - $this->assertEquals(json_encode($nav_system), $result); - } public function testGetOrder() { $nav_system = ['/app/calendar/', '/app/tasks/']; |