From d429f74fdd2ba1220f24bdd597262616a8cdbbd5 Mon Sep 17 00:00:00 2001 From: ousid Date: Tue, 7 Nov 2023 15:24:56 +0100 Subject: [PATCH] add filament v3 compatiblity --- composer.json | 2 +- .../views/components/turnstile.blade.php | 32 +++++++--------- src/FilamentTurnstileServiceProvider.php | 4 +- src/Forms/Components/Turnstile.php | 15 ++++++-- tests/Database/Factories/UserFactory.php | 10 +++++ .../Migrations/create_users_table.php | 21 ++++++++++ tests/Fixtures/Login.php | 24 +++++++----- tests/Fixtures/Register.php | 26 +++++++++++++ tests/Fixtures/TurnstilePanelProvider.php | 20 ++++++++++ tests/Models/User.php | 13 +++++++ tests/TestCase.php | 38 ++++++++++++------- tests/TurnstileTest.php | 19 +++++++++- 12 files changed, 176 insertions(+), 48 deletions(-) create mode 100644 tests/Database/Factories/UserFactory.php create mode 100644 tests/Database/Migrations/create_users_table.php create mode 100644 tests/Fixtures/Register.php create mode 100644 tests/Fixtures/TurnstilePanelProvider.php create mode 100644 tests/Models/User.php diff --git a/composer.json b/composer.json index 9f73980..fe087f4 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "spatie/laravel-package-tools": "^1.14.0" }, "require-dev": { - "filament/filament": "^2.17", + "filament/filament": "^3.0", "laravel/pint": "^1.0", "nunomaduro/collision": "^7.9", "nunomaduro/larastan": "^2.0.1", diff --git a/resources/views/components/turnstile.blade.php b/resources/views/components/turnstile.blade.php index e359b12..274d202 100644 --- a/resources/views/components/turnstile.blade.php +++ b/resources/views/components/turnstile.blade.php @@ -1,26 +1,22 @@ - -
+
+ :data-theme="$theme" + :data-language="$language" + :data-size="$size">
diff --git a/src/FilamentTurnstileServiceProvider.php b/src/FilamentTurnstileServiceProvider.php index cfe6ce4..02fa4a1 100644 --- a/src/FilamentTurnstileServiceProvider.php +++ b/src/FilamentTurnstileServiceProvider.php @@ -2,10 +2,10 @@ namespace Coderflex\FilamentTurnstile; -use Filament\PluginServiceProvider; use Spatie\LaravelPackageTools\Package; +use Spatie\LaravelPackageTools\PackageServiceProvider; -class FilamentTurnstileServiceProvider extends PluginServiceProvider +class FilamentTurnstileServiceProvider extends PackageServiceProvider { public function configurePackage(Package $package): void { diff --git a/src/Forms/Components/Turnstile.php b/src/Forms/Components/Turnstile.php index 309d11b..bd11784 100644 --- a/src/Forms/Components/Turnstile.php +++ b/src/Forms/Components/Turnstile.php @@ -47,17 +47,26 @@ public function language(string $language): static return $this; } - public function getTheme(): string + /** + * @return string + */ + public function getTheme() { return $this->evaluate($this->theme); } - public function getSize(): string + /** + * @return string + */ + public function getSize() { return $this->evaluate($this->size); } - public function getLanguage(): string + /** + * @return string + */ + public function getLanguage() { return $this->evaluate($this->language); } diff --git a/tests/Database/Factories/UserFactory.php b/tests/Database/Factories/UserFactory.php new file mode 100644 index 0000000..9e4518a --- /dev/null +++ b/tests/Database/Factories/UserFactory.php @@ -0,0 +1,10 @@ +id(); + $table->string('name'); + $table->string('email')->unique(); + $table->timestamp('email_verified_at')->nullable(); + $table->string('password'); + $table->rememberToken(); + $table->timestamps(); + }); + } +}; diff --git a/tests/Fixtures/Login.php b/tests/Fixtures/Login.php index ae2d1b8..ea194c1 100644 --- a/tests/Fixtures/Login.php +++ b/tests/Fixtures/Login.php @@ -4,16 +4,22 @@ use Coderflex\FilamentTurnstile\Forms\Components\Turnstile; -class Login extends \Filament\Http\Livewire\Auth\Login +class Login extends \Filament\Pages\Auth\Login { - protected function getFormSchema(): array + protected function getForms(): array { - return array_merge( - parent::getFormSchema(), - [ - Turnstile::make('cf-captcha') - ->theme('auto'), - ] - ); + return [ + 'form' => $this->form( + $this->makeForm() + ->schema([ + $this->getEmailFormComponent(), + $this->getPasswordFormComponent(), + $this->getRememberFormComponent(), + Turnstile::make('cf-captcha') + ->theme('auto'), + ]) + ->statePath('data'), + ), + ]; } } diff --git a/tests/Fixtures/Register.php b/tests/Fixtures/Register.php new file mode 100644 index 0000000..6abfd68 --- /dev/null +++ b/tests/Fixtures/Register.php @@ -0,0 +1,26 @@ + $this->form( + $this->makeForm() + ->schema([ + $this->getNameFormComponent(), + $this->getEmailFormComponent(), + $this->getPasswordFormComponent(), + $this->getPasswordConfirmationFormComponent(), + Turnstile::make('cf-captcha') + ->theme('auto'), + ]) + ->statePath('data'), + ), + ]; + } +} diff --git a/tests/Fixtures/TurnstilePanelProvider.php b/tests/Fixtures/TurnstilePanelProvider.php new file mode 100644 index 0000000..c6d4a43 --- /dev/null +++ b/tests/Fixtures/TurnstilePanelProvider.php @@ -0,0 +1,20 @@ +id('turnstile') + ->path('filament') + ->authMiddleware([ + Authenticate::class, + ]); + } +} diff --git a/tests/Models/User.php b/tests/Models/User.php new file mode 100644 index 0000000..91e0a22 --- /dev/null +++ b/tests/Models/User.php @@ -0,0 +1,13 @@ + 'Coderflex\\FilamentTurnstile\\Tests\\Database\\Factories\\'.class_basename($modelName).'Factory' + ); + config()->set('app.key', '6rE9Nz59bGRbeMATftriyQjrpF7DcOQm'); - $this->registerLivewireComponents(); + $this->setCurrentFilamentPanel(); } protected function getPackageProviders($app) { return [ - FilamentServiceProvider::class, - FormsServiceProvider::class, - LivewireServiceProvider::class, - SupportServiceProvider::class, FilamentTurnstileServiceProvider::class, + TurnstilePanelProvider::class, ]; } public function getEnvironmentSetUp($app) { - // + config()->set('database.default', 'testing'); + + $migrations = [ + include __DIR__.'/Database/Migrations/create_users_table.php', + ]; + + collect($migrations)->each( + fn ($migration) => $migration->up() + ); } - protected function registerLivewireComponents(): void + protected function setCurrentFilamentPanel(): void { - Livewire::component('login', Login::class); + Filament::setCurrentPanel( + Filament::getPanel('turnstile') + ); } } diff --git a/tests/TurnstileTest.php b/tests/TurnstileTest.php index 2394942..a1149ac 100644 --- a/tests/TurnstileTest.php +++ b/tests/TurnstileTest.php @@ -1,9 +1,26 @@ assertOk(); +}); + +it('can render register page', function () { + livewire(Register::class) + ->assertOk(); +}); + +test('login page has captcha field', function () { + livewire(Login::class) + ->assertFormFieldExists('cf-captcha'); +}); + +test('register page has captcha field', function () { + livewire(Register::class) ->assertFormFieldExists('cf-captcha'); });