upskill-event-manager/wordpress-dev/tests/HVAC_Role_Manager_Test.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

224 lines
No EOL
8.6 KiB
PHP

<?php
/**
* Tests for HVAC_Role_Manager class
*/
class HVAC_Role_Manager_Test extends HVAC_Base_Test_Case {
public function test_role_creation() {
// Test basic role creation
$role_name = 'test_role_' . uniqid();
$this->assertTrue(HVAC_Role_Manager::create_role($role_name));
$this->assertTrue(role_exists($role_name));
}
public function test_role_deletion() {
// Test role deletion
$role_name = 'test_role_' . uniqid();
HVAC_Role_Manager::create_role($role_name);
$this->assertTrue(HVAC_Role_Manager::delete_role($role_name));
$this->assertFalse(role_exists($role_name));
}
public function test_permission_management() {
// Test adding/removing permissions
$role_name = 'test_role_' . uniqid();
HVAC_Role_Manager::create_role($role_name);
$capability = 'edit_posts';
$this->assertTrue(HVAC_Role_Manager::add_capability($role_name, $capability));
$this->assertTrue(HVAC_Role_Manager::has_capability($role_name, $capability));
$this->assertTrue(HVAC_Role_Manager::remove_capability($role_name, $capability));
$this->assertFalse(HVAC_Role_Manager::has_capability($role_name, $capability));
}
public function test_role_conflict_handling() {
// Test handling of duplicate role creation
$role_name = 'test_role_' . uniqid();
$this->assertTrue(HVAC_Role_Manager::create_role($role_name));
$this->assertFalse(HVAC_Role_Manager::create_role($role_name));
}
public function test_permission_inheritance() {
// Test permission inheritance from parent roles
$parent_role = 'test_parent_' . uniqid();
$child_role = 'test_child_' . uniqid();
HVAC_Role_Manager::create_role($parent_role);
HVAC_Role_Manager::create_role($child_role);
$capability = 'edit_posts';
HVAC_Role_Manager::add_capability($parent_role, $capability);
HVAC_Role_Manager::add_parent_role($child_role, $parent_role);
$this->assertTrue(HVAC_Role_Manager::has_capability($child_role, $capability));
}
public function test_invalid_role_names() {
// Test handling of invalid role names
$this->assertFalse(HVAC_Role_Manager::create_role(''));
$this->assertFalse(HVAC_Role_Manager::create_role('role with spaces'));
$this->assertFalse(HVAC_Role_Manager::create_role('role-with-special-chars!@#'));
$this->assertFalse(HVAC_Role_Manager::create_role(str_repeat('a', 65))); // Too long
}
public function test_invalid_capability_names() {
// Test handling of invalid capability names
$role_name = 'test_role_' . uniqid();
HVAC_Role_Manager::create_role($role_name);
$this->assertFalse(HVAC_Role_Manager::add_capability($role_name, ''));
$this->assertFalse(HVAC_Role_Manager::add_capability($role_name, 'cap with spaces'));
$this->assertFalse(HVAC_Role_Manager::add_capability($role_name, 'cap-with-special-chars!@#'));
}
public function test_bulk_permission_assignment() {
// Test performance with large permission sets
$role_name = 'test_role_' . uniqid();
HVAC_Role_Manager::create_role($role_name);
$start_time = microtime(true);
$cap_count = 100;
for ($i = 0; $i < $cap_count; $i++) {
$capability = 'test_cap_' . $i;
$this->assertTrue(HVAC_Role_Manager::add_capability($role_name, $capability));
}
$elapsed = microtime(true) - $start_time;
$this->assertLessThan(0.5, $elapsed, "Adding $cap_count capabilities took too long ($elapsed seconds)");
// Verify all capabilities were added
for ($i = 0; $i < $cap_count; $i++) {
$capability = 'test_cap_' . $i;
$this->assertTrue(HVAC_Role_Manager::has_capability($role_name, $capability));
}
}
public function test_role_hierarchy_depth() {
// Test nested role inheritance with increasing depth
$max_depth = 10;
$roles = [];
$base_cap = 'base_capability';
// Create chain of roles
for ($i = 0; $i < $max_depth; $i++) {
$roles[$i] = 'test_role_' . $i . '_' . uniqid();
HVAC_Role_Manager::create_role($roles[$i]);
if ($i > 0) {
HVAC_Role_Manager::add_parent_role($roles[$i], $roles[$i-1]);
}
}
// Add capability to base role
HVAC_Role_Manager::add_capability($roles[0], $base_cap);
// Verify capability propagates through all levels
for ($i = 1; $i < $max_depth; $i++) {
$this->assertTrue(HVAC_Role_Manager::has_capability($roles[$i], $base_cap),
"Capability not inherited at depth $i");
}
// Test circular reference prevention
$this->assertFalse(HVAC_Role_Manager::add_parent_role($roles[0], $roles[$max_depth-1]),
"Circular reference should be prevented");
}
public function test_concurrent_role_modifications() {
// Test thread safety with concurrent modifications
$role_name = 'test_concurrent_role_' . uniqid();
HVAC_Role_Manager::create_role($role_name);
$iterations = 50;
$cap_prefix = 'concurrent_cap_';
// Run concurrent operations
$results = [];
$threads = [];
for ($i = 0; $i < $iterations; $i++) {
$threads[] = new class($role_name, $cap_prefix . $i) extends Thread {
private $role_name;
private $capability;
public function __construct($role_name, $capability) {
$this->role_name = $role_name;
$this->capability = $capability;
}
public function run() {
return HVAC_Role_Manager::add_capability($this->role_name, $this->capability);
}
};
}
// Start all threads
foreach ($threads as $thread) {
$thread->start();
}
// Wait for completion and collect results
foreach ($threads as $thread) {
$thread->join();
$results[] = $thread->getResult();
}
// Verify all operations succeeded
$this->assertCount($iterations, array_filter($results),
"Not all concurrent operations succeeded");
// Verify all capabilities were added
for ($i = 0; $i < $iterations; $i++) {
$this->assertTrue(HVAC_Role_Manager::has_capability($role_name, $cap_prefix . $i),
"Capability $i not properly added");
}
}
public function test_account_cleanup_integration() {
// Test role cleanup when accounts are deleted
$role_name = 'test_cleanup_role_' . uniqid();
HVAC_Role_Manager::create_role($role_name);
// Create test user with this role
$user_id = wp_insert_user([
'user_login' => 'testuser_' . uniqid(),
'user_pass' => 'password',
'role' => $role_name
]);
// Verify role assignment
$user = get_user_by('id', $user_id);
$this->assertTrue(in_array($role_name, $user->roles),
"Role was not assigned to test user");
// Delete user and verify role cleanup
wp_delete_user($user_id);
$this->assertFalse(HVAC_Role_Manager::role_exists($role_name),
"Role was not cleaned up after last user deletion");
// Test with multiple users
$role_name2 = 'test_cleanup_role2_' . uniqid();
HVAC_Role_Manager::create_role($role_name2);
$user_ids = [];
for ($i = 0; $i < 3; $i++) {
$user_ids[] = wp_insert_user([
'user_login' => 'testuser2_' . uniqid(),
'user_pass' => 'password',
'role' => $role_name2
]);
}
// Delete all but one user - role should persist
wp_delete_user($user_ids[0]);
wp_delete_user($user_ids[1]);
$this->assertTrue(HVAC_Role_Manager::role_exists($role_name2),
"Role was incorrectly cleaned up when users still exist");
// Delete last user - role should be cleaned up
wp_delete_user($user_ids[2]);
$this->assertFalse(HVAC_Role_Manager::role_exists($role_name2),
"Role was not cleaned up after last user deletion");
}
}