Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -778,6 +782,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;
Expand Down
18 changes: 5 additions & 13 deletions playwright/e2e/form-settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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

Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Service/SubmissionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,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
[
Expand Down
Loading