Files
siakad/tests/Feature/Auth/LegacyRoleSwitchTest.php
T
2026-11-09 12:01:30 +07:00

108 lines
4.2 KiB
PHP

<?php
namespace Tests\Feature\Auth;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Testing\TestResponse;
use Tests\Concerns\FakesLegacyDatabase;
use Tests\TestCase;
/** Pemilih peran untuk akun DB lama yang memiliki lebih dari satu role. */
class LegacyRoleSwitchTest extends TestCase
{
use FakesLegacyDatabase;
protected function setUp(): void
{
parent::setUp();
$this->useLegacyDatabase();
}
/** Akun 2 (Siti Aminah) tercatat dosen dan juga memegang otoritas Staff Fakultas: dua role, bawaan Operator. */
private function loginAsOperatorAndLecturer(): void
{
DB::connection('untan')->table('t_otoritasuser')->insert(['iduser' => 2, 'idotoritas' => 7]);
$this->post(route('login.store'), ['username' => '197501012000031002', 'password' => 'rahasia-dosen'])
->assertRedirect(route('operator.dashboard'));
}
/** Uji memakai satu instance aplikasi; di produksi tiap permintaan memuat ulang pengguna, jadi guard dilupakan agar sama. */
private function switchTo(string $role): TestResponse
{
$response = $this->post(route('peran.ganti'), ['role' => $role]);
Auth::forgetGuards();
return $response;
}
public function test_default_role_is_the_highest_priority_and_menu_lists_both(): void
{
$this->loginAsOperatorAndLecturer();
$html = $this->get(route('operator.dashboard'))->assertOk()->getContent();
$this->assertStringContainsString(route('peran.ganti'), $html);
$this->assertStringContainsString('name="role" value="dosen"', $html);
$this->assertStringNotContainsString('name="role" value="operator"', $html, 'Peran aktif tidak ditawarkan lagi.');
$this->assertStringNotContainsString('belum aktif pada tampilan prototipe', $html);
}
public function test_switching_changes_the_active_role_and_its_access(): void
{
$this->loginAsOperatorAndLecturer();
$this->switchTo('dosen')
->assertRedirect(route('dosen.dashboard'))
->assertSessionHas('status', 'Beralih ke peran Dosen.');
$this->get(route('dosen.dashboard'))->assertOk()->assertSee('Selamat Datang, Siti Aminah, M.Pd.!');
$this->get(route('operator.dashboard'))->assertForbidden();
$this->get(route('dashboard'))->assertRedirect(route('dosen.dashboard'));
// Menu kini menawarkan Operator, bukan Dosen.
$html = $this->get(route('dosen.jadwal'))->assertOk()->getContent();
$this->assertStringContainsString('name="role" value="operator"', $html);
$this->assertStringNotContainsString('name="role" value="dosen"', $html);
$this->switchTo('operator')->assertRedirect(route('operator.dashboard'));
$this->get(route('operator.dashboard'))->assertOk();
$this->get(route('dosen.dashboard'))->assertForbidden();
}
public function test_roles_the_account_does_not_own_are_refused(): void
{
$this->loginAsOperatorAndLecturer();
foreach (['superuser', 'admin', 'mahasiswa'] as $role) {
$this->post(route('peran.ganti'), ['role' => $role])->assertForbidden();
}
$this->post(route('peran.ganti'), ['role' => 'bukan-peran'])->assertSessionHasErrors('role');
$this->post(route('peran.ganti'), [])->assertSessionHasErrors('role');
$this->get(route('operator.dashboard'))->assertOk();
}
public function test_single_role_account_cannot_switch_and_sees_no_other_roles(): void
{
$this->post(route('login.store'), ['username' => '197501012000031002', 'password' => 'rahasia-dosen'])
->assertRedirect(route('dosen.dashboard'));
$html = $this->get(route('dosen.dashboard'))->assertOk()->getContent();
$this->assertStringNotContainsString('name="role"', $html);
$this->post(route('peran.ganti'), ['role' => 'operator'])->assertForbidden();
}
public function test_selection_ends_with_the_session(): void
{
$this->loginAsOperatorAndLecturer();
$this->switchTo('dosen');
$this->post(route('logout'));
$this->post(route('login.store'), ['username' => '197501012000031002', 'password' => 'rahasia-dosen'])
->assertRedirect(route('operator.dashboard'));
}
}