From 345dbc3b14038589910e26421826028f76ec07d4 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Mon, 23 Nov 2026 08:49:17 +0100 Subject: [PATCH 1/2] refactor: keep the option types with the other constants The option types row and column were defined on the Option entity, while the question types, grid cell types and the other shared values live in Constants. Moving them there keeps the types an option can have next to the question types that use them. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- lib/Constants.php | 4 ++++ lib/Db/Option.php | 3 --- lib/Service/SubmissionService.php | 11 +++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Constants.php b/lib/Constants.php index 3e465cd0cd..37e0c9e9b9 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -118,6 +118,10 @@ class Constants { public const ANSWER_GRID_TYPE_NUMBER = 'number'; public const ANSWER_GRID_TYPE_RADIO = 'radio'; + // Option types + public const OPTION_TYPE_ROW = 'row'; + public const OPTION_TYPE_COLUMN = 'column'; + // All AnswerTypes public const ANSWER_TYPES = [ self::ANSWER_TYPE_COLOR, diff --git a/lib/Db/Option.php b/lib/Db/Option.php index c36b04f76e..ad6e2ba660 100644 --- a/lib/Db/Option.php +++ b/lib/Db/Option.php @@ -31,9 +31,6 @@ class Option extends Entity { protected ?int $order; protected ?string $optionType; - public const OPTION_TYPE_ROW = 'row'; - public const OPTION_TYPE_COLUMN = 'column'; - /** * Option constructor. */ diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index 78e4a64379..60fe14ae18 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -13,7 +13,6 @@ use OCA\Forms\Db\Answer; use OCA\Forms\Db\AnswerMapper; use OCA\Forms\Db\Form; -use OCA\Forms\Db\Option; use OCA\Forms\Db\OptionMapper; use OCA\Forms\Db\Question; use OCA\Forms\Db\QuestionMapper; @@ -301,10 +300,10 @@ public function getSubmissionsData(Form $form, string $fileFormat, ?File $file = foreach ($options as $option) { $optionPerOptionId[$option->getId()] = $option; - if ($option->getOptionType() === Option::OPTION_TYPE_ROW) { + if ($option->getOptionType() === Constants::OPTION_TYPE_ROW) { $gridRowsPerQuestionId[$question->getId()][] = $option->getId(); } - if ($option->getOptionType() === Option::OPTION_TYPE_COLUMN) { + if ($option->getOptionType() === Constants::OPTION_TYPE_COLUMN) { $gridColumnsPerQuestionId[$question->getId()][] = $option->getId(); } } @@ -607,7 +606,7 @@ private static function isShownToRespondents(array $question): bool { $optionTypes = array_map(static fn (mixed $option): mixed => is_array($option) ? ($option['optionType'] ?? null) : null, $question['options'] ?? []); return match ($question['type'] ?? null) { Constants::ANSWER_TYPE_MULTIPLE, Constants::ANSWER_TYPE_MULTIPLEUNIQUE, Constants::ANSWER_TYPE_DROPDOWN, Constants::ANSWER_TYPE_RANKING => $optionTypes !== [], - Constants::ANSWER_TYPE_GRID => in_array(Option::OPTION_TYPE_ROW, $optionTypes, true) && in_array(Option::OPTION_TYPE_COLUMN, $optionTypes, true), + Constants::ANSWER_TYPE_GRID => in_array(Constants::OPTION_TYPE_ROW, $optionTypes, true) && in_array(Constants::OPTION_TYPE_COLUMN, $optionTypes, true), default => true, }; } @@ -884,8 +883,8 @@ private function validateShortQuestion(array $question, string $data): bool { } private function validateGridQuestion(array $question, array $answers): void { - $rowIds = $this->normalizeOptionIds($question['options'] ?? [], Option::OPTION_TYPE_ROW); - $columnIds = $this->normalizeOptionIds($question['options'] ?? [], Option::OPTION_TYPE_COLUMN); + $rowIds = $this->normalizeOptionIds($question['options'] ?? [], Constants::OPTION_TYPE_ROW); + $columnIds = $this->normalizeOptionIds($question['options'] ?? [], Constants::OPTION_TYPE_COLUMN); $gridType = $question['extraSettings']['questionType'] ?? null; foreach ($answers as $rowId => $rowAnswer) { From 142c37ab3e0b64bef7c05a6611d7f296eaf9e62a Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Mon, 23 Nov 2026 08:50:27 +0100 Subject: [PATCH 2/2] fix(api): add options only to questions that take them The endpoint that creates options checked the form and the question, but not the question type, so options could be stored on short text, date, file, linear scale and other questions that have no choices. They were never shown or used and stayed as dead rows. The option type was not checked either, so a dropdown could get grid rows. Options are now accepted only for dropdown, checkbox, radio button and ranking questions (option type choice) and for grid questions (row or column), and are otherwise rejected with 400. A missing option type still means choice. Fixes #3981 Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- docs/API_v3.md | 3 +- docs/DataStructure.md | 2 +- lib/Constants.php | 12 +++++ lib/Controller/ApiController.php | 14 ++++++ openapi.json | 2 +- tests/Integration/Api/ApiV3Test.php | 16 ++++++ tests/Unit/Controller/ApiControllerTest.php | 54 +++++++++++++++++++++ 7 files changed, 100 insertions(+), 3 deletions(-) diff --git a/docs/API_v3.md b/docs/API_v3.md index 7ad999c5fc..645211cfd6 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -547,7 +547,8 @@ Contains only manipulative question-endpoints. To retrieve options, request the | Parameter | Type | Optional | Description | |-----------|---------|----------|-------------| | _optionTexts_ | Array | | Array of strings containing the new options | - | _optionType_ | [Option Types](DataStructure.md#Option-Types) | yes | The option-type to assign to the new options (e.g. `row`). | + | _optionType_ | [Option Types](DataStructure.md#Option-Types) | yes | The option-type to assign to the new options (e.g. `row`). Defaults to `choice`. | +- Restrictions: Options can only be added to `dropdown`, `multiple`, `multiple_unique` and `ranking` questions, with option type `choice`, and to `grid` questions, with option type `row` or `column`. Any other question type or option type is rejected with **400 Bad Request**. - Response: The new array of option objects ``` diff --git a/docs/DataStructure.md b/docs/DataStructure.md index d0e9acfa34..7d861565d3 100644 --- a/docs/DataStructure.md +++ b/docs/DataStructure.md @@ -277,7 +277,7 @@ Supported option types and their intended usage. | Type-ID | Description | | -------- | ------------------------------------------------------------------------------------------ | -| `choice` | Standard selectable option used by `dropdown`, `multiple` and `multiple_unique` questions. | +| `choice` | Standard selectable option used by `dropdown`, `multiple`, `multiple_unique` and `ranking` questions. | | `row` | Represents a row header when rendering options in a grid-style question. | | `column` | Represents a column header when rendering options in a grid-style question. | diff --git a/lib/Constants.php b/lib/Constants.php index 37e0c9e9b9..0766c108ac 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -119,9 +119,21 @@ class Constants { public const ANSWER_GRID_TYPE_RADIO = 'radio'; // Option types + public const OPTION_TYPE_CHOICE = 'choice'; public const OPTION_TYPE_ROW = 'row'; public const OPTION_TYPE_COLUMN = 'column'; + /** + * The option types each question type takes; question types not listed take no options + */ + public const OPTION_TYPES_PER_ANSWER_TYPE = [ + self::ANSWER_TYPE_DROPDOWN => [self::OPTION_TYPE_CHOICE], + self::ANSWER_TYPE_MULTIPLE => [self::OPTION_TYPE_CHOICE], + self::ANSWER_TYPE_MULTIPLEUNIQUE => [self::OPTION_TYPE_CHOICE], + self::ANSWER_TYPE_RANKING => [self::OPTION_TYPE_CHOICE], + self::ANSWER_TYPE_GRID => [self::OPTION_TYPE_ROW, self::OPTION_TYPE_COLUMN], + ]; + // All AnswerTypes public const ANSWER_TYPES = [ self::ANSWER_TYPE_COLOR, diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 8501a30bc9..4c52582877 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -971,6 +971,8 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse { * @param string|null $optionType the new option type (e.g. 'row'), defaults to 'choice' * @return DataResponse, array{}> Returns a DataResponse containing the added options * @throws OCSBadRequestException This question is not part ot the given form + * @throws OCSBadRequestException This question type takes no options + * @throws OCSBadRequestException Invalid option type for this question * @throws OCSForbiddenException This form is archived and can not be modified * @throws OCSForbiddenException Current user has no permission to edit * @throws OCSNotFoundException Could not find form @@ -1016,6 +1018,18 @@ public function newOption(int $formId, int $questionId, array $optionTexts, ?str throw new OCSNotFoundException('Could not find question'); } + $allowedOptionTypes = Constants::OPTION_TYPES_PER_ANSWER_TYPE[$question->getType()] ?? []; + if ($allowedOptionTypes === []) { + $this->logger->debug('This question type takes no options'); + throw new OCSBadRequestException('This question type takes no options'); + } + + $optionType ??= Constants::OPTION_TYPE_CHOICE; + if (!in_array($optionType, $allowedOptionTypes, true)) { + $this->logger->debug('Invalid option type for this question'); + throw new OCSBadRequestException('Invalid option type for this question'); + } + // Retrieve all options sorted by 'order'. Takes the order of the last array-element and adds one. $options = $this->optionMapper->findByQuestion($questionId, $optionType); $lastOption = array_pop($options); diff --git a/openapi.json b/openapi.json index 4e11633cea..45ef51791e 100644 --- a/openapi.json +++ b/openapi.json @@ -2802,7 +2802,7 @@ } }, "400": { - "description": "This question is not part ot the given form", + "description": "Invalid option type for this question", "content": { "application/json": { "schema": { diff --git a/tests/Integration/Api/ApiV3Test.php b/tests/Integration/Api/ApiV3Test.php index dd8f87dc7e..0a9483cb8f 100644 --- a/tests/Integration/Api/ApiV3Test.php +++ b/tests/Integration/Api/ApiV3Test.php @@ -1127,6 +1127,22 @@ public function testCreateNewOption(array $expected): void { $this->assertEquals($expected, $data); } + public function testCreateNewOptionRejectsQuestionWithoutOptions(): void { + $formId = $this->testForms[0]['id']; + $questionId = $this->testForms[0]['questions'][0]['id']; + $resp = $this->http->request('POST', "api/v3/forms/{$formId}/questions/{$questionId}/options", [ + 'json' => [ + 'optionTexts' => ['Coffee', 'Tea'], + ], + 'http_errors' => false, + ]); + + $this->assertEquals(400, $resp->getStatusCode()); + + $question = $this->OcsResponse2Data($this->http->request('GET', "api/v3/forms/{$formId}/questions/{$questionId}")); + $this->assertSame([], $question['options']); + } + public static function dataUpdateOptionProperties() { $fullFormExpected = self::dataGetFullForm()['getFullForm']['expected']; $fullFormExpected['questions'][1]['options'][0]['text'] = 'New option Text.'; diff --git a/tests/Unit/Controller/ApiControllerTest.php b/tests/Unit/Controller/ApiControllerTest.php index 81fdf8bf02..62d1b381cb 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -37,6 +37,7 @@ function is_uploaded_file(string|bool|null $filename) { use OCA\Forms\Db\AnswerMapper; use OCA\Forms\Db\Form; use OCA\Forms\Db\FormMapper; +use OCA\Forms\Db\Option; use OCA\Forms\Db\OptionMapper; use OCA\Forms\Db\Question; use OCA\Forms\Db\QuestionMapper; @@ -1418,6 +1419,59 @@ public function testChangeDeletedQuestion(string $method, array $arguments): voi $this->apiController->$method(...$arguments); } + public static function dataNewOptionOptionType(): array { + return [ + 'short text' => [Constants::ANSWER_TYPE_SHORT, 'choice', false], + 'long text' => [Constants::ANSWER_TYPE_LONG, 'choice', false], + 'date' => [Constants::ANSWER_TYPE_DATE, 'choice', false], + 'file' => [Constants::ANSWER_TYPE_FILE, 'choice', false], + 'linear scale' => [Constants::ANSWER_TYPE_LINEARSCALE, 'choice', false], + 'dropdown with a row' => [Constants::ANSWER_TYPE_DROPDOWN, 'row', false], + 'grid with a choice' => [Constants::ANSWER_TYPE_GRID, 'choice', false], + 'grid without option type' => [Constants::ANSWER_TYPE_GRID, null, false], + 'dropdown' => [Constants::ANSWER_TYPE_DROPDOWN, 'choice', true], + 'dropdown without option type' => [Constants::ANSWER_TYPE_DROPDOWN, null, true], + 'checkboxes' => [Constants::ANSWER_TYPE_MULTIPLE, 'choice', true], + 'radio buttons' => [Constants::ANSWER_TYPE_MULTIPLEUNIQUE, 'choice', true], + 'ranking' => [Constants::ANSWER_TYPE_RANKING, 'choice', true], + 'grid row' => [Constants::ANSWER_TYPE_GRID, 'row', true], + 'grid column' => [Constants::ANSWER_TYPE_GRID, 'column', true], + ]; + } + + /** + * @dataProvider dataNewOptionOptionType + */ + public function testNewOptionOptionType(string $questionType, ?string $optionType, bool $allowed): void { + $form = new Form(); + $form->setId(1); + $form->setOwnerId('currentUser'); + + $question = new Question(); + $question->setId(7); + $question->setFormId(1); + $question->setOrder(1); + $question->setType($questionType); + + $this->formsService->method('getFormIfAllowed')->with(1, Constants::PERMISSION_EDIT)->willReturn($form); + $this->formsService->method('isFormArchived')->with($form)->willReturn(false); + $this->questionMapper->method('findById')->with(7)->willReturn($question); + $this->optionMapper->method('findByQuestion')->willReturn([]); + + if (!$allowed) { + $this->optionMapper->expects($this->never())->method('insert'); + $this->expectException(OCSBadRequestException::class); + } else { + $this->optionMapper->expects($this->once()) + ->method('insert') + ->with($this->callback(fn (Option $option): bool => $option->getOptionType() === ($optionType ?? Constants::OPTION_TYPE_CHOICE))) + ->willReturnArgument(0); + } + + $response = $this->apiController->newOption(1, 7, ['Coffee'], $optionType); + $this->assertSame(Http::STATUS_CREATED, $response->getStatus()); + } + public function testUpdateQuestionRejectsTypeChange(): void { $form = new Form(); $form->setId(1);