diff --git a/docs/API_v3.md b/docs/API_v3.md index a8de1054c5..356bfef22c 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -921,7 +921,7 @@ Store Submission to Database - Restrictions: The array of answers has the following structure: - QuestionID as key - An **array** of values as value --> Even for short Text Answers, wrapped into Array. - - For Question-Types with pre-defined answers (`multiple`, `multiple_unique`, `dropdown`), the array contains the corresponding option-IDs. + - For Question-Types with pre-defined answers (`multiple`, `multiple_unique`, `dropdown`), the array contains the corresponding option-IDs, each as a number or as a string of exactly its digits, e.g. `12` or `"12"`, but not `"012"` or `" 12"`. - For File-Uploads, the array contains objects with `uploadedFileId` and `uploadToken` (both from the [Upload a file] endpoint for the same `questionId`). ``` diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 6d9a5d2428..e6d9176669 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -1839,7 +1839,7 @@ private function storeAnswersForQuestion(Form $form, $submissionId, array $quest $optionIndex = array_search($answer, array_column($question['options'], 'id')); if ($optionIndex !== false) { $answerText = $question['options'][$optionIndex]['text']; - } elseif (!empty($question['extraSettings']['allowOtherAnswer']) && str_starts_with($answer, Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX)) { + } elseif (SubmissionService::isOtherAnswer($question, $answer)) { $answerText = str_replace(Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX, '', $answer); } } elseif ($question['type'] === Constants::ANSWER_TYPE_FILE) { diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index 395da39371..d11dc7ca20 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -477,7 +477,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'] @@ -545,7 +549,7 @@ public function validateSubmission(array $questions, array $answers, string $for } // Check if all answers are within the possible options - if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED) && empty($question['extraSettings']['allowOtherAnswer'])) { + if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED)) { // Normalize option IDs once for consistent comparison (DB may return ints, request may send strings) $optionIds = $this->normalizeOptionIds($question['options'] ?? []); @@ -563,18 +567,11 @@ public function validateSubmission(array $questions, array $answers, string $for $this->validateGridQuestion($question, $answers[$questionId]); } // Search corresponding option, return false if non-existent - else { - // Accept numeric strings like "46" from JSON payloads reliably (e.g. with hardening extensions enabled) - $answerId = is_int($answer) ? $answer : (is_string($answer) ? intval(trim($answer)) : null); - - // Reject non-numeric / malformed values early - if ($answerId === null || (string)$answerId !== (string)intval($answerId)) { + elseif (!self::isOtherAnswer($question, $answer)) { + $answerId = $this->normalizeNumericOptionId($answer); + if ($answerId === null || !in_array($answerId, $optionIds, true)) { throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', is_scalar($answer) ? (string)$answer : gettype($answer), $question['text'])); } - - if (!in_array($answerId, $optionIds, true)) { - throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', $answer, $question['text'])); - } } } } @@ -636,8 +633,8 @@ public function validateSubmission(array $questions, array $answers, string $for // Check for excess answers foreach ($answers as $id => $answerArray) { // Search corresponding question, return false if not found - if (!in_array($id, array_column($questions, 'id'))) { - throw new \InvalidArgumentException(sprintf('Answer for non-existent question with ID %d.', $id)); + if (!in_array($id, array_column($questions, 'id'), true)) { + throw new \InvalidArgumentException(sprintf('Answer for non-existent question with ID %s.', $id)); } } } @@ -771,19 +768,52 @@ private function validateGridQuestion(array $question, array $answers): void { } foreach ($selectedColumnIds as $columnId) { - if ((!is_int($columnId) && (!is_string($columnId) || !ctype_digit($columnId))) || !in_array((int)$columnId, $columnIds, true)) { + if (!in_array($this->normalizeNumericOptionId($columnId), $columnIds, true)) { throw new \InvalidArgumentException(sprintf('Column "%s" for question "%s" is not a valid option.', is_scalar($columnId) ? (string)$columnId : gettype($columnId), $question['text'])); } } } } + /** + * Whether the value is the answer "Other" of a question that allows it + * + * @param array $question the question + * @param mixed $answer a value of the answer + */ + public static function isOtherAnswer(array $question, mixed $answer): bool { + return !empty($question['extraSettings']['allowOtherAnswer']) + && is_string($answer) + && str_starts_with($answer, Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX); + } + + /** + * 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; } - if (is_string($value) && ctype_digit($value)) { + if (is_string($value) && ctype_digit($value) && (string)(int)$value === $value) { return (int)$value; } diff --git a/playwright/e2e/form-settings.spec.ts b/playwright/e2e/form-settings.spec.ts index f860ac6d0a..50d92b41b8 100644 --- a/playwright/e2e/form-settings.spec.ts +++ b/playwright/e2e/form-settings.spec.ts @@ -37,16 +37,12 @@ test.describe('Form settings', () => { await page.getByRole('button', { name: /Share/ }).click() const settingsTab = page.getByRole('tab', { name: /Settings/ }) await settingsTab.click() - await expect( - page.getByRole('checkbox', { name: /Close form/ }), - ).toBeVisible() + await expect(page.getByRole('switch', { name: /Close form/ })).toBeVisible() }) test('Closing a form blocks submissions', async ({ page, topBar }) => { const saved = waitForApiResponse(page, 'PATCH') - await page - .getByRole('checkbox', { name: /Close form/ }) - .click({ force: true }) + await page.getByRole('switch', { name: /Close form/ }).click({ force: true }) await saved await topBar.toggleView(FormsView.View) @@ -67,16 +63,12 @@ test.describe('Form settings', () => { }) => { // Close the form const closed = waitForApiResponse(page, 'PATCH') - await page - .getByRole('checkbox', { name: /Close form/ }) - .click({ force: true }) + await page.getByRole('switch', { name: /Close form/ }).click({ force: true }) await closed // Reopen the form const reopened = waitForApiResponse(page, 'PATCH') - await page - .getByRole('checkbox', { name: /Close form/ }) - .click({ force: true }) + await page.getByRole('switch', { name: /Close form/ }).click({ force: true }) await reopened await topBar.toggleView(FormsView.View) @@ -91,7 +83,7 @@ test.describe('Form settings', () => { }) => { const saved = waitForApiResponse(page, 'PATCH') await page - .getByRole('checkbox', { name: /Store responses anonymously/ }) + .getByRole('switch', { name: /Store responses anonymously/ }) .click({ force: true }) await saved diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index e33f84d3bc..4f8198cb26 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -883,6 +883,50 @@ public static function dataValidateSubmission() { // Expected Result 'Answer for non-existent question with ID 2.', ], + 'question-id-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['validationType' => 'email']] + ], + [ + '1.0' => ['not an email'] + ], + 'Answer for non-existent question with ID 1.0.', + ], + 'option-id-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => false, 'options' => [ + ['id' => 3], + ['id' => 5] + ]], + ], + [ + '1' => ['03'] + ], + 'Answer "03" for question "q1" is not a valid option.', + ], + 'option-id-in-other-notation-with-other-answer' => [ + [ + ['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['allowOtherAnswer' => true], 'options' => [ + ['id' => 3], + ['id' => 5] + ]], + ], + [ + '1' => ['05'] + ], + 'Answer "05" for question "q1" is not a valid option.', + ], + 'unknown-option-with-other-answer' => [ + [ + ['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => true, 'extraSettings' => ['allowOtherAnswer' => true], 'options' => [ + ['id' => 3] + ]], + ], + [ + '1' => ['not an option'] + ], + 'Answer "not an option" for question "q1" is not a valid option.', + ], 'invalid-multiple-too-many-answers' => [ // Questions [ @@ -934,6 +978,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 [ @@ -1305,6 +1390,46 @@ public static function dataValidateSubmission() { ['1' => ['10' => ['21' => '1']]], 'Column "21" for question "Number grid" is not a valid option.', ], + 'invalid-number-grid-row-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'grid', 'text' => 'Number grid', 'isRequired' => false, 'extraSettings' => ['questionType' => Constants::ANSWER_GRID_TYPE_NUMBER], 'options' => [ + ['id' => 10, 'optionType' => 'row'], + ['id' => 20, 'optionType' => 'column'], + ]], + ], + ['1' => ['010' => ['20' => '1']]], + 'Row "010" for question "Number grid" is not a valid option.', + ], + 'valid-radio-grid' => [ + [ + ['id' => 1, 'type' => 'grid', 'text' => 'Radio grid', 'isRequired' => false, 'extraSettings' => ['questionType' => Constants::ANSWER_GRID_TYPE_RADIO], 'options' => [ + ['id' => 10, 'optionType' => 'row'], + ['id' => 20, 'optionType' => 'column'], + ]], + ], + ['1' => ['10' => '20']], + null, + ], + 'invalid-radio-grid-column-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'grid', 'text' => 'Radio grid', 'isRequired' => false, 'extraSettings' => ['questionType' => Constants::ANSWER_GRID_TYPE_RADIO], 'options' => [ + ['id' => 10, 'optionType' => 'row'], + ['id' => 20, 'optionType' => 'column'], + ]], + ], + ['1' => ['10' => '020']], + 'Column "020" for question "Radio grid" is not a valid option.', + ], + 'invalid-checkbox-grid-column-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'grid', 'text' => 'Checkbox grid', 'isRequired' => false, 'extraSettings' => ['questionType' => Constants::ANSWER_GRID_TYPE_CHECKBOX], 'options' => [ + ['id' => 10, 'optionType' => 'row'], + ['id' => 20, 'optionType' => 'column'], + ]], + ], + ['1' => ['10' => ['20', '020']]], + 'Column "020" for question "Checkbox grid" is not a valid option.', + ], 'invalid-ranking-missing-option' => [ // Questions [