opnform/tests/Feature/Submissions/EditSubmissionTest.php

60 lines
2.6 KiB
PHP
Raw Permalink Normal View History

<?php
use Tests\Helpers\FormSubmissionDataFactory;
it('can update form submission', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->makeForm($user, $workspace);
$form = $this->createForm($user, $workspace, [
'closes_at' => \Carbon\Carbon::now()->addDays(1)->toDateTimeString(),
]);
2024-02-23 10:54:12 +00:00
$formData = FormSubmissionDataFactory::generateSubmissionData($form, ['text' => 'John']);
$textFieldId = array_keys($formData)[0];
$updatedFormData = $formData;
2024-02-23 10:54:12 +00:00
$updatedFormTextValue = 'Updated text';
$updatedFormData[$textFieldId] = $updatedFormTextValue;
$this->postJson(route('forms.answer', $form->slug), $formData)
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 10:54:12 +00:00
'message' => 'Form submission saved.',
]);
$submission = $form->submissions()->first();
2024-02-23 10:54:12 +00:00
$updateResponse = $this->putJson(route('open.forms.submissions.update', ['id' => $form->id, 'submission_id' => $submission->id]), $updatedFormData)
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 10:54:12 +00:00
'message' => 'Record successfully updated.',
]);
$expectedTextString = $updateResponse->json('data')['data'][$textFieldId];
expect($expectedTextString)->toBe($updatedFormTextValue);
$updatedSubmission = $form->submissions()->first();
expect($updatedSubmission->data[$textFieldId])->toBe($updatedFormTextValue);
});
it('cannot update form submission as non admin', function () {
2024-02-23 10:54:12 +00:00
$secondUser = $this->createUser();
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->makeForm($user, $workspace);
$form = $this->createForm($user, $workspace, [
'closes_at' => \Carbon\Carbon::now()->addDays(1)->toDateTimeString(),
]);
2024-02-23 10:54:12 +00:00
$formData = FormSubmissionDataFactory::generateSubmissionData($form, ['text' => 'John']);
$textFieldId = array_keys($formData)[0];
$updatedFormData = $formData;
2024-02-23 10:54:12 +00:00
$updatedFormTextValue = 'Updated text';
$updatedFormData[$textFieldId] = $updatedFormTextValue;
$this->postJson(route('forms.answer', $form->slug), $formData)
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 10:54:12 +00:00
'message' => 'Form submission saved.',
]);
$submission = $form->submissions()->first();
$this->actingAs($secondUser);
2024-02-23 10:54:12 +00:00
$updateResponse = $this->putJson(route('open.forms.submissions.update', ['id' => $form->id, 'submission_id' => $submission->id]), $updatedFormData)
->assertStatus(403);
});