From 56e02234929cfe8cc2a4daa4f964010782e5813c Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 13 Apr 2024 21:57:58 +0200 Subject: [PATCH] Add test for email validation --- app/Form/Fields/EmailField.php | 2 +- tests/Feature/Form/FormRegisterActionTest.php | 12 ++++++++++++ tests/Lib/CreatesFormFields.php | 8 +++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/Form/Fields/EmailField.php b/app/Form/Fields/EmailField.php index 1a4be774..064aa995 100644 --- a/app/Form/Fields/EmailField.php +++ b/app/Form/Fields/EmailField.php @@ -17,6 +17,6 @@ class EmailField extends TextField */ public function getRegistrationRules(Form $form): array { - return [$this->key => $this->required ? ['required', 'string', 'email'] : ['nullable', 'string']]; + return [$this->key => $this->required ? ['required', 'string', 'email'] : ['nullable', 'email', 'string']]; } } diff --git a/tests/Feature/Form/FormRegisterActionTest.php b/tests/Feature/Form/FormRegisterActionTest.php index b69eb227..e7113597 100644 --- a/tests/Feature/Form/FormRegisterActionTest.php +++ b/tests/Feature/Form/FormRegisterActionTest.php @@ -254,6 +254,18 @@ class FormRegisterActionTest extends FormTestCase ['vorname' => 5], ['vorname' => 'Vorname der Mutter muss ein String sein.'] ]; + + yield [ + $this->emailField('email')->name('Mail')->required(true), + ['email' => 'alaaa'], + ['email' => 'Mail muss eine gültige E-Mail-Adresse sein.'] + ]; + + yield [ + $this->emailField('email')->name('Mail')->required(false), + ['email' => 'alaaa'], + ['email' => 'Mail muss eine gültige E-Mail-Adresse sein.'] + ]; } public function testItValidatesGroupFieldWithParentGroupField(): void diff --git a/tests/Lib/CreatesFormFields.php b/tests/Lib/CreatesFormFields.php index ec3c42f2..b932105e 100644 --- a/tests/Lib/CreatesFormFields.php +++ b/tests/Lib/CreatesFormFields.php @@ -6,6 +6,7 @@ use App\Form\Fields\CheckboxesField; use App\Form\Fields\CheckboxField; use App\Form\Fields\DateField; use App\Form\Fields\DropdownField; +use App\Form\Fields\EmailField; use App\Form\Fields\GroupField; use App\Form\Fields\NamiField; use App\Form\Fields\RadioField; @@ -26,6 +27,11 @@ trait CreatesFormFields return FormtemplateFieldRequest::type(TextField::class)->key($key ?? $this->randomKey()); } + protected function emailField(?string $key = null): FormtemplateFieldRequest + { + return FormtemplateFieldRequest::type(EmailField::class)->key($key ?? $this->randomKey()); + } + protected function checkboxesField(?string $key = null): FormtemplateFieldRequest { return FormtemplateFieldRequest::type(CheckboxesField::class)->key($key ?? $this->randomKey()); @@ -63,6 +69,6 @@ trait CreatesFormFields protected function randomKey(): string { - return preg_replace('/[\-0-9]/', '', str()->uuid().str()->uuid()); + return preg_replace('/[\-0-9]/', '', str()->uuid() . str()->uuid()); } }