From e4fab708c984dbde657ab9db78691245785a9da8 Mon Sep 17 00:00:00 2001 From: ousid Date: Wed, 8 Nov 2023 16:10:25 +0100 Subject: [PATCH] refactoring --- .../views/components/turnstile.blade.php | 14 +++---- src/Forms/Components/Turnstile.php | 4 +- tests/Fixtures/ContactUs.php | 37 ++----------------- tests/TurnstileTest.php | 12 +++++- .../views/fixtures/contact-us.blade.php | 9 ++--- 5 files changed, 26 insertions(+), 50 deletions(-) diff --git a/resources/views/components/turnstile.blade.php b/resources/views/components/turnstile.blade.php index 9c1fb9f..278133b 100644 --- a/resources/views/components/turnstile.blade.php +++ b/resources/views/components/turnstile.blade.php @@ -1,5 +1,4 @@ @php - $id = $getId(); $statePath = $getStatePath(); $fieldWrapperView = $getFieldWrapperView(); @@ -13,6 +12,7 @@
-
diff --git a/src/Forms/Components/Turnstile.php b/src/Forms/Components/Turnstile.php index 73df2e4..bd27458 100644 --- a/src/Forms/Components/Turnstile.php +++ b/src/Forms/Components/Turnstile.php @@ -23,7 +23,9 @@ protected function setUp(): void $this->label(''); - $this->rules(['required', new TurnstileCheck()]); + $this->required(); + + $this->rule(new TurnstileCheck()); $this->dehydrated(false); } diff --git a/tests/Fixtures/ContactUs.php b/tests/Fixtures/ContactUs.php index 533adb0..3081880 100644 --- a/tests/Fixtures/ContactUs.php +++ b/tests/Fixtures/ContactUs.php @@ -4,19 +4,12 @@ use Coderflex\FilamentTurnstile\Forms\Components\Turnstile; use Coderflex\FilamentTurnstile\Tests\Models\Contact; -use Filament\Actions\Action; -use Filament\Actions\Concerns\InteractsWithActions; use Filament\Forms; use Filament\Forms\Form; -use Filament\Pages\Concerns\InteractsWithFormActions; -use Filament\Pages\SimplePage; -use Illuminate\Contracts\View\View; +use Filament\Forms\FormsComponent; -class ContactUs extends SimplePage +class ContactUs extends FormsComponent { - use InteractsWithActions; - use InteractsWithFormActions; - public ?array $data = []; public function mount(): void @@ -58,30 +51,8 @@ public function send() Contact::create($this->form->getState()); } - /** - * @return array - */ - protected function getFormActions(): array - { - return [ - $this->getSendFormAction(), - ]; - } - - protected function getSendFormAction(): Action - { - return Action::make('Send') - ->label(__('Send')) - ->submit('send'); - } - - protected function hasFullWidthFormActions(): bool - { - return true; - } - - public function render(): View + public function render() { - return view('fixtures.contact-us'); + return 'fixtures.contact-us'; } } diff --git a/tests/TurnstileTest.php b/tests/TurnstileTest.php index fc9490b..58a9909 100644 --- a/tests/TurnstileTest.php +++ b/tests/TurnstileTest.php @@ -20,18 +20,26 @@ /** * Setting Turnstile keys to always pass the request * - * @see https://developers.cloudflare.com/turnstile/reference/testing/#dummy-sitekeys-and-secret-keys + * @link https://developers.cloudflare.com/turnstile/reference/testing/#dummy-sitekeys-and-secret-keys */ Config::set('turnstile', [ 'turnstile_site_key' => '1x00000000000000000000AA', 'turnstile_secret_key' => '1x0000000000000000000000000000000AA', ]); + /** + * In this context, Alpine.js didn't function as expected due to the need to pass the `cf-captcha` field. + * The value of the mentioned key is dynamically determined by the response from Cloudflare (CF) in the UI. + * For more information, refer to the Cloudflare Turnstile documentation: + * + * @link https://developers.cloudflare.com/turnstile/ + */ livewire(ContactUs::class) ->fillForm([ 'name' => 'John Doe', 'email' => 'john@example.com', 'content' => 'This is a simple message', + 'cf-captcha' => 'XXXX.DUMMY.TOKEN.XXXX', ]) ->call('send') ->assertHasNoFormErrors(); @@ -44,7 +52,7 @@ /** * Setting Turnstile keys to always block the request * - * @see https://developers.cloudflare.com/turnstile/reference/testing/#dummy-sitekeys-and-secret-keys + * @link https://developers.cloudflare.com/turnstile/reference/testing/#dummy-sitekeys-and-secret-keys */ Config::set('turnstile', [ 'turnstile_site_key' => '2x00000000000000000000AB', diff --git a/tests/resources/views/fixtures/contact-us.blade.php b/tests/resources/views/fixtures/contact-us.blade.php index 128dd85..fccf563 100644 --- a/tests/resources/views/fixtures/contact-us.blade.php +++ b/tests/resources/views/fixtures/contact-us.blade.php @@ -1,10 +1,7 @@ - +
{{ $this->form }} - + - \ No newline at end of file +
\ No newline at end of file