Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
luismulinari committed Sep 14, 2023
1 parent ed87a3e commit dbbf13e
Showing 1 changed file with 94 additions and 74 deletions.
168 changes: 94 additions & 74 deletions tests/security/test-class-user-last-seen.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,34 @@ public function test__should_not_load_actions_and_filters_when_env_vars_is_set_t
}

public function test__is_considered_inactive__should_consider_user_registered() {
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 30 );
update_option( User_Last_Seen::LAST_SEEN_RELEASE_DATE_TIMESTAMP_OPTION_KEY, strtotime('-100 days' ) );
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 30 );
update_option( User_Last_Seen::LAST_SEEN_RELEASE_DATE_TIMESTAMP_OPTION_KEY, strtotime( '-100 days' ) );

// Recent registered user
$user1 = $this->factory()->user->create( array( 'role' => 'administrator', 'user_registered' => date('Y-m-d' ) ) );
$user1 = $this->factory()->user->create( array(
'role' => 'administrator',
'user_registered' => gmdate( 'Y-m-d' ),
) );

// Inactive user (last seen 20 days ago)
$user2 = $this->factory()->user->create( array( 'role' => 'administrator', 'user_registered' => '2020-01-01' ) );
add_user_meta( $user2, User_Last_Seen::LAST_SEEN_META_KEY, strtotime('-31 days') );
$user2 = $this->factory()->user->create( array(
'role' => 'administrator',
'user_registered' => '2020-01-01',
) );
add_user_meta( $user2, User_Last_Seen::LAST_SEEN_META_KEY, strtotime( '-31 days' ) );

// Active user (last seen 2 days ago)
$user3 = $this->factory()->user->create( array( 'role' => 'administrator', 'user_registered' => '2020-01-01' ) );
add_user_meta( $user3, User_Last_Seen::LAST_SEEN_META_KEY, strtotime('-2 days') );
$user3 = $this->factory()->user->create( array(
'role' => 'administrator',
'user_registered' => '2020-01-01',
) );
add_user_meta( $user3, User_Last_Seen::LAST_SEEN_META_KEY, strtotime( '-2 days' ) );

// Old user without meta
$user4 = $this->factory()->user->create( array( 'role' => 'administrator', 'user_registered' => date('Y-m-d', strtotime('-40 days' ) ) ) );
$user4 = $this->factory()->user->create( array(
'role' => 'administrator',
'user_registered' => gmdate( 'Y-m-d', strtotime( '-40 days' ) ),
) );

$last_seen = new User_Last_Seen();
$last_seen->init();
Expand All @@ -73,12 +85,14 @@ public function test__is_considered_inactive__should_consider_user_registered()
$this->assertTrue( $last_seen->is_considered_inactive( $user4 ) );
}

public function test__is_considered_inactive__add_extra_time_when_user_is_promoted()
{
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 30);
public function test__is_considered_inactive__add_extra_time_when_user_is_promoted() {
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 30 );

$user_id = $this->factory()->user->create( array( 'role' => 'subscriber', 'user_registered' => '2020-01-01' ) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime('-31 days') );
$user_id = $this->factory()->user->create( array(
'role' => 'subscriber',
'user_registered' => '2020-01-01',
) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime( '-31 days' ) );

$user = get_user_by( 'ID', $user_id );
$user->set_role( 'administrator' );
Expand All @@ -89,15 +103,20 @@ public function test__is_considered_inactive__add_extra_time_when_user_is_promot
$this->assertTrue( $last_seen->is_considered_inactive( $user_id ) );
}

public function test__is_considered_inactive__should_consider_user_meta()
{
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 30);
public function test__is_considered_inactive__should_consider_user_meta() {
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 30 );

$user_inactive_id = $this->factory()->user->create( array( 'role' => 'administrator', 'user_registered' => '2020-01-01' ) );
add_user_meta( $user_inactive_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime('-31 days') );
$user_inactive_id = $this->factory()->user->create( array(
'role' => 'administrator',
'user_registered' => '2020-01-01',
) );
add_user_meta( $user_inactive_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime( '-31 days' ) );

$user_active_id = $this->factory()->user->create( array( 'role' => 'administrator', 'user_registered' => '2020-01-01' ) );
add_user_meta( $user_active_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime('-29 days') );
$user_active_id = $this->factory()->user->create( array(
'role' => 'administrator',
'user_registered' => '2020-01-01',
) );
add_user_meta( $user_active_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime( '-29 days' ) );

