285 lines
15 KiB
PHP
285 lines
15 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Superuser;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Tests\Concerns\FakesLegacyDatabase;
|
|
use Tests\Concerns\InspectsHtml;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Menu Superuser untuk akun DB lama: Dashboard, Pengguna, Hak akses, Log aktivitas, Kesehatan sistem, dan Kalender akademik
|
|
* membaca data asli (t_account, t_otoritas, t_historilogin, t_kalender_kegiatan). Status akun dan otoritas bisa diubah;
|
|
* menu tanpa sumber data (sesi aktif, pengaturan, cadangan, pengumuman) disembunyikan dan ditutup.
|
|
*
|
|
* Akun fake: 1 Budi Santoso (staf fakultas, otoritas 7), 2 Siti Aminah (dosen), 3 Akun Beku (superuser dibekukan),
|
|
* 4 Tanpa Otoritas, 5 Rina Lestari (mahasiswa), 6 Admin Pusat (superuser, yang masuk).
|
|
*/
|
|
class LegacyAdminPagesTest extends TestCase
|
|
{
|
|
use FakesLegacyDatabase;
|
|
use InspectsHtml;
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->useLegacyDatabase();
|
|
$this->seedLegacyOverview();
|
|
$this->seedLegacyPortal();
|
|
|
|
$db = DB::connection('untan');
|
|
$db->table('t_historilogin')->insert([
|
|
['iduser' => 1, 'logintimestamp' => '2018-04-10 04:39:02', 'logincname' => 'UPTtik-PC', 'logincip' => '192.168.32.37/32'],
|
|
['iduser' => 5, 'logintimestamp' => '2017-05-01 08:00:00', 'logincname' => 'LAB-01', 'logincip' => '192.168.1.5/32'],
|
|
['iduser' => 1, 'logintimestamp' => '2016-01-15 09:30:00', 'logincname' => 'TU04', 'logincip' => '192.168.32.55/32'],
|
|
['iduser' => 99, 'logintimestamp' => '2015-03-01 10:00:00', 'logincname' => 'HAPUS', 'logincip' => '10.0.0.9/32'],
|
|
]);
|
|
$db->table('t_menu')->insert([
|
|
['idmenu' => 1, 'kdmenu' => 'M01', 'menu' => 'Organisasi', 'idmenuinduk' => null, 'islink' => false],
|
|
['idmenu' => 2, 'kdmenu' => 'M0101', 'menu' => 'Data Fakultas', 'idmenuinduk' => 1, 'islink' => true],
|
|
['idmenu' => 3, 'kdmenu' => 'M0102', 'menu' => 'Data Jurusan', 'idmenuinduk' => 1, 'islink' => true],
|
|
]);
|
|
$db->table('t_menuotoritas')->insert([
|
|
['idmenu' => 2, 'idotoritas' => 7, 'bybagian' => true],
|
|
['idmenu' => 3, 'idotoritas' => 7, 'bybagian' => false],
|
|
]);
|
|
|
|
$this->post(route('login.store'), ['username' => '197001011995011001', 'password' => 'rahasia-super'])
|
|
->assertRedirect(route('superuser.dashboard'));
|
|
}
|
|
|
|
private function accountStatus(int $id): int
|
|
{
|
|
return (int) DB::connection('untan')->table('t_account')->where('iduser', $id)->value('status');
|
|
}
|
|
|
|
/** @return list<int> */
|
|
private function authorities(int $id): array
|
|
{
|
|
return DB::connection('untan')->table('t_otoritasuser')->where('iduser', $id)->orderBy('idotoritas')->pluck('idotoritas')->map(fn ($v): int => (int) $v)->all();
|
|
}
|
|
|
|
public function test_menu_hides_entries_without_a_data_source(): void
|
|
{
|
|
$page = $this->get(route('superuser.dashboard'))->assertOk();
|
|
$menu = $this->textOf($this->dom($page)->query('//nav')->item(0));
|
|
|
|
foreach (['Pengguna', 'Atur ulang kata sandi', 'Hak akses', 'Periode akademik', 'Log aktivitas', 'Kesehatan sistem', 'Kalender akademik', 'Profil saya'] as $item) {
|
|
$this->assertStringContainsString($item, $menu);
|
|
}
|
|
foreach (['Sesi aktif', 'Cadangan data', 'Pengaturan', 'Pengumuman'] as $item) {
|
|
$this->assertStringNotContainsString($item, $menu);
|
|
}
|
|
|
|
foreach (['superuser.sesi-aktif', 'superuser.pengaturan', 'superuser.cadangan'] as $route) {
|
|
$this->get(route($route))->assertNotFound();
|
|
}
|
|
$this->get(route('pengumuman.index'))->assertNotFound();
|
|
$this->delete(route('superuser.sesi-aktif.akhiri-semua'))->assertNotFound();
|
|
$this->post(route('superuser.cadangan.store'))->assertNotFound();
|
|
}
|
|
|
|
public function test_dashboard_shows_real_counts(): void
|
|
{
|
|
$page = $this->get(route('superuser.dashboard'))->assertOk()
|
|
->assertSee('Selamat Datang, Admin Pusat!')
|
|
->assertSee('Total akun')
|
|
->assertSee('5 akun aktif')
|
|
->assertSee('Pegawai per otoritas')
|
|
->assertSee('Masuk per tahun')
|
|
->assertSee('Masuk terbaru')
|
|
->assertSee('Budi Santoso')
|
|
->assertDontSee('Cadangan terakhir')
|
|
->assertDontSee('Pengumuman terbaru')
|
|
->assertDontSee('Sesi aktif');
|
|
|
|
$stats = collect($this->dom($page)->query("//*[@aria-label='Ringkasan akun']//*[contains(@class,'stat')]"))->map(fn ($n) => $this->textOf($n))->implode(' | ');
|
|
$this->assertStringContainsString('Total akun 6', $stats);
|
|
$this->assertStringContainsString('Akun pegawai 5', $stats);
|
|
$this->assertStringContainsString('Akun mahasiswa 1', $stats);
|
|
$this->assertStringContainsString('Akun dibekukan 1', $stats);
|
|
|
|
// Satu pegawai tanpa otoritas dan bukan dosen; dua pemegang otoritas Superuser.
|
|
$page->assertSee('1 akun')->assertSee('2 akun');
|
|
}
|
|
|
|
public function test_user_list_is_real_and_searchable(): void
|
|
{
|
|
$page = $this->get(route('superuser.pengguna.index'))->assertOk()
|
|
->assertSee('Dr. Budi Santoso, S.T., M.T.')
|
|
->assertSee('Rina Lestari')
|
|
->assertSee('Akun Beku')
|
|
->assertSee('Staff Fakultas')
|
|
->assertSee('Dibekukan')
|
|
->assertDontSee('Tambah pengguna');
|
|
$this->assertSame(6, $this->dom($page)->query('//tbody/tr')->length);
|
|
|
|
$this->get(route('superuser.pengguna.index', ['q' => 'rina']))->assertSee('D1041191001')->assertDontSee('Budi Santoso');
|
|
$this->get(route('superuser.pengguna.index', ['q' => '1975010']))->assertSee('Siti Aminah')->assertDontSee('Rina Lestari');
|
|
$this->get(route('superuser.pengguna.index', ['jenis' => 1]))->assertSee('Rina Lestari')->assertDontSee('Budi Santoso');
|
|
$this->get(route('superuser.pengguna.index', ['jenis' => 0, 'status' => 'beku']))->assertSee('Akun Beku')->assertDontSee('Siti Aminah');
|
|
$this->get(route('superuser.pengguna.index', ['status' => 'aktif']))->assertDontSee('Akun Beku')->assertSee('Siti Aminah');
|
|
$this->get(route('superuser.pengguna.index', ['otoritas' => 7]))->assertSee('Budi Santoso')->assertDontSee('Siti Aminah');
|
|
}
|
|
|
|
public function test_user_roles_follow_the_login_mapping(): void
|
|
{
|
|
$roles = collect($this->dom($this->get(route('superuser.pengguna.index')))->query('//tbody/tr'))
|
|
->mapWithKeys(function ($tr) {
|
|
$cells = collect($tr->getElementsByTagName('td'))->map(fn ($td) => $this->textOf($td));
|
|
|
|
return [$cells[0] => $cells[4]];
|
|
})->all();
|
|
|
|
$this->assertSame('Operator', $roles['Dr. Budi Santoso, S.T., M.T.']);
|
|
$this->assertSame('Dosen', $roles['Siti Aminah, M.Pd.']);
|
|
$this->assertSame('Superuser', $roles['Admin Pusat']);
|
|
$this->assertSame('Mahasiswa', $roles['Rina Lestari']);
|
|
$this->assertSame('Tanpa peran', $roles['Tanpa Otoritas']);
|
|
}
|
|
|
|
public function test_user_detail(): void
|
|
{
|
|
$this->get(route('superuser.pengguna.show', 1))->assertOk()
|
|
->assertSee('Dr. Budi Santoso, S.T., M.T.')
|
|
->assertSee('198001012005011001')
|
|
->assertSee('Fakultas Teknik')
|
|
->assertSee('Staff Fakultas')
|
|
->assertSee('UPTtik-PC')
|
|
->assertSee('192.168.32.37')
|
|
->assertDontSee('192.168.32.37/32')
|
|
->assertSee('Bekukan akun')
|
|
->assertSee('Tambah otoritas');
|
|
|
|
$this->get(route('superuser.pengguna.show', 5))->assertOk()->assertSee('Rina Lestari')->assertSee('Teknik Informatika')->assertDontSee('Tambah otoritas');
|
|
$this->get(route('superuser.pengguna.show', 999))->assertNotFound();
|
|
$this->get(route('superuser.pengguna.create'))->assertNotFound();
|
|
}
|
|
|
|
public function test_freeze_and_activate_an_account_with_audit(): void
|
|
{
|
|
$this->put(route('superuser.pengguna.status', 2), ['aktif' => 0])
|
|
->assertRedirect(route('superuser.pengguna.show', 2))->assertSessionHas('status', 'Akun dibekukan.');
|
|
$row = DB::connection('untan')->table('t_account')->where('iduser', 2)->first();
|
|
$this->assertSame(1, (int) $row->status);
|
|
$this->assertSame('197001011995011', $row->uname);
|
|
$this->assertSame('127.0.0.1', $row->cip);
|
|
$this->assertSame(config('app.name'), $row->cname);
|
|
|
|
$this->put(route('superuser.pengguna.status', 2), ['aktif' => 1])->assertSessionHas('status', 'Akun diaktifkan kembali.');
|
|
$this->assertSame(0, $this->accountStatus(2));
|
|
$this->assertSame(1, $this->accountStatus(3), 'Akun lain tidak tersentuh.');
|
|
}
|
|
|
|
public function test_frozen_account_cannot_log_in_until_reactivated(): void
|
|
{
|
|
$this->put(route('superuser.pengguna.status', 2), ['aktif' => 0]);
|
|
$this->post(route('logout'));
|
|
Auth::forgetGuards();
|
|
|
|
$this->post(route('login.store'), ['username' => '197501012000031002', 'password' => 'rahasia-dosen'])->assertSessionHasErrors('username');
|
|
$this->assertGuest();
|
|
}
|
|
|
|
public function test_own_account_cannot_be_changed(): void
|
|
{
|
|
$this->get(route('superuser.pengguna.show', 6))->assertOk()->assertSee('Akun Anda sendiri')->assertDontSee('Bekukan akun');
|
|
|
|
$this->put(route('superuser.pengguna.status', 6), ['aktif' => 0])->assertSessionHas('error');
|
|
$this->post(route('superuser.pengguna.otoritas.store', 6), ['otoritas' => 7])->assertSessionHas('error');
|
|
$this->delete(route('superuser.pengguna.otoritas.destroy', [6, 1]))->assertSessionHas('error');
|
|
|
|
$this->assertSame(0, $this->accountStatus(6));
|
|
$this->assertSame([1], $this->authorities(6));
|
|
}
|
|
|
|
public function test_grant_and_revoke_authority(): void
|
|
{
|
|
$this->post(route('superuser.pengguna.otoritas.store', 2), ['otoritas' => 7])
|
|
->assertRedirect(route('superuser.pengguna.show', 2))->assertSessionHas('status', 'Otoritas ditambahkan.');
|
|
$this->assertSame([7], $this->authorities(2));
|
|
$row = DB::connection('untan')->table('t_otoritasuser')->where('iduser', 2)->first();
|
|
$this->assertSame('197001011995011', $row->uname);
|
|
$this->assertSame('127.0.0.1', $row->cip);
|
|
$this->assertNotNull($row->tanggal);
|
|
|
|
$this->post(route('superuser.pengguna.otoritas.store', 2), ['otoritas' => 7])->assertSessionHas('error', 'Akun ini sudah memiliki otoritas tersebut.');
|
|
$this->post(route('superuser.pengguna.otoritas.store', 2), ['otoritas' => 404])->assertSessionHas('error', 'Otoritas tidak ditemukan.');
|
|
$this->post(route('superuser.pengguna.otoritas.store', 5), ['otoritas' => 7])->assertSessionHas('error', 'Otoritas hanya dapat diatur untuk akun pegawai.');
|
|
$this->assertSame([7], $this->authorities(2));
|
|
$this->assertSame([], $this->authorities(5));
|
|
|
|
$this->delete(route('superuser.pengguna.otoritas.destroy', [2, 7]))->assertSessionHas('status', 'Otoritas dicabut.');
|
|
$this->assertSame([], $this->authorities(2));
|
|
$this->delete(route('superuser.pengguna.otoritas.destroy', [2, 7]))->assertSessionHas('error', 'Akun ini tidak memiliki otoritas tersebut.');
|
|
$this->assertSame([7], $this->authorities(1), 'Akun lain tidak tersentuh.');
|
|
}
|
|
|
|
public function test_authority_list_and_detail(): void
|
|
{
|
|
$this->get(route('superuser.hak-akses'))->assertOk()
|
|
->assertSee('Staff Fakultas')->assertSee('Administrator Pusat')
|
|
->assertSee('Operator')->assertSee('Superuser')
|
|
->assertDontSee('Matriks');
|
|
|
|
$this->get(route('superuser.hak-akses.show', 7))->assertOk()
|
|
->assertSee('Organisasi')->assertSee('Data Fakultas')->assertSee('M0101')->assertSee('Data Jurusan')->assertSee('Dibatasi unit')
|
|
->assertSee(route('superuser.pengguna.index', ['otoritas' => 7]), false);
|
|
$this->get(route('superuser.hak-akses.show', 1))->assertOk()->assertSee('Belum ada menu');
|
|
$this->get(route('superuser.hak-akses.show', 404))->assertNotFound();
|
|
}
|
|
|
|
public function test_activity_log_is_real_history(): void
|
|
{
|
|
$page = $this->get(route('superuser.log-aktivitas.index'))->assertOk()
|
|
->assertSee('UPTtik-PC')->assertSee('192.168.32.37')->assertDontSee('192.168.32.37/32')
|
|
->assertSee('10 Apr 2018, 04.39')->assertSee('Akun tidak ditemukan');
|
|
$rows = collect($this->dom($page)->query('//tbody/tr'))->map(fn ($tr) => $this->textOf($tr))->all();
|
|
$this->assertCount(4, $rows);
|
|
$this->assertStringContainsString('Dr. Budi Santoso', $rows[3]);
|
|
$this->assertStringContainsString('HAPUS', $rows[0], 'Terbaru dulu (idhistori terbesar).');
|
|
|
|
$this->get(route('superuser.log-aktivitas.index', ['q' => 'rina']))->assertSee('LAB-01')->assertDontSee('UPTtik-PC');
|
|
$this->get(route('superuser.log-aktivitas.index', ['pengguna' => 1]))->assertSee('UPTtik-PC')->assertSee('TU04')->assertDontSee('LAB-01');
|
|
$this->get(route('superuser.log-aktivitas.index', ['dari' => '2017-01-01', 'sampai' => '2017-12-31']))->assertSee('LAB-01')->assertDontSee('UPTtik-PC')->assertDontSee('TU04');
|
|
$this->get(route('superuser.log-aktivitas.index', ['dari' => 'bukan-tanggal']))->assertOk()->assertSee('UPTtik-PC');
|
|
}
|
|
|
|
public function test_health_reports_the_database_connection(): void
|
|
{
|
|
$this->get(route('superuser.kesehatan-sistem'))->assertOk()
|
|
->assertSee('Koneksi basis data SIAKAD')
|
|
->assertSee('Terhubung ke basis data SIAKAD.')
|
|
->assertSee('Basis data SIAKAD')
|
|
->assertSee('Waktu respons');
|
|
}
|
|
|
|
public function test_academic_calendar_is_real(): void
|
|
{
|
|
$this->get(route('kalender.index', ['periode' => 368]))->assertOk()
|
|
->assertSee('Perkuliahan tahap I')->assertSee('Maulid Nabi')->assertSee('UTS')
|
|
->assertDontSee('Libur antar semester')
|
|
->assertSee('Periode akademik');
|
|
|
|
$this->get(route('kalender.index', ['periode' => 371]))->assertOk()
|
|
->assertSee('Libur antar semester')->assertDontSee('Maulid Nabi');
|
|
}
|
|
|
|
public function test_other_roles_are_forbidden(): void
|
|
{
|
|
$this->post(route('logout'));
|
|
Auth::forgetGuards();
|
|
$this->post(route('login.store'), ['username' => '198001012005011001', 'password' => 'rahasia-staf']);
|
|
|
|
foreach (['superuser.pengguna.index', 'superuser.hak-akses', 'superuser.log-aktivitas.index', 'superuser.kesehatan-sistem'] as $route) {
|
|
$this->get(route($route))->assertForbidden();
|
|
}
|
|
$this->put(route('superuser.pengguna.status', 2), ['aktif' => 0])->assertForbidden();
|
|
$this->assertSame(0, $this->accountStatus(2));
|
|
}
|
|
}
|