user->create( [ 'role' => 'hvac_trainer', ] ); // Define constants manually if the class couldn't be loaded but we need them // (Should be loaded by bootstrap if TEC is active) if (!defined('Tribe__Events__Main::POSTTYPE')) { define('Tribe__Events__Main::POSTTYPE', 'tribe_events'); } if (!defined('Tribe__Events__Main::VENUE_POST_TYPE')) { define('Tribe__Events__Main::VENUE_POST_TYPE', 'tribe_venue'); } if (!defined('Tribe__Events__Main::ORGANIZER_POST_TYPE')) { define('Tribe__Events__Main::ORGANIZER_POST_TYPE', 'tribe_organizer'); } } /** * Set up the test environment before each test method runs. */ public function set_up() { parent::set_up(); // Removed skip check - tests will now run assuming TEC CE is active or our handler works. // Set the current user to the test trainer wp_set_current_user( self::$trainer_user_id ); // Clear POST data before each test $_POST = []; } /** * Tear down the test environment after each test method runs. */ public function tear_down() { // Reset the current user wp_set_current_user( 0 ); $_POST = []; // Clear POST data parent::tear_down(); } // --- Helper Methods --- /** * Prepares a basic valid POST array for event submission. * * @param int $event_id 0 for creation, > 0 for modification. * @param int $venue_id * @param int $organizer_id * @return array */ protected function prepare_valid_post_data( $event_id = 0, $venue_id = 0, $organizer_id = 0 ) { $start_date = date( 'Y-m-d H:i:s', strtotime( '+5 day' ) ); $end_date = date( 'Y-m-d H:i:s', strtotime( '+5 day +2 hours' ) ); // Create venue/organizer if IDs not provided if ( ! $venue_id ) { $venue_id = $this->factory()->post->create( [ 'post_type' => Tribe__Events__Main::VENUE_POST_TYPE, 'post_title' => 'Integration Test Venue', 'post_status' => 'publish' ] ); } if ( ! $organizer_id ) { $organizer_id = $this->factory()->post->create( [ 'post_type' => Tribe__Events__Main::ORGANIZER_POST_TYPE, 'post_title' => 'Integration Test Organizer', 'post_status' => 'publish' ] ); } return [ 'action' => 'hvac_save_event', 'event_id' => $event_id, '_hvac_event_nonce' => wp_create_nonce( 'hvac_save_event_nonce' ), 'post_title' => 'Integration Test Event ' . uniqid(), // Use post_title for TEC CE 'post_content' => 'Integration test event description.', // Use post_content for TEC CE 'EventStartDate' => date( 'Y-m-d', strtotime( $start_date ) ), 'EventStartTime' => date( 'h:i A', strtotime( $start_date ) ), 'EventEndDate' => date( 'Y-m-d', strtotime( $end_date ) ), 'EventEndTime' => date( 'h:i A', strtotime( $end_date ) ), 'venue' => [ 'VenueID' => $venue_id ], 'organizer' => [ 'OrganizerID' => $organizer_id ], // Add other fields TEC CE might require (e.g., cost, website) 'EventCost' => '10', 'EventURL' => 'http://example.com/integration-test', ]; } // --- Test Cases --- /** * Test successful event creation via the TEC CE handler. * @test */ public function test_tec_ce_handler_creates_event_successfully() { // 1. Prepare valid POST data for creation $_POST = $this->prepare_valid_post_data(); $test_title = $_POST['post_title']; // Store for assertion // 2. Instantiate handler and call method (expecting TEC CE to handle it) $handler = HVAC_Event_Handler::get_instance(); ob_start(); @$handler->process_event_submission(); // Should delegate to TEC CE and redirect/exit ob_end_clean(); // 3. Assertions: Verify event was created by TEC CE $args = [ 'post_type' => Tribe__Events__Main::POSTTYPE, 'post_status' => ['publish', 'pending'], // TEC CE might save as pending based on settings 'title' => $test_title, 'author' => self::$trainer_user_id, 'posts_per_page' => 1, 'orderby' => 'ID', 'order' => 'DESC', // Get the latest one ]; $events = get_posts( $args ); $this->assertCount( 1, $events, 'Expected one event to be created via TEC CE handler.' ); $created_event = $events[0]; $created_event_id = $created_event->ID; // Assert basic data $this->assertEquals( $test_title, $created_event->post_title ); $this->assertEquals( $_POST['post_content'], $created_event->post_content ); $this->assertEquals( self::$trainer_user_id, $created_event->post_author ); // Assert meta data saved by TEC CE $expected_start_date = date( 'Y-m-d H:i:s', strtotime( $_POST['EventStartDate'] . ' ' . $_POST['EventStartTime'] ) ); $expected_end_date = date( 'Y-m-d H:i:s', strtotime( $_POST['EventEndDate'] . ' ' . $_POST['EventEndTime'] ) ); $this->assertEquals( $expected_start_date, get_post_meta( $created_event_id, '_EventStartDate', true ) ); $this->assertEquals( $expected_end_date, get_post_meta( $created_event_id, '_EventEndDate', true ) ); $this->assertEquals( $_POST['venue']['VenueID'], get_post_meta( $created_event_id, '_EventVenueID', true ) ); $this->assertEquals( $_POST['organizer']['OrganizerID'], get_post_meta( $created_event_id, '_EventOrganizerID', true ) ); $this->assertEquals( $_POST['EventCost'], get_post_meta( $created_event_id, '_EventCost', true ) ); $this->assertEquals( $_POST['EventURL'], get_post_meta( $created_event_id, '_EventURL', true ) ); } /** * Test successful event modification via the TEC CE handler. * @test */ public function test_tec_ce_handler_modifies_event_successfully() { // 1. Create an initial event $initial_post_data = $this->prepare_valid_post_data(); $_POST = $initial_post_data; $handler = HVAC_Event_Handler::get_instance(); ob_start(); @$handler->process_event_submission(); ob_end_clean(); $initial_event = get_posts(['post_type' => Tribe__Events__Main::POSTTYPE, 'title' => $initial_post_data['post_title'], 'posts_per_page' => 1, 'author' => self::$trainer_user_id, 'post_status' => ['publish', 'pending']]); $this->assertCount(1, $initial_event, "Failed to create initial event for modification test."); $event_id = $initial_event[0]->ID; // 2. Prepare POST data for modification $mod_post_data = $this->prepare_valid_post_data( $event_id, $_POST['venue']['VenueID'], $_POST['organizer']['OrganizerID'] ); // Reuse venue/org $mod_post_data['post_title'] = 'MODIFIED Integration Test Event ' . uniqid(); $mod_post_data['EventCost'] = '25'; // Change cost $_POST = $mod_post_data; $test_mod_title = $_POST['post_title']; // 3. Call submission handler again for modification ob_start(); @$handler->process_event_submission(); // Should delegate to TEC CE and redirect/exit ob_end_clean(); // 4. Assertions: Verify event was modified $modified_event = get_post( $event_id ); $this->assertNotNull( $modified_event, 'Modified event post should still exist.' ); // Assert changed data $this->assertEquals( $test_mod_title, $modified_event->post_title ); $this->assertEquals( $mod_post_data['post_content'], $modified_event->post_content ); $this->assertEquals( $mod_post_data['EventCost'], get_post_meta( $event_id, '_EventCost', true ) ); // Assert unchanged data (author) $this->assertEquals( self::$trainer_user_id, $modified_event->post_author ); // Assert dates updated $expected_mod_start_date = date( 'Y-m-d H:i:s', strtotime( $mod_post_data['EventStartDate'] . ' ' . $mod_post_data['EventStartTime'] ) ); $this->assertEquals( $expected_mod_start_date, get_post_meta( $event_id, '_EventStartDate', true ) ); } /** * Test that TEC CE handler path prevents creation with invalid data (e.g., missing title). * @test */ public function test_tec_ce_handler_prevents_creation_with_invalid_data() { // 1. Prepare invalid POST data (missing title) $_POST = $this->prepare_valid_post_data(); $original_title = $_POST['post_title']; // Keep track for assertion $_POST['post_title'] = ''; // Invalidate title // 2. Instantiate handler and call method $handler = HVAC_Event_Handler::get_instance(); ob_start(); @$handler->process_event_submission(); // Should delegate to TEC CE, which should handle error/redirect ob_end_clean(); // 3. Assertions: Verify event was NOT created with the original title or empty title $args_orig = [ 'post_type' => Tribe__Events__Main::POSTTYPE, 'post_status' => ['publish', 'pending'], 'title' => $original_title, // Check if it somehow got created anyway 'posts_per_page' => 1, ]; $args_empty = [ 'post_type' => Tribe__Events__Main::POSTTYPE, 'post_status' => ['publish', 'pending'], 'title' => '', // Check if it got created with empty title 'posts_per_page' => 1, ]; $events_orig = get_posts( $args_orig ); $events_empty = get_posts( $args_empty ); $this->assertCount( 0, $events_orig, 'Event should not have been created with original title when submitted with empty title.' ); $this->assertCount( 0, $events_empty, 'Event should not have been created with empty title via TEC CE handler.' ); // Note: Asserting the specific error message or redirect is difficult here. // We rely on TEC CE's internal handling. } }