diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index ebc411fd1..6b54111fa 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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 */ diff --git a/tests/Unit/Controller/ApiControllerTest.php b/tests/Unit/Controller/ApiControllerTest.php index dbd0952cd..92a6096d7 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -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)); } @@ -1041,7 +1045,8 @@ public static function dataTestDeletePermission() { 'expires' => 0, 'isAnonymous' => false, 'submitMultiple' => false, - 'showExpiration' => false + 'showExpiration' => false, + 'fileId' => 100, ], ] ]; @@ -1107,6 +1112,7 @@ public function testDeleteAllSubmissionsAsOwner(): void { 'isAnonymous' => false, 'submitMultiple' => false, 'showExpiration' => false, + 'fileId' => 100, ]); $this->formsService @@ -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)); } @@ -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)); } @@ -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); @@ -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); } @@ -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);