From 3c3ab3b9f7ff49f30b246969e26d494e69d146c2 Mon Sep 17 00:00:00 2001 From: JJ Date: Fri, 23 Aug 2024 12:08:01 -0400 Subject: [PATCH] add tests, update log messages --- connectors/class-connector-two-factor.php | 57 +++++-- connectors/class-connector-users.php | 2 +- phpunit-multisite.xml | 2 +- phpunit.xml | 2 +- .../test-class-connector-two-factor.php | 156 ++++++++++++++++++ 5 files changed, 203 insertions(+), 16 deletions(-) create mode 100644 tests/phpunit/connectors/test-class-connector-two-factor.php diff --git a/connectors/class-connector-two-factor.php b/connectors/class-connector-two-factor.php index 3510e18f..f22dee13 100644 --- a/connectors/class-connector-two-factor.php +++ b/connectors/class-connector-two-factor.php @@ -139,8 +139,15 @@ public function log_override( $data ) { * @param object $provider The 2FA Provider used. */ public function callback_two_factor_user_authenticated( $user, $provider ) { - $this->log( + + /* Translators: %s is the Two Factor provider. */ + $message = __( 'Authenticated via %s', + 'stream' + ); + + $this->log( + $message, array( 'provider' => $provider->get_key(), ), @@ -167,10 +174,16 @@ public function callback_wp_login_failed( $user_login, $error ) { $user = get_user_by( 'email', $user_login ); } + /* Translators: %1$s is the user display name, %2$s is the error code, %3$s is the error message. */ + $message = __( + '%1$s Failed 2FA: %2$s %3$s', + 'stream' + ); + $this->log( - '%s Failed 2FA: %s %s', + $message, array( - 'display_name' => $user->display_name, + 'display_name' => $this->escape_percentages( $user->display_name ), 'code' => $error->get_error_code(), 'error' => $error->get_error_message(), ), @@ -219,7 +232,7 @@ public function callback_updated_user_meta( $meta_id, $user_id, $meta_key, $new_ switch ( $meta_key ) { case '_two_factor_backup_codes': $this->log( - esc_html__( 'Updated backup codes', 'stream' ), + __( 'Updated backup codes', 'stream' ), array(), $user_id, 'user-settings', @@ -228,7 +241,7 @@ public function callback_updated_user_meta( $meta_id, $user_id, $meta_key, $new_ break; case '_two_factor_totp_key': $this->log( - esc_html__( 'Set TOTP secret key' ), + __( 'Set TOTP secret key', 'stream' ), array(), $user_id, 'user-settings', @@ -243,9 +256,15 @@ public function callback_updated_user_meta( $meta_id, $user_id, $meta_key, $new_ $disabled_providers = array_diff( $old_providers, $new_providers ); foreach ( $enabled_providers as $provider ) { + + /* Translators: %s is the Two Factor provider. */ + $message = __( + 'Enabled provider: %s', + 'stream' + ); + $this->log( - /* Translators: %s is the provider */ - esc_html__( 'Enabled provider: %s', 'stream' ), + $message, array( 'provider' => $provider, ), @@ -256,9 +275,15 @@ public function callback_updated_user_meta( $meta_id, $user_id, $meta_key, $new_ } foreach ( $disabled_providers as $provider ) { + + /* Translators: %s is the Two Factor provider. */ + $message = __( + 'Disabled provider: %s', + 'stream' + ); + $this->log( - /* Translators: %s is the provider */ - esc_html__( 'Disabled provider: %s', 'stream' ), + $message, array( 'provider' => $provider, ), @@ -284,7 +309,7 @@ public function callback_added_user_meta( $meta_id, $user_id, $meta_key, $meta_v switch ( $meta_key ) { case '_two_factor_backup_codes': $this->log( - esc_html__( 'Added backup codes', 'stream' ), + __( 'Added backup codes', 'stream' ), array(), $user_id, 'user-settings', @@ -293,7 +318,7 @@ public function callback_added_user_meta( $meta_id, $user_id, $meta_key, $meta_v break; case '_two_factor_totp_key': $this->log( - esc_html__( 'Added TOTP secret key' ), + __( 'Added TOTP secret key', 'stream' ), array(), $user_id, 'user-settings', @@ -302,9 +327,15 @@ public function callback_added_user_meta( $meta_id, $user_id, $meta_key, $meta_v break; case '_two_factor_enabled_providers': foreach ( $meta_value as $provider ) { + + /* Translators: %s is the Two Factor provider. */ + $message = __( + 'Enabled provider: %s', + 'stream' + ); + $this->log( - /* Translators: %s is the provider */ - esc_html__( 'Enabled provider: %s', 'stream' ), + $message, array( 'provider' => $provider, ), diff --git a/connectors/class-connector-users.php b/connectors/class-connector-users.php index 6eb24357..91b6287e 100644 --- a/connectors/class-connector-users.php +++ b/connectors/class-connector-users.php @@ -182,7 +182,7 @@ public function callback_user_register( $user_id ) { * @param int $user_id Registered user ID. * @param \WP_User $user Registered user object. */ - public function callback_profile_update( $user_id, $user, $new_userdata ) { + public function callback_profile_update( $user_id, $user ) { unset( $user_id ); $this->log( diff --git a/phpunit-multisite.xml b/phpunit-multisite.xml index d3107e84..6169376b 100644 --- a/phpunit-multisite.xml +++ b/phpunit-multisite.xml @@ -27,7 +27,7 @@ - + diff --git a/phpunit.xml b/phpunit.xml index f02a303d..990e46c6 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -27,7 +27,7 @@ - + diff --git a/tests/phpunit/connectors/test-class-connector-two-factor.php b/tests/phpunit/connectors/test-class-connector-two-factor.php new file mode 100644 index 00000000..da06ae25 --- /dev/null +++ b/tests/phpunit/connectors/test-class-connector-two-factor.php @@ -0,0 +1,156 @@ +plugin->connectors->unload_connectors(); + + // Make partial of Connector_Two_Factor class, with mocked "log" function. + $this->mock = $this->getMockBuilder( Connector_Two_Factor::class ) + ->onlyMethods( array( 'log' ) ) + ->getMock(); + + // Register connector. + $this->mock->register(); + + // Allow us to have the Two_Factor_Dummy option. + remove_all_filters( 'two_factor_providers' ); + + if ( empty( $this->user_id ) ) { + $this->user_id = self::factory()->user->create( + array( + 'user_login' => 'testuser', + 'user_role' => 'administrator', + 'display_name' => 'testuserdisplay', + ) + ); + + $this->user = get_user_by( 'ID', $this->user_id ); + + \Two_Factor_Core::enable_provider_for_user( $this->user_id, 'Two_Factor_Dummy' ); + } + } + + /** + * Confirm that Two Factor is installed and active. + */ + public function test_two_factor_installed_and_activated() { + $this->assertTrue( class_exists( 'Two_Factor_Core' ) ); + } + + /** + * Test that adding a provider triggers the log. + */ + public function test_callback_added_user_meta() { + + $this->mock->expects( $this->once() ) + ->method( 'log' ) + ->with( + $this->equalTo( + __( + 'Enabled provider: %s', + 'stream' + ) + ), + $this->equalTo( + array( + 'provider' => 'Two_Factor_Email', + ) + ), + $this->user_id, + 'user-settings', + 'enabled' + ); + + \Two_Factor_Core::enable_provider_for_user( $this->user_id, 'Two_Factor_Email' ); + } + + /** + * Tests the "callback_save_two_factor_user_authenticated" callback. + * This tests the log via doing the action. + */ + public function test_callback_two_factor_user_authenticated() { + + wp_set_current_user( $this->user_id ); + + $this->mock->expects( $this->once() ) + ->method( 'log' ) + ->with( + $this->equalTo( + __( + 'Authenticated via %s', + 'stream' + ) + ), + $this->equalTo( + array( + 'provider' => 'Two_Factor_Dummy', + ) + ), + $this->user_id, + 'auth', + 'authenticated', + $this->user_id + ); + + $provider = \Two_Factor_Core::get_provider_for_user( $this->user, 'Two_Factor_Dummy' ); + + // We can't test the method so we'll trigger the action. + do_action( 'two_factor_user_authenticated', $this->user, \Two_Factor_Core::get_provider_for_user( $this->user, $provider ) ); + } + + /** + * Test that adding a provider triggers the log. + */ + public function test_callback_updated_user_meta() { + + $this->mock->expects( $this->once() ) + ->method( 'log' ) + ->with( + $this->equalTo( + __( + 'Disabled provider: %s', + 'stream' + ) + ), + $this->equalTo( + array( + 'provider' => 'Two_Factor_Dummy', + ), + ), + $this->user_id, + 'user-settings', + 'disabled' + ); + + \Two_Factor_Core::disable_provider_for_user( $this->user_id, 'Two_Factor_Dummy' ); + } +}