From fed85e9f314366ba7fd99a8f1ad944c3fe1e8bc0 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Mon, 16 Nov 2026 20:19:49 +0100 Subject: [PATCH] fix(submission): only accept a list of values as an answer An answer is a list of values, and the answer to a grid question an object with the values by row, as the API describes it. Other values are rejected as an invalid answer with a 400 instead of failing with a server error, and an empty list counts as no answer, so an optional question answered with an empty list is not checked any further. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- lib/Service/SubmissionService.php | 27 +++++++++++- tests/Unit/Service/SubmissionServiceTest.php | 43 +++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index 8fc51b6e2..b54f16022 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -617,7 +617,11 @@ public function validateSubmission(array $questions, array $answers, string $for // Check by questions foreach ($questions as $question) { $questionId = $question['id']; - $questionAnswered = array_key_exists($questionId, $answers); + $questionAnswered = array_key_exists($questionId, $answers) && $answers[$questionId] !== []; + + if ($questionAnswered && !self::isListOfValues($question['type'], $answers[$questionId])) { + throw new \InvalidArgumentException(sprintf('Invalid answer for question "%s".', $question['text'])); + } // Check if all required questions have an answer if ($question['isRequired'] @@ -921,6 +925,27 @@ private function validateGridQuestion(array $question, array $answers): void { } } + /** + * Whether the answer is a list of values: texts or numbers, uploaded files for file questions, or an object with the values by row for grid questions + * + * @param string $questionType the question type + * @param mixed $answer the answer + */ + private static function isListOfValues(string $questionType, mixed $answer): bool { + if ($questionType === Constants::ANSWER_TYPE_GRID) { + return is_array($answer); + } + if (!is_array($answer) || !array_is_list($answer)) { + return false; + } + foreach ($answer as $value) { + if ($questionType === Constants::ANSWER_TYPE_FILE ? !is_array($value) : !is_scalar($value)) { + return false; + } + } + return true; + } + private function normalizeNumericOptionId(mixed $value): ?int { if (is_int($value)) { return $value; diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index 1bd18c291..9b6778a99 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -997,6 +997,47 @@ public static function dataValidateSubmission() { // Expected Result null, ], + 'empty-answers-to-optional-questions' => [ + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false], + ['id' => 2, 'type' => 'multiple', 'text' => 'q2', 'isRequired' => false, 'extraSettings' => ['optionsLimitMin' => 2], 'options' => [ + ['id' => 3], + ['id' => 5], + ]], + ], + ['1' => [], '2' => []], + null, + ], + 'empty-answer-to-required-question' => [ + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => true], + ], + ['1' => []], + 'Question "q1" is required.', + ], + 'answer-that-is-no-list' => [ + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false], + ], + ['1' => ['first' => 'text']], + 'Invalid answer for question "q1".', + ], + 'answer-with-a-value-that-is-no-text' => [ + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false], + ], + ['1' => [null]], + 'Invalid answer for question "q1".', + ], + 'answer-that-is-no-array' => [ + [ + ['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => false, 'options' => [ + ['id' => 3], + ]], + ], + ['1' => '3'], + 'Invalid answer for question "q1".', + ], 'invalid-short-phone' => [ // Questions [ @@ -1265,7 +1306,7 @@ public static function dataValidateSubmission() { [ '1' => [['3']] ], - 'The answer for question "q1" must be an integer between 0 and 5.', + 'Invalid answer for question "q1".', ], 'invalid-color-question' => [ // Questions