diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 2a77fd351..01d4eec4b 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -681,6 +681,10 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair throw new OCSBadRequestException('Question doesn\'t belong to given Form'); } + if ($question->getOrder() === 0) { + throw new OCSNotFoundException('Could not find question'); + } + // Don't allow empty array if (count($keyValuePairs) === 0) { $this->logger->info('Empty keyValuePairs, will not update.'); @@ -951,6 +955,10 @@ public function newOption(int $formId, int $questionId, array $optionTexts, ?str throw new OCSBadRequestException('This question is not part ot the given form'); } + if ($question->getOrder() === 0) { + throw new OCSNotFoundException('Could not find 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); @@ -1035,6 +1043,10 @@ public function updateOption(int $formId, int $questionId, int $optionId, array throw new OCSBadRequestException('The given option id doesn\'t match the question or form.'); } + if ($question->getOrder() === 0) { + throw new OCSNotFoundException('Could not find question'); + } + // Don't allow empty array if (count($keyValuePairs) === 0) { $this->logger->info('Empty keyValuePairs, will not update'); @@ -1104,6 +1116,10 @@ public function deleteOption(int $formId, int $questionId, int $optionId): DataR throw new OCSBadRequestException('The given option id doesn\'t match the question or form.'); } + if ($question->getOrder() === 0) { + throw new OCSNotFoundException('Could not find question'); + } + $this->optionMapper->delete($option); // Reorder the remaining options @@ -1162,6 +1178,10 @@ public function reorderOptions(int $formId, int $questionId, array $newOrder, ?s throw new OCSBadRequestException('The given question id doesn\'t match the form.'); } + if ($question->getOrder() === 0) { + throw new OCSNotFoundException('Could not find question'); + } + // Check if array contains duplicates if (array_unique($newOrder) !== $newOrder) { $this->logger->debug('The given array contains duplicates'); diff --git a/tests/Unit/Controller/ApiControllerTest.php b/tests/Unit/Controller/ApiControllerTest.php index a06d08112..00d384d40 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -1285,6 +1285,56 @@ public function testGetDeletedQuestion(): void { $this->apiController->getQuestion(1, 2); } + public static function dataChangeDeletedQuestion(): array { + return [ + 'update question' => ['updateQuestion', [1, 7, ['text' => 'New text']]], + 'new option' => ['newOption', [1, 7, ['New option']]], + 'update option' => ['updateOption', [1, 7, 3, ['text' => 'New text']]], + 'delete option' => ['deleteOption', [1, 7, 3]], + 'reorder options' => ['reorderOptions', [1, 7, [3]]], + ]; + } + + /** + * @dataProvider dataChangeDeletedQuestion + */ + public function testChangeDeletedQuestion(string $method, array $arguments): void { + $form = new Form(); + $form->setId(1); + $form->setOwnerId('currentUser'); + + $question = new Question(); + $question->setId(7); + $question->setFormId(1); + $question->setType(Constants::ANSWER_TYPE_MULTIPLE); + $question->setOrder(0); + + $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('findById') + ->with(3) + ->willReturn(\OCA\Forms\Db\Option::fromParams(['id' => 3, 'questionId' => 7])); + $this->questionMapper->expects($this->never())->method('update'); + $this->optionMapper->expects($this->never())->method('insert'); + $this->optionMapper->expects($this->never())->method('update'); + $this->optionMapper->expects($this->never())->method('delete'); + $this->formMapper->expects($this->never())->method('update'); + + $this->expectException(OCSNotFoundException::class); + $this->apiController->$method(...$arguments); + } + public function testUpdateQuestionRejectsTypeChange(): void { $form = new Form(); $form->setId(1);