upskill-event-manager/wordpress-dev/tests/integration/test-event-management-integration.php
bengizmo 7fa7459fad Refactors event submission and listing to use TEC Community Events
shortcodes ([tribe_community_events view=\"...\"]) on dedicated pages
(/manage-event/, /my-events/) instead of child theme template overrides.

This change addresses persistent content duplication and layout issues
encountered with the template override method.

Changes include:
- Updating plugin activation hook to create new pages with shortcodes.
- Updating dashboard links to point to new pages.
- Removing child theme override files and related custom handler logic.
- Updating integration tests for activation.
- Adding/updating E2E tests for dashboard links and new pages.
- Fixing `run-tests.sh` corruption and adding pre-E2E setup steps
  (plugin reactivation, rewrite flush) to resolve 404s.
- Updating relevant documentation and memory bank files.

Testing:
- Integration tests pass.
- E2E tests pass for core functionality (login, dashboard, links).
- E2E tests for rendering of TEC CE shortcodes on new pages fail due
  to environment-specific issues (likely JS/timing) and are recommended
  to be skipped. Manual verification confirms pages render correctly."
2025-04-02 22:23:37 -03:00

185 lines
No EOL
6.8 KiB
PHP

<?php
/**
* Integration tests for plugin activation/deactivation.
*
* @package Hvac_Community_Events
*/
use Yoast\WPTestUtils\WPIntegration;
/**
* Class Event_Management_Integration_Test
*
* Tests plugin activation and deactivation hooks.
*/
class Event_Management_Integration_Test extends WP_UnitTestCase {
// --- Properties for Activation Tests ---
protected $plugin_slug = 'hvac-community-events/hvac-community-events.php';
protected $created_pages_option = 'hvac_community_pages';
// Updated list of pages expected to be created by activation
protected $required_pages = [
'community-login' => [
'title' => 'Community Login',
'shortcode' => '[hvac_community_login]', // Assuming this is still needed
],
'trainer-registration' => [
'title' => 'Trainer Registration',
'shortcode' => '[hvac_trainer_registration]', // Assuming this is still needed
],
'hvac-dashboard' => [
'title' => 'Trainer Dashboard',
'shortcode' => null, // No specific shortcode expected in content
],
'manage-event' => [ // New page
'title' => 'Manage Event',
'shortcode' => '[tribe_community_events view="submission_form"]',
],
'my-events' => [ // New page
'title' => 'My Events',
'shortcode' => '[tribe_community_events view="my_events"]',
],
];
protected $trainer_role = 'hvac_trainer';
// --- End Properties for Activation Tests ---
/**
* Set up the test environment before each test method runs.
*/
public function set_up() {
parent::set_up();
// Ensure plugin is deactivated before tests that need to activate it
if ( is_plugin_active( $this->plugin_slug ) ) {
deactivate_plugins( $this->plugin_slug );
}
// Clean up potential leftovers before tests
$this->clean_up_plugin_artifacts();
// 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
// Clean up artifacts after tests
$this->clean_up_plugin_artifacts();
// Ensure plugin is deactivated after tests
if ( is_plugin_active( $this->plugin_slug ) ) {
deactivate_plugins( $this->plugin_slug );
}
parent::tear_down();
}
/**
* Helper function to delete pages, roles, and options created by activation.
*/
protected function clean_up_plugin_artifacts() {
// Delete pages
foreach ( array_keys($this->required_pages) as $slug ) {
$page = get_page_by_path( $slug, OBJECT, 'page' );
if ( $page ) {
wp_delete_post( $page->ID, true ); // Force delete
}
}
// Delete option
delete_option( $this->created_pages_option );
// Delete role
if ( get_role( $this->trainer_role ) ) {
remove_role( $this->trainer_role );
}
}
// --- Test Cases ---
// REMOVED Obsolete tests for old custom event handler:
// - test_tec_ce_handler_creates_event_successfully
// - test_tec_ce_handler_modifies_event_successfully
// - test_tec_ce_handler_prevents_creation_with_invalid_data
// --- Activation/Deactivation Tests ---
/**
* Test that activating the plugin creates the required pages with correct content.
* @test
*/
public function test_activation_creates_required_pages_with_shortcodes() {
// Ensure pages don't exist initially
foreach ( array_keys($this->required_pages) as $slug ) {
$this->assertNull( get_page_by_path( $slug, OBJECT, 'page' ), "Page '{$slug}' should not exist before activation." );
}
// Activate the plugin
activate_plugin( $this->plugin_slug );
// Verify pages now exist, are published, and have correct content/shortcode
$created_pages_data = get_option( $this->created_pages_option );
$this->assertIsArray( $created_pages_data, "Option '{$this->created_pages_option}' should be an array." );
foreach ( $this->required_pages as $slug => $details ) {
$page = get_page_by_path( $slug, OBJECT, 'page' );
$this->assertNotNull( $page, "Page '{$slug}' should exist after activation." );
if ($page) {
$this->assertEquals( 'publish', $page->post_status, "Page '{$slug}' should be published." );
// Check if shortcode exists in content (if one is expected)
if ( !is_null($details['shortcode']) ) {
$this->assertStringContainsString(
$details['shortcode'],
$page->post_content,
"Page '{$slug}' content should contain the shortcode '{$details['shortcode']}'."
);
}
// Verify page ID stored in option matches actual page ID
$feature_key = str_replace('-', '_', $slug);
$this->assertArrayHasKey($feature_key, $created_pages_data, "Option should contain key '{$feature_key}'.");
$this->assertEquals($page->ID, $created_pages_data[$feature_key], "Stored page ID for '{$slug}' should match actual page ID.");
}
}
// Verify the option tracking created pages exists and is not empty
$this->assertNotEmpty( $created_pages_data, "Option '{$this->created_pages_option}' should not be empty after activation." );
}
/**
* Test that activating the plugin creates the required hvac_trainer role.
* @test
*/
public function test_activation_creates_trainer_role() {
// Ensure role doesn't exist initially
$this->assertNull( get_role( $this->trainer_role ), "Role '{$this->trainer_role}' should not exist before activation." );
// Activate the plugin
activate_plugin( $this->plugin_slug );
// Verify role now exists
$role = get_role( $this->trainer_role );
$this->assertNotNull( $role, "Role '{$this->trainer_role}' should exist after activation." );
$this->assertInstanceOf( 'WP_Role', $role, "Role '{$this->trainer_role}' should be a WP_Role object." );
}
/**
* Test that deactivating the plugin removes the hvac_trainer role.
* @test
*/
public function test_deactivation_removes_trainer_role() {
// First, activate the plugin to ensure the role exists
activate_plugin( $this->plugin_slug );
$this->assertNotNull( get_role( $this->trainer_role ), "Role '{$this->trainer_role}' should exist before deactivation test." );
// Deactivate the plugin - this should trigger the hook
deactivate_plugins( $this->plugin_slug );
// Verify the role no longer exists
$this->assertNull( get_role( $this->trainer_role ), "Role '{$this->trainer_role}' should not exist after deactivation." );
}
}