Skip to content

Commit

Permalink
add tests, update log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tharsheblows committed Aug 23, 2024
1 parent e8e737c commit 3c3ab3b
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 16 deletions.
57 changes: 44 additions & 13 deletions connectors/class-connector-two-factor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
),
Expand All @@ -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(),
),
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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,
),
Expand All @@ -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,
),
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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,
),
Expand Down
2 changes: 1 addition & 1 deletion connectors/class-connector-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion phpunit-multisite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</report>
</coverage>
<php>
<const name="WP_TEST_ACTIVATED_PLUGINS" value="advanced-custom-fields/acf.php,easy-digital-downloads/easy-digital-downloads.php,jetpack/jetpack.php,user-switching/user-switching.php"/>
<const name="WP_TEST_ACTIVATED_PLUGINS" value="advanced-custom-fields/acf.php,easy-digital-downloads/easy-digital-downloads.php,jetpack/jetpack.php,user-switching/user-switching.php,two-factor/two-factor.php"/>
</php>
<testsuites>
<testsuite name="stream">
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</report>
</coverage>
<php>
<const name="WP_TEST_ACTIVATED_PLUGINS" value="advanced-custom-fields/acf.php,easy-digital-downloads/easy-digital-downloads.php,jetpack/jetpack.php,user-switching/user-switching.php,wordpress-seo/wp-seo.php"/>
<const name="WP_TEST_ACTIVATED_PLUGINS" value="advanced-custom-fields/acf.php,easy-digital-downloads/easy-digital-downloads.php,jetpack/jetpack.php,user-switching/user-switching.php,wordpress-seo/wp-seo.php,two-factor/two-factor.php"/>
</php>
<testsuites>
<testsuite name="stream">
Expand Down
156 changes: 156 additions & 0 deletions tests/phpunit/connectors/test-class-connector-two-factor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* WP Integration Test w/ Advanced Custom Fields
*
* Tests for ACF connector class callbacks.
*
* @package WP_Stream
*/

namespace WP_Stream;

class Test_WP_Stream_Connector_Two_Factor extends WP_StreamTestCase {

/**
* Our user's id.
*
* @var string
*/
public $user_id;

/**
* Our user.
*
* @var \WP_User
*/
public $user;

/**
* Runs before each test
*/
public function setUp(): void {
parent::setUp();

$this->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' );
}
}

0 comments on commit 3c3ab3b

Please sign in to comment.