189 lines
13 KiB
PHP
189 lines
13 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature\Operator;
|
||
|
||
use App\Models\User;
|
||
use Illuminate\Database\ConnectionInterface;
|
||
use Illuminate\Database\Schema\Blueprint;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use Illuminate\Testing\TestResponse;
|
||
use Tests\Concerns\FakesLegacyDatabase;
|
||
use Tests\Concerns\InspectsHtml;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* Tahapan Evaluasi DO (menu lama M0203): CRUD t_klasifikasi_do per kurikulum prodi akun, dengan aturan fungsi DB lama
|
||
* (tahap unik per kurikulum, semester tempuh tidak berbalik urutan). Kurikulum 900 (prodi 21) punya tahap 1 (semester 5) dan
|
||
* tahap 2 (semester 9); 902 (prodi 22) kosong; 901 (prodi 99, di luar akun) punya tahap 1.
|
||
*/
|
||
class LegacyTahapEvaluasiTest extends TestCase
|
||
{
|
||
use FakesLegacyDatabase;
|
||
use InspectsHtml;
|
||
use RefreshDatabase;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
parent::setUp();
|
||
|
||
$this->useLegacyDatabase();
|
||
$this->seedLegacyOverview();
|
||
$this->seedLegacyEmployees();
|
||
$this->seedLegacyCurriculum();
|
||
Schema::connection('untan')->create('t_klasifikasi_do', function (Blueprint $table) {
|
||
$table->increments('idklasifikasi');
|
||
$table->integer('idkurikulum');
|
||
$table->smallInteger('tahap');
|
||
$table->smallInteger('semestertempuh');
|
||
$table->double('ipkmin');
|
||
$table->smallInteger('skshitung');
|
||
$table->smallInteger('sksmin');
|
||
$table->string('cname')->nullable();
|
||
$table->string('cip')->nullable();
|
||
$table->string('uname')->nullable();
|
||
$table->timestamp('tanggal')->nullable();
|
||
});
|
||
$stage = fn (array $row) => $this->db()->table('t_klasifikasi_do')->insert($row);
|
||
$stage(['idklasifikasi' => 1, 'idkurikulum' => 900, 'tahap' => 1, 'semestertempuh' => 5, 'ipkmin' => 2, 'skshitung' => 35, 'sksmin' => 35, 'uname' => 'lama', 'tanggal' => '2015-08-24 06:07:51']);
|
||
$stage(['idklasifikasi' => 2, 'idkurikulum' => 900, 'tahap' => 2, 'semestertempuh' => 9, 'ipkmin' => 2, 'skshitung' => 90, 'sksmin' => 90]);
|
||
$stage(['idklasifikasi' => 3, 'idkurikulum' => 901, 'tahap' => 1, 'semestertempuh' => 5, 'ipkmin' => 2, 'skshitung' => 35, 'sksmin' => 35]);
|
||
$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 input(array $override = []): array
|
||
{
|
||
return $override + ['prodi' => 21, 'kurikulum' => 900, 'tahap' => 3, 'semester' => 14, 'ipk' => '2,00', 'sks_hitung' => 150, 'sks_min' => 150];
|
||
}
|
||
|
||
private function store(array $override = []): TestResponse
|
||
{
|
||
return $this->postJson(route('operator.tahap-evaluasi.store'), $this->input($override));
|
||
}
|
||
|
||
public function test_halaman_dan_daftar(): void
|
||
{
|
||
$page = $this->get(route('operator.halaman', 'set_tahapevaluasi'))->assertOk()->assertSee('Tahapan Evaluasi DO')->assertSee('Tahapan evaluasi DO asli');
|
||
$this->assertSame(['Program Studi', 'Kurikulum'], array_map(fn ($n): string => $this->textOf($n), iterator_to_array($this->dom($page)->query("//section[@aria-label='Filter data']//label"))));
|
||
$this->assertSame(['Tambah', 'Hapus'], array_map(fn ($n): string => $this->textOf($n), iterator_to_array($this->dom($page)->query("//div[contains(@class, 'satu-bar__tools')]//button"))));
|
||
$this->assertSame(['', 'Kurikulum', 'Program Studi', 'Tahap', 'Semester Tempuh', 'IPK Minimum', 'SKS Dihitung', 'SKS Minimum', 'Aksi'], array_map(fn ($n): string => $this->textOf($n), iterator_to_array($this->dom($page)->query("//*[@aria-label='Daftar Tahapan Evaluasi DO']//thead/tr/th"))));
|
||
|
||
$rows = $this->getJson(route('operator.tahap-evaluasi.data'))->assertOk()->json('rows');
|
||
$this->assertSame([1, 2], array_column($rows, 'id'), 'Tahap kurikulum di luar akun (901) tidak tampil; urut tahap.');
|
||
$this->assertSame(['2020 – Kurikulum 2020', 'Teknik Informatika', 1, 5, '2,00', 35, 35, 'lama, 24 Agt 2015 06.07'], [$rows[0]['kurikulum'], $rows[0]['prodi'], $rows[0]['tahap'], $rows[0]['semester'], $rows[0]['ipk'], $rows[0]['sks_hitung'], $rows[0]['sks_min'], $rows[0]['diubah']]);
|
||
$this->assertSame(['prodi' => 21, 'kurikulum' => 900, 'tahap' => 1, 'semester' => 5, 'ipk' => '2,00', 'sks_hitung' => 35, 'sks_min' => 35], $rows[0]['form']);
|
||
|
||
$this->assertCount(0, $this->getJson(route('operator.tahap-evaluasi.data', ['prodi' => 22]))->json('rows'));
|
||
$this->assertCount(2, $this->getJson(route('operator.tahap-evaluasi.data', ['kurikulum' => 900]))->json('rows'));
|
||
$this->assertCount(2, $this->getJson(route('operator.tahap-evaluasi.data', ['q' => 'informatika']))->json('rows'));
|
||
$this->assertCount(0, $this->getJson(route('operator.tahap-evaluasi.data', ['q' => 'tidak-ada']))->json('rows'));
|
||
}
|
||
|
||
public function test_tambah_tahap(): void
|
||
{
|
||
$this->store()->assertCreated()->assertJsonPath('message', 'Tahap evaluasi ditambahkan.');
|
||
$row = $this->db()->table('t_klasifikasi_do')->where('idkurikulum', 900)->where('tahap', 3)->first();
|
||
$this->assertSame([14, 2.0, 150, 150], [(int) $row->semestertempuh, (float) $row->ipkmin, (int) $row->skshitung, (int) $row->sksmin]);
|
||
$this->assertSame(['SIAKAD', '198001012005011'], [$row->cname, $row->uname]);
|
||
$this->assertNotNull($row->tanggal);
|
||
|
||
// Tahap kosong = tahap tertinggi + 1 (f_klasifikasi_do_maxtahap); kurikulum tanpa tahap mulai dari 1; IPK boleh berkoma atau titik.
|
||
$this->store(['tahap' => '', 'semester' => 16, 'ipk' => '2,5'])->assertCreated();
|
||
$this->assertSame(2.5, (float) $this->db()->table('t_klasifikasi_do')->where('idkurikulum', 900)->where('tahap', 4)->value('ipkmin'));
|
||
$this->store(['kurikulum' => 902, 'prodi' => 22, 'tahap' => null, 'semester' => 3, 'ipk' => '1.75'])->assertCreated();
|
||
$this->assertSame([1, 1.75], [(int) $this->db()->table('t_klasifikasi_do')->where('idkurikulum', 902)->value('tahap'), (float) $this->db()->table('t_klasifikasi_do')->where('idkurikulum', 902)->value('ipkmin')]);
|
||
}
|
||
|
||
public function test_aturan_tahap_dan_semester_tempuh(): void
|
||
{
|
||
$this->store(['tahap' => 1])->assertStatus(422)->assertJsonPath('message', 'Tahap 1 untuk kurikulum ini sudah ada.');
|
||
$this->store(['tahap' => 3, 'semester' => 8])->assertStatus(422)->assertJsonPath('message', 'Semester tempuh tidak boleh lebih kecil dari 9 (tahap 2).');
|
||
// Tahap 1 tidak sama dengan tahap yang dicari: sisipan di antara tahap menjaga urutan semester.
|
||
$this->db()->table('t_klasifikasi_do')->where('idklasifikasi', 2)->update(['tahap' => 3]);
|
||
$this->store(['tahap' => 2, 'semester' => 4])->assertStatus(422)->assertJsonPath('message', 'Semester tempuh tidak boleh lebih kecil dari 5 (tahap 1).');
|
||
$this->store(['tahap' => 2, 'semester' => 10])->assertStatus(422)->assertJsonPath('message', 'Semester tempuh tidak boleh lebih besar dari 9 (tahap 3).');
|
||
$this->store(['tahap' => 2, 'semester' => 7])->assertCreated();
|
||
$this->assertSame(3, $this->db()->table('t_klasifikasi_do')->where('idkurikulum', 900)->count());
|
||
|
||
$this->store(['ipk' => 'abc'])->assertStatus(422)->assertJsonPath('message', 'IPK minimum harus berupa angka antara 0 dan 4.');
|
||
$this->store(['ipk' => '4,5'])->assertStatus(422)->assertJsonPath('message', 'IPK minimum harus berupa angka antara 0 dan 4.');
|
||
$this->store(['prodi' => 99])->assertStatus(422)->assertJsonPath('message', 'Program studi harus dipilih dari program studi Anda.');
|
||
$this->store(['kurikulum' => 902])->assertStatus(422)->assertJsonPath('message', 'Kurikulum harus dipilih dari program studi yang dipilih.');
|
||
$this->store(['prodi' => 21, 'kurikulum' => 901])->assertStatus(422)->assertJsonPath('message', 'Kurikulum harus dipilih dari program studi yang dipilih.');
|
||
$this->postJson(route('operator.tahap-evaluasi.store'), [])->assertStatus(422)->assertJsonValidationErrors(['prodi', 'kurikulum', 'semester', 'ipk', 'sks_hitung', 'sks_min']);
|
||
$this->store(['semester' => 0])->assertStatus(422)->assertJsonValidationErrors('semester');
|
||
$this->store(['sks_min' => 1000])->assertStatus(422)->assertJsonValidationErrors('sks_min');
|
||
$this->assertSame(3, $this->db()->table('t_klasifikasi_do')->where('idkurikulum', 900)->count(), 'Isian tidak valid tidak menambah baris.');
|
||
}
|
||
|
||
public function test_ubah_tahap(): void
|
||
{
|
||
$this->putJson(route('operator.tahap-evaluasi.update', 1), $this->input(['tahap' => 1, 'semester' => 6, 'ipk' => '2,25', 'sks_hitung' => 36, 'sks_min' => 36]))->assertOk()->assertJsonPath('message', 'Tahap evaluasi diperbarui.');
|
||
$row = $this->db()->table('t_klasifikasi_do')->where('idklasifikasi', 1)->first();
|
||
$this->assertSame([1, 6, 2.25, 36, 36, '198001012005011'], [(int) $row->tahap, (int) $row->semestertempuh, (float) $row->ipkmin, (int) $row->skshitung, (int) $row->sksmin, $row->uname], 'Tahap sendiri tidak dianggap duplikat.');
|
||
|
||
$this->putJson(route('operator.tahap-evaluasi.update', 1), $this->input(['tahap' => 1, 'semester' => 10]))->assertStatus(422)->assertJsonPath('message', 'Semester tempuh tidak boleh lebih besar dari 9 (tahap 2).');
|
||
$this->putJson(route('operator.tahap-evaluasi.update', 1), $this->input(['tahap' => 2, 'semester' => 6]))->assertStatus(422)->assertJsonPath('message', 'Tahap 2 untuk kurikulum ini sudah ada.');
|
||
// Tahap di kurikulum di luar akun.
|
||
$this->putJson(route('operator.tahap-evaluasi.update', 3), $this->input(['kurikulum' => 900, 'tahap' => 5, 'semester' => 20]))->assertStatus(422)->assertJsonPath('message', 'Tahap evaluasi tidak ditemukan.');
|
||
$this->putJson(route('operator.tahap-evaluasi.update', 999), $this->input())->assertStatus(422)->assertJsonPath('message', 'Tahap evaluasi tidak ditemukan.');
|
||
$this->assertSame(6, (int) $this->db()->table('t_klasifikasi_do')->where('idklasifikasi', 1)->value('semestertempuh'));
|
||
$this->assertSame(5, (int) $this->db()->table('t_klasifikasi_do')->where('idklasifikasi', 3)->value('semestertempuh'));
|
||
}
|
||
|
||
public function test_hapus_tahap(): void
|
||
{
|
||
$this->postJson(route('operator.tahap-evaluasi.destroy'), ['ids' => [2]])->assertOk()->assertJsonPath('message', '1 tahap evaluasi dihapus.');
|
||
$this->assertSame([1], $this->db()->table('t_klasifikasi_do')->where('idkurikulum', 900)->pluck('idklasifikasi')->all());
|
||
|
||
// Satu id di luar akun membatalkan seluruh penghapusan.
|
||
$this->postJson(route('operator.tahap-evaluasi.destroy'), ['ids' => [1, 3]])->assertStatus(422)->assertJsonPath('message', 'Tahap evaluasi tidak ditemukan.');
|
||
$this->postJson(route('operator.tahap-evaluasi.destroy'), ['ids' => [999]])->assertStatus(422);
|
||
$this->postJson(route('operator.tahap-evaluasi.destroy'), [])->assertStatus(422);
|
||
$this->assertSame(2, $this->db()->table('t_klasifikasi_do')->count());
|
||
|
||
$this->postJson(route('operator.tahap-evaluasi.destroy'), ['ids' => [1, 1]])->assertOk()->assertJsonPath('message', '1 tahap evaluasi dihapus.');
|
||
}
|
||
|
||
public function test_hak_mengikuti_kode_menu_lama_tahapan_evaluasi_do(): void
|
||
{
|
||
$this->db()->table('t_otoritasuser')->where('iduser', 1)->where('idotoritas', 4)->delete();
|
||
$this->seedLegacyMenus(['M030201']);
|
||
$this->get(route('operator.halaman', 'set_tahapevaluasi'))->assertForbidden();
|
||
$this->getJson(route('operator.tahap-evaluasi.data'))->assertForbidden();
|
||
$this->store()->assertForbidden();
|
||
$this->putJson(route('operator.tahap-evaluasi.update', 1), $this->input())->assertForbidden();
|
||
$this->postJson(route('operator.tahap-evaluasi.destroy'), ['ids' => [1]])->assertForbidden();
|
||
$this->assertSame(3, $this->db()->table('t_klasifikasi_do')->count());
|
||
|
||
$this->seedLegacyMenus(['M0203']);
|
||
$this->get(route('operator.halaman', 'set_tahapevaluasi'))->assertOk();
|
||
$this->store()->assertCreated();
|
||
}
|
||
|
||
public function test_akses_tamu_role_lain_dan_akun_demo(): void
|
||
{
|
||
Auth::logout();
|
||
$this->getJson(route('operator.tahap-evaluasi.data'))->assertUnauthorized();
|
||
$this->postJson(route('operator.tahap-evaluasi.store'), $this->input())->assertUnauthorized();
|
||
|
||
config(['auth.guards.web.provider' => 'users']);
|
||
Auth::forgetGuards();
|
||
$this->actingAs(User::factory()->mahasiswa()->create())->getJson(route('operator.tahap-evaluasi.data'))->assertForbidden();
|
||
$demo = User::factory()->operator()->create();
|
||
$this->actingAs($demo)->getJson(route('operator.tahap-evaluasi.data'))->assertNotFound();
|
||
$this->actingAs($demo)->postJson(route('operator.tahap-evaluasi.store'), $this->input())->assertNotFound();
|
||
$this->actingAs($demo)->get(route('operator.halaman', 'set_tahapevaluasi'))->assertOk()->assertDontSee('Tahapan evaluasi DO asli');
|
||
$this->assertSame(3, $this->db()->table('t_klasifikasi_do')->count());
|
||
}
|
||
}
|