Skip to content

Commit 2d3fddb

Browse files
committed
Add tests for record_activity() behavior across REPORT and BLOCK modes
1 parent f576997 commit 2d3fddb

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

tests/phpunit/test-inactive-users.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,92 @@ public function test_single_site_unblock_action() {
10401040
unset( $_GET['action'], $_GET['user_id'], $_GET['reset_last_seen_nonce'] );
10411041
}
10421042

1043+
/**
1044+
* Test that record_activity() updates last seen meta for an inactive user in REPORT mode.
1045+
*
1046+
* This is a regression test for the bug where REPORT mode created a permanent
1047+
* inactive flag: the early-return in record_activity() only checked
1048+
* is_considered_inactive() without also checking is_block_action_enabled(),
1049+
* so once a user was flagged inactive in REPORT mode, their activity was
1050+
* never recorded again — making the flag permanent.
1051+
*/
1052+
public function test_record_activity_updates_last_seen_in_report_mode_for_inactive_user() {
1053+
// Create anonymous subclass to set mode to REPORT.
1054+
$inactive_users_class = new class() extends Inactive_Users {
1055+
public static function reset_for_test() {
1056+
self::$mode = 'REPORT';
1057+
self::$elevated_roles = [ 'administrator' ];
1058+
self::$considered_inactive_after_days = 90;
1059+
}
1060+
};
1061+
1062+
$inactive_users_class::reset_for_test();
1063+
1064+
// Create an inactive admin user.
1065+
$user_id = $this->factory->user->create( [
1066+
'role' => 'administrator',
1067+
'user_registered' => gmdate( 'Y-m-d H:i:s', strtotime( '-100 days' ) ),
1068+
] );
1069+
delete_user_meta( $user_id, Inactive_Users::LAST_SEEN_IGNORE_INACTIVITY_CHECK_UNTIL_META_KEY );
1070+
1071+
$old_timestamp = strtotime( '-91 days' );
1072+
update_user_meta( $user_id, Inactive_Users::LAST_SEEN_META_KEY, $old_timestamp );
1073+
1074+
// Confirm the user is considered inactive.
1075+
$this->assertTrue( Inactive_Users::is_considered_inactive( $user_id ) );
1076+
1077+
// Simulate the user being logged in and clear the cache so record_activity() proceeds.
1078+
wp_set_current_user( $user_id );
1079+
wp_cache_delete( $user_id, Inactive_Users::LAST_SEEN_CACHE_GROUP );
1080+
1081+
// Call record_activity() — in REPORT mode this should NOT early-return.
1082+
Inactive_Users::record_activity();
1083+
1084+
// The last seen timestamp should have been updated.
1085+
$new_timestamp = get_user_meta( $user_id, Inactive_Users::LAST_SEEN_META_KEY, true );
1086+
$this->assertGreaterThan( $old_timestamp, (int) $new_timestamp, 'record_activity() should update last_seen in REPORT mode even for inactive users' );
1087+
1088+
// Clean up.
1089+
wp_delete_user( $user_id );
1090+
}
1091+
1092+
/**
1093+
* Test that record_activity() still skips updating last seen for inactive users in BLOCK mode.
1094+
*
1095+
* In BLOCK mode, inactive users must be manually unblocked by an admin before
1096+
* their activity is recorded again.
1097+
*/
1098+
public function test_record_activity_skips_inactive_user_in_block_mode() {
1099+
// The setUp() already defines mode as BLOCK via VIP_SECURITY_BOOST_CONFIGS.
1100+
1101+
// Create an inactive admin user.
1102+
$user_id = $this->factory->user->create( [
1103+
'role' => 'administrator',
1104+
'user_registered' => gmdate( 'Y-m-d H:i:s', strtotime( '-100 days' ) ),
1105+
] );
1106+
delete_user_meta( $user_id, Inactive_Users::LAST_SEEN_IGNORE_INACTIVITY_CHECK_UNTIL_META_KEY );
1107+
1108+
$old_timestamp = strtotime( '-91 days' );
1109+
update_user_meta( $user_id, Inactive_Users::LAST_SEEN_META_KEY, $old_timestamp );
1110+
1111+
// Confirm the user is considered inactive.
1112+
$this->assertTrue( Inactive_Users::is_considered_inactive( $user_id ) );
1113+
1114+
// Simulate the user being logged in and clear the cache.
1115+
wp_set_current_user( $user_id );
1116+
wp_cache_delete( $user_id, Inactive_Users::LAST_SEEN_CACHE_GROUP );
1117+
1118+
// Call record_activity() — in BLOCK mode this should early-return.
1119+
Inactive_Users::record_activity();
1120+
1121+
// The last seen timestamp should NOT have been updated.
1122+
$new_timestamp = get_user_meta( $user_id, Inactive_Users::LAST_SEEN_META_KEY, true );
1123+
$this->assertEquals( $old_timestamp, (int) $new_timestamp, 'record_activity() should NOT update last_seen in BLOCK mode for inactive users' );
1124+
1125+
// Clean up.
1126+
wp_delete_user( $user_id );
1127+
}
1128+
10431129
/**
10441130
* Test XML-RPC authentication for inactive users returns proper error message
10451131
*/

0 commit comments

Comments
 (0)