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; } }