user->create( array( 'role' => 'hvac_trainer' ) ); self::$subscriber_user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); // Create the dashboard page (assuming activation hook might not run in tests) self::$dashboard_page_id = self::factory()->post->create( array( 'post_type' => 'page', 'post_title' => 'Trainer Dashboard', 'post_name' => 'hvac-dashboard', // Use the slug defined in activation hook 'post_status' => 'publish', ) ); // Create test events assigned to the trainer $now = time(); $one_day = DAY_IN_SECONDS; // Published Past $event1_id = self::factory()->post->create( array( 'post_type' => Tribe__Events__Main::POSTTYPE, 'post_title' => 'Past Published Event', 'post_status' => 'publish', 'meta_input' => array( '_EventStartDate' => date( 'Y-m-d H:i:s', $now - ( 7 * $one_day ) ), '_EventEndDate' => date( 'Y-m-d H:i:s', $now - ( 7 * $one_day ) + HOUR_IN_SECONDS ), '_EventOrganizerID' => self::$trainer_user_id, ), ) ); update_post_meta( $event1_id, '_EventOrganizerID', self::$trainer_user_id ); self::$event_ids[] = $event1_id; // Published Future $event2_id = self::factory()->post->create( array( 'post_type' => Tribe__Events__Main::POSTTYPE, 'post_title' => 'Future Published Event', 'post_status' => 'publish', 'meta_input' => array( '_EventStartDate' => date( 'Y-m-d H:i:s', $now + ( 7 * $one_day ) ), '_EventEndDate' => date( 'Y-m-d H:i:s', $now + ( 7 * $one_day ) + HOUR_IN_SECONDS ), '_EventOrganizerID' => self::$trainer_user_id, ), ) ); update_post_meta( $event2_id, '_EventOrganizerID', self::$trainer_user_id ); self::$event_ids[] = $event2_id; // Draft Future $event3_id = self::factory()->post->create( array( 'post_type' => Tribe__Events__Main::POSTTYPE, 'post_title' => 'Future Draft Event', 'post_status' => 'draft', 'meta_input' => array( '_EventStartDate' => date( 'Y-m-d H:i:s', $now + ( 14 * $one_day ) ), '_EventEndDate' => date( 'Y-m-d H:i:s', $now + ( 14 * $one_day ) + HOUR_IN_SECONDS ), '_EventOrganizerID' => self::$trainer_user_id, ), ) ); update_post_meta( $event3_id, '_EventOrganizerID', self::$trainer_user_id ); self::$event_ids[] = $event3_id; } public static function tearDownAfterClass(): void { wp_delete_post( self::$dashboard_page_id, true ); wp_delete_user( self::$trainer_user_id ); wp_delete_user( self::$subscriber_user_id ); foreach ( self::$event_ids as $event_id ) { wp_delete_post( $event_id, true ); } self::$event_ids = []; parent::tearDownAfterClass(); } /** * Test dashboard access for logged-out users. * Should redirect to login. */ public function test_dashboard_access_logged_out() { $this->markTestSkipped('Redirect verification is unreliable in integration tests; covered by E2E.'); $this->go_to( get_permalink( self::$dashboard_page_id ) ); // Asserting redirects is difficult here. Rely on E2E tests. // We can check that the queried object is not the dashboard page ID. $queried_object_id = get_queried_object_id(); $this->assertNotEquals( self::$dashboard_page_id, $queried_object_id, 'Logged-out user should be redirected away from dashboard.' ); } /** * Test dashboard access for users with incorrect role (e.g., subscriber). * Should redirect or show an error (currently redirects). */ public function test_dashboard_access_wrong_role() { $this->markTestSkipped('Redirect verification is unreliable in integration tests; covered by E2E.'); wp_set_current_user( self::$subscriber_user_id ); $this->go_to( get_permalink( self::$dashboard_page_id ) ); // Asserting redirects is difficult here. Rely on E2E tests. // We can check that the queried object is not the dashboard page ID. $queried_object_id = get_queried_object_id(); $this->assertNotEquals( self::$dashboard_page_id, $queried_object_id, 'User with wrong role should be redirected away from dashboard.' ); } /** * Test dashboard access for the correct role (hvac_trainer). * Should display the dashboard content. */ public function test_dashboard_access_correct_role() { wp_set_current_user( self::$trainer_user_id ); $this->go_to( get_permalink( self::$dashboard_page_id ) ); // Check if the global $post object matches the dashboard page ID global $post; $this->assertInstanceOf( 'WP_Post', $post, 'Global $post object should be set.' ); $this->assertEquals( self::$dashboard_page_id, $post->ID, 'Trainer should land on the dashboard page.' ); // Check title as a basic content verification $this->assertEquals( 'Trainer Dashboard', $post->post_title, 'Dashboard page title should be correct.' ); } /** * Test if the events table displays the correct events for the 'all' filter. */ public function test_events_table_all_filter() { wp_set_current_user( self::$trainer_user_id ); $this->go_to( get_permalink( self::$dashboard_page_id ) ); // URL without filter param // Need a way to capture the rendered HTML output of the template // This is complex in WP Unit Tests. Often better suited for E2E tests. // Placeholder assertion: $this->assertTrue( true, "Integration test for table content needs implementation or E2E test." ); // Ideally, you'd capture output buffer, parse HTML, and check for event titles. } /** * Test if the events table displays the correct events for the 'publish' filter. */ public function test_events_table_publish_filter() { wp_set_current_user( self::$trainer_user_id ); $url = add_query_arg( 'event_status', 'publish', get_permalink( self::$dashboard_page_id ) ); $this->go_to( $url ); // Placeholder assertion: $this->assertTrue( true, "Integration test for filtered table content needs implementation or E2E test." ); } /** * Test if the events table displays the correct events for the 'draft' filter. */ public function test_events_table_draft_filter() { wp_set_current_user( self::$trainer_user_id ); $url = add_query_arg( 'event_status', 'draft', get_permalink( self::$dashboard_page_id ) ); $this->go_to( $url ); // Placeholder assertion: $this->assertTrue( true, "Integration test for filtered table content needs implementation or E2E test." ); } } // End class Test_HVAC_Dashboard_Display