WordPress.org

Make WordPress Core

Ticket #29785: 29785.3.diff

File 29785.3.diff, 2.4 KB (added by ptbello, 4 years ago)

added fix and test also for when 'memory' == $strategy

  • src/wp-includes/user.php

    diff --git src/wp-includes/user.php src/wp-includes/user.php
    index d02b022..3181921 100644
    function count_users($strategy = 'time') { 
    857857                $select_count = implode(', ', $select_count);
    858858
    859859                // Add the meta_value index to the selection list, then run the query.
    860                 $row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N );
     860                $row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta JOIN $wpdb->users ON user_id = ID WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N );
    861861
    862862                // Run the previous loop again to associate results with role names.
    863863                $col = 0;
    function count_users($strategy = 'time') { 
    881881                        'none' => 0,
    882882                );
    883883
    884                 $users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" );
     884                $users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta JOIN $wpdb->users ON user_id = ID WHERE meta_key = '{$blog_prefix}capabilities'" );
    885885
    886886                foreach ( $users_of_blog as $caps_meta ) {
    887887                        $b_roles = maybe_unserialize($caps_meta);
  • tests/phpunit/tests/user/countUsers.php

    diff --git tests/phpunit/tests/user/countUsers.php tests/phpunit/tests/user/countUsers.php
    index d3192bb..b998d5e 100644
    class Tests_User_CountUsers extends WP_UnitTestCase { 
    186186                );
    187187        }
    188188
     189        /**
     190         * @ticket 29785
     191         */
     192        public function test_should_not_count_users_who_are_not_in_posts_table_memory() {
     193                global $wpdb;
     194
     195                // Get a 'before' count for comparison.
     196                $count = count_users( 'memory' );
     197
     198                $u = self::factory()->user->create( array( 'role' => 'editor' ) );
     199
     200                // Manually delete the user, but leave the capabilities usermeta.
     201                $wpdb->delete( $wpdb->users, array( 'ID' => $u ) );
     202
     203                $count2 = count_users( 'memory' );
     204
     205                $this->assertEqualSets( $count, $count2 );
     206        }
     207        /**
     208         * @ticket 29785
     209         */
     210        public function test_should_not_count_users_who_are_not_in_posts_table_time() {
     211                global $wpdb;
     212
     213                // Get a 'before' count for comparison.
     214                $count = count_users( 'time' );
     215
     216                $u = self::factory()->user->create( array( 'role' => 'editor' ) );
     217
     218                // Manually delete the user, but leave the capabilities usermeta.
     219                $wpdb->delete( $wpdb->users, array( 'ID' => $u ) );
     220
     221                $count2 = count_users( 'time' );
     222
     223                $this->assertEqualSets( $count, $count2 );
     224        }
    189225}