Skip to content

Commit 3c3ab3b

Browse files
committed
add tests, update log messages
1 parent e8e737c commit 3c3ab3b

File tree

5 files changed

+203
-16
lines changed

5 files changed

+203
-16
lines changed

connectors/class-connector-two-factor.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,15 @@ public function log_override( $data ) {
139139
* @param object $provider The 2FA Provider used.
140140
*/
141141
public function callback_two_factor_user_authenticated( $user, $provider ) {
142-
$this->log(
142+
143+
/* Translators: %s is the Two Factor provider. */
144+
$message = __(
143145
'Authenticated via %s',
146+
'stream'
147+
);
148+
149+
$this->log(
150+
$message,
144151
array(
145152
'provider' => $provider->get_key(),
146153
),
@@ -167,10 +174,16 @@ public function callback_wp_login_failed( $user_login, $error ) {
167174
$user = get_user_by( 'email', $user_login );
168175
}
169176

177+
/* Translators: %1$s is the user display name, %2$s is the error code, %3$s is the error message. */
178+
$message = __(
179+
'%1$s Failed 2FA: %2$s %3$s',
180+
'stream'
181+
);
182+
170183
$this->log(
171-
'%s Failed 2FA: %s %s',
184+
$message,
172185
array(
173-
'display_name' => $user->display_name,
186+
'display_name' => $this->escape_percentages( $user->display_name ),
174187
'code' => $error->get_error_code(),
175188
'error' => $error->get_error_message(),
176189
),
@@ -219,7 +232,7 @@ public function callback_updated_user_meta( $meta_id, $user_id, $meta_key, $new_
219232
switch ( $meta_key ) {
220233
case '_two_factor_backup_codes':
221234
$this->log(
222-
esc_html__( 'Updated backup codes', 'stream' ),
235+
__( 'Updated backup codes', 'stream' ),
223236
array(),
224237
$user_id,
225238
'user-settings',
@@ -228,7 +241,7 @@ public function callback_updated_user_meta( $meta_id, $user_id, $meta_key, $new_
228241
break;
229242
case '_two_factor_totp_key':
230243
$this->log(
231-
esc_html__( 'Set TOTP secret key' ),
244+
__( 'Set TOTP secret key', 'stream' ),
232245
array(),
233246
$user_id,
234247
'user-settings',
@@ -243,9 +256,15 @@ public function callback_updated_user_meta( $meta_id, $user_id, $meta_key, $new_
243256
$disabled_providers = array_diff( $old_providers, $new_providers );
244257

245258
foreach ( $enabled_providers as $provider ) {
259+
260+
/* Translators: %s is the Two Factor provider. */
261+
$message = __(
262+
'Enabled provider: %s',
263+
'stream'
264+
);
265+
246266
$this->log(
247-
/* Translators: %s is the provider */
248-
esc_html__( 'Enabled provider: %s', 'stream' ),
267+
$message,
249268
array(
250269
'provider' => $provider,
251270
),
@@ -256,9 +275,15 @@ public function callback_updated_user_meta( $meta_id, $user_id, $meta_key, $new_
256275
}
257276

258277
foreach ( $disabled_providers as $provider ) {
278+
279+
/* Translators: %s is the Two Factor provider. */
280+
$message = __(
281+
'Disabled provider: %s',
282+
'stream'
283+
);
284+
259285
$this->log(
260-
/* Translators: %s is the provider */
261-
esc_html__( 'Disabled provider: %s', 'stream' ),
286+
$message,
262287
array(
263288
'provider' => $provider,
264289
),
@@ -284,7 +309,7 @@ public function callback_added_user_meta( $meta_id, $user_id, $meta_key, $meta_v
284309
switch ( $meta_key ) {
285310
case '_two_factor_backup_codes':
286311
$this->log(
287-
esc_html__( 'Added backup codes', 'stream' ),
312+
__( 'Added backup codes', 'stream' ),
288313
array(),
289314
$user_id,
290315
'user-settings',
@@ -293,7 +318,7 @@ public function callback_added_user_meta( $meta_id, $user_id, $meta_key, $meta_v
293318
break;
294319
case '_two_factor_totp_key':
295320
$this->log(
296-
esc_html__( 'Added TOTP secret key' ),
321+
__( 'Added TOTP secret key', 'stream' ),
297322
array(),
298323
$user_id,
299324
'user-settings',
@@ -302,9 +327,15 @@ public function callback_added_user_meta( $meta_id, $user_id, $meta_key, $meta_v
302327
break;
303328
case '_two_factor_enabled_providers':
304329
foreach ( $meta_value as $provider ) {
330+
331+
/* Translators: %s is the Two Factor provider. */
332+
$message = __(
333+
'Enabled provider: %s',
334+
'stream'
335+
);
336+
305337
$this->log(
306-
/* Translators: %s is the provider */
307-
esc_html__( 'Enabled provider: %s', 'stream' ),
338+
$message,
308339
array(
309340
'provider' => $provider,
310341
),

connectors/class-connector-users.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function callback_user_register( $user_id ) {
182182
* @param int $user_id Registered user ID.
183183
* @param \WP_User $user Registered user object.
184184
*/
185-
public function callback_profile_update( $user_id, $user, $new_userdata ) {
185+
public function callback_profile_update( $user_id, $user ) {
186186
unset( $user_id );
187187

188188
$this->log(

phpunit-multisite.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</report>
2828
</coverage>
2929
<php>
30-
<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"/>
30+
<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"/>
3131
</php>
3232
<testsuites>
3333
<testsuite name="stream">

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</report>
2828
</coverage>
2929
<php>
30-
<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"/>
30+
<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"/>
3131
</php>
3232
<testsuites>
3333
<testsuite name="stream">
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
/**
3+
* WP Integration Test w/ Advanced Custom Fields
4+
*
5+
* Tests for ACF connector class callbacks.
6+
*
7+
* @package WP_Stream
8+
*/
9+
10+
namespace WP_Stream;
11+
12+
class Test_WP_Stream_Connector_Two_Factor extends WP_StreamTestCase {
13+
14+
/**
15+
* Our user's id.
16+
*
17+
* @var string
18+
*/
19+
public $user_id;
20+
21+
/**
22+
* Our user.
23+
*
24+
* @var \WP_User
25+
*/
26+
public $user;
27+
28+
/**
29+
* Runs before each test
30+
*/
31+
public function setUp(): void {
32+
parent::setUp();
33+
34+
$this->plugin->connectors->unload_connectors();
35+
36+
// Make partial of Connector_Two_Factor class, with mocked "log" function.
37+
$this->mock = $this->getMockBuilder( Connector_Two_Factor::class )
38+
->onlyMethods( array( 'log' ) )
39+
->getMock();
40+
41+
// Register connector.
42+
$this->mock->register();
43+
44+
// Allow us to have the Two_Factor_Dummy option.
45+
remove_all_filters( 'two_factor_providers' );
46+
47+
if ( empty( $this->user_id ) ) {
48+
$this->user_id = self::factory()->user->create(
49+
array(
50+
'user_login' => 'testuser',
51+
'user_role' => 'administrator',
52+
'display_name' => 'testuserdisplay',
53+
)
54+
);
55+
56+
$this->user = get_user_by( 'ID', $this->user_id );
57+
58+
\Two_Factor_Core::enable_provider_for_user( $this->user_id, 'Two_Factor_Dummy' );
59+
}
60+
}
61+
62+
/**
63+
* Confirm that Two Factor is installed and active.
64+
*/
65+
public function test_two_factor_installed_and_activated() {
66+
$this->assertTrue( class_exists( 'Two_Factor_Core' ) );
67+
}
68+
69+
/**
70+
* Test that adding a provider triggers the log.
71+
*/
72+
public function test_callback_added_user_meta() {
73+
74+
$this->mock->expects( $this->once() )
75+
->method( 'log' )
76+
->with(
77+
$this->equalTo(
78+
__(
79+
'Enabled provider: %s',
80+
'stream'
81+
)
82+
),
83+
$this->equalTo(
84+
array(
85+
'provider' => 'Two_Factor_Email',
86+
)
87+
),
88+
$this->user_id,
89+
'user-settings',
90+
'enabled'
91+
);
92+
93+
\Two_Factor_Core::enable_provider_for_user( $this->user_id, 'Two_Factor_Email' );
94+
}
95+
96+
/**
97+
* Tests the "callback_save_two_factor_user_authenticated" callback.
98+
* This tests the log via doing the action.
99+
*/
100+
public function test_callback_two_factor_user_authenticated() {
101+
102+
wp_set_current_user( $this->user_id );
103+
104+
$this->mock->expects( $this->once() )
105+
->method( 'log' )
106+
->with(
107+
$this->equalTo(
108+
__(
109+
'Authenticated via %s',
110+
'stream'
111+
)
112+
),
113+
$this->equalTo(
114+
array(
115+
'provider' => 'Two_Factor_Dummy',
116+
)
117+
),
118+
$this->user_id,
119+
'auth',
120+
'authenticated',
121+
$this->user_id
122+
);
123+
124+
$provider = \Two_Factor_Core::get_provider_for_user( $this->user, 'Two_Factor_Dummy' );
125+
126+
// We can't test the method so we'll trigger the action.
127+
do_action( 'two_factor_user_authenticated', $this->user, \Two_Factor_Core::get_provider_for_user( $this->user, $provider ) );
128+
}
129+
130+
/**
131+
* Test that adding a provider triggers the log.
132+
*/
133+
public function test_callback_updated_user_meta() {
134+
135+
$this->mock->expects( $this->once() )
136+
->method( 'log' )
137+
->with(
138+
$this->equalTo(
139+
__(
140+
'Disabled provider: %s',
141+
'stream'
142+
)
143+
),
144+
$this->equalTo(
145+
array(
146+
'provider' => 'Two_Factor_Dummy',
147+
),
148+
),
149+
$this->user_id,
150+
'user-settings',
151+
'disabled'
152+
);
153+
154+
\Two_Factor_Core::disable_provider_for_user( $this->user_id, 'Two_Factor_Dummy' );
155+
}
156+
}

0 commit comments

Comments
 (0)