-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4654125
commit 25ad641
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' | ||
]); | ||
}); |