From c8a24f72b840b946a691029f7697afc1175bca77 Mon Sep 17 00:00:00 2001 From: Alexander Fuhr Date: Fri, 26 Sep 2014 14:15:50 +0200 Subject: [PATCH] Introduce getValue() in Preferences and fix the loading of values --- library/Icinga/Application/Web.php | 5 ++-- library/Icinga/User/Preferences.php | 27 ++++++++++++++++--- .../Web/Controller/ActionController.php | 2 +- .../library/Icinga/User/PreferencesTest.php | 14 ++++++++++ 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/library/Icinga/Application/Web.php b/library/Icinga/Application/Web.php index 6a15d392a..89cad22ab 100644 --- a/library/Icinga/Application/Web.php +++ b/library/Icinga/Application/Web.php @@ -278,7 +278,8 @@ class Web extends ApplicationBootstrap if ($this->user !== null && $this->user->getPreferences() !== null) { $detect = new TimezoneDetect(); - $userTimezone = $this->user->getPreferences()->get('app.timezone', $detect->getTimezoneName()); + $userTimezone = $this->user->getPreferences() + ->getValue('icingaweb', 'timezone', $detect->getTimezoneName()); } try { @@ -302,7 +303,7 @@ class Web extends ApplicationBootstrap { parent::setupInternationalization(); if ($this->user !== null && $this->user->getPreferences() !== null - && (($locale = $this->user->getPreferences()->get('app.language')) !== null) + && (($locale = $this->user->getPreferences()->getValue('icingaweb', 'language')) !== null) ) { try { Translator::setupLocale($locale); diff --git a/library/Icinga/User/Preferences.php b/library/Icinga/User/Preferences.php index b1dae867a..364112a9e 100644 --- a/library/Icinga/User/Preferences.php +++ b/library/Icinga/User/Preferences.php @@ -79,19 +79,38 @@ class Preferences implements Countable } /** - * Retrieve a preference and return $default if the preference is not set + * Retrieve a preference section * * @param string $name - * @param mixed $default * - * @return mixed + * @return array|null */ - public function get($name, $default = null) + public function get($name) { if (array_key_exists($name, $this->preferences)) { return $this->preferences[$name]; } + return null; + } + + /** + * Retrieve a value from a specific section + * + * @param string $section + * @param string $name + * @param null $default + * + * @return array|null + */ + public function getValue($section, $name, $default = null) + { + if (array_key_exists($section, $this->preferences) + && array_key_exists($name, $this->preferences[$section]) + ) { + return $this->preferences[$section][$name]; + } + return $default; } diff --git a/library/Icinga/Web/Controller/ActionController.php b/library/Icinga/Web/Controller/ActionController.php index d3fc54535..315209232 100644 --- a/library/Icinga/Web/Controller/ActionController.php +++ b/library/Icinga/Web/Controller/ActionController.php @@ -365,7 +365,7 @@ class ActionController extends Zend_Controller_Action if ($user = $req->getUser()) { // Cast preference app.show_benchmark to bool because preferences loaded from a preferences storage are // always strings - if ((bool) $user->getPreferences()->get('app.show_benchmark', false) === true) { + if ((bool) $user->getPreferences()->getValue('icingaweb', 'show_benchmark', false) === true) { if (!$this->_helper->viewRenderer->getNoRender()) { $layout->benchmark = $this->renderBenchmark(); } diff --git a/test/php/library/Icinga/User/PreferencesTest.php b/test/php/library/Icinga/User/PreferencesTest.php index da5c318fa..540ea1b14 100644 --- a/test/php/library/Icinga/User/PreferencesTest.php +++ b/test/php/library/Icinga/User/PreferencesTest.php @@ -44,4 +44,18 @@ class PreferfencesTest extends BaseTestCase $this->assertEquals(2, count($prefs)); } + + public function testWhetherGetValueReturnsExpectedValue() + { + $prefs = new Preferences(array( + 'test' => array ( + 'key1' => '1', + 'key2' => '2', + ) + )); + + $result = $prefs->getValue('test', 'key2'); + + $this->assertEquals('2', $result, 'Preferences::getValue() do not return an expected value'); + } }