semua pasangan (akun, dashboard yang dibuka) */ public static function matrix(): array { $cases = []; foreach (Role::cases() as $actor) { foreach (Role::cases() as $target) { $cases["{$actor->value} membuka {$target->value}"] = [$actor, $target]; } } return $cases; } /** @return array */ public static function protectedRoutes(): array { return [ 'dashboard' => ['get', '/dashboard'], 'logout' => ['post', '/logout'], 'superuser dashboard' => ['get', '/superuser/dashboard'], 'admin dashboard' => ['get', '/admin/dashboard'], 'operator dashboard' => ['get', '/operator/dashboard'], 'dosen dashboard' => ['get', '/dosen/dashboard'], 'mahasiswa dashboard' => ['get', '/mahasiswa/dashboard'], ]; } #[DataProvider('matrix')] public function test_a_user_can_only_open_the_dashboard_of_their_own_role(Role $actor, Role $target): void { $this->actingAs($this->userWithRole($actor)); $response = $this->get(route($target->dashboardRoute())); if ($actor === $target) { $response->assertOk(); } else { $response->assertForbidden(); } } #[DataProvider('protectedRoutes')] public function test_guests_are_redirected_to_login_from_every_protected_route(string $method, string $uri): void { $this->{$method}($uri)->assertRedirect(route('login')); $this->assertGuest(); } public function test_route_names_and_paths_match_the_contract(): void { $this->assertSame('/login', route('login', [], false)); $this->assertSame('/logout', route('logout', [], false)); $this->assertSame('/dashboard', route('dashboard', [], false)); foreach (Role::cases() as $role) { $this->assertSame("/{$role->value}/dashboard", route($role->dashboardRoute(), [], false)); } } public function test_every_role_group_is_protected_by_auth_active_and_its_own_role_middleware(): void { foreach (Role::cases() as $role) { $middleware = Route::getRoutes()->getByName($role->dashboardRoute())->gatherMiddleware(); $this->assertContains('auth', $middleware); $this->assertContains('active', $middleware); $this->assertContains("role:{$role->value}", $middleware); } } public function test_dashboard_redirect_sends_each_role_to_its_own_dashboard(): void { foreach (Role::cases() as $role) { $this->actingAs($this->userWithRole($role, ['username' => 'akun-'.$role->value])) ->get(route('dashboard')) ->assertRedirect(route($role->dashboardRoute())); } } public function test_forbidden_page_is_the_indonesian_error_view(): void { $this->actingAs($this->userWithRole(Role::Mahasiswa)) ->get(route('admin.dashboard')) ->assertForbidden() ->assertSee('Akses ditolak') ->assertSee('Kode galat'); } public function test_role_middleware_accepts_several_roles_and_has_no_implicit_superuser_bypass(): void { Route::middleware(['web', 'auth', EnsureUserHasRole::class.':admin,operator']) ->get('/_uji/multi-role', fn (): string => 'ok'); $this->actingAs($this->userWithRole(Role::Admin))->get('/_uji/multi-role')->assertOk(); $this->actingAs($this->userWithRole(Role::Operator))->get('/_uji/multi-role')->assertOk(); $this->actingAs($this->userWithRole(Role::Superuser))->get('/_uji/multi-role')->assertForbidden(); $this->actingAs($this->userWithRole(Role::Dosen))->get('/_uji/multi-role')->assertForbidden(); } public function test_role_middleware_alias_and_active_alias_are_registered(): void { $aliases = app(Kernel::class)->getMiddlewareAliases(); $this->assertSame(EnsureUserHasRole::class, $aliases['role']); $this->assertArrayHasKey('active', $aliases); } public function test_unknown_pages_return_the_404_view_for_guests_and_users(): void { $this->get('/halaman-tidak-ada')->assertNotFound()->assertSee('Kode galat'); $this->actingAs($this->userWithRole(Role::Admin)) ->get('/halaman-tidak-ada') ->assertNotFound() ->assertSee('Kode galat'); } }