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
4 changes: 3 additions & 1 deletion docs/API_v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ Update a single or multiple properties of a question-object.
| Parameter | Type | Description |
|-----------|---------|-------------|
| _keyValuePairs_ | Array | Array of key-value pairs to update |
- Restrictions: It is **not allowed** to update one of the following key-value pairs: _id, formId, order, type_.
- Restrictions:
- It is **not allowed** to update one of the following key-value pairs: _id, formId, order, type_.
- The sub type `extraSettings.questionType` is fixed when the question is created. An `extraSettings` value that adds, removes or changes `questionType` is rejected with **403 Forbidden**, so send the stored `questionType` with every `extraSettings` update.
- Response: **Status-Code OK**, as well as the id of the updated question.

```
Expand Down
7 changes: 7 additions & 0 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
* @throws OCSBadRequestException Invalid extraSettings, will not update.
* @throws OCSForbiddenException Empty keyValuePairs, will not update
* @throws OCSForbiddenException Not allowed to update `id`, `formId` or `type`
* @throws OCSForbiddenException Not allowed to update `extraSettings.questionType`
* @throws OCSForbiddenException Please use reorderQuestions() to change order
* @throws OCSForbiddenException This form is archived and can not be modified
* @throws OCSForbiddenException User has no permissions to get this form
Expand Down Expand Up @@ -709,6 +710,12 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair
throw new OCSBadRequestException('Invalid extraSettings, will not update.');
}

if (array_key_exists('extraSettings', $keyValuePairs)
&& ($keyValuePairs['extraSettings']['questionType'] ?? null) !== ($question->getExtraSettings()['questionType'] ?? null)) {
$this->logger->debug('Not allowed to update \'extraSettings.questionType\'');
throw new OCSForbiddenException('Not allowed to update \'extraSettings.questionType\'');
}

if ($form->getConfirmationEmailQuestionId() === $question->getId()
&& !$question->isEmailType($keyValuePairs['type'] ?? null, $keyValuePairs['extraSettings'] ?? null)) {
$form->setConfirmationEmailQuestionId(null);
Expand Down
39 changes: 39 additions & 0 deletions tests/Integration/Api/ApiV3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,45 @@ public function testUpdateQuestionProperties(array $fullFormExpected): void {
$this->testGetFullForm($fullFormExpected);
}

public function testUpdateQuestionRejectsSubtypeChange(): void {
$resp = $this->http->request('POST', "api/v3/forms/{$this->testForms[0]['id']}/questions", [
'json' => [
'type' => 'grid',
'subtype' => 'radio',
'text' => 'Grid question',
],
]);
$question = $this->OcsResponse2Data($resp);
$this->testForms[0]['questions'][] = $question;
$url = "api/v3/forms/{$this->testForms[0]['id']}/questions/{$question['id']}";

foreach ([['questionType' => 'number'], []] as $extraSettings) {
$resp = $this->http->request('PATCH', $url, [
'json' => [
'keyValuePairs' => [
'extraSettings' => $extraSettings,
],
],
'http_errors' => false,
]);
$this->assertEquals(403, $resp->getStatusCode());
}

$resp = $this->http->request('PATCH', $url, [
'json' => [
'keyValuePairs' => [
'text' => 'Renamed grid question',
'extraSettings' => ['questionType' => 'radio'],
],
],
]);
$this->assertEquals(200, $resp->getStatusCode());

$data = $this->OcsResponse2Data($this->http->request('GET', $url));
$this->assertEquals('Renamed grid question', $data['text']);
$this->assertEquals(['questionType' => 'radio'], $data['extraSettings']);
}

public static function dataReorderQuestions() {
$fullFormExpected = self::dataGetFullForm()['getFullForm']['expected'];
$fullFormExpected['questions'][0]['order'] = 2;
Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,62 @@ public function testUpdateQuestionRejectsTypeChange(): void {
$this->apiController->updateQuestion(1, 7, ['type' => Constants::ANSWER_TYPE_LONG]);
}

public static function dataUpdateQuestionSubtype(): array {
return [
'grid sub type changed' => [Constants::ANSWER_TYPE_GRID, ['questionType' => 'radio'], ['questionType' => 'number'], false],
'grid sub type removed' => [Constants::ANSWER_TYPE_GRID, ['questionType' => 'radio'], ['rows' => []], false],
'sub type added' => [Constants::ANSWER_TYPE_LINEARSCALE, [], ['questionType' => 'rating'], false],
'grid sub type unchanged' => [Constants::ANSWER_TYPE_GRID, ['questionType' => 'radio'], ['questionType' => 'radio', 'rows' => []], true],
'no sub type' => [Constants::ANSWER_TYPE_LINEARSCALE, ['optionsHighest' => 5], ['optionsHighest' => 7], true],
];
}

/**
* @dataProvider dataUpdateQuestionSubtype
*/
public function testUpdateQuestionSubtype(string $type, array $storedSettings, array $newSettings, 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($type);
$question->setExtraSettings($storedSettings);

$this->formsService
->method('getFormIfAllowed')
->with(1, Constants::PERMISSION_EDIT)
->willReturn($form);
$this->formsService
->method('isFormArchived')
->with($form)
->willReturn(false);
$this->formsService
->method('areExtraSettingsValid')
->with($newSettings, $type)
->willReturn(true);

$this->questionMapper->expects($this->once())
->method('findById')
->with(7)
->willReturn($question);

if (!$allowed) {
$this->questionMapper->expects($this->never())->method('update');
$this->formMapper->expects($this->never())->method('update');
$this->expectException(OCSForbiddenException::class);
} else {
$this->questionMapper->expects($this->once())
->method('update')
->with($this->callback(fn (Question $updatedQuestion): bool => $updatedQuestion->getExtraSettings() === $newSettings));
}

$this->assertEquals(new DataResponse(7), $this->apiController->updateQuestion(1, 7, ['extraSettings' => $newSettings]));
}

public function testUpdateQuestionClearsConfirmationEmailQuestionIdWhenQuestionStopsBeingEmail(): void {
$form = new Form();
$form->setId(1);
Expand Down
Loading