Skip to content

Commit

Permalink
Move profile email change actions to fucntion
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Dec 17, 2019
1 parent a5bc517 commit 6c7178e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 70 deletions.
17 changes: 9 additions & 8 deletions WP_Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,13 @@ public function init() {
$initial_setup = new WP_Auth0_InitialSetup( $this->a0_options );
$initial_setup->init();

$users_repo = new WP_Auth0_UsersRepo( $this->a0_options );

$this->router = new WP_Auth0_Routes( $this->a0_options );

$error_log = new WP_Auth0_ErrorLog();
$error_log->init();

$import_settings = new WP_Auth0_Import_Settings( $this->a0_options );
$import_settings->init();

$api_client_creds = new WP_Auth0_Api_Client_Credentials( $this->a0_options );

$api_change_email = new WP_Auth0_Api_Change_Email( $this->a0_options, $api_client_creds );
$profile_change_email = new WP_Auth0_Profile_Change_Email( $api_change_email );
$profile_change_email->init();
}

/**
Expand Down Expand Up @@ -491,6 +483,15 @@ function wp_auth0_db_check_update() {
* Core WP hooks
*/

function wp_auth0_profile_change_email( $wp_user_id, $old_user_data ) {
$options = WP_Auth0_Options::Instance();
$api_client_creds = new WP_Auth0_Api_Client_Credentials( $options );
$api_change_email = new WP_Auth0_Api_Change_Email( $options, $api_client_creds );
$profile_change_email = new WP_Auth0_Profile_Change_Email( $api_change_email );
return $profile_change_email->update_email( $wp_user_id, $old_user_data );
}
add_action( 'profile_update', 'wp_auth0_profile_change_email', 100, 2 );

