240 lines
16 KiB
PHP
240 lines
16 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Operator;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Database\ConnectionInterface;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Tests\Concerns\FakesLegacyDatabase;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* CRUD Portal > Mahasiswa (t_mahasiswa) dan Portal > Pegawai (t_pegawai + t_dosen) berdata asli lewat modal remote-list.
|
|
* Akun staf fakultas memegang prodi 21 dan 22 (fakultas 5); prodi 99 di luar cakupan.
|
|
*/
|
|
class LegacyStudentEmployeeCrudTest extends TestCase
|
|
{
|
|
use FakesLegacyDatabase;
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->useLegacyDatabase();
|
|
$this->seedLegacyOverview();
|
|
$this->seedLegacyEmployees();
|
|
$this->seedLegacyCurriculum();
|
|
$this->seedLegacyPortal();
|
|
$this->post(route('login.store'), ['username' => '198001012005011001', 'password' => 'rahasia-staf'])
|
|
->assertRedirect(route('operator.dashboard'));
|
|
}
|
|
|
|
private function db(): ConnectionInterface
|
|
{
|
|
return DB::connection('untan');
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
private function student(array $override = []): array
|
|
{
|
|
return $override + [
|
|
'nim' => 'd1041211001', 'nama' => 'Siti Rahma', 'prodi' => 21, 'kurikulum' => 900, 'angkatan' => 160, 'program' => 1, 'jalur' => 3,
|
|
'kelamin' => 2, 'tmplahir' => 'Pontianak', 'tgllahir' => '2003-05-17', 'hp' => '081200000000', 'email' => 'siti.rahma@student.untan.ac.id', 'status' => 1,
|
|
];
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
private function employee(array $override = []): array
|
|
{
|
|
return $override + [
|
|
'nip' => '199201012020121001', 'nama' => 'Hendra Saputra', 'gelardepan' => 'Dr.', 'gelarbelakang' => 'M.Kom.', 'kelamin' => 1,
|
|
'nidn' => '0001019201', 'hp' => '08111111', 'telepon' => '0561-7777', 'email' => 'hendra@untan.ac.id', 'status' => 2, 'aktif' => 1,
|
|
];
|
|
}
|
|
|
|
private function studentRow(int $id): ?array
|
|
{
|
|
return collect($this->getJson(route('operator.mahasiswa.data', ['per' => 25]))->assertOk()->json('rows'))->firstWhere('id', $id);
|
|
}
|
|
|
|
public function test_guests_and_other_roles_cannot_write(): void
|
|
{
|
|
$this->post(route('logout'));
|
|
foreach ([['post', route('operator.list-mahasiswa.store')], ['put', route('operator.list-mahasiswa.update', 500)], ['post', route('operator.list-mahasiswa.destroy')],
|
|
['put', route('operator.list-pegawai.update', 11)], ['post', route('operator.list-pegawai.destroy')]] as [$method, $url]) {
|
|
$this->{$method}($url)->assertRedirect(route('login'));
|
|
}
|
|
|
|
Auth::forgetGuards();
|
|
config(['auth.guards.web.provider' => 'users']);
|
|
$this->actingAs(User::factory()->dosen()->create())->post(route('operator.list-mahasiswa.store'), $this->student())->assertForbidden();
|
|
$this->actingAs(User::factory()->mahasiswa()->create())->put(route('operator.list-pegawai.update', 11), $this->employee())->assertForbidden();
|
|
$this->assertSame(0, $this->db()->table('t_mahasiswa')->where('nim', 'D1041211001')->count());
|
|
}
|
|
|
|
public function test_pages_wire_the_real_buttons(): void
|
|
{
|
|
$this->get(route('operator.halaman', 'list_mahasiswa'))->assertOk()
|
|
->assertSee('startInsert()', false)->assertSee('Tambah mahasiswa')->assertSee('Program kuliah')->assertSee('Hapus mahasiswa?')
|
|
->assertDontSee("demo('Tambah')", false);
|
|
$this->get(route('operator.halaman', 'list_pegawai'))->assertOk()
|
|
->assertSee('startEdit(row)', false)->assertSee('Status kepegawaian')->assertSee('Hapus pegawai?')->assertSee('askRemove([row.id])', false)
|
|
->assertSee('startView(row)', false)->assertSee('Detail pegawai')->assertDontSee("demo('", false)->assertDontSee('Aksi'.': tampilan prototipe');
|
|
}
|
|
|
|
public function test_student_create_and_rows(): void
|
|
{
|
|
$id = $this->postJson(route('operator.list-mahasiswa.store'), $this->student())->assertCreated()->assertJsonPath('message', 'Mahasiswa ditambahkan.')->json('id');
|
|
|
|
$m = $this->db()->table('t_mahasiswa')->where('idmahasiswa', $id)->first();
|
|
$this->assertSame(['D1041211001', 'Siti Rahma', 21, 900, 160, 1, 3, 2, 1], [$m->nim, $m->nama, (int) $m->idprogdi, (int) $m->idkurikulum, (int) $m->idangkatan, (int) $m->idprogram, (int) $m->jalur_masuk, (int) $m->kelamin, (int) $m->stataktif]);
|
|
$this->assertSame(['SIAKAD', '198001012005011'], [$m->cname, $m->uname]);
|
|
|
|
$row = $this->studentRow($id);
|
|
$this->assertSame('D1041211001', $row['nim']);
|
|
$this->assertSame(
|
|
['nim' => 'D1041211001', 'nama' => 'Siti Rahma', 'prodi' => 21, 'kurikulum' => 900, 'angkatan' => 160, 'program' => 1, 'jalur' => 3, 'kelamin' => 2,
|
|
'tmplahir' => 'Pontianak', 'tgllahir' => '2003-05-17', 'hp' => '081200000000', 'email' => 'siti.rahma@student.untan.ac.id', 'status' => 1],
|
|
$row['form'],
|
|
);
|
|
|
|
// Jalur masuk boleh kosong pada tambah (bawaan 1).
|
|
$other = $this->postJson(route('operator.list-mahasiswa.store'), $this->student(['nim' => 'D1041211002', 'jalur' => null]))->assertCreated()->json('id');
|
|
$this->assertSame(1, (int) $this->db()->table('t_mahasiswa')->where('idmahasiswa', $other)->value('jalur_masuk'));
|
|
}
|
|
|
|
public function test_student_create_is_validated_and_guarded(): void
|
|
{
|
|
$url = route('operator.list-mahasiswa.store');
|
|
$this->postJson($url, [])->assertStatus(422)->assertJsonValidationErrors(['nim', 'nama', 'prodi', 'kurikulum', 'angkatan', 'program', 'kelamin', 'status']);
|
|
$this->postJson($url, $this->student(['email' => 'bukan-email', 'tgllahir' => 'kemarin', 'kelamin' => 7]))->assertStatus(422)->assertJsonValidationErrors(['email', 'tgllahir', 'kelamin']);
|
|
|
|
$this->postJson($url, $this->student(['prodi' => 99, 'kurikulum' => 901]))->assertStatus(422)->assertJsonPath('message', 'Program studi harus dipilih dari program studi Anda.');
|
|
// Kurikulum prodi 22 untuk mahasiswa prodi 21 ditolak.
|
|
$this->postJson($url, $this->student(['kurikulum' => 902]))->assertStatus(422)->assertJsonPath('message', 'Kurikulum harus dipilih dari program studi mahasiswa.');
|
|
$this->postJson($url, $this->student(['angkatan' => 9999]))->assertStatus(422)->assertJsonPath('message', 'Angkatan tidak ditemukan.');
|
|
$this->postJson($url, $this->student(['program' => 9]))->assertStatus(422)->assertJsonPath('message', 'Program kuliah tidak ditemukan.');
|
|
$this->postJson($url, $this->student(['jalur' => 9]))->assertStatus(422)->assertJsonPath('message', 'Jalur masuk tidak ditemukan.');
|
|
$this->postJson($url, $this->student(['nim' => 'a b!']))->assertStatus(422)->assertJsonPath('message', 'NIM hanya boleh huruf dan angka (5 sampai 15 karakter).');
|
|
$this->postJson($url, $this->student(['nim' => 'd1041191001']))->assertStatus(422)->assertJsonPath('message', 'NIM D1041191001 sudah terdaftar.');
|
|
$this->postJson($url, $this->student(['status' => 2]))->assertStatus(422)->assertJsonPath('message', 'Status Lulus ditetapkan lewat yudisium, bukan di sini.');
|
|
$this->assertSame(0, $this->db()->table('t_mahasiswa')->where('nim', 'D1041211001')->count());
|
|
}
|
|
|
|
public function test_student_update_keeps_scope_and_nim(): void
|
|
{
|
|
$url = fn (int $id): string => route('operator.list-mahasiswa.update', $id);
|
|
$payload = $this->student(['nim' => 'X501', 'nama' => 'Nama Baru', 'prodi' => 22, 'kurikulum' => 902, 'status' => 3, 'jalur' => null]);
|
|
|
|
$this->putJson($url(501), $payload)->assertOk()->assertJsonPath('message', 'Data mahasiswa diperbarui.');
|
|
$m = $this->db()->table('t_mahasiswa')->where('idmahasiswa', 501)->first();
|
|
$this->assertSame(['Nama Baru', 22, 902, 3, 1], [$m->nama, (int) $m->idprogdi, (int) $m->idkurikulum, (int) $m->stataktif, (int) $m->jalur_masuk]);
|
|
$this->assertSame('198001012005011', $m->uname);
|
|
|
|
$this->putJson($url(502), $this->student(['nim' => 'X999']))->assertStatus(422)->assertJsonPath('message', 'NIM tidak dapat diubah. Buat mahasiswa baru dengan NIM yang sesuai.');
|
|
$this->putJson($url(502), $this->student(['nim' => 'X502', 'status' => 2]))->assertStatus(422)->assertJsonPath('message', 'Status Lulus ditetapkan lewat yudisium, bukan di sini.');
|
|
// Mahasiswa yang sudah lulus boleh disunting tanpa mengubah status.
|
|
$this->putJson($url(505), $this->student(['nim' => 'X505', 'status' => 2]))->assertOk();
|
|
// Mahasiswa prodi 99 dan ID yang tidak ada ditolak.
|
|
$this->putJson($url(600), $this->student(['nim' => 'X600']))->assertStatus(422)->assertJsonPath('message', 'Mahasiswa tidak ditemukan.');
|
|
$this->putJson($url(9999), $this->student())->assertStatus(422)->assertJsonPath('message', 'Mahasiswa tidak ditemukan.');
|
|
$this->assertSame('Mahasiswa Luar', $this->db()->table('t_mahasiswa')->where('idmahasiswa', 600)->value('nama'));
|
|
// Pindah ke prodi di luar cakupan ditolak.
|
|
$this->putJson($url(502), $this->student(['nim' => 'X502', 'prodi' => 99, 'kurikulum' => 901]))->assertStatus(422)->assertJsonPath('message', 'Program studi harus dipilih dari program studi Anda.');
|
|
}
|
|
|
|
public function test_student_delete_only_without_dependents(): void
|
|
{
|
|
$url = route('operator.list-mahasiswa.destroy');
|
|
$new = $this->postJson(route('operator.list-mahasiswa.store'), $this->student())->assertCreated()->json('id');
|
|
$this->db()->table('t_evaluasi_rekap')->insert(['idmahasiswa' => $new]);
|
|
|
|
// Punya KRS (500), akun login, atau pembayaran: ditolak, dan tidak ada yang terhapus walau dicampur baris yang boleh.
|
|
$this->postJson($url, ['ids' => [$new, 500]])->assertStatus(422)->assertJsonPath('message', 'Mahasiswa D1041191001 sudah punya data akademik, keuangan, atau akun login sehingga tidak boleh dihapus.');
|
|
$this->assertSame(1, $this->db()->table('t_mahasiswa')->where('idmahasiswa', $new)->count());
|
|
|
|
$this->db()->table('t_account')->insert(['iduser' => 900, 'username' => 'x', 'passwd' => 'y', 'identitas' => 503, 'jenis' => 1]);
|
|
$this->postJson($url, ['ids' => [503]])->assertStatus(422)->assertJsonPath('message', 'Mahasiswa X503 sudah punya data akademik, keuangan, atau akun login sehingga tidak boleh dihapus.');
|
|
$this->db()->table('t_keuanganmhs')->insert(['idmahasiswa' => 504, 'jenis' => 1, 'idperiode' => 368, 'jumlah' => 1000]);
|
|
$this->postJson($url, ['ids' => [504]])->assertStatus(422);
|
|
$this->assertSame(2, $this->db()->table('t_mahasiswa')->whereIn('idmahasiswa', [503, 504])->count());
|
|
|
|
// Di luar cakupan prodi atau tidak ada.
|
|
$this->postJson($url, ['ids' => [600]])->assertStatus(422)->assertJsonPath('message', 'Mahasiswa tidak ditemukan.');
|
|
$this->postJson($url, ['ids' => []])->assertStatus(422)->assertJsonValidationErrors('ids');
|
|
$this->assertSame(1, $this->db()->table('t_mahasiswa')->where('idmahasiswa', 600)->count());
|
|
|
|
$this->postJson($url, ['ids' => [$new]])->assertOk()->assertJsonPath('message', '1 mahasiswa dihapus.');
|
|
$this->assertSame(0, $this->db()->table('t_mahasiswa')->where('idmahasiswa', $new)->count());
|
|
$this->assertSame(0, $this->db()->table('t_evaluasi_rekap')->where('idmahasiswa', $new)->count());
|
|
}
|
|
|
|
public function test_employee_rows_carry_the_edit_form_and_have_no_create_route(): void
|
|
{
|
|
$rows = collect($this->getJson(route('operator.pegawai.data', ['per' => 25]))->assertOk()->json('rows'));
|
|
$this->assertSame(
|
|
['nip' => '197501012000031002', 'nama' => 'Siti Aminah', 'gelardepan' => '', 'gelarbelakang' => 'M.Pd.', 'kelamin' => 2, 'nidn' => '0001017501', 'hp' => '081234567890',
|
|
'telepon' => '', 'email' => 'siti@untan.ac.id', 'status' => 2, 'aktif' => 1],
|
|
$rows->firstWhere('id', 11)['form'],
|
|
);
|
|
$this->assertSame(0, $rows->firstWhere('id', 20)['form']['aktif']);
|
|
$this->assertFalse(Route::has('operator.list-pegawai.store'));
|
|
}
|
|
|
|
public function test_employee_update_is_scoped_and_keeps_activity_code(): void
|
|
{
|
|
$url = fn (int $id): string => route('operator.list-pegawai.update', $id);
|
|
|
|
$this->putJson($url(11), $this->employee(['nip' => '197501012000031002', 'nama' => 'Siti Aminah Baru', 'nidn' => '', 'status' => null]))
|
|
->assertOk()->assertJsonPath('message', 'Data pegawai diperbarui.');
|
|
$p = $this->db()->table('t_pegawai')->where('idpegawai', 11)->first();
|
|
$this->assertSame(['Siti Aminah Baru', null, null, 'M.Kom.'], [$p->nama, $p->nidn, $p->idstatuspegawai, $p->gelarbelakang]);
|
|
$this->assertSame(21, (int) $this->db()->table('t_dosen')->where('idpegawai', 11)->value('idprogdi'));
|
|
|
|
// Kode aktif lain (3) dipertahankan saat tetap aktif; Tidak aktif menjadi 0; aktif kembali menjadi 1.
|
|
$this->putJson($url(24), $this->employee(['nip' => 'lina']))->assertOk();
|
|
$this->assertSame(3, (int) $this->db()->table('t_pegawai')->where('idpegawai', 24)->value('statusaktif'));
|
|
$this->putJson($url(24), $this->employee(['nip' => 'lina', 'aktif' => 0]))->assertOk();
|
|
$this->assertSame(0, (int) $this->db()->table('t_pegawai')->where('idpegawai', 24)->value('statusaktif'));
|
|
$this->putJson($url(24), $this->employee(['nip' => 'lina', 'aktif' => 1]))->assertOk();
|
|
$this->assertSame(1, (int) $this->db()->table('t_pegawai')->where('idpegawai', 24)->value('statusaktif'));
|
|
|
|
$this->putJson($url(21), $this->employee(['nip' => '197501012000031002']))->assertStatus(422)->assertJsonPath('message', 'NIP 197501012000031002 sudah terdaftar.');
|
|
// Pegawai di luar cakupan (home base prodi 99, jabatan tidak aktif) dan ID tidak ada.
|
|
$this->putJson($url(22), $this->employee(['nip' => '197001012000011099']))->assertStatus(422)->assertJsonPath('message', 'Pegawai tidak ditemukan.');
|
|
$this->putJson($url(23), $this->employee(['nip' => '196501011990011023']))->assertStatus(422)->assertJsonPath('message', 'Pegawai tidak ditemukan.');
|
|
$this->putJson($url(9999), $this->employee())->assertStatus(422)->assertJsonPath('message', 'Pegawai tidak ditemukan.');
|
|
$this->assertSame('Joko Prodi Lain', $this->db()->table('t_pegawai')->where('idpegawai', 22)->value('nama'));
|
|
}
|
|
|
|
public function test_employee_delete_only_when_unreferenced(): void
|
|
{
|
|
$url = route('operator.list-pegawai.destroy');
|
|
$new = 30;
|
|
$this->db()->table('t_pegawai')->insert(['idpegawai' => $new, 'nip' => '199201012020121001', 'nama' => 'Hendra Saputra']);
|
|
$this->db()->table('t_dosen')->insert(['idpegawai' => $new, 'idprogdi' => 21]);
|
|
|
|
// 11 membimbing mahasiswa (idpa), 21 memegang jabatan aktif: ditolak, baris yang boleh ikut batal.
|
|
$this->postJson($url, ['ids' => [$new, 11]])->assertStatus(422)->assertJsonPath('message', 'Pegawai Siti Aminah masih dirujuk jabatan, jadwal, bimbingan, skripsi, atau akun login sehingga tidak boleh dihapus.');
|
|
$this->assertSame(1, $this->db()->table('t_pegawai')->where('idpegawai', $new)->count());
|
|
$this->postJson($url, ['ids' => [21]])->assertStatus(422);
|
|
|
|
$this->db()->table('t_account')->insert(['iduser' => 901, 'username' => 'p', 'passwd' => 'q', 'identitas' => 24, 'jenis' => 0]);
|
|
$this->postJson($url, ['ids' => [24]])->assertStatus(422);
|
|
$this->assertSame(3, $this->db()->table('t_pegawai')->whereIn('idpegawai', [11, 21, 24])->count());
|
|
|
|
// Di luar cakupan atau tidak ada.
|
|
$this->postJson($url, ['ids' => [22, 9999]])->assertStatus(422)->assertJsonPath('message', 'Pegawai tidak ditemukan.');
|
|
$this->assertSame(1, $this->db()->table('t_pegawai')->where('idpegawai', 22)->count());
|
|
$this->postJson($url, [])->assertStatus(422)->assertJsonValidationErrors('ids');
|
|
|
|
$this->postJson($url, ['ids' => [$new]])->assertOk()->assertJsonPath('message', '1 pegawai dihapus.');
|
|
$this->assertSame(0, $this->db()->table('t_pegawai')->where('idpegawai', $new)->count());
|
|
$this->assertSame(0, $this->db()->table('t_dosen')->where('idpegawai', $new)->count());
|
|
}
|
|
}
|