Skip to content

Commit

Permalink
Test case for export
Browse files Browse the repository at this point in the history
  • Loading branch information
chiragchhatrala committed Jan 6, 2025
1 parent 4654125 commit 25ad641
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions api/tests/Feature/Forms/FormSubmissionExportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

use App\Models\User;
use Laravel\Sanctum\Sanctum;
use Tests\Helpers\FormSubmissionDataFactory;

it('can export form submissions with selected columns', function () {
$user = $this->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' => '[email protected]'],
['name_field' => 'Jane Smith', 'email_field' => '[email protected]']
];

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.'
]);
});

0 comments on commit 25ad641

Please sign in to comment.