$last_seen = new User_Last_Seen();
$last_seen->init();
Expand All @@ -106,9 +125,8 @@ public function test__is_considered_inactive__should_consider_user_meta()
$this->assertFalse( $last_seen->is_considered_inactive( $user_active_id ) );
}

public function test__is_considered_inactive__should_return_false_if_user_meta_and_option_are_not_present()
{
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 30);
public function test__is_considered_inactive__should_return_false_if_user_meta_and_option_are_not_present() {
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 30 );

delete_option( User_Last_Seen::LAST_SEEN_RELEASE_DATE_TIMESTAMP_OPTION_KEY );

Expand All @@ -120,28 +138,29 @@ public function test__is_considered_inactive__should_return_false_if_user_meta_a
$this->assertFalse( $last_seen->is_considered_inactive( $user_without_meta ) );
}

public function test__is_considered_inactive__should_use_release_date_option_when_user_meta_is_not_defined()
{
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15);
public function test__is_considered_inactive__should_use_release_date_option_when_user_meta_is_not_defined() {
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15 );

add_option( User_Last_Seen::LAST_SEEN_RELEASE_DATE_TIMESTAMP_OPTION_KEY, strtotime('-16 days') );
add_option( User_Last_Seen::LAST_SEEN_RELEASE_DATE_TIMESTAMP_OPTION_KEY, strtotime( '-16 days' ) );

$user_without_meta = $this->factory()->user->create( array( 'role' => 'administrator', 'user_registered' => '2020-01-01' ) );
$user_without_meta = $this->factory()->user->create( array(
'role' => 'administrator',
'user_registered' => '2020-01-01',
) );

$last_seen = new \Automattic\VIP\Security\User_Last_Seen();
$last_seen->init();

$this->assertTrue( $last_seen->is_considered_inactive( $user_without_meta ) );

update_option( User_Last_Seen::LAST_SEEN_RELEASE_DATE_TIMESTAMP_OPTION_KEY, strtotime('-10 days') );
update_option( User_Last_Seen::LAST_SEEN_RELEASE_DATE_TIMESTAMP_OPTION_KEY, strtotime( '-10 days' ) );

$this->assertFalse( $last_seen->is_considered_inactive( $user_without_meta ) );
}

