*/ public static function roles(): array { return array_combine(Role::values(), array_map(fn (Role $role): array => [$role], Role::cases())); } public function test_login_page_renders_for_guests(): void { $this->get(route('login')) ->assertOk() ->assertSee('Masuk ke SIAKAD'); } public function test_login_page_uses_indonesian_html_language(): void { $this->get(route('login'))->assertSee('dom($this->get(route('login'))); $form = $xpath->query("//form[@action='".route('login.store')."']"); $this->assertSame(1, $form->length); $this->assertSame('post', strtolower($this->attr($form, 'method'))); $formText = $this->textOf($form->item(0)); $username = $xpath->query("//form//input[@name='username']"); $this->assertSame(1, $username->length); $this->assertSame('username', $this->attr($username, 'autocomplete')); $password = $xpath->query("//form//input[@name='password']"); $this->assertSame(1, $password->length); $this->assertSame('password', $this->attr($password, 'type')); $this->assertSame('current-password', $this->attr($password, 'autocomplete')); $this->assertSame(1, $xpath->query("//form//input[@name='remember'][@type='checkbox']")->length); $this->assertSame(1, $xpath->query("//form//input[@name='_token']")->length); $this->assertStringContainsString('NIM, NIDN, NIP, atau username', $formText); $this->assertStringContainsString('Kata sandi', $formText); $this->assertStringContainsString('Ingat saya', $formText); $submit = $xpath->query("//form//button[@type='submit']"); $this->assertSame(1, $submit->length); $this->assertSame('Masuk', $this->textOf($submit->item(0))); } public function test_login_page_has_no_dead_links(): void { $response = $this->get(route('login')); $response->assertDontSee('Daftar', false); $response->assertDontSee('href="#"', false); $xpath = $this->dom($response); // Petunjuk lupa kata sandi hanya teks, bukan tautan. $this->assertSame(0, $xpath->query("//a[contains(., 'Lupa')]")->length); $this->assertSame(0, $xpath->query('//a[not(@href)]')->length); } #[DataProvider('roles')] public function test_each_role_logs_in_and_lands_on_its_own_dashboard(Role $role): void { $user = $this->userWithRole($role, ['username' => 'akun-'.$role->value]); $response = $this->post(route('login.store'), [ 'username' => 'akun-'.$role->value, 'password' => 'password', ]); $response->assertRedirect(route($role->dashboardRoute())); $this->assertAuthenticatedAs($user); $this->assertNotNull($user->fresh()->last_login_at); } public function test_login_redirects_back_to_the_intended_page(): void { $this->userWithRole(Role::Dosen, ['username' => 'dosen']); $this->get(route('dosen.dashboard'))->assertRedirect(route('login')); $this->post(route('login.store'), ['username' => 'dosen', 'password' => 'password']) ->assertRedirect(route('dosen.dashboard')); } public function test_wrong_password_is_rejected_with_a_generic_message(): void { $this->userWithRole(Role::Admin, ['username' => 'admin']); $this->from(route('login')) ->post(route('login.store'), ['username' => 'admin', 'password' => 'salah']) ->assertRedirect(route('login')) ->assertSessionHasErrors(['username' => trans('auth.failed')]); $this->assertGuest(); } public function test_unknown_username_gets_the_same_message_as_a_wrong_password(): void { $this->post(route('login.store'), ['username' => 'tidak-ada', 'password' => 'password']) ->assertSessionHasErrors(['username' => trans('auth.failed')]); $this->assertGuest(); } public function test_failed_login_shows_the_error_on_the_page_accessibly(): void { $this->userWithRole(Role::Admin, ['username' => 'admin']); $response = $this->followingRedirects() ->from(route('login')) ->post(route('login.store'), ['username' => 'admin', 'password' => 'salah']); $response->assertOk(); $response->assertSee(trans('auth.failed')); $alerts = $this->dom($response) ->query("//*[@role='alert' or @aria-live][contains(., '".trans('auth.failed')."')]"); $this->assertGreaterThanOrEqual(1, $alerts->length, 'Pesan galat harus berada dalam wilayah role=alert atau aria-live.'); } public function test_inactive_account_cannot_log_in_and_gets_the_same_generic_message(): void { $this->userWithRole(Role::Mahasiswa, ['username' => 'nonaktif', 'is_active' => false]); $this->post(route('login.store'), ['username' => 'nonaktif', 'password' => 'password']) ->assertSessionHasErrors(['username' => trans('auth.failed')]); $this->assertGuest(); // Pesan sama persis dengan kata sandi salah pada akun aktif; status akun tidak bocor. $this->assertSame(trans('auth.failed'), session('errors')->first('username')); $this->assertStringNotContainsStringIgnoringCase('aktif', session('errors')->first('username')); } public function test_required_fields_are_validated_in_indonesian(): void { $this->post(route('login.store'), []) ->assertSessionHasErrors(['username', 'password']); $this->assertStringContainsString('wajib diisi', session('errors')->first('username')); $this->assertStringContainsStringIgnoringCase('kata sandi', session('errors')->first('password')); } public function test_login_is_throttled_after_five_failed_attempts(): void { Event::fake([Lockout::class]); $this->userWithRole(Role::Admin, ['username' => 'admin']); for ($attempt = 1; $attempt <= 5; $attempt++) { $this->post(route('login.store'), ['username' => 'admin', 'password' => 'salah']) ->assertSessionHasErrors(['username' => trans('auth.failed')]); } // Percobaan ke-6 terkunci, bahkan dengan kata sandi yang benar. $this->post(route('login.store'), ['username' => 'admin', 'password' => 'password']) ->assertSessionHasErrors('username'); $this->assertStringContainsString('Terlalu banyak percobaan', session('errors')->first('username')); $this->assertStringContainsString('detik', session('errors')->first('username')); $this->assertGuest(); Event::assertDispatched(Lockout::class); } public function test_throttle_is_per_username_and_case_insensitive(): void { $this->userWithRole(Role::Admin, ['username' => 'admin']); $this->userWithRole(Role::Dosen, ['username' => 'dosen']); foreach (['admin', 'ADMIN', 'Admin', 'admin', 'AdMiN'] as $username) { $this->post(route('login.store'), ['username' => $username, 'password' => 'salah']); } $this->post(route('login.store'), ['username' => 'admin', 'password' => 'password']) ->assertSessionHasErrors('username'); $this->assertGuest(); // Username lain tidak ikut terkunci. $this->post(route('login.store'), ['username' => 'dosen', 'password' => 'password']) ->assertRedirect(route('dosen.dashboard')); } public function test_four_failures_do_not_lock_and_success_clears_the_counter(): void { $this->userWithRole(Role::Admin, ['username' => 'admin']); $key = 'admin|127.0.0.1'; for ($attempt = 1; $attempt <= 4; $attempt++) { $this->post(route('login.store'), ['username' => 'admin', 'password' => 'salah']); } $this->assertSame(4, RateLimiter::attempts($key)); $this->post(route('login.store'), ['username' => 'admin', 'password' => 'password']) ->assertRedirect(route('admin.dashboard')); $this->assertSame(0, RateLimiter::attempts($key)); } #[DataProvider('roles')] public function test_authenticated_users_are_redirected_away_from_the_login_page(Role $role): void { $this->actingAs($this->userWithRole($role)) ->get(route('login')) ->assertRedirect(route('dashboard')); } public function test_authenticated_users_cannot_post_to_login_again(): void { $this->actingAs($this->userWithRole(Role::Admin)) ->post(route('login.store'), ['username' => 'x', 'password' => 'y']) ->assertRedirect(route('dashboard')); } public function test_an_account_deactivated_mid_session_is_logged_out_on_the_next_request(): void { $user = $this->userWithRole(Role::Dosen, ['username' => 'dosen']); $this->post(route('login.store'), ['username' => 'dosen', 'password' => 'password']); $this->assertAuthenticatedAs($user); $this->get(route('dosen.dashboard'))->assertOk(); User::whereKey($user->id)->update(['is_active' => false]); // Permintaan berikutnya berjalan di proses PHP baru; di tes guard harus dilepas agar akun dibaca ulang dari DB. Auth::forgetGuards(); $this->get(route('dosen.dashboard')) ->assertRedirect(route('login')) ->assertSessionHasErrors(['username' => trans('auth.inactive')]); $this->assertGuest(); $this->get(route('dosen.dashboard'))->assertRedirect(route('login')); } #[DataProvider('roles')] public function test_deactivated_account_of_every_role_is_logged_out_on_a_protected_route(Role $role): void { $user = $this->userWithRole($role, ['is_active' => false]); $this->actingAs($user)->get(route('dashboard')) ->assertRedirect(route('login')); $this->assertGuest(); } public function test_root_redirects_guests_to_the_login_page(): void { $this->get('/')->assertRedirect(route('login')); } #[DataProvider('roles')] public function test_root_sends_every_signed_in_role_to_its_dashboard(Role $role): void { $this->actingAs($this->userWithRole($role)); $this->get('/')->assertRedirect(route('dashboard')); $this->get(route('dashboard'))->assertRedirect(route($role->dashboardRoute())); $this->followingRedirects()->get('/')->assertOk()->assertSee('Selamat Datang,'); } public function test_page_load_shows_no_blank_dark_screen(): void { // Kanvas sebelum CSS dimuat mengikuti tema: skrip di
menyamakan meta color-scheme dengan tema terpilih. $html = $this->get(route('login'))->assertOk()->getContent(); $this->assertStringContainsString('', $html); $this->assertMatchesRegularExpression('/dataset\.theme = mode;.*querySelector\(\'meta\[name="color-scheme"\]\'\).*setAttribute\(\'content\', mode\)/s', $html); $this->assertLessThan(strpos($html, 'app.css') ?: PHP_INT_MAX, strpos($html, 'meta[name="color-scheme"]'), 'Skrip tema berjalan sebelum stylesheet diminta.'); // Pindah halaman: halaman lama ditahan sampai halaman baru siap digambar, tanpa animasi (termasuk saat gerak dikurangi). $css = file_get_contents(resource_path('css/dark.css')); $this->assertStringContainsString('@view-transition { navigation: auto; }', $css); $this->assertMatchesRegularExpression('/@media \(prefers-reduced-motion: reduce\) \{\s*::view-transition-group\(\*\),\s*::view-transition-old\(\*\),\s*::view-transition-new\(\*\) \{ animation: none; \}/', $css); } public function test_login_page_never_lists_demo_accounts(): void { foreach (['local', 'testing', 'production', 'staging'] as $environment) { $this->app->detectEnvironment(fn (): string => $environment); $this->get(route('login')) ->assertOk() ->assertDontSee('Akun demo'); } } }