2026-09-02 00:01:12 +02:00
<? php
declare ( strict_types = 1 );
require_once __DIR__ . '/../app/Domain/User/PasswordPolicy.php' ;
require_once __DIR__ . '/../app/Domain/User/UserRecord.php' ;
require_once __DIR__ . '/../app/Domain/User/UserAdminService.php' ;
function user_admin_assert ( bool $ok , string $message ) : void { if ( ! $ok ) throw new RuntimeException ( $message ); }
$service = new App\Domain\User\UserAdminService ();
$existing = [
[ 'id' => 1 , 'name' => 'System Administrator' , 'email' => 'admin@example.com' , 'role_id' => 1 , 'role_name' => 'Administrator' , 'is_active' => 1 ],
[ 'id' => 2 , 'name' => 'Technician' , 'email' => 'tech@example.com' , 'role_id' => 3 , 'role_name' => 'Technician' , 'is_active' => 1 ],
];
$edit = $service -> validateForEdit ( 2 , [ 'name' => 'Updated Tech' , 'email' => 'new-tech@example.com' , 'role_id' => 3 , 'is_active' => '1' ], $existing );
user_admin_assert ( $edit [ 'valid' ] === true , 'A valid user edit should pass.' );
$adminDeactivate = $service -> validateForEdit ( 1 , [ 'name' => 'System Administrator' , 'email' => 'admin@example.com' , 'role_id' => 1 , 'is_active' => '0' ], $existing );
user_admin_assert ( $adminDeactivate [ 'valid' ] === false && isset ( $adminDeactivate [ 'errors' ][ 'is_active' ]), 'Administrator deactivation must be rejected.' );
$password = $service -> validatePasswordReset ([ 'id' => 2 ], 'Strong-Password-42' );
user_admin_assert ( $password [ 'valid' ] === true , 'A strong password reset should pass.' );
$front = file_get_contents ( dirname ( __DIR__ ) . '/public/index.php' );
user_admin_assert ( $front !== false && str_contains ( $front , "if ( \$ route === 'user_edit')" ) && str_contains ( $front , "if ( \$ route === 'user_delete')" ), 'User edit and delete routes must exist.' );
2026-09-02 00:50:06 +02:00
user_admin_assert ( str_contains ( $front , 'SELECT u.id, u.name, u.email, u.role_id, u.is_active, r.name AS role_name FROM users u JOIN roles r' ), 'User edit must qualify user columns when joining the roles table.' );
2026-09-02 00:47:21 +02:00
2026-09-02 00:01:12 +02:00
user_admin_assert ( str_contains ( $front , "role_name'] ?? '') !== 'Administrator'" ), 'User deletion must be Administrator-only.' );
printf ( "User administration tests: 5 passed \n " );