Commit awal: prototipe UI SIAKAD Untan

This commit is contained in:
Ariyutta
2026-10-24 11:45:32 +07:00
commit d9b9cb5c0f
477 changed files with 51958 additions and 0 deletions
+141
View File
@@ -0,0 +1,141 @@
<?php
namespace Tests\Unit;
use App\Enums\Role;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;
class ProfileModelTest extends TestCase
{
use RefreshDatabase;
/** @return array<string, array{Role, string, string}> role, tabel, kolom nomor induk */
public static function profiles(): array
{
return [
'superuser' => [Role::Superuser, 'profil_superuser', 'nip'],
'admin' => [Role::Admin, 'profil_admin', 'nip'],
'operator' => [Role::Operator, 'profil_operator', 'nip'],
'dosen' => [Role::Dosen, 'profil_dosen', 'nidn'],
'mahasiswa' => [Role::Mahasiswa, 'profil_mahasiswa', 'nim'],
];
}
#[DataProvider('profiles')]
public function test_profile_factory_creates_a_valid_row_linked_to_a_user_of_the_same_role(Role $role, string $table, string $identifier): void
{
$profile = $role->profileModel()::factory()->create();
$this->assertDatabaseHas($table, ['id' => $profile->id, 'user_id' => $profile->user_id]);
$this->assertSame($table, $profile->getTable());
$this->assertSame($role, $profile->user->role);
$this->assertNotNull($profile->{$identifier});
$this->assertSame($profile->{$identifier}, $profile->nomor_induk);
}
#[DataProvider('profiles')]
public function test_profile_belongs_to_user_and_user_exposes_it(Role $role, string $table, string $identifier): void
{
$profile = $role->profileModel()::factory()->create();
$this->assertInstanceOf(BelongsTo::class, $profile->user());
$this->assertTrue($profile->user->is(User::findOrFail($profile->user_id)));
$this->assertTrue($profile->user->{$role->profileRelation()}->is($profile));
}
#[DataProvider('profiles')]
public function test_profile_factory_can_be_attached_to_an_existing_user(Role $role, string $table, string $identifier): void
{
$user = User::factory()->state(['role' => $role])->create();
$profile = $role->profileModel()::factory()->create(['user_id' => $user->id]);
$this->assertSame(1, User::count());
$this->assertSame($user->id, $profile->user_id);
}
#[DataProvider('profiles')]
public function test_a_user_can_have_only_one_profile_row_per_role(Role $role, string $table, string $identifier): void
{
$profile = $role->profileModel()::factory()->create();
$this->expectException(UniqueConstraintViolationException::class);
$role->profileModel()::factory()->create(['user_id' => $profile->user_id]);
}
#[DataProvider('profiles')]
public function test_identifier_is_unique(Role $role, string $table, string $identifier): void
{
$role->profileModel()::factory()->create([$identifier => '123456']);
$this->expectException(UniqueConstraintViolationException::class);
$role->profileModel()::factory()->create([$identifier => '123456']);
}
#[DataProvider('profiles')]
public function test_deleting_the_user_deletes_the_profile(Role $role, string $table, string $identifier): void
{
$profile = $role->profileModel()::factory()->create();
$profile->user->delete();
$this->assertDatabaseMissing($table, ['id' => $profile->id]);
}
#[DataProvider('profiles')]
public function test_profile_table_has_the_documented_columns(Role $role, string $table, string $identifier): void
{
$expected = match ($role) {
Role::Dosen => ['nidn', 'nip', 'jabatan_fungsional'],
Role::Mahasiswa => ['nim', 'angkatan', 'status'],
default => ['nip', 'jabatan', 'unit_kerja'],
};
foreach (['id', 'user_id', 'created_at', 'updated_at', ...$expected] as $column) {
$this->assertTrue(Schema::hasColumn($table, $column), "{$table}.{$column} tidak ada.");
}
}
public function test_staff_profiles_allow_missing_optional_columns(): void
{
foreach ([Role::Superuser, Role::Admin, Role::Operator] as $role) {
$profile = $role->profileModel()::factory()->create(['nip' => null, 'jabatan' => null, 'unit_kerja' => null]);
$this->assertNull($profile->nomor_induk);
}
}
public function test_dosen_profile_allows_missing_optional_columns(): void
{
$dosen = Role::Dosen->profileModel()::factory()->create(['nidn' => null, 'nip' => null, 'jabatan_fungsional' => null]);
$this->assertNull($dosen->nomor_induk);
}
public function test_mahasiswa_defaults_and_casts(): void
{
$user = User::factory()->mahasiswa()->create();
$mahasiswa = Role::Mahasiswa->profileModel()::create(['user_id' => $user->id, 'nim' => 'D1041250001', 'angkatan' => 2025]);
$this->assertSame('aktif', $mahasiswa->fresh()->status);
$this->assertSame('D1041250001', $mahasiswa->fresh()->nomor_induk);
$this->assertEquals(2025, $mahasiswa->fresh()->angkatan);
}
public function test_every_user_factory_default_creates_no_profile(): void
{
User::factory()->count(3)->create();
foreach (Role::cases() as $role) {
$this->assertSame(0, $role->profileModel()::count());
}
}
}
+100
View File
@@ -0,0 +1,100 @@
<?php
namespace Tests\Unit;
use App\Enums\Role;
use App\Models\Admin\Admin;
use App\Models\Dosen\Dosen;
use App\Models\Mahasiswa\Mahasiswa;
use App\Models\Operator\Operator;
use App\Models\Superuser\Superuser;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\HasOne;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;
class RoleTest extends TestCase
{
/** @return array<string, array{Role, string, string, string, class-string}> */
public static function expectations(): array
{
return [
'superuser' => [Role::Superuser, 'superuser', 'Superuser', 'NIP', Superuser::class],
'admin' => [Role::Admin, 'admin', 'Admin', 'NIP', Admin::class],
'operator' => [Role::Operator, 'operator', 'Operator', 'NIP', Operator::class],
'dosen' => [Role::Dosen, 'dosen', 'Dosen', 'NIDN', Dosen::class],
'mahasiswa' => [Role::Mahasiswa, 'mahasiswa', 'Mahasiswa', 'NIM', Mahasiswa::class],
];
}
public function test_there_are_exactly_five_roles_in_the_documented_order(): void
{
$this->assertSame(
['superuser', 'admin', 'operator', 'dosen', 'mahasiswa'],
array_map(fn (Role $role): string => $role->value, Role::cases()),
);
$this->assertSame(['superuser', 'admin', 'operator', 'dosen', 'mahasiswa'], Role::values());
}
/** @param class-string $model */
#[DataProvider('expectations')]
public function test_role_metadata(Role $role, string $value, string $label, string $identifier, string $model): void
{
$this->assertSame($value, $role->value);
$this->assertSame($label, $role->label());
$this->assertSame("{$value}.dashboard", $role->dashboardRoute());
$this->assertSame($value, $role->routePrefix());
$this->assertSame($value, $role->profileRelation());
$this->assertSame($model, $role->profileModel());
$this->assertSame($identifier, $role->identifierLabel());
}
#[DataProvider('expectations')]
public function test_role_can_be_resolved_from_its_value(Role $role, string $value): void
{
$this->assertSame($role, Role::from($value));
$this->assertSame($role, Role::tryFrom($value));
}
public function test_unknown_values_do_not_resolve(): void
{
$this->assertNull(Role::tryFrom('rektor'));
$this->assertNull(Role::tryFrom('Admin'));
}
public function test_options_map_values_to_labels(): void
{
$this->assertSame([
'superuser' => 'Superuser',
'admin' => 'Admin',
'operator' => 'Operator',
'dosen' => 'Dosen',
'mahasiswa' => 'Mahasiswa',
], Role::options());
}
public function test_profile_relation_is_a_has_one_on_user_for_every_role(): void
{
foreach (Role::cases() as $role) {
$this->assertTrue(method_exists(User::class, $role->profileRelation()), "User::{$role->profileRelation()}() belum ada.");
$relation = (new User)->{$role->profileRelation()}();
$this->assertInstanceOf(HasOne::class, $relation);
$this->assertInstanceOf($role->profileModel(), $relation->getRelated());
}
}
public function test_every_profile_model_has_its_own_namespace_folder(): void
{
foreach (Role::cases() as $role) {
$this->assertSame("App\\Models\\{$role->label()}\\{$role->label()}", $role->profileModel());
}
}
public function test_identifier_labels_are_nip_nidn_nim_only(): void
{
$labels = array_values(array_unique(array_map(fn (Role $role): string => $role->identifierLabel(), Role::cases())));
$this->assertEqualsCanonicalizing(['NIP', 'NIDN', 'NIM'], $labels);
}
}
+196
View File
@@ -0,0 +1,196 @@
<?php
namespace Tests\Unit;
use App\Enums\Role;
use App\Models\User;
use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\MassAssignmentException;
use Illuminate\Database\UniqueConstraintViolationException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;
class UserTest extends TestCase
{
use RefreshDatabase;
/** @return array<string, array{Role}> */
public static function roles(): array
{
return array_combine(Role::values(), array_map(fn (Role $role): array => [$role], Role::cases()));
}
/** @return array<string, array{string, string}> */
public static function names(): array
{
return [
'gelar depan dan belakang' => ['Dr. Rina Kartika Sari, M.Si.', 'RK'],
'gelar Ir.' => ['Ir. Budi Santoso, M.T.', 'BS'],
'gelar belakang saja' => ['Siti Nurhaliza, S.Kom.', 'SN'],
'tanpa gelar' => ['Muhammad Rizky Ramadhan', 'MR'],
'satu kata' => ['Budi', 'B'],
'huruf kecil' => ['andi pratama', 'AP'],
'spasi berlebih' => [' Andi Pratama ', 'AP'],
];
}
#[DataProvider('names')]
public function test_initials(string $name, string $expected): void
{
$this->assertSame($expected, User::factory()->make(['name' => $name])->initials());
}
#[DataProvider('roles')]
public function test_factory_states_set_the_role(Role $role): void
{
$user = User::factory()->{$role->value}()->make();
$this->assertSame($role, $user->role);
$this->assertTrue($user->is_active);
}
public function test_inactive_state(): void
{
$this->assertFalse(User::factory()->inactive()->make()->is_active);
}
#[DataProvider('roles')]
public function test_has_role_matches_only_its_own_role(Role $role): void
{
$user = User::factory()->{$role->value}()->make();
$this->assertTrue($user->hasRole($role));
$this->assertTrue($user->hasRole($role->value));
foreach (Role::cases() as $other) {
if ($other !== $role) {
$this->assertFalse($user->hasRole($other));
$this->assertFalse($user->hasRole($other->value));
}
}
}
public function test_has_role_accepts_several_roles_enums_and_strings(): void
{
$user = User::factory()->dosen()->make();
$this->assertTrue($user->hasRole(Role::Admin, Role::Dosen));
$this->assertTrue($user->hasRole('admin', 'dosen'));
$this->assertTrue($user->hasRole(Role::Admin, 'dosen'));
$this->assertFalse($user->hasRole(Role::Admin, Role::Operator));
$this->assertFalse($user->hasRole());
}
public function test_has_role_ignores_unknown_role_strings(): void
{
$user = User::factory()->admin()->make();
$this->assertFalse($user->hasRole('rektor'));
$this->assertFalse($user->hasRole('Admin'));
$this->assertTrue($user->hasRole('rektor', 'admin'));
}
#[DataProvider('roles')]
public function test_profile_accessor_returns_the_profile_of_the_users_role(Role $role): void
{
$user = $this->userWithRole($role);
$fresh = User::findOrFail($user->id);
$profile = $fresh->profile;
$this->assertInstanceOf($role->profileModel(), $profile);
$this->assertSame($user->id, $profile->user_id);
$this->assertTrue($fresh->relationLoaded($role->profileRelation()));
}
public function test_profile_accessor_is_null_when_the_row_is_missing(): void
{
$user = User::factory()->dosen()->create();
$this->assertNull(User::findOrFail($user->id)->profile);
}
public function test_profile_accessor_loads_only_the_matching_relation(): void
{
$user = $this->userWithRole(Role::Mahasiswa);
$fresh = User::findOrFail($user->id);
$fresh->profile;
$this->assertTrue($fresh->relationLoaded('mahasiswa'));
$this->assertFalse($fresh->relationLoaded('dosen'));
$this->assertFalse($fresh->relationLoaded('admin'));
}
public function test_role_is_not_mass_assignable(): void
{
$user = new User;
$this->assertFalse($user->isFillable('role'));
$this->assertFalse($user->isFillable('is_active'));
$this->assertFalse($user->isFillable('last_login_at'));
foreach (['name', 'username', 'email', 'password'] as $attribute) {
$this->assertTrue($user->isFillable($attribute), "{$attribute} harus mass-assignable.");
}
}
public function test_mass_assigning_a_role_never_sets_it(): void
{
$user = new User;
try {
$user->fill(['name' => 'Penyusup', 'role' => 'superuser']);
} catch (MassAssignmentException) {
// Mode ketat melempar galat; di produksi atribut dibuang diam-diam. Keduanya aman.
}
$this->assertArrayNotHasKey('role', $user->getAttributes());
}
public function test_role_is_cast_to_the_enum_and_persisted_as_its_value(): void
{
$user = User::factory()->operator()->create();
$this->assertSame(Role::Operator, $user->fresh()->role);
$this->assertSame('operator', $user->getRawOriginal('role'));
$this->assertDatabaseHas('users', ['id' => $user->id, 'role' => 'operator']);
}
public function test_password_is_hashed_and_hidden(): void
{
$user = User::factory()->create(['password' => 'rahasia-banget']);
$this->assertNotSame('rahasia-banget', $user->password);
$this->assertTrue(Hash::isHashed($user->password));
$this->assertTrue(Hash::check('rahasia-banget', $user->password));
$this->assertNotSame('rahasia-banget', $user->getRawOriginal('password'));
$this->assertArrayNotHasKey('password', $user->toArray());
$this->assertArrayNotHasKey('remember_token', $user->toArray());
}
public function test_factory_default_password_is_password(): void
{
$this->assertTrue(Hash::check('password', User::factory()->create()->password));
}
public function test_username_is_unique(): void
{
User::factory()->create(['username' => 'kembar']);
$this->expectException(UniqueConstraintViolationException::class);
User::factory()->create(['username' => 'kembar']);
}
public function test_casts_for_active_flag_and_last_login(): void
{
$user = User::factory()->create(['last_login_at' => '2026-10-23 08:00:00']);
$this->assertTrue($user->fresh()->is_active);
$this->assertInstanceOf(CarbonInterface::class, $user->fresh()->last_login_at);
$this->assertNull(User::factory()->create()->fresh()->last_login_at);
}
}