upskill-event-manager/wordpress-dev/tests/HVAC_Test_User_Factory.php
bengizmo d6211ee364 feat(testing): Implement HVAC_Test_User_Factory and update .gitignore
- Add HVAC_Test_User_Factory class with:
  * User creation with specific roles
  * Multiple role support
  * Persona management system
  * Account cleanup integration
- Create comprehensive test suite in HVAC_Test_User_Factory_Test.php
- Update testing improvement plan documentation
- Add implementation decisions to project memory bank
- Restructure .gitignore with:
  * Whitelist approach for better file management
  * Explicit backup exclusions
  * Specific bin directory inclusions

Part of the Account Management component from the testing framework improvement plan.
2025-04-14 17:41:36 -03:00

114 lines
No EOL
3.2 KiB
PHP

<?php
/**
* Creates test users with specific roles and personas
*/
class HVAC_Test_User_Factory {
private static $instance;
private $created_users = [];
private $role_manager;
private function __construct() {
$this->role_manager = HVAC_Role_Manager::get_instance();
}
public static function get_instance() {
if (!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Create a test user with specified role
*/
public function create_user($username, $role = 'subscriber', $persona = 'basic') {
$user_id = wp_create_user($username, 'password', $username . '@example.com');
if (is_wp_error($user_id)) {
return false;
}
$user = new WP_User($user_id);
$user->set_role($role);
$this->created_users[] = [
'id' => $user_id,
'username' => $username,
'role' => $role,
'persona' => $persona
];
return $user_id;
}
/**
* Create a user with a custom role and capabilities
*/
public function create_user_with_custom_role($username, $role, $display_name, $capabilities = []) {
$this->role_manager->create_role($role, $display_name, $capabilities);
return $this->create_user($username, $role);
}
/**
* Create a user with a predefined persona
*/
public function create_user_with_persona($username, $persona) {
$personas = $this->get_personas();
if (!isset($personas[$persona])) {
return false;
}
$persona_data = $personas[$persona];
return $this->create_user_with_custom_role(
$username,
$persona_data['role'],
$persona_data['display_name'],
$persona_data['capabilities']
);
}
/**
* Get predefined user personas
*/
public function get_personas() {
return [
'basic_trainer' => [
'role' => 'hvac_trainer_basic',
'display_name' => 'Basic HVAC Trainer',
'capabilities' => [
'edit_events' => true,
'publish_events' => false
]
],
'advanced_trainer' => [
'role' => 'hvac_trainer_advanced',
'display_name' => 'Advanced HVAC Trainer',
'capabilities' => [
'edit_events' => true,
'publish_events' => true,
'manage_attendees' => true
]
],
'admin' => [
'role' => 'hvac_admin',
'display_name' => 'HVAC Administrator',
'capabilities' => [
'manage_options' => true,
'edit_users' => true
]
]
];
}
/**
* Clean up all created users and roles
*/
public function cleanup() {
foreach ($this->created_users as $user) {
wp_delete_user($user['id']);
}
$this->created_users = [];
$this->role_manager->cleanup();
}
}