- Rollenbasierte Policies, Navigationsgruppen - Media Library (Spatie) für die kommende Bildverwaltung installiert Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
2.5 KiB
PHP
77 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Enums\UserRole;
|
|
use App\Filament\Resources\Bookings\BookingResource;
|
|
use App\Filament\Resources\Properties\PropertyResource;
|
|
use App\Filament\Resources\Users\Pages\CreateUser;
|
|
use App\Filament\Resources\Users\UserResource;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Livewire\Livewire;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use Tests\TestCase;
|
|
|
|
class AdminAccessTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
/**
|
|
* @return array<string, array{?UserRole, bool, bool, bool}>
|
|
*/
|
|
public static function roleProvider(): array
|
|
{
|
|
return [
|
|
'Administrator' => [UserRole::Admin, true, true, true],
|
|
'Vermietung' => [UserRole::Rentals, true, true, false],
|
|
'Inhalte' => [UserRole::Content, false, false, false],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('roleProvider')]
|
|
public function test_roles_see_only_their_areas(UserRole $role, bool $bookings, bool $properties, bool $users): void
|
|
{
|
|
$this->actingAs(User::factory()->role($role)->create());
|
|
|
|
$this->get(BookingResource::getUrl('index'))->assertStatus($bookings ? 200 : 403);
|
|
$this->get(PropertyResource::getUrl('index'))->assertStatus($properties ? 200 : 403);
|
|
$this->get(UserResource::getUrl('index'))->assertStatus($users ? 200 : 403);
|
|
}
|
|
|
|
public function test_account_without_role_cannot_enter_the_admin(): void
|
|
{
|
|
$this->actingAs(User::factory()->role(null)->create());
|
|
|
|
$this->get(BookingResource::getUrl('index'))->assertForbidden();
|
|
}
|
|
|
|
public function test_administrator_creates_a_user_with_role(): void
|
|
{
|
|
$this->actingAs(User::factory()->create());
|
|
|
|
Livewire::test(CreateUser::class)
|
|
->fillForm([
|
|
'name' => 'Brigitte Stommer',
|
|
'email' => 'brigitte@example.com',
|
|
'role' => UserRole::Rentals->value,
|
|
'password' => 'sehr-geheim-123',
|
|
])
|
|
->call('create')
|
|
->assertHasNoFormErrors();
|
|
|
|
$user = User::where('email', 'brigitte@example.com')->sole();
|
|
$this->assertSame(UserRole::Rentals, $user->role);
|
|
$this->assertTrue(Hash::check('sehr-geheim-123', $user->password));
|
|
}
|
|
|
|
public function test_administrators_cannot_delete_themselves(): void
|
|
{
|
|
$admin = User::factory()->create();
|
|
|
|
$this->assertFalse($admin->can('delete', $admin));
|
|
$this->assertTrue($admin->can('delete', User::factory()->create()));
|
|
}
|
|
}
|