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 ); } }