Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ousid committed Nov 7, 2023
1 parent d429f74 commit f85e50e
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 95 deletions.
13 changes: 8 additions & 5 deletions resources/views/components/turnstile.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
@php
$id = $getId();
$statePath = $getStatePath();
$fieldWrapperView = $getFieldWrapperView();
$theme = $getTheme();
$size = $getSize();
$language = $getLanguage();
@endphp

<x-dynamic-component :component="$getFieldWrapperView()" :field="$field">
<x-dynamic-component :component="$fieldWrapperView" :field="$turnstile">

<div x-data="{
state: $wire.entangle('{{ $statePath }}').defer
}"
Expand All @@ -16,7 +18,8 @@
data-sitekey="{{config('turnstile.turnstile_site_key')}}"
:data-theme="$theme"
:data-language="$language"
:data-size="$size">
:data-size="$size"
>
</div>
</div>

Expand All @@ -27,12 +30,12 @@
callback: function(token) {
window.Livewire
.find('{{$this->id}}')
.$set('{{$getStatePath()}}', token);
.$set('{{$statePath}}', token);
},
errorCallback: function () {
window.Livewire
.find('{{$this->id}}')
.$set('{{$getStatePath()}}', 'error');
.$set('{{$statePath}}', 'error');
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Forms/Components/Turnstile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

class Turnstile extends Field
{
protected string $viewIdentifier = 'turnstile';

protected string $view = 'turnstile::components.turnstile';

protected string $theme = 'auto';
Expand Down
19 changes: 19 additions & 0 deletions tests/Database/Migrations/create_contacts_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration
{
public function up()
{
Schema::create('contacts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->text('content');
$table->timestamps();
});
}
};
88 changes: 88 additions & 0 deletions tests/Fixtures/ContactUs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Coderflex\FilamentTurnstile\Tests\Fixtures;

use Coderflex\FilamentTurnstile\Forms\Components\Turnstile;
use Coderflex\FilamentTurnstile\Tests\Models\Contact;
use Filament\Actions\Action;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Pages\Concerns\InteractsWithFormActions;
use Filament\Pages\SimplePage;
use Illuminate\Contracts\View\View;

class ContactUs extends SimplePage
{
use InteractsWithFormActions;

public ?array $data = [];

public function mount(): void
{
$this->form->fill();
}

public function form(Form $form): Form
{
return $form;
}

protected function getForms(): array
{
return [
'form' => $this->form(
$this->makeForm()
->schema([
Forms\Components\TextInput::make('name')
->label('Name')
->required(),
Forms\Components\TextInput::make('email')
->label('Email')
->required(),
Forms\Components\TextInput::make('content')
->label('Content')
->required(),
Turnstile::make('turnstile')
// ->id('cf-captcha-field')
->theme('auto'),
])
)
->statePath('data')
->model(Contact::class),
];
}

/**
* @return array<Action | ActionGroup>
*/
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 send()
{
dd($this->form->getState());

Contact::create($this->form->getState());
}

public function render(): View
{
return view('fixtures.contact-us');
}
}
25 changes: 0 additions & 25 deletions tests/Fixtures/Login.php

This file was deleted.

26 changes: 0 additions & 26 deletions tests/Fixtures/Register.php

This file was deleted.

20 changes: 0 additions & 20 deletions tests/Fixtures/TurnstilePanelProvider.php

This file was deleted.

10 changes: 10 additions & 0 deletions tests/Models/Contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Coderflex\FilamentTurnstile\Tests\Models;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
protected $guarded = [];
}
35 changes: 33 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@

namespace Coderflex\FilamentTurnstile\Tests;

use BladeUI\Heroicons\BladeHeroiconsServiceProvider;
use BladeUI\Icons\BladeIconsServiceProvider;
use Coderflex\FilamentTurnstile\FilamentTurnstileServiceProvider;
use Coderflex\FilamentTurnstile\Tests\Fixtures\TurnstilePanelProvider;
use Filament\Actions\ActionsServiceProvider;
use Filament\Facades\Filament;
use Filament\FilamentServiceProvider;
use Filament\Forms\FormsServiceProvider;
use Filament\Infolists\InfolistsServiceProvider;
use Filament\Notifications\NotificationsServiceProvider;
use Filament\Support\SupportServiceProvider;
use Filament\Tables\TablesServiceProvider;
use Filament\Widgets\WidgetsServiceProvider;
use Illuminate\Database\Eloquent\Factories\Factory;
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
use RyanChandler\BladeCaptureDirective\BladeCaptureDirectiveServiceProvider;

class TestCase extends Orchestra
{
protected $enablesPackageDiscoveries = true;
// protected $enablesPackageDiscoveries = true;

protected function setUp(): void
{
Expand All @@ -21,13 +32,27 @@ protected function setUp(): void
);

config()->set('app.key', '6rE9Nz59bGRbeMATftriyQjrpF7DcOQm');
config()->set('turnstile.turnstile_site_key', '2x00000000000000000000AB');
config()->set('turnstile.turnstile_secret_key', '2x0000000000000000000000000000000AA');

$this->setCurrentFilamentPanel();
}

protected function getPackageProviders($app)
{
return [
ActionsServiceProvider::class,
BladeCaptureDirectiveServiceProvider::class,
BladeHeroiconsServiceProvider::class,
BladeIconsServiceProvider::class,
FilamentServiceProvider::class,
FormsServiceProvider::class,
InfolistsServiceProvider::class,
LivewireServiceProvider::class,
NotificationsServiceProvider::class,
SupportServiceProvider::class,
TablesServiceProvider::class,
WidgetsServiceProvider::class,
FilamentTurnstileServiceProvider::class,
TurnstilePanelProvider::class,
];
Expand All @@ -37,8 +62,14 @@ public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');

$app['config']->set('view.paths', [
...$app['config']->get('view.paths'),
__DIR__.'/resources/views',
]);

$migrations = [
include __DIR__.'/Database/Migrations/create_users_table.php',
include __DIR__.'/Database/Migrations/create_contacts_table.php',
];

collect($migrations)->each(
Expand Down
43 changes: 43 additions & 0 deletions tests/TurnstilePanelProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Coderflex\FilamentTurnstile\Tests;

use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Panel;
use Filament\PanelProvider;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;

class TurnstilePanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->id('turnstile')
->login()
->registration()
->resources([])
->pages([])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
]);
}
}
23 changes: 6 additions & 17 deletions tests/TurnstileTest.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
<?php

use Coderflex\FilamentTurnstile\Tests\Fixtures\Login;
use Coderflex\FilamentTurnstile\Tests\Fixtures\Register;
use Coderflex\FilamentTurnstile\Tests\Fixtures\ContactUs;

use function Pest\Livewire\livewire;

it('can render login page', function () {
livewire(Login::class)
it('can render contact page', function () {
livewire(ContactUs::class)
->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');
test('contact page has captcha field', function () {
livewire(ContactUs::class)
->assertFormFieldExists('cf-captcha', 'form');
});
Loading

0 comments on commit f85e50e

Please sign in to comment.