Introduce getValue() in Preferences and fix the loading of values

This commit is contained in:
Alexander Fuhr 2014-09-26 14:15:50 +02:00
parent d7f8a7823f
commit c8a24f72b8
4 changed files with 41 additions and 7 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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();
}

View File

@ -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');
}
}