employee = User::factory()->create(['first_name' => 'Anna', 'last_name' => 'Schmidt', 'email' => 'anna@firma.de', 'department' => 'Pflege']); } private function trainingForm(array $settings = [], array $attributes = []): Form { return Form::query()->create([ 'title' => 'Antrag auf Fortbildung', 'fields' => [ ['type' => 'heading', 'data' => ['text' => 'Angaben zur Person']], ['type' => 'text', 'data' => ['label' => 'Name', 'required' => true, 'prefill' => 'name']], ['type' => 'select', 'data' => ['label' => 'Art', 'required' => true, 'options' => ['Seminar', 'Studium', 'Sonstiges']]], ['type' => 'textarea', 'data' => ['label' => 'Welche?', 'required' => true, 'condition_field' => 'art', 'condition_operator' => 'equals', 'condition_value' => 'Sonstiges']], ['type' => 'number', 'data' => ['label' => 'Kosten', 'min' => 0, 'max' => 5000]], ['type' => 'checkboxes', 'data' => ['label' => 'Tage', 'options' => ['Mo', 'Di', 'Mi']]], ['type' => 'file', 'data' => ['label' => 'Nachweis', 'accept' => 'documents', 'max_size' => 2]], ['type' => 'checkbox', 'data' => ['label' => 'Ich stimme der Verarbeitung zu', 'key' => 'zustimmung', 'required' => true]], ], 'settings' => array_merge(['notify_emails' => ['personal@firma.de']], $settings), ...$attributes, ]); } private function resultsRole(Form $form, bool $delete = false): Role { $role = Role::query()->create(['name' => 'Personalabteilung']); $role->grants()->create(['ability' => 'forms.results', 'resource_type' => 'form', 'resource_id' => $form->id]); if ($delete) { $role->grants()->create(['ability' => 'forms.results.delete', 'resource_type' => 'form', 'resource_id' => $form->id]); } return $role; } public function test_keys_are_generated_and_unique(): void { $form = Form::query()->create(['title' => 'Test', 'fields' => [ ['type' => 'text', 'data' => ['label' => 'Straße & Nr.']], ['type' => 'text', 'data' => ['label' => 'Straße & Nr.']], ['type' => 'unbekannt', 'data' => ['label' => 'x']], ]]); $this->assertSame(['strasse_nr', 'strasse_nr_2'], array_column(array_column($form->fields, 'data'), 'key')); $this->assertSame('test', $form->slug); } public function test_employee_fills_in_the_form_and_hr_is_notified(): void { $form = $this->trainingForm(['notify_viewers' => true, 'send_copy' => true]); $hr = User::factory()->withRoles([$this->resultsRole($form)])->create(); $this->actingAs($this->employee)->get('/formulare/antrag-auf-fortbildung')->assertOk()->assertInertia(fn (Assert $p) => $p ->component('forms::Show') ->where('form.prefill.name', 'Anna Schmidt') ->where('form.fields.3.condition', ['field' => 'art', 'operator' => 'equals', 'value' => 'Sonstiges'])); // Pflichtfelder, Auswahl nur aus den Optionen, Bedingung greift. $this->post('/formulare/antrag-auf-fortbildung', ['answers' => ['name' => 'Anna Schmidt', 'art' => 'Sonstiges', 'kosten' => 9999]]) ->assertSessionHasErrors(['answers.welche', 'answers.kosten', 'answers.zustimmung']); $this->post('/formulare/antrag-auf-fortbildung', ['answers' => ['name' => 'Anna', 'art' => 'Erfunden', 'zustimmung' => '1']]) ->assertSessionHasErrors(['answers.art']); $this->from('/formulare/antrag-auf-fortbildung')->post('/formulare/antrag-auf-fortbildung', ['answers' => [ 'name' => 'Anna Schmidt', 'art' => 'Seminar', 'welche' => 'wird ignoriert, weil ausgeblendet', 'kosten' => '450', 'tage' => ['Mo', 'Mi'], 'nachweis' => [UploadedFile::fake()->create('Angebot.pdf', 100, 'application/pdf')], 'zustimmung' => '1', ]])->assertRedirect('/formulare/antrag-auf-fortbildung')->assertSessionHas('form_submitted', $form->id); $submission = FormSubmission::query()->sole(); $this->assertSame($this->employee->id, $submission->user_id); $this->assertArrayNotHasKey('welche', $submission->data); $this->assertSame('Mo, Mi', $submission->display('tage')); $this->assertSame('Ja', $submission->display('zustimmung')); $this->assertSame('Angebot.pdf', $submission->display('nachweis')); Storage::disk('local')->assertExists($submission->files->sole()->path); Mail::assertQueued(FormSubmitted::class, fn (FormSubmitted $m) => $m->hasTo('personal@firma.de') && ! $m->copy && $m->hasReplyTo('anna@firma.de')); Mail::assertQueued(FormSubmitted::class, fn (FormSubmitted $m) => $m->hasTo($hr->email) && ! $m->copy); Mail::assertQueued(FormSubmitted::class, fn (FormSubmitted $m) => $m->hasTo('anna@firma.de') && $m->copy); Notification::assertSentTo($hr, PortalNotification::class); $html = (new FormSubmitted($form, $submission, $this->employee, false))->render(); $this->assertStringContainsString('Anna Schmidt (anna@firma.de)', $html); $this->assertStringContainsString('Mo, Mi', $html); } public function test_results_are_only_visible_to_roles_granted_for_this_form(): void { $training = $this->trainingForm(); $other = Form::query()->create(['title' => 'Meldung an die MAV', 'fields' => [['type' => 'text', 'data' => ['label' => 'Anliegen']]]]); $training->submissions()->create(['user_id' => $this->employee->id, 'data' => ['name' => ['label' => 'Name', 'type' => 'text', 'value' => 'Anna Schmidt']]]); $other->submissions()->create(['data' => ['anliegen' => ['label' => 'Anliegen', 'type' => 'text', 'value' => 'Vertraulich']]]); $hr = User::factory()->withRoles([$this->resultsRole($training)])->create(); $this->actingAs($hr)->get('/admin/formulare')->assertOk(); Livewire::test(ListForms::class)->assertCanSeeTableRecords([$training])->assertCanNotSeeTableRecords([$other]); $this->get('/admin/formulare/'.$training->id.'/ergebnisse')->assertOk()->assertSee('Anna Schmidt'); $this->get('/admin/formulare/'.$other->id.'/ergebnisse')->assertNotFound(); $this->get('/admin/formulare/'.$training->id.'/bearbeiten')->assertForbidden(); $this->get('/admin/formulare/'.$other->id.'/export')->assertForbidden(); $csv = $this->get('/admin/formulare/'.$training->id.'/export')->assertOk()->streamedContent(); $this->assertStringStartsWith("\xEF\xBB\xBF", $csv); $this->assertStringContainsString('Anna Schmidt', $csv); // Ohne Löschrecht kein Löschen; ansehen markiert als gelesen. $submission = $training->submissions()->sole(); Livewire::test(FormResults::class, ['record' => $training->id]) ->assertTableActionHidden('delete', $submission) ->callTableAction('view', $submission); $this->assertNotNull($submission->fresh()->read_at); $this->actingAs($this->employee)->get('/admin/formulare/'.$training->id.'/ergebnisse')->assertForbidden(); } public function test_uploaded_files_are_only_served_to_result_viewers(): void { $form = $this->trainingForm(); $this->actingAs($this->employee)->post('/formulare/antrag-auf-fortbildung', ['answers' => [ 'name' => 'Anna', 'art' => 'Seminar', 'zustimmung' => '1', 'nachweis' => [UploadedFile::fake()->create('Zeugnis.pdf', 50, 'application/pdf')], ]])->assertSessionHasNoErrors(); $file = FormSubmission::query()->sole()->files->sole(); $this->get($file->url())->assertForbidden(); $this->actingAs(User::factory()->withRoles([$this->resultsRole($form)])->create())->get($file->url())->assertOk()->assertDownload('Zeugnis.pdf'); $this->actingAs($this->employee)->post('/formulare/antrag-auf-fortbildung', ['answers' => [ 'name' => 'Anna', 'art' => 'Seminar', 'zustimmung' => '1', 'nachweis' => [UploadedFile::fake()->create('virus.php', 1)], ]])->assertSessionHasErrors('answers.nachweis.0'); } public function test_anonymous_forms_store_no_identity_but_allow_only_one_submission(): void { $form = Form::query()->create([ 'title' => 'Mitarbeiterbefragung', 'fields' => [['type' => 'radio', 'data' => ['label' => 'Zufriedenheit', 'required' => true, 'options' => ['gut', 'mittel', 'schlecht']]]], 'settings' => ['anonymous' => true, 'once_per_user' => true, 'notify_emails' => ['mav@firma.de']], ]); $this->actingAs($this->employee)->post($form->url(), ['answers' => ['zufriedenheit' => 'gut']])->assertSessionHasNoErrors(); $this->post($form->url(), ['answers' => ['zufriedenheit' => 'schlecht']])->assertSessionHasErrors('form'); $submission = FormSubmission::query()->sole(); $this->assertNull($submission->user_id); $this->assertNotNull($submission->user_hash); Mail::assertQueued(FormSubmitted::class, fn (FormSubmitted $m) => $m->person === null); $this->get($form->url())->assertInertia(fn (Assert $p) => $p->where('form.already_submitted', true)->where('form.anonymous', true)); } public function test_audience_and_deadline_limit_who_can_submit(): void { $form = $this->trainingForm(attributes: ['closes_at' => now()->addDay()]); Audiences::sync('form', $form->id, ['department' => ['Verwaltung']]); $this->actingAs($this->employee)->get($form->url())->assertNotFound(); $this->post($form->url(), ['answers' => []])->assertNotFound(); $this->get('/formulare')->assertInertia(fn (Assert $p) => $p->where('forms', [])); Audiences::sync('form', $form->id, []); $this->travel(2)->days(); $this->post($form->url(), ['answers' => ['name' => 'x', 'art' => 'Seminar', 'zustimmung' => '1']])->assertSessionHasErrors('form'); $this->get('/formulare')->assertInertia(fn (Assert $p) => $p->where('forms', [])); } public function test_confidential_forms_leave_answers_out_of_the_mail(): void { $form = $this->trainingForm(['mail_answers' => false]); $submission = $form->submissions()->create(['user_id' => $this->employee->id, 'data' => ['name' => ['label' => 'Name', 'type' => 'text', 'value' => 'Geheim']]]); $html = (new FormSubmitted($form, $submission, $this->employee, false))->render(); $this->assertStringNotContainsString('Geheim', $html); $this->assertStringContainsString('/admin/formulare/'.$form->id.'/ergebnisse', $html); } public function test_retention_deletes_old_submissions_with_files(): void { $form = $this->trainingForm(['retention_days' => 30]); $old = $form->submissions()->create(['data' => []]); $old->forceFill(['created_at' => now()->subDays(40)])->save(); Storage::disk('local')->put('forms/'.$form->id.'/alt.pdf', 'x'); $old->files()->create(['field' => 'nachweis', 'name' => 'alt.pdf', 'path' => 'forms/'.$form->id.'/alt.pdf']); $form->submissions()->create(['data' => []]); $this->assertSame(1, app(Retention::class)->purge()); $this->assertSame(1, $form->submissions()->count()); Storage::disk('local')->assertMissing('forms/'.$form->id.'/alt.pdf'); } public function test_admin_creates_a_form_and_edits_fields_in_the_visual_builder(): void { $this->actingAs(User::factory()->admin()->create()); $hr = Role::query()->create(['name' => 'Personal']); Livewire::test(CreateForm::class) ->fillForm([ 'title' => 'Urlaubsantrag', 'settings' => ['notify_emails' => ['personal@firma.de'], 'mail_answers' => true], 'result_viewers' => [$hr->id], ]) ->call('create') ->assertHasNoFormErrors() ->assertRedirect(route('forms.builder', Form::query()->sole())); $form = Form::query()->sole(); $this->assertSame(['personal@firma.de'], $form->setting('notify_emails')); $this->assertTrue(User::factory()->withRoles([$hr])->create()->can('viewResults', $form)); $this->get(route('forms.builder', $form))->assertOk()->assertInertia(fn (Assert $p) => $p ->component('forms::Builder') ->where('form.title', 'Urlaubsantrag') ->has('types', 13)); $fields = [ ['type' => 'date', 'data' => ['label' => 'Von', 'required' => true, 'width' => 'half', 'evil' => '