Merge branch 'settingsTests'

This commit is contained in:
brantje 2016-12-30 17:10:14 +01:00
commit 8655bb991c
No known key found for this signature in database
GPG key ID: 5FF1D117F918687F
3 changed files with 198 additions and 14 deletions

View file

@ -46,14 +46,6 @@ class SettingsService {
$this->userId = $UserId;
$this->config = $config;
$this->appName = $AppName;
}
/**
* Get all app settings
*
* @return array
*/
public function getAppSettings() {
$this->settings = array(
'link_sharing_enabled' => intval($this->config->getAppValue('passman', 'link_sharing_enabled', 1)),
'user_sharing_enabled' => intval($this->config->getAppValue('passman', 'user_sharing_enabled', 1)),
@ -63,6 +55,14 @@ class SettingsService {
'disable_contextmenu' => intval($this->config->getAppValue('passman', 'disable_contextmenu', 1)),
'settings_loaded' => 1
);
}
/**
* Get all app settings
*
* @return array
*/
public function getAppSettings() {
return $this->settings;
}
@ -74,9 +74,13 @@ class SettingsService {
* @return mixed
*/
public function getAppSetting($key, $default_value = null) {
$value = $this->config->getAppValue('passman', $key, $default_value);
if (in_array($key, $this->numeric_settings)) {
$value = intval($value);
if (isset($this->settings[$key])) {
$value = $this->settings[$key];
if (in_array($key, $this->numeric_settings)) {
$value = intval($value);
}
} else {
$value = $this->config->getAppValue('passman', $key, $default_value);
}
return $value;
}
@ -88,6 +92,7 @@ class SettingsService {
* @param $value mixed Value of the setting
*/
public function setAppSetting($key, $value) {
$this->settings[$key] = $value;
$this->config->setAppValue('passman', $key, $value);
}
@ -98,7 +103,7 @@ class SettingsService {
* @param $value mixed Value of the setting
*/
public function setUserSetting($key, $value){
public function setUserSetting($key, $value) {
return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
}
@ -108,8 +113,8 @@ class SettingsService {
* @param $setting
* @return bool
*/
public function isEnabled($setting){
$value = intval($this->config->getAppValue('passman', $setting, false));
public function isEnabled($setting) {
$value = intval($this->getAppSetting($setting, false));
return ($value === 1);
}
}

View file

@ -0,0 +1,93 @@
<?php
/**
* Nextcloud - passman
*
* @copyright Copyright (c) 2016, Sander Brand (brantje@gmail.com)
* @copyright Copyright (c) 2016, Marcos Zuriaga Miguel (wolfi@wolfi.es)
* @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\Passman\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use PHPUnit_Framework_TestCase;
use OCA\Passman\Service\SettingsService;
/**
* Class PageControllerTest
*
* @package OCA\Passman\Controller
* @coversDefaultClass \OCA\Passman\Controller\SettingsController
*/
class SettingsControllerTest extends PHPUnit_Framework_TestCase {
private $controller;
public function setUp() {
$request = $this->getMockBuilder('OCP\IRequest')->getMock();
$IL10N = $this->getMockBuilder('OCP\IL10N')->getMock();
$config = $this->getMockBuilder('OCP\IConfig')->getMock();
$userId = 'admin';
$settings = new SettingsService($userId, $config, 'passman');
$this->controller = new SettingsController(
'passman', $request, $userId, $settings, $IL10N
);
}
/**
* @covers ::getForm
*/
public function testGetForm() {
$result = $this->controller->getForm();
$this->assertTrue($result instanceof TemplateResponse);
}
/**
* @covers ::getPriority
*/
public function testGetPriority() {
$result = $this->controller->getPriority();
$this->assertTrue($result === 0);
}
/**
* @covers ::getSettings
*/
public function testGetSettings() {
$result = $this->controller->getsettings();
$this->assertTrue($result instanceof JSONResponse);
}
/**
* @covers ::saveUserSetting
*/
public function testSaveUserSetting() {
$result = $this->controller->saveUserSetting('test','value');
$this->assertTrue($result instanceof JSONResponse);
}
/**
* @covers ::saveAdminSetting
*/
public function testSaveAdminSetting() {
$result = $this->controller->saveAdminSetting('admin', 'value');
$this->assertTrue($result instanceof JSONResponse);
}
}

View file

@ -0,0 +1,86 @@
<?php
/**
* Nextcloud - passman
*
* @copyright Copyright (c) 2016, Sander Brand (brantje@gmail.com)
* @copyright Copyright (c) 2016, Marcos Zuriaga Miguel (wolfi@wolfi.es)
* @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\Passman\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;
use PHPUnit_Framework_TestCase;
use OCA\Passman\Service\SettingsService;
/**
* Class PageControllerTest
*
* @package OCA\Passman\Controller
* @coversDefaultClass \OCA\Passman\Service\SettingsServiceTest
*/
class SettingsServiceTest extends PHPUnit_Framework_TestCase {
private $service;
public function setUp() {
$config = $this->getMockBuilder('OCP\IConfig')->getMock();
$userId = 'admin';
$this->service = new SettingsService($userId, $config, 'passman');
}
/**
* @covers ::getAppSettings
*/
public function testGetAppSettings() {
$result = $this->service->getAppSettings();
$this->assertTrue(is_array($result));
}
/**
* @covers ::getAppSetting
*/
public function testGetAppSetting() {
$result = $this->service->getAppSetting('settings_loaded', 1);
$this->assertTrue($result === 1);
}
/**
* @covers ::setAppSetting
*/
public function testSetAppSetting() {
$this->service->setAppSetting('settings_loaded', 0);
$this->assertTrue( $this->service->getAppSetting('settings_loaded') === 0);
}
/**
* @covers ::setUserSetting
*/
public function testSetUserSetting() {
$this->service->setUserSetting('test','value');
}
/**
* On tests link_sharing is disabled
* @covers ::isEnabled
*/
public function testIsEnabled() {
$result = $this->service->isEnabled('link_sharing_enabled');
$this->assertFalse($result);
}
}