From 25ad6416a680b1405cab7c7d393b970a69ba3dc7 Mon Sep 17 00:00:00 2001 From: Chirag Chhatrala Date: Mon, 6 Jan 2025 19:33:56 +0530 Subject: [PATCH] Test case for export --- .../Forms/FormSubmissionExportTest.php | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 api/tests/Feature/Forms/FormSubmissionExportTest.php diff --git a/api/tests/Feature/Forms/FormSubmissionExportTest.php b/api/tests/Feature/Forms/FormSubmissionExportTest.php new file mode 100644 index 00000000..c2f1241a --- /dev/null +++ b/api/tests/Feature/Forms/FormSubmissionExportTest.php @@ -0,0 +1,98 @@ +actingAsProUser(); + $workspace = $this->createUserWorkspace($user); + $form = $this->createForm($user, $workspace, [ + 'properties' => [ + [ + 'id' => 'name_field', + 'name' => 'Name', + 'type' => 'text', + 'required' => true, + ], + [ + 'id' => 'email_field', + 'name' => 'Email', + 'type' => 'email', + 'required' => true, + ] + ] + ]); + + // Create some submissions + $submissions = [ + ['name_field' => 'John Doe', 'email_field' => 'john@example.com'], + ['name_field' => 'Jane Smith', 'email_field' => 'jane@example.com'] + ]; + + foreach ($submissions as $submission) { + $formData = FormSubmissionDataFactory::generateSubmissionData($form, $submission); + $this->postJson(route('forms.answer', $form->slug), $formData) + ->assertSuccessful(); + } + + // Test export with selected columns + $response = $this->postJson(route('open.forms.submissions.export', [ + 'id' => $form->id, + 'columns' => [ + 'name_field' => true, + 'email_field' => true, + 'created_at' => true + ] + ])); + + $response->assertSuccessful() + ->assertHeader('content-disposition', 'attachment; filename=' . $form->slug . '-submission-data.csv'); +}); + +it('cannot export form submissions with invalid columns', function () { + $user = $this->actingAsProUser(); + $workspace = $this->createUserWorkspace($user); + $form = $this->createForm($user, $workspace, [ + 'properties' => [ + [ + 'id' => 'name_field', + 'name' => 'Name', + 'type' => 'text', + 'required' => true, + ] + ] + ]); + + $response = $this->postJson(route('open.forms.submissions.export', [ + 'id' => $form->id, + 'columns' => [ + 'invalid_field' => true, + 'name_field' => true + ] + ])); + + $response->assertStatus(422) + ->assertJsonValidationErrors(['columns']); +}); + +it('cannot export form submissions from another user form', function () { + $user = User::factory()->create(); + $user2 = User::factory()->create(); + $workspace = createUserWorkspace($user2); + + $form = createForm($user, $workspace); + + Sanctum::actingAs($user2); + + $response = $this->postJson(route('open.forms.submissions.export', [ + 'id' => $form->id, + 'columns' => [ + 'name_field' => true + ] + ])); + + $response->assertJson([ + 'message' => 'Unauthenticated.' + ]); +});