Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/providers/class-two-factor-backup-codes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
48 changes: 48 additions & 0 deletions tests/providers/class-two-factor-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}