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); + } }