Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.0] Fix settings with falsey values not getting returned #435

Merged
merged 1 commit into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Modules/Setting/Support/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public function get($name, $locale = null, $default = null)
$defaultFromConfig = $this->getDefaultFromConfigFor($name);

$setting = $this->setting->findByName($name);
if (! $setting) {
if ($setting === null) {
return is_null($default) ? $defaultFromConfig : $default;
}

if ($setting->isTranslatable) {
if ($setting->hasTranslation($locale)) {
return empty($setting->translate($locale)->value) ? $defaultFromConfig : $setting->translate($locale)->value;
return trim($setting->translate($locale)->value) === '' ? $defaultFromConfig : $setting->translate($locale)->value;
}
} else {
return $setting->plainValue === null ? $defaultFromConfig : $setting->plainValue;
return trim($setting->plainValue) === '' ? $defaultFromConfig : $setting->plainValue;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't completely sure about this change here... It all depends on if the ConvertEmptyStringsToNull middleware is being used or not.

}

return $defaultFromConfig;
Expand Down
43 changes: 43 additions & 0 deletions Modules/Setting/Tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,49 @@ public function it_gets_setting_in_given_locale()
$this->assertEquals('AsgardCMS_fr', $setting);
}

/** @test */
public function it_returns_correctly_if_setting_is_falsey()
{
// Prepare
$data = [
'blog::posts-per-page' => 0,
];

// Run
$this->settingRepository->createOrUpdate($data);

// Assert
$setting = $this->setting->get('blog::posts-per-page');
$this->assertEquals(0, $setting);
}

/** @test */
public function it_returns_correctly_if_setting_for_locale_is_falsey()
{
// Prepare
$this->app['config']->set('asgard.block.settings', [
'display-some-feature' => [
'description' => 'block::settings.display-some-feature',
'view' => 'text',
'translatable' => true,
],
]);

$data = [
'block::display-some-feature' => [
'en' => 0,
'fr' => 1,
],
];

// Run
$this->settingRepository->createOrUpdate($data);

// Assert
$this->assertEquals(0, $this->setting->get('block::display-some-feature', 'en'));
$this->assertEquals(1, $this->setting->get('block::display-some-feature', 'fr'));
}

/** @test */
public function it_returns_a_default_value_if_no_setting_found()
{
Expand Down