Make WordPress Core

Changeset 35707


Ignore:
Timestamp:
11/19/2015 05:10:47 PM (10 years ago)
Author:
johnbillion
Message:

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

See #34495

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user-functions.php

    r35630 r35707  
    759759            $select_count[] = $wpdb->prepare( "COUNT(NULLIF(`meta_value` LIKE %s, false))", '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%');
    760760        }
     761        $select_count[] = "COUNT(NULLIF(`meta_value` = 'a:0:{}', false))";
    761762        $select_count = implode(', ', $select_count);
    762763
     
    774775        }
    775776
     777        $role_counts['none'] = (int) $row[$col++];
     778
    776779        // Get the meta_value index from the end of the result set.
    777780        $total_users = (int) $row[$col];
    778 
    779         $role_counts['none'] = ( $total_users - array_sum( $role_counts ) );
    780781
    781782        $result['total_users'] = $total_users;
  • trunk/tests/phpunit/tests/user/countUsers.php

    r35242 r35707  
    141141    }
    142142
     143    /**
     144     * @ticket 34495
     145     *
     146     * @dataProvider data_count_users_strategies
     147     */
     148    public function test_count_users_is_accurate_with_multiple_roles( $strategy ) {
     149
     150        // Setup users
     151        $admin = self::factory()->user->create( array(
     152            'role' => 'administrator',
     153        ) );
     154        $editor = self::factory()->user->create( array(
     155            'role' => 'editor',
     156        ) );
     157
     158        get_userdata( $editor )->add_role( 'author' );
     159
     160        $this->assertEquals( array(
     161            'editor',
     162            'author'
     163        ), get_userdata( $editor )->roles );
     164
     165        // Test user counts
     166        $count = count_users( $strategy );
     167
     168        $this->assertEquals( 3, $count['total_users'] );
     169        $this->assertEquals( array(
     170            'administrator' => 2,
     171            'editor'        => 1,
     172            'author'        => 1,
     173            'none'          => 0,
     174        ), $count['avail_roles'] );
     175
     176    }
     177
    143178    function data_count_users_strategies() {
    144179        return array(
Note: See TracChangeset for help on using the changeset viewer.