From 72fef5686b1d2b2fd04eefe1a3b50fb195be7fd5 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 19 May 2014 09:53:40 +0200 Subject: [PATCH] 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. --- src/Helpers/TextHelper.php | 11 ++++++++++ .../JobTests/EditUserSettingsJobTest.php | 21 +++++++++++++++++++ tests/Tests/MiscTests/TextHelperTest.php | 11 ++++++++++ 3 files changed, 43 insertions(+) diff --git a/src/Helpers/TextHelper.php b/src/Helpers/TextHelper.php index 71f803ef..1eb92e0c 100644 --- a/src/Helpers/TextHelper.php +++ b/src/Helpers/TextHelper.php @@ -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; } diff --git a/tests/Tests/JobTests/EditUserSettingsJobTest.php b/tests/Tests/JobTests/EditUserSettingsJobTest.php index 97fce453..b6d58f09 100644 --- a/tests/Tests/JobTests/EditUserSettingsJobTest.php +++ b/tests/Tests/JobTests/EditUserSettingsJobTest.php @@ -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() diff --git a/tests/Tests/MiscTests/TextHelperTest.php b/tests/Tests/MiscTests/TextHelperTest.php index b537ae0c..758086c7 100644 --- a/tests/Tests/MiscTests/TextHelperTest.php +++ b/tests/Tests/MiscTests/TextHelperTest.php @@ -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); + } }