Fixed user settings across sessions

When getting settings from database, running across NULL loads default
setting for given option. Becuase entity unserializer always returned
NULLs instead of FALSEs due to bug in TextHelper, it ended up always
loading default settings (but only after reloading entity, setting it in
the settings page worked correctly until relog).

This fix is closely related to fde6fc2.
This commit is contained in:
Marcin Kurczewski 2014-05-19 09:53:40 +02:00
parent 361a221dc0
commit 72fef5686b
3 changed files with 43 additions and 0 deletions

View file

@ -46,6 +46,15 @@ class TextHelper
public static function toBooleanOrNull($x)
{
if ($x === null)
return null;
if ($x === true)
return true;
if ($x === false)
return false;
switch (strtolower($x))
{
case '1':
@ -54,12 +63,14 @@ class TextHelper
case 'yes':
case 'y':
return true;
case '0':
case 'false':
case 'off':
case 'no':
case 'n':
return false;
default:
return null;
}

View file

@ -29,6 +29,27 @@ class EditUserSettingsJobTest extends AbstractTest
$this->assert->isTrue($settings->get(UserSettings::SETTING_ENDLESS_SCROLLING));
$this->assert->isTrue($settings->get(UserSettings::SETTING_POST_TAG_TITLES));
$this->assert->isTrue($settings->get(UserSettings::SETTING_HIDE_DISLIKED_POSTS));
$user = $this->assert->doesNotThrow(function() use ($user, $expectedSafety)
{
return Api::run(
new EditUserSettingsJob(),
[
JobArgs::ARG_USER_NAME => $user->getName(),
JobArgs::ARG_NEW_SETTINGS =>
[
UserSettings::SETTING_ENDLESS_SCROLLING => false,
UserSettings::SETTING_POST_TAG_TITLES => false,
UserSettings::SETTING_HIDE_DISLIKED_POSTS => false,
]
]);
});
$settings = $user->getSettings();
$this->assert->isFalse($settings->get(UserSettings::SETTING_ENDLESS_SCROLLING));
$this->assert->isFalse($settings->get(UserSettings::SETTING_POST_TAG_TITLES));
$this->assert->isFalse($settings->get(UserSettings::SETTING_HIDE_DISLIKED_POSTS));
}
public function testSettingAdditional()

View file

@ -51,9 +51,20 @@ class TextHelperTest extends AbstractTest
$this->assert->isFalse(TextHelper::toBooleanOrNull('FaLsE'));
$this->assert->isFalse(TextHelper::toBooleanOrNull('false'));
$this->assert->isFalse(TextHelper::toBooleanOrNull(false));
$this->assert->isNotNull(TextHelper::toBooleanOrNull(false));
$this->assert->isNull(TextHelper::toBooleanOrNull(2));
$this->assert->isNull(TextHelper::toBooleanOrNull('2'));
$this->assert->isNull(TextHelper::toBooleanOrNull('rubbish'));
$this->assert->isNull(TextHelper::toBooleanOrNull(null));
}
public function testAssert()
{
$this->assert->isNull(null);
$this->assert->isNotNull(false);
$this->assert->isFalse(false);
$this->assert->areNotEqual(true, '1');
$this->assert->isTrue(true);
$this->assert->areNotEqual(false, null);
}
}