Skip to content

Commit 1139f15

Browse files
committed
add unit test testUpdateSubmission_answers
Signed-off-by: Timotheus Pokorra <[email protected]>
1 parent ef8beb3 commit 1139f15

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

lib/Controller/ApiController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,10 @@ public function updateSubmission(int $formId, int $submissionId, array $answers,
12161216
throw new OCSBadRequestException('Can only update if AllowEdit is set');
12171217
}
12181218

1219+
if ($submissionId != $submission->getId()) {
1220+
throw new OCSBadRequestException('Can only update your own submissions');
1221+
}
1222+
12191223
$submission->setTimestamp(time());
12201224
$this->submissionMapper->update($submission);
12211225

@@ -1470,7 +1474,7 @@ public function uploadFiles(int $formId, int $questionId, string $shareHash = ''
14701474
* @param string[]|array<array{uploadedFileId: string, uploadedFileName: string}> $answerArray
14711475
* @param bool $update
14721476
*/
1473-
private function storeAnswersForQuestion(Form $form, $submissionId, array $question, array $answerArray, bool $update) {
1477+
private function storeAnswersForQuestion(Form $form, int $submissionId, array $question, array $answerArray, bool $update) {
14741478
// get stored answers for this question
14751479
$storedAnswers = [];
14761480
if ($update) {

tests/Unit/Controller/ApiControllerTest.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,10 @@ public function testUploadFiles() {
680680
$this->apiController->uploadFiles(1, 10, '');
681681
}
682682

683-
public function testNewSubmission_answers() {
683+
/**
684+
* Values for the mock objects for the following methods: testNewSubmission_answers, testUpdateSubmission_answers.
685+
*/
686+
public function dataForSubmission_answers() {
684687
$form = new Form();
685688
$form->setId(1);
686689
$form->setHash('hash');
@@ -728,6 +731,16 @@ public function testNewSubmission_answers() {
728731
5 => ['ignore unknown question'],
729732
];
730733

734+
return [
735+
'test' => [$form, $questions, $answers],
736+
];
737+
}
738+
739+
/**
740+
* @dataProvider dataForSubmission_answers()
741+
*/
742+
public function testNewSubmission_answers($form, $questions, $answers) {
743+
731744
$this->formMapper->expects($this->once())
732745
->method('findById')
733746
->with(1)
@@ -882,6 +895,61 @@ public function testNewSubmission_validateSubmission() {
882895
$this->apiController->newSubmission(1, [], '');
883896
}
884897

898+
/**
899+
* @dataProvider dataForSubmission_answers()
900+
*/
901+
public function testUpdateSubmission_answers($form, $questions, $answers) {
902+
903+
$form->setAllowEdit(true);
904+
$submission = new Submission();
905+
$submission->setId(12);
906+
907+
$this->formMapper->expects($this->once())
908+
->method('findById')
909+
->with(1)
910+
->willReturn($form);
911+
912+
$this->formsService->expects($this->once())
913+
->method('getQuestions')
914+
->with(1)
915+
->willReturn($questions);
916+
917+
$this->formAccess();
918+
919+
$this->submissionService
920+
->method('validateSubmission')
921+
->willReturn(true);
922+
923+
$this->submissionMapper->expects($this->once())
924+
->method('findByFormAndUser')
925+
->with(1, 'currentUser')
926+
->willReturn($submission);
927+
928+
$userFolder = $this->createMock(Folder::class);
929+
$userFolder->expects($this->once())
930+
->method('nodeExists')
931+
->willReturn(true);
932+
933+
$file = $this->createMock(File::class);
934+
935+
$userFolder->expects($this->once())
936+
->method('getById')
937+
->willReturn([$file]);
938+
939+
$folder = $this->createMock(Folder::class);
940+
941+
$userFolder->expects($this->once())
942+
->method('get')
943+
->willReturn($folder);
944+
945+
$this->storage->expects($this->once())
946+
->method('getUserFolder')
947+
->with('admin')
948+
->willReturn($userFolder);
949+
950+
$this->apiController->updateSubmission(1, 12, $answers, '');
951+
}
952+
885953
public function testDeleteSubmissionNotFound() {
886954
$exception = $this->createMock(MapperException::class);
887955

0 commit comments

Comments
 (0)