@@ -2707,4 +2707,66 @@ public function test_add_settings_action_link() {
27072707 $ this ->assertStringContainsString ( 'Settings ' , $ first );
27082708 $ this ->assertStringContainsString ( 'options-general.php ' , $ first );
27092709 }
2710+
2711+ /**
2712+ * @covers Two_Factor_Core::action_revalidate_session
2713+ */
2714+ public function test_action_revalidate_session_redirects_when_not_recent () {
2715+ $ user_id = self ::factory ()->user ->create ();
2716+ wp_set_current_user ( $ user_id );
2717+ update_user_meta ( $ user_id , Two_Factor_Core::ENABLED_PROVIDERS_USER_META_KEY , array ( 'Two_Factor_Dummy ' ) );
2718+ update_user_meta ( $ user_id , Two_Factor_Core::PROVIDER_USER_META_KEY , 'Two_Factor_Dummy ' );
2719+
2720+ // Simulate an old session (20 minutes ago)
2721+ $ old_time = time () - ( 20 * MINUTE_IN_SECONDS );
2722+ Two_Factor_Core::update_current_user_session (
2723+ array (
2724+ 'two-factor-provider ' => 'Two_Factor_Dummy ' ,
2725+ 'two-factor-login ' => $ old_time ,
2726+ )
2727+ );
2728+
2729+ $ _SERVER ['REQUEST_URI ' ] = '/wp-admin/post.php?post=1&action=edit ' ;
2730+
2731+ // Catch the redirect exception
2732+ $ redirected = false ;
2733+ try {
2734+ Two_Factor_Core::action_revalidate_session ( 300 , '/custom-redirect/ ' );
2735+ } catch ( Two_Factor_Redirect_Exception $ e ) {
2736+ $ redirected = true ;
2737+ $ location = $ e ->getMessage ();
2738+ $ this ->assertStringContainsString ( 'action=revalidate_2fa ' , $ location );
2739+ $ this ->assertStringContainsString ( 'redirect_to=%2Fcustom-redirect%2F ' , $ location );
2740+ }
2741+
2742+ $ this ->assertTrue ( $ redirected , 'Expected wp_safe_redirect to throw Two_Factor_Redirect_Exception. ' );
2743+ }
2744+
2745+ /**
2746+ * @covers Two_Factor_Core::action_revalidate_session
2747+ */
2748+ public function test_action_revalidate_session_bypasses_when_recent () {
2749+ $ user_id = self ::factory ()->user ->create ();
2750+ wp_set_current_user ( $ user_id );
2751+ update_user_meta ( $ user_id , Two_Factor_Core::ENABLED_PROVIDERS_USER_META_KEY , array ( 'Two_Factor_Dummy ' ) );
2752+
2753+ // Simulate a recent session (1 minute ago)
2754+ $ recent_time = time () - MINUTE_IN_SECONDS ;
2755+ Two_Factor_Core::update_current_user_session (
2756+ array (
2757+ 'two-factor-provider ' => 'Two_Factor_Dummy ' ,
2758+ 'two-factor-login ' => $ recent_time ,
2759+ )
2760+ );
2761+
2762+ // Should not throw an exception (no redirect)
2763+ $ exception = false ;
2764+ try {
2765+ Two_Factor_Core::action_revalidate_session ( 300 );
2766+ } catch ( Two_Factor_Redirect_Exception $ e ) {
2767+ $ exception = true ;
2768+ }
2769+
2770+ $ this ->assertFalse ( $ exception , 'Expected no redirect for a recent session. ' );
2771+ }
27102772}
0 commit comments