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: 17 additions & 3 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,8 @@ public function deleteAllSubmissions(int $formId): DataResponse {
$this->submissionMapper->deleteByForm($formId);
$this->formMapper->update($form);

$this->queueLinkedFileSync($form);

return new DataResponse($formId);
}

Expand Down Expand Up @@ -1486,9 +1488,7 @@ public function newSubmission(int $formId, array $answers, string $shareHash = '
//Create Activity
$this->formsService->notifyNewSubmission($form, $submission);

if ($form->getFileId() !== null) {
$this->jobList->add(SyncSubmissionsWithLinkedFileJob::class, ['form_id' => $form->getId()]);
}
$this->queueLinkedFileSync($form);

return new DataResponse(null, Http::STATUS_CREATED);
}
Expand Down Expand Up @@ -1564,6 +1564,8 @@ public function updateSubmission(int $formId, int $submissionId, array $answers)
//Create Activity
$this->formsService->notifyNewSubmission($form, $submission);

$this->queueLinkedFileSync($form);

return new DataResponse($submissionId);
}

Expand Down Expand Up @@ -1612,6 +1614,8 @@ public function deleteSubmission(int $formId, int $submissionId): DataResponse {
$this->submissionMapper->deleteById($form, $submissionId);
$this->formMapper->update($form);

$this->queueLinkedFileSync($form);

return new DataResponse($submissionId);
}

Expand Down Expand Up @@ -1877,6 +1881,16 @@ private function storeAnswersForQuestion(Form $form, $submissionId, array $quest
}
}

/**
* Schedule a background job to re-export submissions into the linked file,
* if the form is linked to one.
*/
private function queueLinkedFileSync(Form $form): void {
if ($form->getFileId() !== null) {
$this->jobList->add(SyncSubmissionsWithLinkedFileJob::class, ['form_id' => $form->getId()]);
}
}

/**
* Throws if forbidden keys are present in update
*/
Expand Down
25 changes: 24 additions & 1 deletion tests/Unit/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,10 @@ public function testDeleteSubmission($submissionData, $formData) {
->method('deleteById')
->with($form, 42);

$this->jobList->expects($this->once())
->method('add')
->with(SyncSubmissionsWithLinkedFileJob::class, ['form_id' => 1]);

$this->assertEquals(new DataResponse(42), $this->apiController->deleteSubmission(1, 42));
}

Expand All @@ -1041,7 +1045,8 @@ public static function dataTestDeletePermission() {
'expires' => 0,
'isAnonymous' => false,
'submitMultiple' => false,
'showExpiration' => false
'showExpiration' => false,
'fileId' => 100,
],
]
];
Expand Down Expand Up @@ -1107,6 +1112,7 @@ public function testDeleteAllSubmissionsAsOwner(): void {
'isAnonymous' => false,
'submitMultiple' => false,
'showExpiration' => false,
'fileId' => 100,
]);

$this->formsService
Expand All @@ -1124,6 +1130,10 @@ public function testDeleteAllSubmissionsAsOwner(): void {
->method('deleteByForm')
->with(1);

$this->jobList->expects($this->once())
->method('add')
->with(SyncSubmissionsWithLinkedFileJob::class, ['form_id' => 1]);

$this->assertEquals(new DataResponse(1), $this->apiController->deleteAllSubmissions(1));
}

Expand Down Expand Up @@ -1162,6 +1172,10 @@ public function testDeleteAllSubmissionsWithResultsDeletePermission(): void {
->method('deleteByForm')
->with(1);

// Form is not linked to a file, so no sync job should be queued
$this->jobList->expects($this->never())
->method('add');

$this->assertEquals(new DataResponse(1), $this->apiController->deleteAllSubmissions(1));
}

Expand Down Expand Up @@ -1640,6 +1654,7 @@ public function testUpdateSubmission_success() {
$form->setId($formId);
$form->setOwnerId('formOwner');
$form->setAllowEditSubmissions(true);
$form->setFileId(100);

$submission = new Submission();
$submission->setId($submissionId);
Expand Down Expand Up @@ -1686,6 +1701,10 @@ public function testUpdateSubmission_success() {
->method('notifyNewSubmission')
->with($form, $submission);

$this->jobList->expects($this->once())
->method('add')
->with(SyncSubmissionsWithLinkedFileJob::class, ['form_id' => $formId]);

$response = $this->apiController->updateSubmission($formId, $submissionId, $answers);
$this->assertEquals(new DataResponse($submissionId), $response);
}
Expand Down Expand Up @@ -2059,6 +2078,10 @@ public function testUpdateSubmission_ownSubmissionWithAllowEditSubmissions() {
->method('notifyNewSubmission')
->with($form, $submission);

// Form is not linked to a file, so no sync job should be queued
$this->jobList->expects($this->never())
->method('add');

// Should succeed - user is owner and allowEditSubmissions=true
$response = $this->apiController->updateSubmission($formId, $submissionId, $answers);
$this->assertEquals(new DataResponse($submissionId), $response);
Expand Down
Loading