From 12b36d2a32a2fc732ffb919b0d53fe5173958f54 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 6 Jan 2023 22:11:22 +0800 Subject: [PATCH] MDL-76362 qtype_numerical: Support empty units for apply_units --- question/type/numerical/questiontype.php | 6 +++++- question/type/numerical/tests/answerprocessor_test.php | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/question/type/numerical/questiontype.php b/question/type/numerical/questiontype.php index 3aa8a2e8f9e..5ba8b3a8681 100644 --- a/question/type/numerical/questiontype.php +++ b/question/type/numerical/questiontype.php @@ -651,9 +651,13 @@ class qtype_numerical_answer_processor { * by the unit multiplier, if any, and the unit string, for reference. */ public function apply_units($response, $separateunit = null) { + if ($response === null || trim($response) === '') { + return [null, null, null]; + } + // Strip spaces (which may be thousands separators) and change other forms // of writing e to e. - $response = str_replace(' ', '', $response ?? ''); + $response = str_replace(' ', '', $response); $response = preg_replace('~(?:e|E|(?:x|\*|×)10(?:\^|\*\*))([+-]?\d+)~', 'e$1', $response); // If a . is present or there are multiple , (i.e. 2,456,789 ) assume , diff --git a/question/type/numerical/tests/answerprocessor_test.php b/question/type/numerical/tests/answerprocessor_test.php index db80069a563..dd764665b5d 100644 --- a/question/type/numerical/tests/answerprocessor_test.php +++ b/question/type/numerical/tests/answerprocessor_test.php @@ -62,7 +62,7 @@ class answerprocessor_test extends \advanced_testcase { $rc = new \ReflectionClass($ap); $rcm = $rc->getMethod('parse_response'); - $rcm->setAccessible(True); + $rcm->setAccessible(true); $this->assertEquals($expected, $rcm->invoke($ap, $args)); } @@ -200,6 +200,9 @@ class answerprocessor_test extends \advanced_testcase { [1, 'frogs', null, '1 frogs'], [null, null, null, '. m/s'], + [null, null, null, null], + [null, null, null, ''], + [null, null, null, ' '], ]; }