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 f85e50e commit b860114
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 31 deletions.
35 changes: 17 additions & 18 deletions resources/views/components/turnstile.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@php
$id = $getId();
$statePath = $getStatePath();
$fieldWrapperView = $getFieldWrapperView();
Expand All @@ -12,36 +13,34 @@
<div x-data="{
state: $wire.entangle('{{ $statePath }}').defer
}"
x-init="(() => {
let options= {
callback: function (token) {
$wire.set('{{ $statePath }}', token)
},
errorCallback: function () {
$wire.set('{{ $statePath }}', null)
},
}
window.onloadTurnstileCallback = () => {
turnstile.render($refs.turnstile, options)
}
})"
wire:ignore
>
<div id="turnstile-widget"
data-sitekey="{{config('turnstile.turnstile_site_key')}}"
:data-theme="$theme"
:data-language="$language"
:data-size="$size"
x-ref="turnstile"
>
</div>
</div>

@push('scripts')
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback" defer></script>
<script>
let options = {
callback: function(token) {
window.Livewire
.find('{{$this->id}}')
.$set('{{$statePath}}', token);
},
errorCallback: function () {
window.Livewire
.find('{{$this->id}}')
.$set('{{$statePath}}', 'error');
}
}
window.onloadTurnstileCallback = () => {
turnstile.render('#turnstile-widget', options)
}
</script>
@endpush
</x-dynamic-component>
4 changes: 2 additions & 2 deletions src/Forms/Components/Turnstile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Coderflex\FilamentTurnstile\Forms\Components;

use Coderflex\LaravelTurnstile\Rules\TurnstileCheck;
use Filament\Forms\Components\Field;
use Filament\Forms\Components\TextInput;

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

Expand Down
17 changes: 8 additions & 9 deletions tests/Fixtures/ContactUs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -13,6 +14,7 @@

class ContactUs extends SimplePage
{
use InteractsWithActions;
use InteractsWithFormActions;

public ?array $data = [];
Expand Down Expand Up @@ -42,8 +44,7 @@ protected function getForms(): array
Forms\Components\TextInput::make('content')
->label('Content')
->required(),
Turnstile::make('turnstile')
// ->id('cf-captcha-field')
Turnstile::make('cf-captcha')
->theme('auto'),
])
)
Expand All @@ -52,6 +53,11 @@ protected function getForms(): array
];
}

public function send()
{
Contact::create($this->form->getState());
}

/**
* @return array<Action | ActionGroup>
*/
Expand All @@ -74,13 +80,6 @@ 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');
Expand Down
2 changes: 0 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ 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();
}
Expand Down
50 changes: 50 additions & 0 deletions tests/TurnstileTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use Coderflex\FilamentTurnstile\Tests\Fixtures\ContactUs;
use Coderflex\FilamentTurnstile\Tests\Models\Contact;
use Illuminate\Support\Facades\Config;

use function Pest\Livewire\livewire;

Expand All @@ -13,3 +15,51 @@
livewire(ContactUs::class)
->assertFormFieldExists('cf-captcha', 'form');
});

it('can send a message', function () {
/**
* Setting Turnstile keys to always pass the request
*
* @see https://developers.cloudflare.com/turnstile/reference/testing/#dummy-sitekeys-and-secret-keys
*/
Config::set('turnstile', [
'turnstile_site_key' => '1x00000000000000000000AA',
'turnstile_secret_key' => '1x0000000000000000000000000000000AA',
]);

livewire(ContactUs::class)
->fillForm([
'name' => 'John Doe',
'email' => '[email protected]',
'content' => 'This is a simple message',
])
->call('send')
->assertHasNoFormErrors();

expect(Contact::get())
->toHaveCount(1);
});

it('cannot send a message', function () {
/**
* Setting Turnstile keys to always block the request
*
* @see https://developers.cloudflare.com/turnstile/reference/testing/#dummy-sitekeys-and-secret-keys
*/
Config::set('turnstile', [
'turnstile_site_key' => '2x00000000000000000000AB',
'turnstile_secret_key' => '2x0000000000000000000000000000000AA',
]);

livewire(ContactUs::class)
->fillForm([
'name' => 'John Doe',
'email' => '[email protected]',
'content' => 'This is a simple message',
])
->call('send')
->assertHasFormErrors(['cf-captcha']);

expect(Contact::get())
->toHaveCount(0);
});

0 comments on commit b860114

Please sign in to comment.