Skip to content

A free open source IT asset/license management system

Implement radioValue properly

Marcus Moorefcef60445c71
diff --git a/app/Models/ReportTemplate.php b/app/Models/ReportTemplate.php
index 6e16011779..73909d0454 100644
--- a/app/Models/ReportTemplate.php
+++ b/app/Models/ReportTemplate.php
@@ -44,16 +44,22 @@ class ReportTemplate extends Model
return $this->options[$property] ?? '0';
}
- public function radioValue(string $property, $value, $return)
+ public function radioValue(string $property, $value, $isDefault = false): bool
{
// @todo: this method feels more like "radioShouldBeChecked" or something...
// @todo: improve the variable names...
- if (array_has($this->options, $property) && $this->options[$property] === $value) {
- return $return;
+ $propertyExists = array_has($this->options, $property);
+
+ if (!$propertyExists && $isDefault) {
+ return true;
+ }
+
+ if ($propertyExists && $this->options[$property] === $value) {
+ return true;
}
- return null;
+ return false;
}
public function selectValue(string $property, string $model = null)
@@ -80,9 +86,9 @@ class ReportTemplate extends Model
// @todo: I think this was added to support the null object pattern
// @todo: Check if this is still needed and if so, add a test for it (testParsingSelectValues()).
- // if ($this->options[$property] === [null]) {
- // return null;
- // }
+ // if ($this->options[$property] === [null]) {
+ // return null;
+ // }
// If a model is provided then we should ensure we only return
// the ids of models that exist and are not deleted.
diff --git a/resources/views/reports/custom.blade.php b/resources/views/reports/custom.blade.php
index 09f0bc510f..c0c65d8727 100644
--- a/resources/views/reports/custom.blade.php
+++ b/resources/views/reports/custom.blade.php
@@ -420,15 +420,15 @@
<div class="col-md-9 col-md-offset-3">
<label class="form-control">
- {{ Form::radio('deleted_assets', '', $reportTemplate->radioValue('deleted_assets', null, true), ['aria-label'=>'deleted_assets', 'id'=>'deleted_assets_exclude_deleted'])}}
+ {{ Form::radio('deleted_assets', '', $reportTemplate->radioValue('deleted_assets', '', true), ['aria-label'=>'deleted_assets', 'id'=>'deleted_assets_exclude_deleted'])}}
{{ trans('general.exclude_deleted') }}
</label>
<label class="form-control">
- {{ Form::radio('deleted_assets', '1', $reportTemplate->radioValue('deleted_assets', '1', '1'), ['aria-label'=>'deleted_assets', 'id'=>'deleted_assets_include_deleted']) }}
+ {{ Form::radio('deleted_assets', '1', $reportTemplate->radioValue('deleted_assets', '1'), ['aria-label'=>'deleted_assets', 'id'=>'deleted_assets_include_deleted']) }}
{{ trans('general.include_deleted') }}
</label>
<label class="form-control">
- {{ Form::radio('deleted_assets', '0', $reportTemplate->radioValue('deleted_assets', '0', '1'), ['aria-label'=>'deleted_assets','id'=>'deleted_assets_only_deleted']) }}
+ {{ Form::radio('deleted_assets', '0', $reportTemplate->radioValue('deleted_assets', '0'), ['aria-label'=>'deleted_assets','id'=>'deleted_assets_only_deleted']) }}
{{ trans('general.only_deleted') }}
</label>
</div>
diff --git a/tests/Unit/ReportTemplateTest.php b/tests/Unit/ReportTemplateTest.php
index 5d404ff5da..0caebae04e 100644
--- a/tests/Unit/ReportTemplateTest.php
+++ b/tests/Unit/ReportTemplateTest.php
@@ -11,15 +11,16 @@ class ReportTemplateTest extends TestCase
{
public function testParsingValuesOnNonExistentReportTemplate()
{
- $this->markTestIncomplete();
+ // $this->markTestIncomplete();
$unsavedTemplate = new ReportTemplate;
// checkmarkValue()
$this->assertEquals('1', $unsavedTemplate->checkmarkValue('is_a_checkbox_field'));
- // @todo:
// radioValue()
+ $this->assertFalse($unsavedTemplate->radioValue('value_on_unsaved_template', 'can_be_anything'));
+ $this->assertTrue($unsavedTemplate->radioValue('value_on_unsaved_template', 'can_be_anything', true));
// selectValue()
$this->assertNull($unsavedTemplate->selectValue('value_on_unsaved_template'));
@@ -62,14 +63,16 @@ class ReportTemplateTest extends TestCase
public function testParsingRadioValue()
{
$template = ReportTemplate::factory()->create([
- 'options' => [
- 'is_a_radio_field' => null,
- ],
+ 'options' => ['property_that_exists' => '1'],
]);
- $this->assertEquals('return_value', $template->radioValue('is_a_radio_field', null, 'return_value'));
- $this->assertEquals(null, $template->radioValue('is_a_radio_field', 'another_value', 'return_value'));
- $this->assertNull($template->radioValue('non_existent_key', '1', true));
+ $this->assertTrue($template->radioValue('property_that_exists', '1'));
+
+ // check non-existent key returns false
+ $this->assertFalse($template->radioValue('non_existent_property', 'doesnt_matter'));
+
+ // check default returns true
+ $this->assertTrue($template->radioValue('non_existent_property', 'doesnt_matter', true));
}
public function testParsingSelectValue()