304 lines
16 KiB
PHP
304 lines
16 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Operator;
|
||
|
||
use App\Models\User;
|
||
use App\Support\Legacy\PortalException;
|
||
use App\Support\Legacy\Reports\ReportContext;
|
||
use App\Support\Legacy\Reports\ReportRegistry;
|
||
use App\Support\Legacy\Reports\ReportResult;
|
||
use App\Support\Legacy\Reports\ReportSource;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use ReflectionProperty;
|
||
use Tests\Concerns\FakesLegacyDatabase;
|
||
use Tests\TestCase;
|
||
|
||
/** Mesin menu Laporan untuk akun DB lama: formulir otomatis, JSON berhalaman, validasi parameter, unduh CSV, cetak, akses. */
|
||
class LegacyLaporanTest extends TestCase
|
||
{
|
||
use FakesLegacyDatabase;
|
||
use RefreshDatabase;
|
||
|
||
private string $slug;
|
||
|
||
private ?array $original = null;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
$this->useLegacyDatabase();
|
||
$this->seedLegacyOverview();
|
||
|
||
// Satu slug laporan nyata diarahkan ke sumber tiruan agar mesinnya diuji tanpa bergantung pada data
|
||
$this->slug = ReportRegistry::slugs()[0];
|
||
$property = new ReflectionProperty(ReportRegistry::class, 'map');
|
||
$this->original = $property->getValue();
|
||
$map = ReportRegistry::slugs();
|
||
$property->setValue(array_fill_keys($map, FakeReportSource::class));
|
||
FakeReportSource::$slugs = $map;
|
||
FakeReportSource::$lastContext = null;
|
||
FakeReportSource::$lastLimit = null;
|
||
FakeReportSource::$textParam = 'q';
|
||
|
||
$this->post(route('login.store'), ['username' => '198001012005011001', 'password' => 'rahasia-staf'])
|
||
->assertRedirect(route('operator.dashboard'));
|
||
}
|
||
|
||
protected function tearDown(): void
|
||
{
|
||
(new ReflectionProperty(ReportRegistry::class, 'map'))->setValue($this->original);
|
||
|
||
parent::tearDown();
|
||
}
|
||
|
||
private function data(array $query = [])
|
||
{
|
||
return $this->getJson(route('operator.laporan.data', ['slug' => $this->slug] + $query));
|
||
}
|
||
|
||
public function test_halaman_menjalankan_laporan_otomatis_tanpa_tombol_tampilkan(): void
|
||
{
|
||
// Laporan tampil sebagai daftar biasa (remote-list): bilah filter dari parameter, tabel berkolom dinamis, tanpa
|
||
// panel "Parameter laporan", KOP, maupun tombol Tampilkan.
|
||
$page = $this->get(route('operator.halaman', ['slug' => $this->slug]))->assertOk()
|
||
->assertSee('satuRemoteTable', false)->assertDontSee('Parameter laporan')->assertDontSee('Gunakan KOP')->assertDontSee('laporanReport', false)
|
||
->assertSee('Unduh (CSV)')->assertDontSee('Lihat di Tab Baru')->assertSee('Catatan uji laporan.')
|
||
->assertSee('Lengkapi filter yang wajib diisi (Program Studi)')->assertDontSee('Tampilkan');
|
||
foreach (['filter_prodi', 'filter_q', 'filter_tgl', 'filter_n'] as $name) {
|
||
$page->assertSee('name="'.$name.'"', false);
|
||
}
|
||
$page->assertDontSee('name="filter_kop"', false)->assertDontSee('id="satu-q"', false);
|
||
|
||
// Nilai awal dari alamat (bisa dibagikan): pilihan dan teks masuk ke nilai filter
|
||
$page = $this->get(route('operator.halaman', ['slug' => $this->slug, 'prodi' => '2', 'q' => 'Budi']))->assertOk();
|
||
// @js menulis tanda kutip JSON sebagai "
|
||
$json = fn (string $text): string => str_replace('"', chr(92).'u0022', $text);
|
||
$page->assertSee($json('"prodi":"2"'), false)->assertSee($json('"q":"Budi"'), false)->assertDontSee('Lengkapi filter yang wajib diisi');
|
||
$this->assertStringContainsString($json('"nama":"Prodi B"'), $page->getContent(), 'Halaman pertama ikut dirender sebagai data awal.');
|
||
$this->assertStringContainsString($json('"columns":[{"key":"nama","label":"Nama","num":false},{"key":"n","label":"Jumlah","num":true}]'), $page->getContent(), 'Kolom awal ikut dikirim agar tabel langsung tergambar.');
|
||
}
|
||
|
||
public function test_json_berhalaman_dengan_penomoran(): void
|
||
{
|
||
$first = $this->data(['prodi' => '1'])->assertOk()
|
||
->assertJsonPath('page', 1)->assertJsonPath('per_page', 25)->assertJsonPath('total', 1500)->assertJsonPath('last_page', 60)->assertJsonPath('from', 1)
|
||
->assertJsonPath('capped', false)->assertJsonPath('note', 'Catatan hasil.')->assertJsonPath('columns.1.num', true);
|
||
$this->assertCount(25, $first->json('rows'));
|
||
$this->assertSame(['id' => 1, 'nama' => 'Prodi A', 'n' => 1], $first->json('rows.0'), 'Baris daftar diberi id urut.');
|
||
$this->assertSame(26, FakeReportSource::$lastLimit, 'Sumber diminta halaman × per halaman + 1 baris.');
|
||
|
||
$second = $this->data(['prodi' => '1', 'page' => 2])->assertOk()->assertJsonPath('page', 2)->assertJsonPath('from', 26);
|
||
$this->assertSame(26, $second->json('rows.0.n'));
|
||
$this->assertSame(51, FakeReportSource::$lastLimit);
|
||
|
||
$this->data(['prodi' => '2', 'page' => 3, 'per' => 100])->assertOk()->assertJsonPath('per_page', 100)->assertJsonPath('from', 201)->assertJsonPath('rows.0.n', 201)->assertJsonPath('rows.0.nama', 'Prodi B');
|
||
$this->data(['prodi' => '1', 'per' => 7])->assertOk()->assertJsonPath('per_page', 25);
|
||
// Halaman di luar jangkauan dijepit ke halaman terakhir
|
||
$last = $this->data(['prodi' => '1', 'page' => 9999])->assertOk()->assertJsonPath('page', 60)->assertJsonPath('from', 1476);
|
||
$this->assertCount(25, $last->json('rows'));
|
||
$this->data(['prodi' => '1', 'page' => 0])->assertOk()->assertJsonPath('page', 1);
|
||
}
|
||
|
||
public function test_batas_baris_yang_bisa_dilihat_berhalaman(): void
|
||
{
|
||
$this->data(['prodi' => '1', 'q' => 'besar', 'per' => 100, 'page' => 999])->assertOk()
|
||
->assertJsonPath('capped', true)->assertJsonPath('cap', 5000)->assertJsonPath('total', 9000)->assertJsonPath('last_page', 50)->assertJsonPath('page', 50);
|
||
$this->assertSame(5001, FakeReportSource::$lastLimit, 'Pengambilan dibatasi 5.000 baris pertama.');
|
||
}
|
||
|
||
public function test_ringkasan_dan_hasil_kosong(): void
|
||
{
|
||
$this->data(['prodi' => '1', 'q' => ' Budi '])->assertOk()->assertJsonPath('summary.0', 'Cari: Budi');
|
||
$this->data(['prodi' => '2', 'q' => 'kosong'])->assertOk()->assertJsonPath('rows', [])->assertJsonPath('total', 0)->assertJsonPath('last_page', 1)->assertJsonPath('page', 1);
|
||
}
|
||
|
||
public function test_parameter_wajib_dan_tidak_valid_ditolak_422(): void
|
||
{
|
||
$this->data()->assertStatus(422)->assertJsonPath('errors.prodi', 'Program Studi wajib diisi.')->assertJsonPath('message', 'Program Studi wajib diisi.');
|
||
$this->data(['prodi' => '999'])->assertStatus(422)->assertJsonPath('errors.prodi', 'Program Studi tidak valid.');
|
||
$this->data(['prodi' => '1', 'tgl' => '31-12-2024'])->assertStatus(422)->assertJsonPath('errors.tgl', 'Tanggal tidak valid.');
|
||
$this->data(['prodi' => '1', 'n' => 'abc'])->assertStatus(422)->assertJsonPath('errors.n', 'Jumlah tidak valid.');
|
||
$this->data(['prodi' => ['1']])->assertStatus(422)->assertJsonPath('errors.prodi', 'Program Studi wajib diisi.');
|
||
$this->data(['prodi' => '1', 'tgl' => '2024-02-30'])->assertStatus(422);
|
||
$this->data(['prodi' => '1', 'tgl' => '2024-02-29', 'n' => '5'])->assertOk();
|
||
}
|
||
|
||
public function test_kesalahan_bisnis_dari_sumber_422(): void
|
||
{
|
||
$this->data(['prodi' => '1', 'q' => 'galat'])->assertStatus(422)->assertJsonPath('message', 'Periode belum dipilih.');
|
||
}
|
||
|
||
public function test_konteks_berisi_prodi_akun(): void
|
||
{
|
||
$this->data(['prodi' => '1'])->assertOk();
|
||
|
||
$this->assertInstanceOf(ReportContext::class, FakeReportSource::$lastContext);
|
||
$this->assertNotEmpty(FakeReportSource::$lastContext->prodi);
|
||
$this->assertSame(FakeReportSource::$lastContext->prodi, array_keys(FakeReportSource::$lastContext->prodiNames));
|
||
}
|
||
|
||
public function test_unduh_csv_dengan_bom_dan_semua_baris(): void
|
||
{
|
||
$response = $this->get(route('operator.laporan.unduh', ['slug' => $this->slug, 'prodi' => '1', 'q' => 'x']))->assertOk();
|
||
$this->assertStringContainsString('text/csv', $response->headers->get('Content-Type'));
|
||
$this->assertStringContainsString('attachment', $response->headers->get('Content-Disposition'));
|
||
$body = $response->streamedContent();
|
||
$this->assertStringStartsWith("\xEF\xBB\xBF", $body);
|
||
$lines = explode("\n", trim(substr($body, 3)));
|
||
$this->assertSame('Nama,Jumlah', $lines[0]);
|
||
$this->assertCount(1 + 1500, $lines, 'Unduh memakai batas besar, bukan satu halaman.');
|
||
$this->assertSame('"Prodi A",1', $lines[1]);
|
||
}
|
||
|
||
public function test_unduh_dan_cetak_dengan_parameter_salah_kembali_ke_halaman(): void
|
||
{
|
||
$this->get(route('operator.laporan.unduh', ['slug' => $this->slug]))->assertRedirect(route('operator.halaman', ['slug' => $this->slug]));
|
||
$this->get(route('operator.laporan.cetak', ['slug' => $this->slug, 'prodi' => '1', 'q' => 'galat']))->assertRedirect(route('operator.halaman', ['slug' => $this->slug, 'prodi' => '1', 'q' => 'galat']));
|
||
}
|
||
|
||
public function test_cetak_dengan_dan_tanpa_kop(): void
|
||
{
|
||
$this->get(route('operator.laporan.cetak', ['slug' => $this->slug, 'prodi' => '1', 'kop' => 1]))->assertOk()
|
||
->assertSee('Universitas Tanjungpura')->assertSee('Sistem Informasi Akademik (SIAKAD)')->assertSee('Program Studi')->assertSee('Prodi A')
|
||
->assertSee('Dicetak oleh 198001012005011001');
|
||
$this->get(route('operator.laporan.cetak', ['slug' => $this->slug, 'prodi' => '1']))->assertOk()
|
||
->assertDontSee('Sistem Informasi Akademik (SIAKAD)');
|
||
}
|
||
|
||
public function test_akses(): void
|
||
{
|
||
$query = ['slug' => $this->slug, 'prodi' => '1'];
|
||
$urls = [route('operator.laporan.data', $query), route('operator.laporan.unduh', $query), route('operator.laporan.cetak', $query)];
|
||
$page = route('operator.halaman', ['slug' => $this->slug]);
|
||
|
||
Auth::logout();
|
||
foreach ([...$urls, $page] as $url) {
|
||
$this->get($url)->assertRedirect(route('login'));
|
||
}
|
||
|
||
$this->post(route('login.store'), ['username' => 'D1041191001', 'password' => 'rahasia-mhs']);
|
||
foreach ([...$urls, $page] as $url) {
|
||
$this->get($url)->assertForbidden();
|
||
}
|
||
|
||
// Operator akun demo tidak memegang data asli: data/unduh/cetak 404, halamannya tetap prototipe
|
||
Auth::logout();
|
||
config(['auth.guards.web.provider' => 'users']);
|
||
Auth::forgetGuards();
|
||
$this->actingAs(User::factory()->operator()->create());
|
||
foreach ($urls as $url) {
|
||
$this->get($url)->assertNotFound();
|
||
}
|
||
$this->get($page)->assertOk()->assertDontSee('Unduh (CSV)')->assertDontSee('laporanReport', false);
|
||
}
|
||
|
||
public function test_laporan_bergaya_daftar_biasa(): void
|
||
{
|
||
// Daftar Pegawai (DosenReports, 'list' => true) tampil seperti menu biasa: bilah filter, tabel dengan cari dan halaman.
|
||
(new ReflectionProperty(ReportRegistry::class, 'map'))->setValue($this->original);
|
||
$this->seedLegacyEmployees();
|
||
|
||
$page = $this->get(route('operator.halaman', 'repp_daftarpeg'))->assertOk()
|
||
->assertSee('satuRemoteTable', false)->assertDontSee('Parameter laporan')->assertDontSee('Gunakan KOP')->assertDontSee('Lihat di Tab Baru')
|
||
->assertSee('Unduh (CSV)')->assertSee('linkWith(', false)->assertSee(str_replace('/', '\\/', route('operator.laporan.unduh', 'repp_daftarpeg')), false)
|
||
->assertSee('id="satu-q"', false);
|
||
foreach (['filter_homebase', 'filter_jenis', 'filter_kelamin', 'filter_aktif'] as $name) {
|
||
$page->assertSee('name="'.$name.'"', false);
|
||
}
|
||
$page->assertDontSee('name="filter_kop"', false);
|
||
|
||
$json = $this->getJson(route('operator.laporan.data', ['slug' => 'repp_daftarpeg', 'per' => 10]))->assertOk()->json();
|
||
$this->assertSame(10, $json['per_page']);
|
||
$this->assertNotEmpty($json['rows']);
|
||
$this->assertSame(1, $json['rows'][0]['id'], 'Baris daftar diberi id urut.');
|
||
$this->assertContains('Andi Wijaya, S.T., M.Eng.', array_column($json['rows'], 'nama'));
|
||
|
||
$cari = $this->getJson(route('operator.laporan.data', ['slug' => 'repp_daftarpeg', 'q' => 'andi wij']))->assertOk()->json('rows');
|
||
$this->assertSame(['Andi Wijaya, S.T., M.Eng.'], array_column($cari, 'nama'), 'Kotak cari daftar menyaring NIP/NIDN/nama.');
|
||
}
|
||
|
||
public function test_kotak_cari_generik_menyaring_hasil_laporan(): void
|
||
{
|
||
// Sumber tanpa parameter q: kotak cari tampil dan menyaring semua kolom hasil di server (sampai CAP baris).
|
||
FakeReportSource::$textParam = 'cari';
|
||
$this->get(route('operator.halaman', ['slug' => $this->slug, 'prodi' => '1']))->assertOk()->assertSee('id="satu-q"', false);
|
||
|
||
$this->data(['prodi' => '1', 'q' => 'prodi a', 'per' => 10])->assertOk()->assertJsonPath('total', 1500)->assertJsonPath('last_page', 150)->assertJsonPath('rows.0.n', 1);
|
||
$this->assertSame(5001, FakeReportSource::$lastLimit, 'Saringan generik mengambil sampai batas CAP.');
|
||
// Angka yang memuat "150" di antara 1–1500: 150, 1150, 1500
|
||
$this->data(['prodi' => '1', 'q' => '150', 'per' => 10])->assertOk()->assertJsonPath('total', 3)->assertJsonPath('rows.0.n', 150)->assertJsonPath('rows.2.n', 1500);
|
||
$this->data(['prodi' => '1', 'q' => 'tidak ada'])->assertOk()->assertJsonPath('total', 0)->assertJsonPath('rows', []);
|
||
|
||
// Sumber dengan parameter bernama q: kotak cari disembunyikan, q tetap menjadi parameter sumber.
|
||
FakeReportSource::$textParam = 'q';
|
||
$this->get(route('operator.halaman', ['slug' => $this->slug]))->assertOk()->assertDontSee('id="satu-q"', false);
|
||
$this->data(['prodi' => '1', 'q' => 'besar'])->assertOk()->assertJsonPath('total', 9000);
|
||
}
|
||
|
||
public function test_slug_tak_terdaftar_404(): void
|
||
{
|
||
$this->get('/operator/repp_tidakada/hasil')->assertNotFound();
|
||
$this->get('/operator/repp_tidakada/cetak')->assertNotFound();
|
||
$this->get('/operator/repp_tidakada/unduh')->assertNotFound();
|
||
$this->assertTrue(ReportRegistry::has($this->slug));
|
||
$this->assertFalse(ReportRegistry::has('repp_tidakada'));
|
||
}
|
||
}
|
||
|
||
/** Sumber laporan tiruan untuk menguji mesin: satu parameter wajib, teks, tanggal, angka, dan hasil berbatas. */
|
||
final class FakeReportSource implements ReportSource
|
||
{
|
||
/** @var list<string> */
|
||
public static array $slugs = [];
|
||
|
||
public static ?ReportContext $lastContext = null;
|
||
|
||
public static ?int $lastLimit = null;
|
||
|
||
/** Nama parameter teks; 'q' membuat kotak cari daftar disembunyikan (nama bentrok), nama lain memakai saringan generik. */
|
||
public static string $textParam = 'q';
|
||
|
||
public function slugs(): array
|
||
{
|
||
return self::$slugs;
|
||
}
|
||
|
||
public function definition(string $slug, ReportContext $context): array
|
||
{
|
||
return [
|
||
'note' => 'Catatan uji laporan.',
|
||
'params' => [
|
||
['name' => 'prodi', 'label' => 'Program Studi', 'type' => 'select', 'options' => [1 => 'Prodi A', 2 => 'Prodi B'], 'required' => true],
|
||
['name' => self::$textParam, 'label' => 'Cari', 'type' => 'text'],
|
||
['name' => 'tgl', 'label' => 'Tanggal', 'type' => 'date'],
|
||
['name' => 'n', 'label' => 'Jumlah', 'type' => 'number'],
|
||
],
|
||
];
|
||
}
|
||
|
||
public function run(string $slug, array $params, ReportContext $context, int $limit): ReportResult
|
||
{
|
||
self::$lastContext = $context;
|
||
self::$lastLimit = $limit;
|
||
$text = $params[self::$textParam] ?? null;
|
||
if ($text === 'galat') {
|
||
throw new PortalException('Periode belum dipilih.');
|
||
}
|
||
$columns = [['key' => 'nama', 'label' => 'Nama'], ['key' => 'n', 'label' => 'Jumlah', 'num' => true]];
|
||
if ($text === 'kosong') {
|
||
return new ReportResult($columns, []);
|
||
}
|
||
|
||
$total = $text === 'besar' ? 9000 : 1500;
|
||
$label = [1 => 'Prodi A', 2 => 'Prodi B'][$params['prodi']];
|
||
$rows = [];
|
||
foreach (range(1, min($limit, $total)) as $i) {
|
||
$rows[] = ['nama' => $label, 'n' => $i];
|
||
}
|
||
|
||
return new ReportResult($columns, $rows, $total, $text ? ['Cari: '.$text] : [], 'Catatan hasil.');
|
||
}
|
||
}
|