Skip to content

Commit

Permalink
Fix settings with falsey values not getting returned
Browse files Browse the repository at this point in the history
Signed-off-by: Micheal Mand <micheal@kmdwebdesigns.com>
  • Loading branch information
mikemand committed Nov 15, 2017
1 parent 84756e9 commit 5aecb48
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
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;
}

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

0 comments on commit 5aecb48

Please sign in to comment.