From 6c7178ed980d2a586889b87ef7ac906574aba4f9 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Mon, 16 Dec 2019 11:20:51 -0800 Subject: [PATCH] Move profile email change actions to fucntion --- WP_Auth0.php | 17 ++--- lib/profile/WP_Auth0_Profile_Change_Email.php | 13 ---- tests/testProfileChangeEmail.php | 62 ++++--------------- 3 files changed, 22 insertions(+), 70 deletions(-) diff --git a/WP_Auth0.php b/WP_Auth0.php index e90c998b..d441def1 100644 --- a/WP_Auth0.php +++ b/WP_Auth0.php @@ -126,8 +126,6 @@ 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(); @@ -135,12 +133,6 @@ public function 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(); } /** @@ -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 ); diff --git a/lib/profile/WP_Auth0_Profile_Change_Email.php b/lib/profile/WP_Auth0_Profile_Change_Email.php index f1e63e93..590c9b1a 100644 --- a/lib/profile/WP_Auth0_Profile_Change_Email.php +++ b/lib/profile/WP_Auth0_Profile_Change_Email.php @@ -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. diff --git a/tests/testProfileChangeEmail.php b/tests/testProfileChangeEmail.php index 95d4e259..29f22cc3 100644 --- a/tests/testProfileChangeEmail.php +++ b/tests/testProfileChangeEmail.php @@ -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' ) ); } @@ -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 ) ); } /** @@ -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 ) ); } /** @@ -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 ) ); } /** @@ -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' ) ); @@ -156,12 +144,8 @@ 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; @@ -169,7 +153,7 @@ public function testThatFailedApiRedirectsOnUserEditPage() { $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() ); } @@ -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 ); - } }