diff --git a/docs/API_v3.md b/docs/API_v3.md index 0cb4a5b2e..9ca1e2752 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -407,6 +407,7 @@ Returns the requested question and options of the given form (without submission | _questionId_ | Integer | ID of the question | - Response: An object of the requested question including options. Before the opening date (`opens`) of the form, users who can neither edit the form nor see its results get a **Forbidden** response. + A deleted question gets a **Not Found** response. ``` "data": { diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 8162e70f0..998c3a937 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -481,6 +481,7 @@ public function getQuestions(int $formId): DataResponse { * @throws OCSBadRequestException Question doesn\'t belong to given Form * @throws OCSForbiddenException Form is not yet open * @throws OCSForbiddenException User has no permissions to get this form + * @throws OCSNotFoundException Question doesn\'t exist * @throws OCSNotFoundException Could not find form * * 200: the requested question @@ -507,7 +508,7 @@ public function getQuestion(int $formId, int $questionId): DataResponse { } $question = $this->formsService->getQuestion($questionId); - if ($question === null) { + if ($question === null || $question['order'] === 0) { throw new OCSNotFoundException('Question doesn\'t exist'); } diff --git a/tests/Unit/Controller/ApiControllerTest.php b/tests/Unit/Controller/ApiControllerTest.php index c2f0355f7..d6e884602 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -1859,6 +1859,26 @@ public function testGetQuestionsAfterOpening(): void { ); } + public function testGetDeletedQuestion(): void { + $form = new Form(); + $form->setId(1); + $this->formMapper->method('findById') + ->with(1) + ->willReturn($form); + $this->formsService->method('hasUserAccess') + ->with($form) + ->willReturn(true); + $this->formsService->method('canSeeQuestions') + ->with($form) + ->willReturn(true); + $this->formsService->method('getQuestion') + ->with(2) + ->willReturn(['id' => 2, 'formId' => 1, 'order' => 0, 'text' => 'Deleted', 'extraSettings' => []]); + + $this->expectException(OCSNotFoundException::class); + $this->apiController->getQuestion(1, 2); + } + public function testUpdateFormLogsLinkedResultsFile(): void { $form = $this->createUpdatableForm(); $file = $this->createMock(File::class);