Skip to content

Commit e6dab20

Browse files
committed
add test cases and count guard
1 parent 6f78be7 commit e6dab20

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

CLI/class-two-factor-cli-command.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,16 @@ public function backup_codes( $args, $assoc_args ) {
543543
}
544544

545545
$count = (int) WP_CLI\Utils\get_flag_value( $assoc_args, 'count', Two_Factor_Backup_Codes::NUMBER_OF_CODES );
546+
if ( $count < 1 ) {
547+
WP_CLI::error(
548+
sprintf(
549+
/* translators: %d: provided count */
550+
__( 'Invalid value for --count: %d. It must be 1 or greater.', 'two-factor' ),
551+
$count
552+
)
553+
);
554+
}
555+
546556
$codes = Two_Factor_Backup_Codes::get_instance()->generate_codes(
547557
$user,
548558
array(

tests/cli/class-two-factor-cli-command.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,38 @@ public function test_backup_codes_generate_custom_count() {
655655
$this->assertSame( 6, $printed );
656656
}
657657

658+
/**
659+
* A count lower than 1 is rejected.
660+
*
661+
* @covers Two_Factor_CLI_Command::backup_codes
662+
*/
663+
public function test_backup_codes_generate_rejects_zero_count() {
664+
$message = $this->assert_command_aborts(
665+
function () {
666+
$this->command->backup_codes( array( 'generate', 'cli_test_user' ), array( 'count' => 0 ) );
667+
}
668+
);
669+
670+
$this->assertStringContainsString( 'Invalid value for --count', $message );
671+
$this->assertSame( 0, Two_Factor_Backup_Codes::codes_remaining_for_user( $this->user ) );
672+
}
673+
674+
/**
675+
* Negative count values are rejected.
676+
*
677+
* @covers Two_Factor_CLI_Command::backup_codes
678+
*/
679+
public function test_backup_codes_generate_rejects_negative_count() {
680+
$message = $this->assert_command_aborts(
681+
function () {
682+
$this->command->backup_codes( array( 'generate', 'cli_test_user' ), array( 'count' => -5 ) );
683+
}
684+
);
685+
686+
$this->assertStringContainsString( 'Invalid value for --count', $message );
687+
$this->assertSame( 0, Two_Factor_Backup_Codes::codes_remaining_for_user( $this->user ) );
688+
}
689+
658690
/**
659691
* Regenerating replaces the previous set of codes.
660692
*

0 commit comments

Comments
 (0)