opnform/tests/Feature/Forms/ConfirmationEmailTest.php

141 lines
5.0 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
use App\Mail\Forms\SubmissionConfirmationMail;
2024-02-23 10:54:12 +00:00
use Illuminate\Support\Facades\Mail;
2022-09-20 19:59:52 +00:00
it('creates confirmation emails with the submitted data', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, [
'send_submission_confirmation' => true,
'notifications_include_submission' => true,
'notification_sender' => 'Custom Sender',
'notification_subject' => 'Test subject',
'notification_body' => 'Test body',
]);
$formData = [
collect($form->properties)->first(function ($property) {
return $property['type'] == 'email';
2024-02-23 10:54:12 +00:00
})['id'] => 'test@test.com',
2022-09-20 19:59:52 +00:00
];
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
$mailable = new SubmissionConfirmationMail($event);
$mailable->assertSeeInHtml('Test body')
->assertSeeInHtml('As a reminder, here are your answers:')
->assertSeeInHtml('You are receiving this email because you answered the form:');
});
it('creates confirmation emails without the submitted data', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, [
'send_submission_confirmation' => true,
'notifications_include_submission' => false,
'notification_sender' => 'Custom Sender',
'notification_subject' => 'Test subject',
'notification_body' => 'Test body',
]);
$formData = [
collect($form->properties)->first(function ($property) {
return $property['type'] == 'email';
2024-02-23 10:54:12 +00:00
})['id'] => 'test@test.com',
2022-09-20 19:59:52 +00:00
];
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
$mailable = new SubmissionConfirmationMail($event);
$mailable->assertSeeInHtml('Test body')
->assertDontSeeInHtml('As a reminder, here are your answers:')
->assertSeeInHtml('You are receiving this email because you answered the form:');
});
it('sends a confirmation email if needed', function () {
$user = $this->actingAsProUser();
2022-09-20 19:59:52 +00:00
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, [
'send_submission_confirmation' => true,
'notifications_include_submission' => true,
'notification_subject' => 'Test subject',
]);
$emailProperty = collect($form->properties)->first(function ($property) {
return $property['type'] == 'email';
});
$formData = [
2024-02-23 10:54:12 +00:00
$emailProperty['id'] => 'test@test.com',
2022-09-20 19:59:52 +00:00
];
Mail::fake();
$this->postJson(route('forms.answer', $form->slug), $formData)
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 10:54:12 +00:00
'message' => 'Form submission saved.',
2022-09-20 19:59:52 +00:00
]);
2024-02-23 10:54:12 +00:00
Mail::assertQueued(
SubmissionConfirmationMail::class,
function (SubmissionConfirmationMail $mail) {
return $mail->hasTo('test@test.com');
}
);
2022-09-20 19:59:52 +00:00
});
it('does not send a confirmation email if not needed', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, [
'send_submission_confirmation' => false,
]);
$emailProperty = collect($form->properties)->first(function ($property) {
return $property['type'] == 'email';
});
$formData = [
2024-02-23 10:54:12 +00:00
$emailProperty['id'] => 'test@test.com',
2022-09-20 19:59:52 +00:00
];
Mail::fake();
$this->postJson(route('forms.answer', $form->slug), $formData)
->assertSuccessful()
->assertJson([
'type' => 'success',
2024-02-23 10:54:12 +00:00
'message' => 'Form submission saved.',
2022-09-20 19:59:52 +00:00
]);
2024-02-23 10:54:12 +00:00
Mail::assertNotQueued(
SubmissionConfirmationMail::class,
function (SubmissionConfirmationMail $mail) {
return $mail->hasTo('test@test.com');
}
);
2022-09-20 19:59:52 +00:00
});
it('does send a confirmation email even when reply to is broken', function () {
$user = $this->actingAsProUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace, [
'send_submission_confirmation' => true,
'notifications_include_submission' => true,
'notification_sender' => 'Custom Sender',
'notification_subject' => 'Test subject',
'notification_body' => 'Test body',
'notification_settings' => [
'confirmation_reply_to' => 'invalid-email',
]
]);
$emailProperty = collect($form->properties)->first(function ($property) {
return $property['type'] == 'email';
});
$formData = [
$emailProperty['id'] => 'test@test.com',
];
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
$mailable = new SubmissionConfirmationMail($event);
$mailable->assertSeeInHtml('Test body')
->assertSeeInHtml('As a reminder, here are your answers:')
->assertSeeInHtml('You are receiving this email because you answered the form:')
->assertHasReplyTo($user->email); // Even though reply to is wrong, it should use the user's email
});