diff --git a/src/wp-includes/user-functions.php b/src/wp-includes/user-functions.php index af3d1e2db1..ec00edc9d0 100644 --- a/src/wp-includes/user-functions.php +++ b/src/wp-includes/user-functions.php @@ -1176,13 +1176,15 @@ function email_exists( $email ) { * Checks whether a username is valid. * * @since 2.0.1 + * @since 4.4.0 Empty sanitized usernames are now considered invalid * * @param string $username Username. * @return bool Whether username given is valid */ function validate_username( $username ) { $sanitized = sanitize_user( $username, true ); - $valid = ( $sanitized == $username ); + $valid = ( $sanitized == $username && ! empty( $sanitized ) ); + /** * Filter whether the provided username is valid or not. * diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index db4006acfd..7c022617e7 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -599,6 +599,29 @@ class Tests_User extends WP_UnitTestCase { } } + /** + * @ticket 24618 + */ + public function test_validate_username_string() { + $this->assertTrue( validate_username( rand_str() ) ); + $this->assertTrue( validate_username( 'JohnDoe' ) ); + $this->assertTrue( validate_username( 'test@test.com' ) ); + } + + /** + * @ticket 24618 + */ + public function test_validate_username_empty() { + $this->assertFalse( validate_username( '' ) ); + } + + /** + * @ticket 24618 + */ + public function test_validate_username_invalid() { + $this->assertFalse( validate_username( '@#&99sd' ) ); + } + /** * @ticket 29696 */