From 96172d97e7c9fa022148c2a668f72e7dca3b219d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 4 Mar 2026 15:50:17 +0000 Subject: [PATCH 1/2] Initial plan From a2d4ee5dd93ffe64fa503c3ef9b946060fe73d58 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 4 Mar 2026 16:00:36 +0000 Subject: [PATCH 2/2] Add test coverage for get_code_length() and two_factor_autosubmit_length filter Co-authored-by: georgestephanis <941023+georgestephanis@users.noreply.github.com> --- .../class-two-factor-backup-codes.php | 23 +++++++++ tests/providers/class-two-factor-provider.php | 48 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/tests/providers/class-two-factor-backup-codes.php b/tests/providers/class-two-factor-backup-codes.php index 8d976eca..d3eec34c 100644 --- a/tests/providers/class-two-factor-backup-codes.php +++ b/tests/providers/class-two-factor-backup-codes.php @@ -218,4 +218,27 @@ function () { remove_all_filters( 'two_factor_backup_code_length' ); } + + /** + * Test that the two_factor_autosubmit_length filter changes the data-digits attribute on the authentication page. + * + * @covers Two_Factor_Backup_Codes::authentication_page + */ + public function test_autosubmit_length_filter_affects_authentication_page() { + // Default: data-digits should reflect the default backup code length (8). + // Pass false as the user since no user-specific code length is needed for this test. + ob_start(); + $this->provider->authentication_page( false ); + $default_output = ob_get_clean(); + $this->assertStringContainsString( 'data-digits="8"', $default_output ); + + // With filter: data-digits should be overridden to 0 (disables autosubmit). + add_filter( 'two_factor_autosubmit_length', '__return_zero' ); + ob_start(); + $this->provider->authentication_page( false ); + $filtered_output = ob_get_clean(); + remove_filter( 'two_factor_autosubmit_length', '__return_zero' ); + + $this->assertStringContainsString( 'data-digits="0"', $filtered_output ); + } } diff --git a/tests/providers/class-two-factor-provider.php b/tests/providers/class-two-factor-provider.php index dcce6c6f..93520ddc 100644 --- a/tests/providers/class-two-factor-provider.php +++ b/tests/providers/class-two-factor-provider.php @@ -87,4 +87,52 @@ public function test_get_instance() { $this->assertSame( $instance_one, $instance_two ); } + + /** + * Test that get_code_length() returns the default value when no filter is applied. + * + * @covers Two_Factor_Provider::get_code_length + */ + public function test_get_code_length_returns_default() { + $this->assertSame( 8, Two_Factor_Provider::get_code_length( 8 ) ); + $this->assertSame( 6, Two_Factor_Provider::get_code_length( 6 ) ); + } + + /** + * Test that the two_factor_code_length filter can override the default code length. + * + * @covers Two_Factor_Provider::get_code_length + */ + public function test_get_code_length_filter_overrides_default() { + $set_length_to_4 = function() { + return 4; + }; + add_filter( 'two_factor_code_length', $set_length_to_4 ); + $this->assertSame( 4, Two_Factor_Provider::get_code_length( 8 ) ); + remove_filter( 'two_factor_code_length', $set_length_to_4 ); + + $set_length_to_12 = function() { + return 12; + }; + add_filter( 'two_factor_code_length', $set_length_to_12 ); + $this->assertSame( 12, Two_Factor_Provider::get_code_length( 8 ) ); + remove_filter( 'two_factor_code_length', $set_length_to_12 ); + } + + /** + * Test that get_code( null ) uses the filtered code length from two_factor_code_length. + * + * @covers Two_Factor_Provider::get_code + * @covers Two_Factor_Provider::get_code_length + */ + public function test_get_code_with_null_uses_filtered_length() { + $set_length_to_5 = function() { + return 5; + }; + add_filter( 'two_factor_code_length', $set_length_to_5 ); + $code = Two_Factor_Provider::get_code( null ); + remove_filter( 'two_factor_code_length', $set_length_to_5 ); + + $this->assertSame( 5, strlen( $code ) ); + } }