Files
siakad/tests/Feature/Operator/LegacyEmployeeListTest.php
T
2026-11-11 09:28:34 +07:00

155 lines
7.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Feature\Operator;
use App\Models\User;
use DOMXPath;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Testing\TestResponse;
use Tests\Concerns\FakesLegacyDatabase;
use Tests\Concerns\InspectsHtml;
use Tests\TestCase;
/**
* Portal > Pegawai (operator.halaman list_pegawai) dari DB lama; data dari seedLegacyEmployees().
* Tampilan sama dengan prototipe SATU; filter, cari, dan halaman diambil dari operator.pegawai.data (JSON).
*/
class LegacyEmployeeListTest extends TestCase
{
use FakesLegacyDatabase;
use InspectsHtml;
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->useLegacyDatabase();
$this->seedLegacyOverview();
$this->seedLegacyEmployees();
$this->post(route('login.store'), ['username' => '198001012005011001', 'password' => 'rahasia-staf'])
->assertRedirect(route('operator.dashboard'));
}
private function data(array $query = []): TestResponse
{
return $this->getJson(route('operator.pegawai.data', $query))->assertOk();
}
/** @return array<string, list<string>> nama => [nip, l/p, nidn, nuptk, telp, email, status] */
private function rows(array $query = []): array
{
return collect($this->data($query)->json('rows'))
->mapWithKeys(fn (array $r): array => [$r['nama'] => [$r['nip'], $r['kelamin'], $r['nidn'], $r['nuptk'], $r['telepon'], $r['email'], $r['status']]])
->all();
}
/** Judul kolom, label filter, tombol bilah, dan aksi baris halaman daftar. */
private function layout(DOMXPath $xpath): array
{
$panel = "//*[@aria-label='Daftar Pegawai']";
$texts = fn (string $query): array => array_map(fn ($node): string => $this->textOf($node), iterator_to_array($xpath->query($query)));
return [
'headers' => $texts("{$panel}//thead/tr/th"),
'filters' => $texts("//section[@aria-label='Filter data']//label"),
'tools' => $texts("{$panel}//div[contains(@class, 'satu-bar__tools')]//button"),
'row_actions' => array_map(fn ($node): string => $node->getAttribute('aria-label'), iterator_to_array($xpath->query("{$panel}//tbody//td[contains(@class, 'table__actions')]//*[self::button or self::a]"))),
'pager' => $xpath->query("//nav[@aria-label='Halaman data']")->length,
];
}
public function test_page_matches_the_prototype_layout(): void
{
$response = $this->get(route('operator.halaman', 'list_pegawai'))->assertOk()
->assertSee('Data pegawai asli dari SIAKAD')
->assertSee('satuRemoteTable', false)
->assertSee('Siti Aminah');
$legacy = $this->layout($this->dom($response));
$this->assertSame(['', 'NIP', 'Nama', 'L/P', 'NIDN', 'NUPTK', 'No. Telp.', 'Email', 'Status', 'Aksi'], $legacy['headers']);
$this->assertSame(['Kuota Pembimbing Akademik', 'Status Aktif', 'Status'], $legacy['filters']);
$this->assertSame(['Tambah', 'Hapus'], $legacy['tools']);
$this->assertSame(['Lihat', 'Hapus'], $legacy['row_actions']);
$this->assertSame(0, $this->dom($response)->query('//main//form[not(ancestor::dialog)]')->length, 'Filter tidak memakai formulir (alamat tetap).');
$this->assertSame(1, $this->dom($response)->query("//a[@aria-current='page' and contains(@href, '/list_pegawai')]")->length);
// Prototipe akun demo: susunan yang sama persis.
Auth::logout();
config(['auth.guards.web.provider' => 'users']);
Auth::forgetGuards();
$prototype = $this->actingAs(User::factory()->operator()->create())->get(route('operator.halaman', 'list_pegawai'))->assertOk();
$expected = $this->layout($this->dom($prototype));
// Akun asli: tanpa Aksi dan Cetak, dengan Tambah dan Hapus yang sudah bekerja; baris tanpa Ubah (diubah di halaman detail).
$expected['tools'] = ['Tambah', 'Hapus'];
$expected['row_actions'] = ['Lihat', 'Hapus'];
$this->assertSame($expected, $legacy);
}
public function test_data_lists_employees_of_the_operators_units(): void
{
$this->data()->assertJson(['page' => 1, 'last_page' => 1, 'total' => 4, 'per_page' => 10]);
$this->assertSame([
'Andi Wijaya, S.T., M.Eng.' => ['198502022010121001', 'L', '0002028501', '–', '–', '–', 'Tidak Tetap'],
'Dewi Kartika' => ['199003032015032001', 'P', '–', '–', '0561-123456', '–', 'Belum diisi'],
'Lina' => ['lina', '–', '–', '–', '–', '–', 'Tanpa status'],
'Siti Aminah, M.Pd.' => ['197501012000031002', 'P', '0001017501', '–', '081234567890', 'siti@untan.ac.id', 'Tetap'],
], $this->rows(), 'Dosen prodi lain, pegawai tanpa kaitan, dan jabatan lama tidak ikut.');
$this->assertSame(['gold', null, null, 'success'], array_column($this->data()->json('rows'), 'tone'));
}
public function test_search_and_filters(): void
{
$names = fn (array $query): array => array_keys($this->rows($query));
$this->assertSame(['Dewi Kartika'], $names(['q' => 'kartika']));
$this->assertSame(['Siti Aminah, M.Pd.'], $names(['q' => '0001017501']));
$this->assertSame(['Siti Aminah, M.Pd.'], $names(['advisor' => 'ada']));
$this->assertSame(['Andi Wijaya, S.T., M.Eng.', 'Dewi Kartika', 'Lina'], $names(['advisor' => 'tidak']));
$this->assertSame(['Andi Wijaya, S.T., M.Eng.'], $names(['active' => 'nonaktif']));
$this->assertSame(['Dewi Kartika', 'Lina', 'Siti Aminah, M.Pd.'], $names(['active' => 'aktif']));
$this->assertSame(['Siti Aminah, M.Pd.'], $names(['status' => 2]));
$this->assertSame(['Lina'], $names(['status' => 1]));
$this->assertSame(['Dewi Kartika'], $names(['status' => 0]));
$this->data(['q' => 'tidak-ada'])->assertJson(['rows' => [], 'total' => 0]);
}
public function test_unknown_filter_values_are_ignored_and_paging_works(): void
{
$this->assertCount(4, $this->rows(['advisor' => 'semua', 'active' => 'x', 'status' => 99]));
foreach (range(30, 40) as $id) {
DB::connection('untan')->table('t_pegawai')->insert(['idpegawai' => $id, 'nip' => 'N'.$id, 'nama' => 'Zaki '.$id]);
DB::connection('untan')->table('t_dosen')->insert(['idpegawai' => $id, 'idprogdi' => 21]);
}
$this->data()->assertJson(['total' => 15, 'last_page' => 2])->assertJsonCount(10, 'rows');
$this->data(['page' => 2])->assertJsonCount(5, 'rows');
$this->data(['per' => 25])->assertJson(['per_page' => 25, 'last_page' => 1]);
}
public function test_account_without_programs_sees_empty_state(): void
{
DB::connection('untan')->table('t_unituser')->whereNotNull('idprogdi')->delete();
$this->get(route('operator.halaman', 'list_pegawai'))->assertOk()->assertSee('Akun belum terhubung ke program studi');
$this->getJson(route('operator.pegawai.data'))->assertNotFound();
}
public function test_data_endpoint_access(): void
{
Auth::logout();
$this->get(route('operator.pegawai.data'))->assertRedirect(route('login'));
$this->post(route('login.store'), ['username' => 'D1041191001', 'password' => 'rahasia-mhs']);
$this->getJson(route('operator.pegawai.data'))->assertForbidden();
Auth::logout();
config(['auth.guards.web.provider' => 'users']);
Auth::forgetGuards();
$this->actingAs(User::factory()->operator()->create())->getJson(route('operator.pegawai.data'))->assertNotFound();
}
}