Skip to content

Commit

Permalink
Merge pull request #1151 from xwp/tests/users-connector
Browse files Browse the repository at this point in the history
Users connector class test implemented.
  • Loading branch information
kidunot89 authored Jan 12, 2021
2 parents c6e834e + 96d03b9 commit abfee72
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 6 deletions.
2 changes: 1 addition & 1 deletion connectors/class-connector-user-switching.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Connector for User-Switching
* Connector for the User-Switching plugin
*
* @package WP_Stream
*/
Expand Down
10 changes: 5 additions & 5 deletions connectors/class-connector-users.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Connector_Users extends Connector {
'delete_user',
'deleted_user',
'set_user_role',
'set_current_user',
);

/**
Expand Down Expand Up @@ -242,7 +243,7 @@ public function callback_password_reset( $user ) {
/* translators: %s: a user display name (e.g. "Jane Doe") */
__( '%s\'s password was reset', 'stream' ),
array(
'email' => $user->display_name,
'display_name' => $user->display_name,
),
$user->ID,
'profiles',
Expand Down Expand Up @@ -313,7 +314,8 @@ public function callback_set_logged_in_cookie( $logged_in_cookie, $expire, $expi
* @action clear_auth_cookie
*/
public function callback_clear_auth_cookie() {
$user = wp_get_current_user();
$user_id = get_current_user_id();
$user = get_user_by( 'ID', $user_id );

// For some reason, incognito mode calls clear_auth_cookie on failed login attempts.
if ( empty( $user ) || ! $user->exists() ) {
Expand All @@ -323,9 +325,7 @@ public function callback_clear_auth_cookie() {
$this->log(
/* translators: %s: a user display name (e.g. "Jane Doe") */
__( '%s logged out', 'stream' ),
array(
'display_name' => $user->display_name,
),
array( 'display_name' => $user->display_name ),
$user->ID,
'sessions',
'logout',
Expand Down
258 changes: 258 additions & 0 deletions tests/tests/connectors/test-class-connector-users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
<?php
/**
* Tests for Users connector class callbacks.
*
* @package WP_Stream
*/
namespace WP_Stream;

class Test_WP_Stream_Connector_Users extends WP_StreamTestCase {

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

// Make partial of Connector_Users class, with mocked "log" function.
$this->mock = $this->getMockBuilder( Connector_Users::class )
->setMethods( array( 'log' ) )
->getMock();

$this->mock->register();
}

public function test_callback_user_register() {
// Expected log calls.
$this->mock->expects( $this->atLeastOnce() )
->method( 'log' )
->withConsecutive(
array(
$this->equalTo( esc_html__( 'New user registration', 'stream' ) ),
$this->equalTo(
array(
'display_name' => 'TestGuy',
'roles' => 'Subscriber',
)
),
$this->greaterThan( 0 ),
$this->equalTo( 'users' ),
$this->equalTo( 'created' ),
$this->greaterThan( 0 )
),
array(
$this->equalTo(
_x(
'New user account created for %1$s (%2$s)',
'1: User display name, 2: User role',
'stream'
)
),
$this->equalTo(
array(
'display_name' => 'TestGuy2',
'roles' => 'Subscriber',
)
),
$this->greaterThan( 0 ),
$this->equalTo( 'users' ),
$this->equalTo( 'created' ),
$this->greaterThan( 0 )
)
);

// Do stuff.
$user_id = self::factory()->user->create( array( 'display_name' => 'TestGuy' ) );
wp_set_current_user( $user_id );
self::factory()->user->create( array( 'display_name' => 'TestGuy2' ) );

// Check callback test action.
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_user_register' ) );
}

public function test_callback_password_reset() {
// Create user.
$user_id = self::factory()->user->create( array( 'display_name' => 'TestGuy' ) );
wp_set_current_user( $user_id );
$user = get_user_by( 'id', $user_id );

// Expected log calls.
$this->mock->expects( $this->once() )
->method( 'log' )
->withConsecutive(
array(
$this->equalTo( __( '%s\'s password was reset', 'stream' ) ),
$this->equalTo( array( 'display_name' => 'TestGuy' ) ),
$this->equalTo( $user_id ),
$this->equalTo( 'profiles' ),
$this->equalTo( 'password-reset' ),
$this->equalTo( $user_id )
)
);

// Do stuff.
$new_pass = 'blahblahblah';
reset_password( $user, $new_pass );

// Check callback test action.
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_password_reset' ) );
}

public function test_callback_retrieve_password_and_profile_update() {
// Create user.
$user_id = self::factory()->user->create( array( 'display_name' => 'TestGuy' ) );
$user = get_user_by( 'id', $user_id );

// Expected log calls.
$this->mock->expects( $this->atLeastOnce() )
->method( 'log' )
->withConsecutive(
array(
$this->equalTo( __( '%s\'s password was requested to be reset', 'stream' ) ),
$this->equalTo( array( 'display_name' => 'TestGuy' ) ),
$this->equalTo( $user_id ),
$this->equalTo( 'sessions' ),
$this->equalTo( 'forgot-password' ),
$this->equalTo( $user_id )
),
array(
$this->equalTo( __( '%s\'s profile was updated', 'stream' ) ),
$this->equalTo( array( 'display_name' => 'TestGuy' ) ),
$this->equalTo( $user_id ),
$this->equalTo( 'profiles' ),
$this->equalTo( 'updated' )
)
);

// Do stuff.
get_password_reset_key( $user );

// Check callback test action.
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_profile_update' ) );
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_retrieve_password' ) );
}

public function test_callback_set_logged_in_cookie() {
// Create user.
$user_id = self::factory()->user->create( array( 'display_name' => 'TestGuy' ) );

// Expected log calls.
$this->mock->expects( $this->once() )
->method( 'log' )
->with(
$this->equalTo( __( '%s logged in', 'stream' ) ),
$this->equalTo( array( 'display_name' => 'TestGuy' ) ),
$this->equalTo( $user_id ),
$this->equalTo( 'sessions' ),
$this->equalTo( 'login' ),
$this->equalTo( $user_id )
);

// Do stuff.
wp_set_auth_cookie( $user_id );

// Check callback test action.
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_set_logged_in_cookie' ) );
}

public function test_callback_clear_auth_cookie() {
// Create and authenticate user.
$user_id = self::factory()->user->create( array( 'display_name' => 'TestGuy' ) );
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id );

// Manually trigger the action to execute callback.
add_filter( 'send_auth_cookies', '__return_false' );
wp_clear_auth_cookie();

// Check callback test action.
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_clear_auth_cookie' ) );
}

public function test_callback_deleted_user() {
// Create Users.
$user_id = self::factory()->user->create( array( 'display_name' => 'TestGuy' ));
$user = get_user_by( 'ID', $user_id );

// Expected log calls.
$this->mock->expects( $this->exactly( 2 ) )
->method( 'log' )
->withConsecutive(
array(
$this->equalTo(
_x(
'%1$s\'s account was deleted (%2$s)',
'1: User display name, 2: User roles',
'stream'
)
),
$this->equalTo(
array(
'display_name' => 'TestGuy',
'roles' => 'Subscriber',
)
),
$this->equalTo( $user_id ),
$this->equalTo( 'users' ),
$this->equalTo( 'deleted' ),
$this->equalTo( 0 )
),
array(
$this->equalTo( esc_html__( 'User account #%d was deleted', 'stream' ) ),
$this->equalTo(
array(
'display_name' => $user_id,
'roles' => '',
)
),
$this->equalTo( $user_id ),
$this->equalTo( 'users' ),
$this->equalTo( 'deleted' ),
$this->equalTo( 0 )
)
);

// Delete user and run action to simulate event and trigger callback.
wp_delete_user( $user_id );
do_action( 'deleted_user', $user_id, null, $user );

// Check callback test action.
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_delete_user' ) );
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_deleted_user' ) );
}

public function test_callback_set_user_role() {
// Create user.
$user_id = self::factory()->user->create( array( 'display_name' => 'TestGuy' ));
$user = get_user_by( 'id', $user_id );

// Expected log calls.
$this->mock->expects( $this->once() )
->method( 'log' )
->with(
$this->equalTo(
_x(
'%1$s\'s role was changed from %2$s to %3$s',
'1: User display name, 2: Old role, 3: New role',
'stream'
)
),
$this->equalTo(
array(
'display_name' => 'TestGuy',
'old_role' => 'Subscriber',
'new_role' => 'Editor',
)
),
$this->equalTo( $user_id ),
$this->equalTo( 'profiles' ),
$this->equalTo( 'updated' )
);

// Do stuff.
$user->set_role( 'editor' );

// Check callback test action.
$this->assertFalse( 0 === did_action( $this->action_prefix . 'callback_set_user_role' ) );
}
}

0 comments on commit abfee72

Please sign in to comment.