Implement radioValue properly

This commit is contained in:
Marcus Moore 2023-12-21 18:07:46 -08:00
parent 9e0897b2cb
commit fcef60445c
No known key found for this signature in database
3 changed files with 27 additions and 18 deletions

View file

@ -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;
}
return null;
if ($propertyExists && $this->options[$property] === $value) {
return true;
}
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.

View file

@ -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>

View file

@ -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()