From 2418711c475d3df788b9d1e1b36dbb57602bf64e Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 1 Apr 2015 18:22:16 +0000 Subject: [PATCH] When updating the email address for an existing user, make sure the email address is not already in use. Adds unit tests. Props rittesh.patel, DrewAPicture. Fixes #30647. git-svn-id: https://develop.svn.wordpress.org/trunk@31963 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/user.php | 10 +++++++++- tests/phpunit/tests/user.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 3a03ea0425..adb134a148 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -1894,7 +1894,15 @@ function wp_insert_user( $userdata ) { */ $user_email = apply_filters( 'pre_user_email', $raw_user_email ); - if ( ! $update && ! defined( 'WP_IMPORTING' ) && email_exists( $user_email ) ) { + /* + * If there is no update, just check for `email_exists`. If there is an update, + * check if current email and new email are the same, or not, and check `email_exists` + * accordingly. + */ + if ( ( ! $update || ( ! empty( $old_user_data ) && $user_email !== $old_user_data->user_email ) ) + && ! defined( 'WP_IMPORTING' ) + && email_exists( $user_email ) + ) { return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) ); } $nickname = empty( $userdata['nickname'] ) ? $user_login : $userdata['nickname']; diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index 8ba4f970d9..43edd3a1cf 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -618,6 +618,41 @@ class Tests_User extends WP_UnitTestCase { $this->assertNotContains( 'key', $metas ); } + /** + * @ticket 30647 + */ + function test_user_update_email_error() { + $id1 = wp_insert_user( array( + 'user_login' => rand_str(), + 'user_pass' => 'password', + 'user_email' => 'blackburn@battlefield3.com', + ) ); + $this->assertEquals( $id1, email_exists( 'blackburn@battlefield3.com' ) ); + + $id2 = wp_insert_user( array( + 'user_login' => rand_str(), + 'user_pass' => 'password', + 'user_email' => 'miller@battlefield3.com', + ) ); + $this->assertEquals( $id2, email_exists( 'miller@battlefield3.com' ) ); + + if( ! is_wp_error( $id2 ) ){ + $return = wp_update_user( array( + 'ID' => $id2, + 'user_email' => 'david@battlefield3.com', + ) ); + $this->assertEquals( $id2, email_exists( 'david@battlefield3.com' ) ); + + $return = wp_update_user( array( + 'ID' => $id2, + 'user_email' => 'blackburn@battlefield3.com', + ) ); + if ( ! defined( 'WP_IMPORTING' ) ) { + $this->assertWPError( $return ); + } + } + } + /** * @ticket 29696 */