*/ public static function roles(): array { return array_combine(Role::values(), array_map(fn (Role $role): array => [$role], Role::cases())); } /** @return array */ 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); } }