From ad78d0c932c4911efe36ab03559bb82c48cf8236 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 17 Feb 2023 10:13:36 +0000 Subject: [PATCH] Users: Correct the error code in `wp_insert_user()` when login matches an existing email. Move the test next to the other tests for `user_login`. Follow-up to [55358]. See #57394. git-svn-id: https://develop.svn.wordpress.org/trunk@55360 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/user.php | 2 +- tests/phpunit/tests/user.php | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index e0cfae9188..96f9edfe04 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -2130,7 +2130,7 @@ function wp_insert_user( $userdata ) { // Username must not match an existing user email. if ( email_exists( $user_login ) ) { - return new WP_Error( 'existing_user_login_as_email', __( 'Sorry, that username is not available.' ) ); + return new WP_Error( 'existing_user_email_as_login', __( 'Sorry, that username is not available.' ) ); } /** diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index 03a5a11872..24496c7895 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -897,6 +897,24 @@ class Tests_User extends WP_UnitTestCase { $this->assertSame( 'user_login_too_long', $u->get_error_code() ); } + /** + * @ticket 57394 + */ + public function test_wp_insert_user_should_reject_user_login_that_matches_existing_user_email() { + $existing_email = get_option( 'admin_email' ); + $user_id = wp_insert_user( + array( + 'user_login' => $existing_email, + 'user_email' => 'whatever@example.com', + 'user_pass' => 'whatever', + 'user_nicename' => 'whatever', + ) + ); + + $this->assertWPError( $user_id ); + $this->assertSame( 'existing_user_email_as_login', $user_id->get_error_code() ); + } + /** * @ticket 33793 */ @@ -934,24 +952,6 @@ class Tests_User extends WP_UnitTestCase { $this->assertSame( $expected, $user->user_nicename ); } - /** - * @ticket 57394 - */ - public function test_wp_insert_user_should_reject_username_that_matches_existing_user_email() { - $existing_email = get_option( 'admin_email' ); - $username = wp_insert_user( - array( - 'user_login' => $existing_email, - 'user_email' => 'whatever@example.com', - 'user_pass' => 'whatever', - 'user_nicename' => 'whatever', - ) - ); - - $this->assertWPError( $username ); - $this->assertSame( 'existing_user_login_as_email', $username->get_error_code() ); - } - /** * @ticket 33793 */