From 4c62e8ade983fd4ce8df599dbd781a701d861ae7 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 21 Dec 2023 13:11:44 -0800 Subject: [PATCH] Add guard against attempting to access property on unsaved template --- app/Models/ReportTemplate.php | 4 ++++ tests/Unit/ReportTemplateTest.php | 2 ++ 2 files changed, 6 insertions(+) diff --git a/app/Models/ReportTemplate.php b/app/Models/ReportTemplate.php index 10ac5a9f1a..5925edbee6 100644 --- a/app/Models/ReportTemplate.php +++ b/app/Models/ReportTemplate.php @@ -62,6 +62,10 @@ class ReportTemplate extends Model public function selectValue(string $property, string $model = null) { + if (!isset($this->options[$property])) { + return null; + } + // If a model is provided then we should ensure we only return // the value if the model still exists. if ($model) { diff --git a/tests/Unit/ReportTemplateTest.php b/tests/Unit/ReportTemplateTest.php index c75433fa1e..b137f1911c 100644 --- a/tests/Unit/ReportTemplateTest.php +++ b/tests/Unit/ReportTemplateTest.php @@ -61,6 +61,7 @@ class ReportTemplateTest extends TestCase $this->assertEquals('4', $savedReport->selectValue('is_a_text_field_as_well')); $this->assertEquals('', $savedReport->selectValue('non_existent_key')); $this->assertNull($savedReport->selectValue('contains_a_null_value')); + $this->assertNull((new ReportTemplate)->selectValue('value_on_unsaved_template')); } public function testParsingSelectValues() @@ -102,6 +103,7 @@ class ReportTemplateTest extends TestCase $this->assertNull($templateWithDeletedId->selectValue('single_value', Location::class)); $this->assertNull($templateWithInvalidId->selectValue('single_value', Location::class)); + $this->assertNull((new ReportTemplate)->selectValue('value_on_unsaved_template', Location::class)); } public function testSelectValuesDoNotIncludeDeletedOrNonExistentModels()