diff --git a/.github/workflows/node-test.yml b/.github/workflows/node-test.yml index b774d3a0a1..83aeb8c903 100644 --- a/.github/workflows/node-test.yml +++ b/.github/workflows/node-test.yml @@ -93,7 +93,7 @@ jobs: run: npm run test:coverage --if-present - name: Collect coverage - uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1 + uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0 with: disable_search: true files: ./coverage/lcov.info diff --git a/.github/workflows/phpunit-sqlite.yml b/.github/workflows/phpunit-sqlite.yml index f78735eef4..be05167a7b 100644 --- a/.github/workflows/phpunit-sqlite.yml +++ b/.github/workflows/phpunit-sqlite.yml @@ -157,7 +157,7 @@ jobs: run: composer run test:unit - name: Upload Unit coverage - uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1 + uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0 with: working-directory: apps/${{ env.APP_NAME }} disable_search: true @@ -184,7 +184,7 @@ jobs: run: composer run test:integration - name: Upload Integration coverage - uses: codecov/codecov-action@5a1091511ad55cbe89839c7260b706298ca349f7 # v5.5.1 + uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0 with: working-directory: apps/${{ env.APP_NAME }} disable_search: true diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index 99c9d38b43..ef08a28922 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -399,7 +399,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'] @@ -601,4 +605,22 @@ private function validateShortQuestion(array $question, string $data): bool { return false; } } + + /** + * Whether the answer is a list of values: texts or numbers, or uploaded files for file questions + * + * @param string $questionType the question type + * @param mixed $answer the answer + */ + private static function isListOfValues(string $questionType, mixed $answer): bool { + 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; + } } diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index 62bc6473d1..e6ba8b2802 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -885,6 +885,47 @@ public 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 [