upskill-event-manager/wordpress-dev/tests/unit/test-login-handler.php
bengizmo 37f7b426b6 feat: Implement auto page creation & fix login E2E tests
Implements automatic creation of required plugin pages (Community Login,
Trainer Registration, Trainer Dashboard) upon plugin activation. This
addresses E2E test failures caused by missing pages in the test
environment.

- Adds activation hook in `hvac-community-events.php` to call
  `hvac_ce_create_required_pages`.
- The callback function checks for existing pages by slug and creates
  them using `wp_insert_post` if missing. Includes debug logging.

Also fixes issues identified during E2E test debugging:
- Corrects fatal error in `includes/community/class-login-handler.php`
  by replacing undefined constant `HVAC_COMMUNITY_EVENTS_PATH` with
  `HVAC_CE_PLUGIN_DIR`.
- Updates `tests/e2e/tests/login.spec.ts` to use the correct selector
  `#wp-submit` for the login form submit button instead of
  `button[type="submit"]`.

Documentation updates:
- Adds `docs/automatic-page-creation-plan.md`.
- Updates `README.md` regarding automatic page creation.
- Updates Memory Bank files (`decisionLog.md`, `progress.md`,
  `activeContext.md`).

Note: Activation hook logging did not appear during WP-CLI activation,
requiring further investigation if page creation issues persist. E2E
test confirmation pending.
2025-03-28 17:18:21 -03:00

119 lines
No EOL
3.8 KiB
PHP

<?php
/**
* Unit tests for the Login_Handler class.
*
* @package HVAC_Community_Events
*/
use HVAC_Community_Events\Community\Login_Handler;
use PHPUnit\Framework\TestCase;
/**
* @group login
*/
class Test_Login_Handler extends TestCase {
/**
* @var Login_Handler
*/
private $login_handler;
/**
* Setup method.
*/
protected function setUp(): void { // Changed to setUp and added :void
parent::setUp();
$this->login_handler = new Login_Handler();
}
/**
* Tear down method.
*/
protected function tearDown(): void { // Changed to tearDown and added :void
unset( $this->login_handler );
parent::tearDown();
}
/**
* Test that the class exists.
*/
public function test_class_exists() {
$this->assertTrue( class_exists( 'HVAC_Community_Events\Community\Login_Handler' ) );
}
/**
* Test that the shortcode is registered.
*/
public function test_shortcode_registered() {
global $shortcode_tags;
$this->assertArrayHasKey( 'hvac_community_login', $shortcode_tags );
}
/**
* Test that handle_authentication exists and doesn't throw an error.
*/
public function test_handle_authentication_exists() {
$this->assertTrue( method_exists( $this->login_handler, 'handle_authentication' ) );
// Since the method currently relies on WP core, we can't easily test its behavior directly.
// This test primarily ensures the method is defined and doesn't cause a fatal error.
$username = 'testuser';
$password = 'testpass';
$this->login_handler->handle_authentication( $username, $password ); // Just call the method
$this->assertNull( null ); // Always pass, just checking for no errors
}
/**
* Test custom_login_redirect with hvac_trainer role.
*/
public function test_custom_login_redirect_hvac_trainer() {
$user = new \WP_User( 1 ); // Create a mock user with ID 1
$user->roles = array( 'hvac_trainer' ); // Assign the hvac_trainer role
$redirect_to = 'default_url'; // Default redirect URL
$requested_redirect_to = ''; // No requested redirect
$result = $this->login_handler->custom_login_redirect( $redirect_to, $requested_redirect_to, $user );
$this->assertEquals( home_url( '/hvac-dashboard/' ), $result );
}
/**
* Test custom_login_redirect with a non-hvac_trainer role (admin).
*/
public function test_custom_login_redirect_non_hvac_trainer() {
$user = new \WP_User( 1 ); // Create a mock user with ID 1
$user->roles = array( 'administrator' ); // Assign the administrator role
$redirect_to = 'default_url'; // Default redirect URL
$requested_redirect_to = ''; // No requested redirect
$result = $this->login_handler->custom_login_redirect( $redirect_to, $requested_redirect_to, $user );
$this->assertEquals( admin_url(), $result );
}
/**
* Test custom_login_redirect with a non-hvac_trainer role and a requested redirect.
*/
public function test_custom_login_redirect_non_hvac_trainer_with_requested_redirect() {
$user = new \WP_User( 1 ); // Create a mock user with ID 1
$user->roles = array( 'administrator' ); // Assign the administrator role
$redirect_to = 'default_url'; // Default redirect URL
$requested_redirect_to = 'http://example.com/admin-page'; // A requested redirect
$result = $this->login_handler->custom_login_redirect( $redirect_to, $requested_redirect_to, $user );
$this->assertEquals( 'http://example.com/admin-page', $result );
}
/**
* Test custom_login_redirect with a WP_Error object (failed login).
*/
public function test_custom_login_redirect_wp_error() {
$user = new \WP_Error( 'authentication_failed', 'Authentication failed.' );
$redirect_to = 'default_url'; // Default redirect URL
$requested_redirect_to = ''; // No requested redirect
$result = $this->login_handler->custom_login_redirect( $redirect_to, $requested_redirect_to, $user );
$this->assertEquals( 'default_url', $result );
}
}