From 19cd709fc595464d5577f6b5fd330d5cacb3f72f Mon Sep 17 00:00:00 2001 From: Timotheus Pokorra Date: Thu, 9 Jan 2025 20:26:52 +0100 Subject: [PATCH] add integration test testUpdateSubmission Signed-off-by: Timotheus Pokorra --- tests/Integration/Api/ApiV3Test.php | 82 +++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/tests/Integration/Api/ApiV3Test.php b/tests/Integration/Api/ApiV3Test.php index fed3fc2a7..46a53b142 100644 --- a/tests/Integration/Api/ApiV3Test.php +++ b/tests/Integration/Api/ApiV3Test.php @@ -41,6 +41,7 @@ private function setTestForms() { 'state' => 0, 'is_anonymous' => false, 'submit_multiple' => false, + 'allow_edit' => true, 'show_expiration' => false, 'last_updated' => 123456789, 'submission_message' => 'Back to website', @@ -164,6 +165,7 @@ private function setTestForms() { 'state' => 0, 'is_anonymous' => false, 'submit_multiple' => false, + 'allow_edit' => true, 'show_expiration' => false, 'last_updated' => 123456789, 'submission_message' => '', @@ -201,6 +203,7 @@ private function setTestForms() { 'state' => 0, 'is_anonymous' => false, 'submit_multiple' => false, + 'allow_edit' => true, 'show_expiration' => false, 'last_updated' => 123456789, 'submission_message' => '', @@ -1349,6 +1352,85 @@ public function testNewSubmission() { ], $data['submissions'][0]); } + /** + * @dataProvider dataNewSubmission + */ + public function testUpdateSubmission() { + + $uploadedFileResponse = $this->http->request('PUT', + "api/v3/forms/{$this->testForms[0]['id']}/submissions/files/{$this->testForms[0]['questions'][2]['id']}", + [ + 'multipart' => [ + [ + 'name' => 'files[]', + 'contents' => 'hello world2', + 'filename' => 'test2.txt' + ] + ] + ]); + + $data = $this->OcsResponse2Data($uploadedFileResponse); + $uploadedFileId = $data[0]['uploadedFileId']; + + $resp = $this->http->request('PUT', "api/v3/forms/{$this->testForms[0]['id']}/submissions/{$this->testForms[0]['submissions'][0]['id']}", [ + 'json' => [ + 'answers' => [ + $this->testForms[0]['questions'][0]['id'] => ['ShortAnswer!2'], + $this->testForms[0]['questions'][1]['id'] => [ + $this->testForms[0]['questions'][1]['options'][1]['id'] + ], + $this->testForms[0]['questions'][2]['id'] => [['uploadedFileId' => $uploadedFileId]] + ] + ] + ]); + $data = $this->OcsResponse2Data($resp); + + $this->assertEquals(200, $resp->getStatusCode()); + + // Check stored submissions + $resp = $this->http->request('GET', "api/v3/forms/{$this->testForms[0]['id']}/submissions"); + $data = $this->OcsResponse2Data($resp); + + // Check Ids + foreach ($data['submissions'][0]['answers'] as $aIndex => $answer) { + $this->assertEquals($data['submissions'][0]['id'], $answer['submissionId']); + unset($data['submissions'][0]['answers'][$aIndex]['id']); + unset($data['submissions'][0]['answers'][$aIndex]['submissionId']); + + if (isset($answer['fileId'])) { + $this->assertIsNumeric($answer['fileId'], 'fileId should be numeric.'); + $this->assertGreaterThan(0, $answer['fileId'], 'fileId should be greater than 0.'); + unset($data['submissions'][0]['answers'][$aIndex]['fileId']); + } + } + unset($data['submissions'][0]['id']); + // Check general behaviour of timestamp (Insert in the last 10 seconds) + $this->assertTrue(time() - $data['submissions'][0]['timestamp'] < 10); + unset($data['submissions'][0]['timestamp']); + + $this->assertEquals([ + 'userId' => 'test', + 'userDisplayName' => 'Test Displayname', + 'formId' => $this->testForms[0]['id'], + 'answers' => [ + [ + 'questionId' => $this->testForms[0]['questions'][0]['id'], + 'text' => 'ShortAnswer2!', + 'fileId' => null, + ], + [ + 'questionId' => $this->testForms[0]['questions'][1]['id'], + 'text' => 'Option 2', + 'fileId' => null, + ], + [ + 'questionId' => $this->testForms[0]['questions'][2]['id'], + 'text' => 'test2.txt', + ], + ] + ], $data['submissions'][0]); + } + public function dataDeleteSingleSubmission() { $submissionsExpected = $this->dataGetSubmissions()['getSubmissions']['expected']; array_splice($submissionsExpected['submissions'], 0, 1);