|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Illuminate\Database\Schema\Blueprint; |
| 6 | +use Illuminate\Foundation\Auth\User; |
| 7 | +use Illuminate\Support\Facades\Schema; |
| 8 | +use Tapp\FilamentFormBuilder\Livewire\FilamentForm\Show; |
| 9 | +use Tapp\FilamentFormBuilder\Models\FilamentForm; |
| 10 | +use Tapp\FilamentFormBuilder\Models\FilamentFormUser; |
| 11 | + |
| 12 | +beforeEach(function (): void { |
| 13 | + Schema::create('users', function (Blueprint $table): void { |
| 14 | + $table->id(); |
| 15 | + $table->string('name'); |
| 16 | + $table->string('email')->unique(); |
| 17 | + $table->string('password'); |
| 18 | + $table->timestamps(); |
| 19 | + }); |
| 20 | + |
| 21 | + $migration = require dirname(__DIR__).'/database/migrations/create_dynamic_filament_form_tables.php.stub'; |
| 22 | + $migration->up(); |
| 23 | +}); |
| 24 | + |
| 25 | +function createFormBuilderTestUser(string $email): User |
| 26 | +{ |
| 27 | + $user = new User; |
| 28 | + $user->forceFill([ |
| 29 | + 'name' => 'Test User', |
| 30 | + 'email' => $email, |
| 31 | + 'password' => bcrypt('password'), |
| 32 | + ]); |
| 33 | + $user->save(); |
| 34 | + |
| 35 | + return $user; |
| 36 | +} |
| 37 | + |
| 38 | +it('updates the existing entry by default for authenticated users', function (): void { |
| 39 | + $user = createFormBuilderTestUser('default@example.com'); |
| 40 | + |
| 41 | + $form = FilamentForm::query()->create([ |
| 42 | + 'name' => 'Feedback', |
| 43 | + 'permit_guest_entries' => false, |
| 44 | + 'locked' => false, |
| 45 | + ]); |
| 46 | + |
| 47 | + $this->actingAs($user); |
| 48 | + |
| 49 | + $show = new Show; |
| 50 | + $show->filamentForm = $form; |
| 51 | + $show->allowMultipleSubmissions = false; |
| 52 | + |
| 53 | + $method = new ReflectionMethod(Show::class, 'persistFormEntry'); |
| 54 | + $method->invoke($show, [['field' => 'rating', 'answer' => 'Agree']]); |
| 55 | + $method->invoke($show, [['field' => 'rating', 'answer' => 'Disagree']]); |
| 56 | + |
| 57 | + expect(FilamentFormUser::query()->count())->toBe(1) |
| 58 | + ->and(FilamentFormUser::query()->first()->entry[0]['answer'])->toBe('Disagree'); |
| 59 | +}); |
| 60 | + |
| 61 | +it('creates a new entry on each submission when allowMultipleSubmissions is enabled', function (): void { |
| 62 | + $user = createFormBuilderTestUser('multi@example.com'); |
| 63 | + |
| 64 | + $form = FilamentForm::query()->create([ |
| 65 | + 'name' => 'Feedback', |
| 66 | + 'permit_guest_entries' => false, |
| 67 | + 'locked' => false, |
| 68 | + ]); |
| 69 | + |
| 70 | + $this->actingAs($user); |
| 71 | + |
| 72 | + $show = new Show; |
| 73 | + $show->filamentForm = $form; |
| 74 | + $show->allowMultipleSubmissions = true; |
| 75 | + |
| 76 | + $method = new ReflectionMethod(Show::class, 'persistFormEntry'); |
| 77 | + $method->invoke($show, [['field' => 'rating', 'answer' => 'Agree']]); |
| 78 | + $method->invoke($show, [['field' => 'rating', 'answer' => 'Disagree']]); |
| 79 | + |
| 80 | + expect(FilamentFormUser::query()->count())->toBe(2) |
| 81 | + ->and(FilamentFormUser::query()->orderBy('id')->pluck('entry')->map(fn (array $entry) => $entry[0]['answer'])->all()) |
| 82 | + ->toBe(['Agree', 'Disagree']); |
| 83 | +}); |
0 commit comments