public function test__determine_current_user_should_record_last_seen_meta()
{
Constant_Mocker::define('VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15);
public function test__determine_current_user_should_record_last_seen_meta() {
Constant_Mocker::define( 'VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15 );

remove_all_filters( 'determine_current_user' );

Expand All @@ -158,14 +177,13 @@ public function test__determine_current_user_should_record_last_seen_meta()
$this->assertIsNumeric( $current_last_seen );
}

public function test__determine_current_user_should_record_once_last_seen_meta()
{
Constant_Mocker::define('VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15);
public function test__determine_current_user_should_record_once_last_seen_meta() {
Constant_Mocker::define( 'VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15 );

remove_all_filters( 'determine_current_user' );

$previous_last_seen = sprintf('%d', strtotime('-10 days') );
$previous_last_seen = sprintf( '%d', strtotime( '-10 days' ) );

$user_id = $this->factory()->user->create( array( 'role' => 'administrator' ) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, $previous_last_seen );
Expand All @@ -186,15 +204,17 @@ public function test__determine_current_user_should_record_once_last_seen_meta()
$this->assertSame( $new_user_id, $user_id );
}

public function test__determine_current_user_should_return_an_error_when_user_is_inactive()
{
Constant_Mocker::define('VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15);
public function test__determine_current_user_should_return_an_error_when_user_is_inactive() {
Constant_Mocker::define( 'VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15 );

remove_all_filters( 'determine_current_user' );

$user_id = $this->factory()->user->create( array( 'role' => 'editor', 'user_registered' => '2020-01-01' ) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime('-100 days') );
$user_id = $this->factory()->user->create( array(
'role' => 'editor',
'user_registered' => '2020-01-01',
) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime( '-100 days' ) );

$last_seen = new \Automattic\VIP\Security\User_Last_Seen();
$last_seen->init();
Expand All @@ -204,15 +224,14 @@ public function test__determine_current_user_should_return_an_error_when_user_is
$this->assertWPError( $user, 'Expected WP_Error object to be returned' );
}

public function test__authenticate_should_not_return_error_when_user_is_active()
{
Constant_Mocker::define('VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15);
public function test__authenticate_should_not_return_error_when_user_is_active() {
Constant_Mocker::define( 'VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15 );

remove_all_filters( 'authenticate' );

$user_id = $this->factory()->user->create( array( 'role' => 'administrator' ) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime('-5 days') );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime( '-5 days' ) );

$user = get_user_by( 'id', $user_id );

Expand All @@ -224,15 +243,17 @@ public function test__authenticate_should_not_return_error_when_user_is_active()
$this->assertSame( $user->ID, $new_user->ID );
}

public function test__authenticate_should_return_an_error_when_user_is_inactive()
{
Constant_Mocker::define('VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15);
public function test__authenticate_should_return_an_error_when_user_is_inactive() {
Constant_Mocker::define( 'VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15 );

remove_all_filters( 'authenticate' );

$user_id = $this->factory()->user->create( array( 'role' => 'administrator', 'user_registered' => '2020-01-01' ) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime('-100 days') );
$user_id = $this->factory()->user->create( array(
'role' => 'administrator',
'user_registered' => '2020-01-01',
) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime( '-100 days' ) );

$user = get_user_by( 'id', $user_id );

Expand All @@ -244,9 +265,8 @@ public function test__authenticate_should_return_an_error_when_user_is_inactive(
$this->assertWPError( $user, 'Expected WP_Error object to be returned' );
}

public function test__register_release_date_should_register_release_date_only_once()
{
Constant_Mocker::define('VIP_SECURITY_INACTIVE_USERS_ACTION', 'RECORD_LAST_SEEN' );
public function test__register_release_date_should_register_release_date_only_once() {
Constant_Mocker::define( 'VIP_SECURITY_INACTIVE_USERS_ACTION', 'RECORD_LAST_SEEN' );

remove_all_actions( 'admin_init' );
delete_option( User_Last_Seen::LAST_SEEN_RELEASE_DATE_TIMESTAMP_OPTION_KEY );
Expand All @@ -263,15 +283,14 @@ public function test__register_release_date_should_register_release_date_only_on
$this->assertSame( $release_date, $new_release_date );
}

public function test__authenticate_should_not_consider_users_without_elevated_capabilities()
{
Constant_Mocker::define('VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15);
public function test__authenticate_should_not_consider_users_without_elevated_capabilities() {
Constant_Mocker::define( 'VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15 );

remove_all_filters( 'authenticate' );

$user_id = $this->factory()->user->create( array( 'role' => 'subscriber' ) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime('-100 days') );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime( '-100 days' ) );

$user = get_user_by( 'id', $user_id );

Expand All @@ -281,16 +300,18 @@ public function test__authenticate_should_not_consider_users_without_elevated_ca
$this->assertSame( $user, apply_filters( 'authenticate', $user, $user ) );
}

public function test__should_check_user_last_seen_should_call_elevated_capabilities_filters()
{
Constant_Mocker::define('VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15);
public function test__should_check_user_last_seen_should_call_elevated_capabilities_filters() {
Constant_Mocker::define( 'VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15 );

remove_all_filters( 'authenticate' );
remove_all_filters( 'vip_security_last_seen_elevated_capabilities' );

$user_id = $this->factory()->user->create( array( 'role' => 'subscriber', 'user_registered' => '2020-01-01' ) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime('-100 days') );
$user_id = $this->factory()->user->create( array(
'role' => 'subscriber',
'user_registered' => '2020-01-01',
) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime( '-100 days' ) );

$user = get_user_by( 'id', $user_id );

Expand All @@ -308,16 +329,15 @@ public function test__should_check_user_last_seen_should_call_elevated_capabilit
$this->assertWPError( $user, 'Expected WP_Error object to be returned' );
}

public function test__should_check_user_last_seen_should_call_skip_users_filters()
{
Constant_Mocker::define('VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define('VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15);
public function test__should_check_user_last_seen_should_call_skip_users_filters() {
Constant_Mocker::define( 'VIP_SECURITY_INACTIVE_USERS_ACTION', 'BLOCK' );
Constant_Mocker::define( 'VIP_SECURITY_CONSIDER_USERS_INACTIVE_AFTER_DAYS', 15 );

remove_all_filters( 'authenticate' );
remove_all_filters( 'vip_security_last_seen_elevated_capabilities' );

$user_id = $this->factory()->user->create( array( 'role' => 'subscriber' ) );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime('-100 days') );
add_user_meta( $user_id, User_Last_Seen::LAST_SEEN_META_KEY, strtotime( '-100 days' ) );

$user = get_user_by( 'id', $user_id );

Expand Down

0 comments on commit dbbf13e

Please sign in to comment.