upskill-event-manager/wordpress-dev/tests/unit/class-hvac-test-environment.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

148 lines
No EOL
3.4 KiB
PHP

<?php
/**
* HVAC Test Environment Class
*
* Handles test environment setup, transaction management, and cleanup.
*/
class HVAC_Test_Environment {
/**
* @var wpdb WordPress database instance
*/
private $wpdb;
/**
* @var array List of created test user IDs for cleanup
*/
private $test_user_ids = array();
/**
* Constructor
*/
public function __construct() {
global $wpdb;
$this->wpdb = $wpdb;
}
/**
* Set up the test environment
*/
public function setUp() {
// Start transaction
$this->start_transaction();
// Ensure TEC CE is active
$this->activate_required_plugins();
// Reset environment
$this->reset_environment();
}
/**
* Clean up after tests
*/
public function tearDown() {
// Rollback transaction
$this->rollback_transaction();
// Clean up test accounts
$this->cleanup_test_accounts();
}
/**
* Start a database transaction
*/
private function start_transaction() {
$this->wpdb->query('START TRANSACTION');
}
/**
* Rollback the current transaction
*/
private function rollback_transaction() {
$this->wpdb->query('ROLLBACK');
}
/**
* Activate required plugins
*
* @throws Exception If TEC CE plugin cannot be activated
*/
private function activate_required_plugins() {
if (!class_exists('Tribe__Events__Main')) {
throw new Exception('The Events Calendar plugin is not active');
}
if (!class_exists('Tribe__Events__Community__Main')) {
throw new Exception('The Events Calendar Community Events plugin is not active');
}
}
/**
* Reset the environment to a clean state
*/
private function reset_environment() {
// Clear any cached data
wp_cache_flush();
// Reset post data
$this->reset_posts();
// Reset user roles and capabilities
$this->reset_roles();
}
/**
* Reset posts and related data
*/
private function reset_posts() {
// Delete all test events
$events = get_posts(array(
'post_type' => 'tribe_events',
'posts_per_page' => -1,
'post_status' => 'any'
));
foreach ($events as $event) {
wp_delete_post($event->ID, true);
}
}
/**
* Reset user roles to default state
*/
private function reset_roles() {
// Refresh roles
wp_roles()->reinit();
}
/**
* Clean up test user accounts
*/
private function cleanup_test_accounts() {
foreach ($this->test_user_ids as $user_id) {
wp_delete_user($user_id, true);
}
$this->test_user_ids = array();
}
/**
* Register a test user for cleanup
*
* @param int $user_id The ID of the test user to register
*/
public function register_test_user($user_id) {
$this->test_user_ids[] = $user_id;
}
/**
* Check if the test environment is properly set up
*
* @return bool True if environment is ready
*/
public function is_ready() {
return class_exists('Tribe__Events__Main') &&
class_exists('Tribe__Events__Community__Main') &&
$this->wpdb->get_var("SELECT @@autocommit") == 0;
}
}