function wp_auth0_validate_new_password( $errors, $user ) {
$options = WP_Auth0_Options::Instance();
$api_client_creds = new WP_Auth0_Api_Client_Credentials( $options );
Expand Down
13 changes: 0 additions & 13 deletions lib/profile/WP_Auth0_Profile_Change_Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,6 @@ public function __construct( WP_Auth0_Api_Change_Email $api_change_email ) {
$this->api_change_email = $api_change_email;
}

/**
* Add actions for the update user process.
*
* @deprecated - 3.10.0, will move add_action calls out of this class in the next major.
*
* @codeCoverageIgnore - Deprecated.
*/
public function init() {

// Used during profile update in wp-admin or email verification link.
add_action( 'profile_update', [ $this, 'update_email' ], 100, 2 );
}

/**
* Update the user's email at Auth0 when changing email for a database connection user.
* This runs AFTER a successful email change is saved in WP.
Expand Down
62 changes: 13 additions & 49 deletions tests/testProfileChangeEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,31 @@ public static function setUpBeforeClass() {
*/
public function testThatHooksAreLoaded() {
$expect_hooked = [
'update_email' => [
'wp_auth0_profile_change_email' => [
'priority' => 100,
'accepted_args' => 2,
],
];
$this->assertHookedClass( 'profile_update', 'WP_Auth0_Profile_Change_Email', $expect_hooked );
$this->assertHookedFunction( 'profile_update', $expect_hooked );
}

/**
* Test that an email update works.
*/
public function testSuccessfulEmailUpdate() {
$this->startHttpMocking();
$this->setApiToken( 'update:users' );
$this->http_request_type = 'success_empty_body';

$user = $this->createUser( [], false );
$new_email = $user->data->user_email;
$old_user = clone $user;
$old_user->data->user_email = 'OLD-' . $new_email;

// API call mocked to succeed.
$change_email = $this->getStub( true );

// Store userinfo for a DB strategy user.
$this->storeAuth0Data( $user->ID, 'auth0' );

$this->assertTrue( $change_email->update_email( $user->ID, $old_user ) );
$this->assertTrue( wp_auth0_profile_change_email( $user->ID, $old_user ) );
$this->assertEquals( $new_email, get_user_by( 'id', $user->ID )->data->user_email );
$this->assertEmpty( WP_Auth0_UsersRepo::get_meta( $user->ID, 'auth0_transient_email_update' ) );
}
Expand All @@ -77,10 +78,7 @@ public function testThatNonAuth0UserSkipsUpdate() {
$old_user = clone $user;
$old_user->data->user_email = 'OLD-' . $old_user->data->user_email;

// API call mocked to succeed.
$change_email = $this->getStub( true );

$this->assertFalse( $change_email->update_email( $user->ID, $old_user ) );
$this->assertFalse( wp_auth0_profile_change_email( $user->ID, $old_user ) );
}

/**
Expand All @@ -91,13 +89,10 @@ public function testThatNonDbUserSkipsUpdate() {
$old_user = clone $user;
$old_user->data->user_email = 'OLD-' . $old_user->data->user_email;

// API call mocked to succeed.
$change_email = $this->getStub( true );

// Store userinfo for a DB strategy user.
$this->storeAuth0Data( $user->ID, 'not-auth0' );

$this->assertFalse( $change_email->update_email( $user->ID, $old_user ) );
$this->assertFalse( wp_auth0_profile_change_email( $user->ID, $old_user ) );
}

/**
Expand All @@ -107,13 +102,10 @@ public function testThatSameEmailSkipsUpdate() {
$user = $this->createUser( [], false );
$old_user = clone $user;

// API call mocked to succeed.
$change_email = $this->getStub( true );

// Store userinfo for a DB strategy user.
$this->storeAuth0Data( $user->ID, 'not-auth0' );

$this->assertFalse( $change_email->update_email( $user->ID, $old_user ) );
$this->assertFalse( wp_auth0_profile_change_email( $user->ID, $old_user ) );
}

/**
Expand All @@ -130,14 +122,10 @@ public function testThatFailedApiCallStopsEmailUpdate() {
// Store the usermeta value set for email verification.
update_user_meta( $user->ID, '_new_email', $user->data->user_email );

// API call mocked to fail.
$change_email = $this->getStub( false );

// Need to remove existing filters and re-init with filters from the test class.
remove_all_filters( 'profile_update' );
$change_email->init();

$this->assertFalse( $change_email->update_email( $user->ID, $old_user ) );
$this->assertFalse( wp_auth0_profile_change_email( $user->ID, $old_user ) );
$this->assertEquals( $old_user->data->user_email, get_user_by( 'id', $user->ID )->data->user_email );
$this->assertEmpty( get_user_meta( $user->ID, '_new_email', true ) );
$this->assertEmpty( WP_Auth0_UsersRepo::get_meta( $user->ID, 'auth0_transient_email_update' ) );
Expand All @@ -156,20 +144,16 @@ public function testThatFailedApiRedirectsOnUserEditPage() {
// Store userinfo for a DB strategy user.
$this->storeAuth0Data( $user->ID, 'auth0' );

// API call mocked to fail.
$change_email = $this->getStub( false );

// Need to remove existing filters and re-init with filters from the test class.
remove_all_filters( 'profile_update' );
$change_email->init();

// Set current page to the user profile.
global $pagenow;
$pagenow = 'user-edit.php';

$caught_redirect = [];
try {
$change_email->update_email( $user->ID, $old_user );
wp_auth0_profile_change_email( $user->ID, $old_user );
} catch ( Exception $e ) {
$caught_redirect = unserialize( $e->getMessage() );
}
Expand All @@ -193,32 +177,12 @@ public function testThatEmailUpdateFlagIsSetBeforeApiCall() {
$old_user->data->user_email = 'OLD-' . $new_email;
$this->storeAuth0Data( $user->ID, 'auth0' );

$api_change_email = new WP_Auth0_Api_Change_Email( self::$opts, self::$api_client_creds );
$change_email = new WP_Auth0_Profile_Change_Email( $api_change_email );

try {
$change_email->update_email( $user->ID, $old_user );
wp_auth0_profile_change_email( $user->ID, $old_user );
} catch ( Exception $e ) {
// Just need to stop the API call.
}

$this->assertEquals( $new_email, WP_Auth0_UsersRepo::get_meta( $user->ID, 'auth0_transient_email_update' ) );
}

/**
* Get an API stub set to pass or fail.
*
* @param boolean $success - True for the API call to succeed, false for it to fail.
*
* @return WP_Auth0_Profile_Change_Email
*/
public function getStub( $success ) {
$mock_api_test_email = $this
->getMockBuilder( WP_Auth0_Api_Change_Email::class )
->setMethods( [ 'call' ] )
->setConstructorArgs( [ self::$opts, self::$api_client_creds ] )
->getMock();
$mock_api_test_email->method( 'call' )->willReturn( $success );
return new WP_Auth0_Profile_Change_Email( $mock_api_test_email );
}
}

0 comments on commit 6c7178e

Please sign in to comment.