Apply Mentions everywhere (#595)

* variables and mentions

* fix lint

* add missing changes

* fix tests

* update quilly, fix bugs

* fix lint

* apply fixes

* apply fixes

* Fix MentionParser

* Apply Mentions everywhere

* Fix MentionParserTest

* Small refactoring

* Fixing quill import issues

* Polished email integration, added customer sender mail

* Add missing changes

* improve migration command

---------

Co-authored-by: Frank <csskfaves@gmail.com>
Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
Chirag Chhatrala
2024-10-22 14:04:29 +05:30
committed by GitHub
parent 2fdf2a439b
commit dad5c825b1
50 changed files with 1903 additions and 874 deletions

View File

@@ -1,144 +0,0 @@
<?php
use App\Mail\Forms\SubmissionConfirmationMail;
use Illuminate\Support\Facades\Mail;
it('creates confirmation emails with the submitted data', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$integrationData = $this->createFormIntegration('submission_confirmation', $form->id, [
'respondent_email' => 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';
})['id'] => 'test@test.com',
];
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
$mailable = new SubmissionConfirmationMail($event, $integrationData);
$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);
$integrationData = $this->createFormIntegration('submission_confirmation', $form->id, [
'respondent_email' => 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';
})['id'] => 'test@test.com',
];
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
$mailable = new SubmissionConfirmationMail($event, $integrationData);
$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();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$this->createFormIntegration('submission_confirmation', $form->id, [
'respondent_email' => true,
'notifications_include_submission' => true,
'notification_sender' => 'Custom Sender',
'notification_subject' => 'Test subject',
'notification_body' => 'Test body',
]);
$emailProperty = collect($form->properties)->first(function ($property) {
return $property['type'] == 'email';
});
$formData = [
$emailProperty['id'] => 'test@test.com',
];
Mail::fake();
$this->postJson(route('forms.answer', $form->slug), $formData)
->assertSuccessful()
->assertJson([
'type' => 'success',
'message' => 'Form submission saved.',
]);
Mail::assertQueued(
SubmissionConfirmationMail::class,
function (SubmissionConfirmationMail $mail) {
return $mail->hasTo('test@test.com');
}
);
});
it('does not send a confirmation email if not needed', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$emailProperty = collect($form->properties)->first(function ($property) {
return $property['type'] == 'email';
});
$formData = [
$emailProperty['id'] => 'test@test.com',
];
Mail::fake();
$this->postJson(route('forms.answer', $form->slug), $formData)
->assertSuccessful()
->assertJson([
'type' => 'success',
'message' => 'Form submission saved.',
]);
Mail::assertNotQueued(
SubmissionConfirmationMail::class,
function (SubmissionConfirmationMail $mail) {
return $mail->hasTo('test@test.com');
}
);
});
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);
$integrationData = $this->createFormIntegration('submission_confirmation', $form->id, [
'respondent_email' => true,
'notifications_include_submission' => true,
'notification_sender' => 'Custom Sender',
'notification_subject' => 'Test subject',
'notification_body' => 'Test body',
'confirmation_reply_to' => ''
]);
$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, $integrationData);
$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
});

View File

