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
20 changes: 20 additions & 0 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading