-
Notifications
You must be signed in to change notification settings - Fork 117
Users connector class test implemented. #1151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
007fa5b
Users connector class test implemented.
kidunot89 50fda58
Users connector test updated
kidunot89 5ef5989
composer.lock updated.
kidunot89 bf21a81
User connector test updated.
kidunot89 abb5276
Debug code removed.
kidunot89 96d03b9
Expected log() mock removed.
kidunot89 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' ) ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.