Skip to content

Commit

Permalink
test case for fill & save with forbidden
Browse files Browse the repository at this point in the history
  • Loading branch information
AravindRam-Ranium committed Mar 4, 2024
1 parent 51ae56f commit 155a20e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Support\Facades\Route;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use Visualbuilder\FilamentUserConsent\Livewire\ConsentOptionFormBuilder;

//Routes for users to view and save their consent
Route::middleware([
EncryptCookies::class,
StartSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
AuthenticateSession::class,
'auth:'.config('filament-user-consent.auth-gurads'),
Expand Down
7 changes: 5 additions & 2 deletions src/Livewire/ConsentOptionFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Filament\Support\Enums\MaxWidth;
use Filament\Notifications\Notification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\HtmlString;
use Visualbuilder\FilamentUserConsent\Notifications\ConsentsUpdatedNotification;

Expand Down Expand Up @@ -155,8 +156,10 @@ public function submit(): void
->send();

// $this->user->notify(new ConsentsUpdatedNotification());

$this->redirect(request()->session()->get('url.saved'));

if(Session::has('url.saved')) {
$this->redirect(request()->session()->get('url.saved'));
}
}

public function prepareField($component, $fieldOption, $consentOption)
Expand Down
29 changes: 22 additions & 7 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

use Carbon\Carbon;
use Visualbuilder\FilamentUserConsent\Livewire\ConsentOptionFormBuilder;
use Visualbuilder\FilamentUserConsent\Models\ConsentOption;
use Visualbuilder\FilamentUserConsent\Tests\Seeders\ConsentOptionSeeder;

use function Pest\Laravel\get;
use function Pest\Livewire\livewire;
use Illuminate\Support\Arr;


it('can access user consent list page', function () {
Expand Down Expand Up @@ -51,30 +53,43 @@

livewire(ConsentOptionFormBuilder::class)
->call('submit')
->assertHasFormErrors()
->fillForm([])
->assertHasFormErrors($fieldValidation);
});


it('can fill and save consents', function() {
$this->seed(ConsentOptionSeeder::class);
$collections = auth()->user()->outstandingConsents();
$fieldValidation = [];
foreach($collections as $consentOption){
$fillForm = [];
foreach($collections as $consentOption) {
if($consentOption->is_mandatory) {
$fieldValidation["consents.$consentOption->id"] = 'required';
$fillForm["consents.$consentOption->id"] = true;
}
if((int)$consentOption->additional_info === 1) {
foreach ($consentOption->fields as $field) {
if((bool)$field['required']) {
$fieldValidation["consents_info.$consentOption->id.{$field['name']}"] = "required";
$fieldValue = match ($field['type']) {
'text' => fake()->name(),
'email' => fake()->email(),
'number' => fake()->phoneNumber(),
'select' => Arr::random(explode(',', $field['options'])),
'textarea' => fake()->sentence(),
'check' => fake()->boolean(),
'radio' => Arr::random(explode(',', $field['options'])),
'date' => Carbon::now()->subDays(rand(0, 10))->format('Y-m-d'),
'datetime' => Carbon::now()->subDays(rand(0, 10))->format('Y-m-d H:i:s'),
};
$fillForm["consents_info.$consentOption->id.{$field['name']}"] = $fieldValue;
}
}
}
}

livewire(ConsentOptionFormBuilder::class)
->fillForm($fillForm)
->call('submit')
->assertHasFormErrors()
->assertHasFormErrors($fieldValidation);
->assertHasNoFormErrors();

get(route('consent-option-request'))->assertForbidden();
});

0 comments on commit 155a20e

Please sign in to comment.