From a796d187b3e9841d5398ad5691a77efd86c35088 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sun, 28 Jun 2015 00:35:42 +0000 Subject: [PATCH] When searching for users using the `search` arg in `get_users()`/`WP_User_Query`, also search the user's email, url, and display name. Adds unit tests. Props mordauk, wonderboymusic. Fixes #27304. git-svn-id: https://develop.svn.wordpress.org/trunk@32980 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/user.php | 2 +- tests/phpunit/tests/user.php | 61 ++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 070970a53b..ba9fa819fd 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -797,7 +797,7 @@ class WP_User_Query { elseif ( preg_match('|^https?://|', $search) && ! ( is_multisite() && wp_is_large_network( 'users' ) ) ) $search_columns = array('user_url'); else - $search_columns = array('user_login', 'user_nicename'); + $search_columns = array('user_login', 'user_url', 'user_email', 'user_nicename', 'display_name'); } /** diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index da5620f028..c32b778593 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -6,6 +6,23 @@ */ class Tests_User extends WP_UnitTestCase { + protected $user_data; + + function setUp() { + parent::setUp(); + + $this->user_data = array( + 'user_login' => 'user1', + 'user_nicename' => 'userone', + 'user_pass' => 'password', + 'first_name' => 'John', + 'last_name' => 'Doe', + 'display_name' => 'John Doe', + 'user_email' => 'blackburn@battlefield3.com', + 'user_url' => 'http://tacos.com' + ); + } + function test_get_users_of_blog() { // add one of each user role $nusers = array(); @@ -636,7 +653,7 @@ class Tests_User extends WP_UnitTestCase { ) ); $this->assertEquals( $id2, email_exists( 'miller@battlefield3.com' ) ); - if( ! is_wp_error( $id2 ) ){ + if( ! is_wp_error( $id2 ) ){ $return = wp_update_user( array( 'ID' => $id2, 'user_email' => 'david@battlefield3.com', @@ -649,7 +666,7 @@ class Tests_User extends WP_UnitTestCase { ) ); if ( ! defined( 'WP_IMPORTING' ) ) { $this->assertWPError( $return ); - } + } } } @@ -699,4 +716,44 @@ class Tests_User extends WP_UnitTestCase { $user = get_userdata( $user->ID ); $this->assertEmpty( $user->user_activation_key ); } + + public function test_search_users_login() { + $id = $this->factory->user->create( $this->user_data ); + + $users = get_users( array( 'search' => 'user1', 'fields' => 'ID' ) ); + + $this->assertTrue( in_array( $id, $users ) ); + } + + public function test_search_users_url() { + $id = $this->factory->user->create( $this->user_data ); + + $users = get_users( array( 'search' => '*tacos*', 'fields' => 'ID' ) ); + + $this->assertTrue( in_array( $id, $users ) ); + } + + public function test_search_users_email() { + $id = $this->factory->user->create( $this->user_data ); + + $users = get_users( array( 'search' => '*battle*', 'fields' => 'ID' ) ); + + $this->assertTrue( in_array( $id, $users ) ); + } + + public function test_search_users_nicename() { + $id = $this->factory->user->create( $this->user_data ); + + $users = get_users( array( 'search' => '*one*', 'fields' => 'ID' ) ); + + $this->assertTrue( in_array( $id, $users ) ); + } + + public function test_search_users_display_name() { + $id = $this->factory->user->create( $this->user_data ); + + $users = get_users( array( 'search' => '*Doe*', 'fields' => 'ID' ) ); + + $this->assertTrue( in_array( $id, $users ) ); + } }