Ensure the count for users with no role remains accurate when users with multiple roles are present.

See #34495


git-svn-id: https://develop.svn.wordpress.org/trunk@35707 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2015-11-19 17:10:47 +00:00
parent bbe6aa01aa
commit 71c0c35c24
2 changed files with 38 additions and 2 deletions

View File

@ -758,6 +758,7 @@ function count_users($strategy = 'time') {
foreach ( $avail_roles as $this_role => $name ) {
$select_count[] = $wpdb->prepare( "COUNT(NULLIF(`meta_value` LIKE %s, false))", '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%');
}
$select_count[] = "COUNT(NULLIF(`meta_value` = 'a:0:{}', false))";
$select_count = implode(', ', $select_count);
// Add the meta_value index to the selection list, then run the query.
@ -773,11 +774,11 @@ function count_users($strategy = 'time') {
}
}
$role_counts['none'] = (int) $row[$col++];
// Get the meta_value index from the end of the result set.
$total_users = (int) $row[$col];
$role_counts['none'] = ( $total_users - array_sum( $role_counts ) );
$result['total_users'] = $total_users;
$result['avail_roles'] =& $role_counts;
} else {

View File

@ -140,6 +140,41 @@ class Tests_User_CountUsers extends WP_UnitTestCase {
}
/**
* @ticket 34495
*
* @dataProvider data_count_users_strategies
*/
public function test_count_users_is_accurate_with_multiple_roles( $strategy ) {
// Setup users
$admin = self::factory()->user->create( array(
'role' => 'administrator',
) );
$editor = self::factory()->user->create( array(
'role' => 'editor',
) );
get_userdata( $editor )->add_role( 'author' );
$this->assertEquals( array(
'editor',
'author'
), get_userdata( $editor )->roles );
// Test user counts
$count = count_users( $strategy );
$this->assertEquals( 3, $count['total_users'] );
$this->assertEquals( array(
'administrator' => 2,
'editor' => 1,
'author' => 1,
'none' => 0,
), $count['avail_roles'] );
}
function data_count_users_strategies() {
return array(
array(