WordPress.org

Make WordPress Core

Ticket #44169: users-pre-query-test-1.patch

File users-pre-query-test-1.patch, 1.3 KB (added by tlovett1, 2 years ago)
  • tests/phpunit/tests/user/query.php

     
    16861686                /* must not include user that has same string in other fields */
    16871687                $this->assertEquals( array(), $ids );
    16881688        }
     1689
     1690        /**
     1691         * @ticket 44169
     1692         */
     1693        public function test_users_pre_query_filter_should_bypass_database_query() {
     1694                global $wpdb;
     1695
     1696                add_filter( 'users_pre_query', array( __CLASS__, 'filter_users_pre_query' ), 10, 2 );
     1697
     1698                $num_queries = $wpdb->num_queries;
     1699                $q           = new WP_User_Query(
     1700                        array(
     1701                                'fields' => 'ID',
     1702                        )
     1703                );
     1704
     1705                remove_filter( 'users_pre_query', array( __CLASS__, 'filter_users_pre_query' ), 10, 2 );
     1706
     1707                // Make sure no queries were executed.
     1708                $this->assertSame( $num_queries, $wpdb->num_queries );
     1709
     1710                // We manually inserted a non-existing user and overrode the results with it.
     1711                $this->assertSame( array( 555 ), $q->results );
     1712
     1713                // Make sure manually setting total_users doesn't get overwritten.
     1714                $this->assertEquals( 1, $q->total_users );
     1715        }
     1716
     1717        public static function filter_users_pre_query( $posts, $query ) {
     1718                $query->total_users = 1;
     1719
     1720                return array( 555 );
     1721        }
    16891722}