@@ -1,7 +1,8 @@
<?php
use App\Mail\Forms\SubmissionConfirmationMail;
use Illuminate\Support\Facades\Mail;
use App\Notifications\Forms\FormEmailNotification;
use Tests\Helpers\FormSubmissionDataFactory;
use Illuminate\Notifications\AnonymousNotifiable;
it('can not save custom SMTP settings if not pro user', function () {
$user = $this->actingAsUser();
@@ -15,7 +16,7 @@ it('can not save custom SMTP settings if not pro user', function () {
])->assertStatus(403);
});
it('creates confirmation emails with custom SMTP settings', function () {
it('send email with custom SMTP settings', function () {
$user = $this->actingAsProUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
@@ -28,21 +29,19 @@ it('creates confirmation emails with custom SMTP settings', function () {
'password' => 'custom_password',
])->assertSuccessful();
$integrationData = $this->createFormIntegration('submission_confirmation', $form->id, [
'respondent_email' => true,
'notifications_include_submission' => true,
'notification_sender' => 'Custom Sender',
'notification_subject' => 'Custom SMTP Test',
'notification_body' => 'This email was sent using custom SMTP settings',
$integrationData = $this->createFormIntegration('email', $form->id, [
'send_to' => 'test@test.com',
'sender_name' => 'OpnForm',
'subject' => 'New form submission',
'email_content' => 'Hello there 👋 <br>New form submission received.',
'include_submission_data' => true,
'include_hidden_fields_submission_data' => false,
'reply_to' => 'reply@example.com',
]);
$formData = [
collect($form->properties)->first(function ($property) {
return $property['type'] == 'email';
})['id'] => 'test@test.com',
];
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
Mail::fake();
Notification::fake();
$this->postJson(route('forms.answer', $form->slug), $formData)
->assertSuccessful()
@@ -51,10 +50,12 @@ it('creates confirmation emails with custom SMTP settings', function () {
'message' => 'Form submission saved.',
]);
Mail::assertQueued(
SubmissionConfirmationMail::class,
function (SubmissionConfirmationMail $mail) {
return $mail->hasTo('test@test.com') && $mail->mailer === 'custom_smtp';
Notification::assertSentTo(
new AnonymousNotifiable(),
FormEmailNotification::class,
function (FormEmailNotification $notification, $channels, $notifiable) {
return $notifiable->routes['mail'] === 'test@test.com' &&
$notification->mailer === 'custom_smtp';
}
);
});

View File

@@ -0,0 +1,165 @@
<?php
use App\Notifications\Forms\FormEmailNotification;
use Tests\Helpers\FormSubmissionDataFactory;
use Illuminate\Notifications\AnonymousNotifiable;
it('send email with the submitted data', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$integrationData = $this->createFormIntegration('email', $form->id, [
'send_to' => 'test@test.com',
'sender_name' => 'OpnForm',
'subject' => 'New form submission',
'email_content' => 'Hello there 👋 <br>Test body',
'include_submission_data' => true,
'include_hidden_fields_submission_data' => false,
'reply_to' => 'reply@example.com',
]);
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
$mailable = new FormEmailNotification($event, $integrationData, 'mail');
$notifiable = new AnonymousNotifiable();
$notifiable->route('mail', 'test@test.com');
$renderedMail = $mailable->toMail($notifiable);
expect($renderedMail->subject)->toBe('New form submission');
expect(trim($renderedMail->render()))->toContain('Test body');
});
it('sends a email if needed', function () {
$user = $this->actingAsProUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$emailProperty = collect($form->properties)->first(function ($property) {
return $property['type'] == 'email';
});
$this->createFormIntegration('email', $form->id, [
'send_to' => '<span mention-field-id="' . $emailProperty['id'] . '" mention-field-name="' . $emailProperty['name'] . '" mention-fallback="" contenteditable="false" mention="true">' . $emailProperty['name'] . '</span>',
'sender_name' => 'OpnForm',
'subject' => 'New form submission',
'email_content' => 'Hello there 👋 <br>New form submission received.',
'include_submission_data' => true,
'include_hidden_fields_submission_data' => false,
'reply_to' => 'reply@example.com',
]);
$formData = [
$emailProperty['id'] => 'test@test.com',
];
Notification::fake();
$this->postJson(route('forms.answer', $form->slug), $formData)
->assertSuccessful()
->assertJson([
'type' => 'success',
'message' => 'Form submission saved.',
]);
Notification::assertSentTo(
new AnonymousNotifiable(),
FormEmailNotification::class,
function (FormEmailNotification $notification, $channels, $notifiable) {
return $notifiable->routes['mail'] === 'test@test.com';
}
);
});
it('does not send a email if not needed', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$emailProperty = collect($form->properties)->first(function ($property) {
return $property['type'] == 'email';
});
$formData = [
$emailProperty['id'] => 'test@test.com',
];
Notification::fake();
$this->postJson(route('forms.answer', $form->slug), $formData)
->assertSuccessful()
->assertJson([
'type' => 'success',
'message' => 'Form submission saved.',
]);
Notification::assertNotSentTo(
new AnonymousNotifiable(),
FormEmailNotification::class,
function (FormEmailNotification $notification, $channels, $notifiable) {
return $notifiable->routes['mail'] === 'test@test.com';
}
);
});
it('uses custom sender email in self-hosted mode', function () {
config(['app.self_hosted' => true]);
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$customSenderEmail = 'custom@example.com';
$integrationData = $this->createFormIntegration('email', $form->id, [
'send_to' => 'test@test.com',
'sender_name' => 'Custom Sender',
'sender_email' => $customSenderEmail,
'subject' => 'Custom Subject',
'email_content' => 'Custom content',
'include_submission_data' => true,
'include_hidden_fields_submission_data' => false,
'reply_to' => 'reply@example.com',
]);
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
$mailable = new FormEmailNotification($event, $integrationData, 'mail');
$notifiable = new AnonymousNotifiable();
$notifiable->route('mail', 'test@test.com');
$renderedMail = $mailable->toMail($notifiable);
expect($renderedMail->from[0])->toBe($customSenderEmail);
expect($renderedMail->from[1])->toBe('Custom Sender');
expect($renderedMail->subject)->toBe('Custom Subject');
expect(trim($renderedMail->render()))->toContain('Custom content');
});
it('does not use custom sender email in non-self-hosted mode', function () {
config(['app.self_hosted' => false]);
config(['mail.from.address' => 'default@example.com']);
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$customSenderEmail = 'custom@example.com';
$integrationData = $this->createFormIntegration('email', $form->id, [
'send_to' => 'test@test.com',
'sender_name' => 'Custom Sender',
'sender_email' => $customSenderEmail,
'subject' => 'Custom Subject',
'email_content' => 'Custom content',
'include_submission_data' => true,
'include_hidden_fields_submission_data' => false,
'reply_to' => 'reply@example.com',
]);
$formData = FormSubmissionDataFactory::generateSubmissionData($form);
$event = new \App\Events\Forms\FormSubmitted($form, $formData);
$mailable = new FormEmailNotification($event, $integrationData, 'mail');
$notifiable = new AnonymousNotifiable();
$notifiable->route('mail', 'test@test.com');
$renderedMail = $mailable->toMail($notifiable);
expect($renderedMail->from[0])->toBe('default@example.com');
expect($renderedMail->from[1])->toBe('Custom Sender');
expect($renderedMail->subject)->toBe('Custom Subject');
expect(trim($renderedMail->render()))->toContain('Custom content');
});

View File

@@ -10,8 +10,13 @@ it('can fetch form integration events', function () {
'integration_id' => 'email',
'logic' => null,
'settings' => [
'notification_emails' => 'test@test.com',
'notification_reply_to' => null
'send_to' => 'test@test.com',
'sender_name' => 'OpnForm',
'subject' => 'New form submission',
'email_content' => 'Hello there 👋 <br>New form submission received.',
'include_submission_data' => true,
'include_hidden_fields_submission_data' => false,
'reply_to' => null
]
];

View File

@@ -10,8 +10,13 @@ it('can CRUD form integration', function () {
'integration_id' => 'email',
'logic' => null,
'settings' => [
'notification_emails' => 'test@test.com',
'notification_reply_to' => null
'send_to' => 'test@test.com',
'sender_name' => 'OpnForm',
'subject' => 'New form submission',
'email_content' => 'Hello there 👋 <br>New form submission received.',
'include_submission_data' => true,
'include_hidden_fields_submission_data' => false,
'reply_to' => null
]
];

View File

@@ -0,0 +1,80 @@
<?php
namespace Tests\Unit;
use App\Console\Commands\EmailNotificationMigration;
use App\Models\Integration\FormIntegration;
use Tests\TestCase;
uses(TestCase::class);
beforeEach(function () {
$this->command = new EmailNotificationMigration();
});
it('updates email integration correctly', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$integration = FormIntegration::create([
'integration_id' => 'email',
'form_id' => $form->id,
'status' => FormIntegration::STATUS_ACTIVE,
'data' => [
'notification_emails' => 'test@example.com',
'notification_reply_to' => 'reply@example.com',
],
]);
$this->command->updateIntegration($integration);
expect($integration->fresh())
->integration_id->toBe('email')
->data->toMatchArray([
'send_to' => 'test@example.com',
'sender_name' => 'OpnForm',
'subject' => 'New form submission',
'email_content' => 'Hello there 👋 <br>New form submission received.',
'include_submission_data' => true,
'include_hidden_fields_submission_data' => false,
'reply_to' => 'reply@example.com',
]);
});
it('updates submission confirmation integration correctly', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$emailProperty = collect($form->properties)->filter(function ($property) {
return $property['type'] == 'email';
})->first();
$integration = FormIntegration::create([
'integration_id' => 'submission_confirmation',
'form_id' => $form->id,
'status' => FormIntegration::STATUS_ACTIVE,
'data' => [
'notification_sender' => 'Sender Name',
'notification_subject' => 'Thank you for your submission',
'notification_body' => 'We received your submission.',
'notifications_include_submission' => true,
'confirmation_reply_to' => 'reply@example.com',
],
]);
$this->command->updateIntegration($integration);
expect($integration->fresh())
->integration_id->toBe('email')
->data->toMatchArray([
'send_to' => '<span mention-field-id="' . $emailProperty['id'] . '" mention-field-name="' . $emailProperty['name'] . '" mention-fallback="" contenteditable="false" mention="true">' . $emailProperty['name'] . '</span>',
'sender_name' => 'Sender Name',
'subject' => 'Thank you for your submission',
'email_content' => 'We received your submission.',
'include_submission_data' => true,
'include_hidden_fields_submission_data' => false,
'reply_to' => 'reply@example.com',
]);
});

View File

@@ -0,0 +1,86 @@
<?php
use App\Open\MentionParser;
test('it replaces mention elements with their corresponding values', function () {
$content = '<p>Hello <span mention mention-field-id="123">Placeholder</span></p>';
$data = [['id' => '123', 'value' => 'World']];
$parser = new MentionParser($content, $data);
$result = $parser->parse();
expect($result)->toBe('<p>Hello World</p>');
});
test('it handles multiple mentions', function () {
$content = '<p><span mention mention-field-id="123">Name</span> is <span mention mention-field-id="456">Age</span> years old</p>';
$data = [
['id' => '123', 'value' => 'John'],
['id' => '456', 'value' => 30],
];
$parser = new MentionParser($content, $data);
$result = $parser->parse();
expect($result)->toBe('<p>John is 30 years old</p>');
});
test('it uses fallback when value is not found', function () {
$content = '<p>Hello <span mention mention-field-id="123" mention-fallback="Friend">Placeholder</span></p>';
$data = [];
$parser = new MentionParser($content, $data);
$result = $parser->parse();
expect($result)->toBe('<p>Hello Friend</p>');
});
test('it removes mention element when no value and no fallback', function () {
$content = '<p>Hello <span mention mention-field-id="123">Placeholder</span></p>';
$data = [];
$parser = new MentionParser($content, $data);
$result = $parser->parse();
expect($result)->toBe('<p>Hello </p>');
});
test('it handles array values', function () {
$content = '<p>Tags: <span mention mention-field-id="123">Placeholder</span></p>';
$data = [['id' => '123', 'value' => ['PHP', 'Laravel', 'Testing']]];
$parser = new MentionParser($content, $data);
$result = $parser->parse();
expect($result)->toBe('<p>Tags: PHP, Laravel, Testing</p>');
});
test('it preserves HTML structure', function () {
$content = '<div><p>Hello <span mention mention-field-id="123">Placeholder</span></p><p>How are you?</p></div>';
$data = [['id' => '123', 'value' => 'World']];
$parser = new MentionParser($content, $data);
$result = $parser->parse();
expect($result)->toBe('<div><p>Hello World</p><p>How are you?</p></div>');
});
test('it handles UTF-8 characters', function () {
$content = '<p>こんにちは <span mention mention-field-id="123">Placeholder</span></p>';
$data = [['id' => '123', 'value' => '世界']];
$parser = new MentionParser($content, $data);
$result = $parser->parse();
expect($result)->toBe('<p>こんにちは 世界</p>');
});
test('it handles content without surrounding paragraph tags', function () {
$content = 'some text <span contenteditable="false" mention="" mention-field-id="123" mention-field-name="Post excerpt" mention-fallback="">Post excerpt</span> dewde';
$data = [['id' => '123', 'value' => 'replaced text']];
$parser = new MentionParser($content, $data);
$result = $parser->parse();
expect($result)->toBe('some text replaced text dewde');
});