upskill-event-manager/wordpress-dev/tests/integration/test-dashboard-display.php
bengizmo cdef12ee80 feat(events): Implement fallback logic and UI for Create/Modify Event page
- Refactored fallback submission logic in `class-event-handler.php` to remove `wp_die`/`exit` calls and use redirects for error handling, enabling proper unit testing.
- Implemented meta-data saving (dates, venue, organizer) in the fallback logic using `update_post_meta`.
- Updated unit tests (`test-event-management.php`) to remove `markTestIncomplete` calls related to handler errors and uncommented meta assertions. Unit tests for fallback logic now pass.
- Added Instructions section and Return to Dashboard button to the event form shortcode (`display_event_form_shortcode`).
- Applied basic theme styling classes (`ast-container`, `notice`, `ast-button`) to the event form.
- Updated `docs/implementation_plan.md` to reflect completion of tasks 4.1-4.5 and set focus to Task 5.

Refs: Task 4.1, 4.2, 4.3, 4.4, 4.5
2025-04-01 11:46:24 -03:00

173 lines
No EOL
6.8 KiB
PHP

<?php
/**
* Integration Tests for HVAC Trainer Dashboard Display
*
* @package HVAC Community Events
* @subpackage Tests
*/
/**
* Class Test_HVAC_Dashboard_Display
*
* Tests the display and basic functionality of the Trainer Dashboard template.
*/
class Test_HVAC_Dashboard_Display extends WP_UnitTestCase {
protected static $trainer_user_id;
protected static $subscriber_user_id;
protected static $dashboard_page_id;
protected static $event_ids = [];
public static function wpSetUpBeforeClass(): void {
// Create users
self::$trainer_user_id = self::factory()->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