Skip to:
Content

BuddyPress.org

Ticket #5839: 5839.2.patch

File 5839.2.patch, 38.0 KB (added by boonebgorges, 10 years ago)

Whoops

  • src/bp-core/bp-core-classes.php

    diff --git src/bp-core/bp-core-classes.php src/bp-core/bp-core-classes.php
    index 877e3ad..6e48852 100644
    class BP_User_Query { 
    162162                                'user_ids'        => false,
    163163                                'meta_key'        => false,
    164164                                'meta_value'      => false,
     165                                'xprofile_query'  => array(),
    165166                                'populate_extras' => true,
    166167                                'count_total'     => 'count_query'
    167168                        ) );
  • src/bp-xprofile/bp-xprofile-classes.php

    diff --git src/bp-xprofile/bp-xprofile-classes.php src/bp-xprofile/bp-xprofile-classes.php
    index 00e8a17..18b3c51 100644
    abstract class BP_XProfile_Field_Type { 
    32503250                return $html;
    32513251        }
    32523252}
     3253
     3254/**
     3255 * Build SQL clauses to limit a user query by xProfile data.
     3256 *
     3257 * Borrowed heavily from {@link WP_Meta_Query}.
     3258 *
     3259 * @since BuddyPress (2.2.0)
     3260 */
     3261class BP_XProfile_Query {
     3262
     3263        /**
     3264        * List of metadata queries. A single query is an associative array:
     3265        * - 'key' string The meta key
     3266        * - 'value' string|array The meta value
     3267        * - 'compare' (optional) string How to compare the key to the value.
     3268        *   Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE',
     3269        *       'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'REGEXP',
     3270        *       'NOT REGEXP', 'RLIKE'. Default: '='.
     3271        * - 'type' string (optional) The type of the value.
     3272        *   Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME',
     3273        *       'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default: 'CHAR'.
     3274        *
     3275        * @since BuddyPress (2.2.0)
     3276        * @access public
     3277        * @var array
     3278        */
     3279        public $queries = array();
     3280
     3281        /**
     3282         * The relation between the queries. Can be one of 'AND' or 'OR'.
     3283         *
     3284         * @since BuddyPress (2.2.0)
     3285         * @access public
     3286         * @var string
     3287         */
     3288        public $relation;
     3289
     3290        /**
     3291         * Table aliases.
     3292         *
     3293         * A list of table aliases used in the xprofile query.
     3294         *
     3295         * @since BuddyPress (2.2.0)
     3296         * @access public
     3297         * @var array
     3298         */
     3299        public $aliases = array();
     3300
     3301        /**
     3302         * Constructor.
     3303         *
     3304         * @since BuddyPress (2.2.0)
     3305         *
     3306         * @param array $xprofile_query Array of query parameters.
     3307         */
     3308        public function __construct( $xprofile_query = array() ) {
     3309                if ( empty( $xprofile_query ) ) {
     3310                        return;
     3311                }
     3312
     3313                // 'relation' defaults to 'AND'
     3314                if ( isset( $xprofile_query['relation'] ) && strtoupper( $xprofile_query['relation'] ) === 'OR' ) {
     3315                        $this->relation = 'OR';
     3316                } else {
     3317                        $this->relation = 'AND';
     3318                }
     3319
     3320                $this->queries = array();
     3321
     3322                foreach ( $xprofile_query as $key => $query ) {
     3323                        // No empties
     3324                        if ( ! is_array( $query ) ) {
     3325                                continue;
     3326                        }
     3327
     3328                        $this->queries[] = $query;
     3329                }
     3330
     3331                // Standardize queries
     3332                $this->transform_queries();
     3333        }
     3334
     3335        /**
     3336         * Transform queries as necessary.
     3337         *
     3338         * Translates field_name param into field_id.
     3339         *
     3340         * @since BuddyPress (2.2.0)
     3341         *
     3342         * @param array $query
     3343         * @return array
     3344         */
     3345        protected function transform_queries() {
     3346                foreach ( $this->queries as $k => &$query ) {
     3347                        // If a field_id is provided, trust it
     3348                        if ( ! empty( $query['field_id'] ) ) {
     3349                                continue;
     3350                        }
     3351
     3352                        // If no field_name is provided, nothing to do here
     3353                        if ( empty( $query['field_name'] ) ) {
     3354                                continue;
     3355                        }
     3356
     3357                        $field_id = xprofile_get_field_id_from_name( $query['field_name'] );
     3358
     3359                        // Runnning through intval() means that failed
     3360                        // lookups will result in field_id = 0. This leads to
     3361                        // expected behavior in the SQL query later on
     3362                        $query['field_id'] = intval( $field_id );
     3363                }
     3364        }
     3365
     3366        /**
     3367         * Given a data type, return the appropriate alias if applicable.
     3368         *
     3369         * The data type will be used to CAST the 'value' column in
     3370         * self::get_sql().
     3371         *
     3372         * @since BuddyPress (2.2.0)
     3373         *
     3374         * @param string $type MySQL type to cast.
     3375         * @return string MySQL type.
     3376         */
     3377        public function get_cast_for_type( $type = '' ) {
     3378                if ( empty( $type ) ) {
     3379                        return 'CHAR';
     3380                }
     3381
     3382                $type = strtoupper( $type );
     3383
     3384                if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $type ) )
     3385                        return 'CHAR';
     3386
     3387                if ( 'NUMERIC' == $type ) {
     3388                        $type = 'SIGNED';
     3389                }
     3390
     3391                return $type;
     3392        }
     3393
     3394        /**
     3395         * Generate SQL for this set of xprofile query clauses.
     3396         *
     3397         * @param string $type This param does nothing in this context. It's
     3398         *        kept here for parity with the function arguments in
     3399         *        WP_Meta_Query.
     3400         * @param string $primary_table SQL alias for the primary table in the
     3401         *        user query. Will typically be 'u'.
     3402         * @param string $primary_id_column Name of the SQL column containing
     3403         *        the user ID in $primary_table. Will be 'ID' in the case of
     3404         *        wp_users, and 'user_id' in the case of other BuddyPress
     3405         *        tables.
     3406         * @return array {
     3407         *     @type string $join JOIN clauses
     3408         *     @type string $where WHERE clauses
     3409         * }
     3410         */
     3411        public function get_sql( $type = 'xprofile', $primary_table, $primary_id_column ) {
     3412                global $wpdb;
     3413
     3414                $data_table = buddypress()->profile->table_name_data;
     3415
     3416                $join = $where = $queries = $field_id_only_queries = array();
     3417
     3418                // We can save a JOIN on some queries by combining EXISTS queries
     3419                // (or queries with no 'value', which amounts to the same
     3420                // thing) into a single WHERE clause. First, find queries with
     3421                // an empty array as the 'value'
     3422                foreach ( $this->queries as $k => $q ) {
     3423                        if ( isset( $q['value'] ) && is_array( $q['value'] ) && empty( $q['value'] ) ) {
     3424                                $field_id_only_queries[ $k ] = $q;
     3425                                unset( $this->queries[ $k ] );
     3426                        }
     3427                }
     3428
     3429                // In the case of multiple clauses joined by OR, we can also
     3430                // group together empty non-array values of 'value'
     3431                if ( 'OR' == $this->relation ) {
     3432                        foreach ( $this->queries as $k => $q ) {
     3433                                if ( ( empty( $q['compare'] ) || 'NOT EXISTS' != $q['compare'] ) && ! array_key_exists( 'value', $q ) && ! empty( $q['field_id'] ) ) {
     3434                                        $field_id_only_queries[ $k ] = $q;
     3435                                } else {
     3436                                        $queries[ $k ] = $q;
     3437                                }
     3438                        }
     3439                } else {
     3440                        $queries = $this->queries;
     3441                }
     3442
     3443                // Specify all the meta_key only queries in one go
     3444                if ( $field_id_only_queries ) {
     3445                        $join[]  = "INNER JOIN $data_table ON $primary_table.$primary_id_column = $data_table.user_id";
     3446
     3447                        foreach ( $field_id_only_queries as $key => $q ) {
     3448                                $where["field-id-only-$key"] = $wpdb->prepare( "$data_table.field_id = %d", $q['field_id'] );
     3449                        }
     3450                }
     3451
     3452                $where_meta_key = array();
     3453
     3454                foreach ( $queries as $k => $q ) {
     3455                        $field_id = isset( $q['field_id'] ) ? intval( $q['field_id'] ) : '';
     3456                        $data_type = $this->get_cast_for_type( isset( $q['type'] ) ? $q['type'] : '' );
     3457
     3458                        if ( array_key_exists( 'value', $q ) && is_null( $q['value'] ) ) {
     3459                                $q['value'] = '';
     3460                        }
     3461
     3462                        $value = isset( $q['value'] ) ? $q['value'] : null;
     3463
     3464                        if ( isset( $q['compare'] ) ) {
     3465                                $compare = strtoupper( $q['compare'] );
     3466                        } else {
     3467                                $compare = is_array( $value ) ? 'IN' : '=';
     3468                        }
     3469
     3470                        if ( ! in_array( $compare, array(
     3471                                '=', '!=', '>', '>=', '<', '<=',
     3472                                'LIKE', 'NOT LIKE',
     3473                                'IN', 'NOT IN',
     3474                                'BETWEEN', 'NOT BETWEEN',
     3475                                'NOT EXISTS',
     3476                                'REGEXP', 'NOT REGEXP', 'RLIKE'
     3477                        ) ) ) {
     3478                                $compare = '=';
     3479                        }
     3480
     3481                        // Iterate the table alias
     3482                        $i = count( $join );
     3483                        $alias = $i ? 'xpd' . $i : $data_table;
     3484
     3485                        if ( 'NOT EXISTS' == $compare ) {
     3486                                $join[ $i ]  = "LEFT JOIN $data_table";
     3487                                $join[ $i ] .= $i ? " AS $alias" : '';
     3488                                $join[ $i ] .= " ON ($primary_table.$primary_id_column = $alias.user_id AND $alias.field_id = '$field_id')";
     3489
     3490                                $where[ $k ] = ' ' . $alias . '.user_id IS NULL';
     3491
     3492                                continue;
     3493                        }
     3494
     3495                        // Store the table alias - we will use it for lookups
     3496                        // in the populate_extras() method
     3497                        $this->aliases[ $k ] = $alias;
     3498
     3499                        $join[ $i ]  = "INNER JOIN $data_table";
     3500                        $join[ $i ] .= $i ? " AS $alias" : '';
     3501                        $join[ $i ] .= " ON ($primary_table.$primary_id_column = $alias.user_id)";
     3502
     3503                        $where[ $k ] = '';
     3504
     3505                        // The is_null() check (instead of empty) allows for
     3506                        // a field_id of 0 to be passed. This is necessary to
     3507                        // ensure a failed lookup when a bad field_id or
     3508                        // field_name is passed
     3509                        if ( ! is_null( $field_id ) ) {
     3510                                if ( isset( $q['compare'] ) ) {
     3511                                        $where_meta_key[ $k ] = $wpdb->prepare( "$alias.field_id = %s", $field_id );
     3512                                } else {
     3513                                        $where[ $k ] = $wpdb->prepare( "$alias.field_id = %s", $field_id );
     3514                                }
     3515                        }
     3516
     3517                        if ( is_null( $value ) ) {
     3518                                if ( empty( $where[ $k ] ) && empty( $where_meta_key ) ) {
     3519                                        unset( $join[ $i ] );
     3520                                }
     3521                                continue;
     3522                        }
     3523
     3524                        if ( in_array( $compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
     3525                                if ( ! is_array( $value ) ) {
     3526                                        $value = preg_split( '/[,\s]+/', $value );
     3527                                }
     3528
     3529                                if ( empty( $value ) ) {
     3530                                        unset( $join[ $i ] );
     3531                                        continue;
     3532                                }
     3533                        } else {
     3534                                $value = trim( $value );
     3535                        }
     3536
     3537                        // SQL clause syntax depends on the $compare operator
     3538                        switch ( $compare ) {
     3539                                case 'IN' :
     3540                                case 'NOT IN' :
     3541                                        $compare_string = '(' . substr( str_repeat( ',%s', count( $value ) ), 1 ) . ')';
     3542                                        break;
     3543
     3544                                case 'BETWEEN' :
     3545                                case 'NOT BETWEEN' :
     3546                                        $value = array_slice( $value, 0, 2 );
     3547                                        $compare_string = '%s AND %s';
     3548                                        break;
     3549
     3550                                case 'LIKE' :
     3551                                case 'NOT LIKE' :
     3552                                        $value = '%' . $wpdb->esc_like( $value ) . '%';
     3553                                        $compare_string = '%s';
     3554
     3555                                default :
     3556                                        $compare_string = '%s';
     3557                                        break;
     3558                        }
     3559
     3560                        if ( ! empty( $where[ $k ] ) ) {
     3561                                $where[ $k ] .= ' AND ';
     3562                        }
     3563
     3564                        $where[ $k ] = ' (' . $where[ $k ] . $wpdb->prepare( "CAST($alias.value AS {$data_type}) {$compare} {$compare_string})", $value );
     3565                }
     3566
     3567                // Remove empties
     3568                $where = array_filter( $where );
     3569
     3570                if ( empty( $where ) ) {
     3571                        $where = '';
     3572                } else {
     3573                        $where = ' AND ( ' . implode( "\n{$this->relation} ", $where ) . ' )';
     3574                }
     3575
     3576                if ( ! empty( $where_meta_key ) ) {
     3577                        $where .= "\nAND ( " . implode( "\nAND ", $where_meta_key ) . ' )';
     3578                }
     3579
     3580                $join = implode( "\n", $join );
     3581                if ( ! empty( $join ) ) {
     3582                        $join = ' ' . $join;
     3583                }
     3584
     3585                /**
     3586                 * Filter the xProfile query's generated SQL.
     3587                 *
     3588                 * @since BuddyPress (2.2.0)
     3589                 *
     3590                 * @param array $args {
     3591                 *     An array of arguments.
     3592                 *
     3593                 *     @type array $clauses Array containing the query's JOIN
     3594                 *           and WHERE clauses.
     3595                 *     @type array $queries Array of xprofile queries, as
     3596                 *           passed to the constructor.
     3597                 *     @type string $type 'xprofile'
     3598                 *     @type string $primary_table Table of the primary query.
     3599                 *     @type string $primary_id_column Primary column ID.
     3600                 * }
     3601                 */
     3602                $clauses = apply_filters_ref_array( 'bp_get_xprofile_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column ) );
     3603
     3604                // Store to make available to later filters
     3605                $this->clauses = $clauses;
     3606
     3607                return $clauses;
     3608        }
     3609
     3610        /**
     3611         * Add information about matched xprofile fields to the BP_User_Query results.
     3612         *
     3613         * @since BuddyPress (2.2.0)
     3614         *
     3615         * @param BP_User_Query User query object.
     3616         * @param string $user_ids_sql
     3617         */
     3618        public function populate_extras( BP_User_Query $query, $user_ids_sql ) {
     3619                global $wpdb;
     3620
     3621                if ( empty( $this->aliases ) ) {
     3622                        return;
     3623                }
     3624
     3625                // We don't need any JOINs because we want to gather all data
     3626                // regardless of duplicates. So we zero out the table aliases
     3627                // and do a simple SELECT query
     3628                $where = preg_replace( '/^\s*AND /', '', $this->clauses['where'] );
     3629                $where = str_replace( $this->aliases, buddypress()->profile->table_name_data, $where );
     3630
     3631                $sql = 'SELECT * FROM ' . buddypress()->profile->table_name_data . ' WHERE ' . $where . ' AND user_id IN (' . $user_ids_sql . ')';
     3632
     3633                $xprofile_data = $wpdb->get_results( $sql );
     3634
     3635                // Add the data to the user objects
     3636                foreach ( $xprofile_data as $xpd ) {
     3637                        if ( ! isset( $xpd->user_id ) ) {
     3638                                continue;
     3639                        }
     3640
     3641                        $user_id = $xpd->user_id;
     3642
     3643                        if ( ! isset( $query->results[ $user_id ]->xprofile_matched_fields ) ) {
     3644                                $query->results[ $user_id ]->xprofile_matched_fields = array();
     3645                        }
     3646
     3647                        foreach ( $xpd as $xpd_k => $xpd_v ) {
     3648                                if ( ! isset( $xpd->field_id ) ) {
     3649                                        continue;
     3650                                }
     3651
     3652                                $field_id = $xpd->field_id;
     3653
     3654                                if ( isset( $query->results[ $user_id ]->xprofile_matched_fields[ $field_id ] ) ) {
     3655                                        continue;
     3656                                }
     3657
     3658                                if ( ! isset( $xpd->value ) ) {
     3659                                        continue;
     3660                                }
     3661
     3662                                $query->results[ $user_id ]->xprofile_matched_fields[ $field_id ] = $xpd->value;
     3663                        }
     3664                }
     3665
     3666                // Don't recurse
     3667                remove_action( 'bp_user_query_populate_extras', array( $this, 'populate_extras' ), 10, 2 );
     3668        }
     3669}
  • src/bp-xprofile/bp-xprofile-filters.php

    diff --git src/bp-xprofile/bp-xprofile-filters.php src/bp-xprofile/bp-xprofile-filters.php
    index a11bf12..1ae80c0 100644
    function bp_xprofile_filter_meta_query( $q ) { 
    417417
    418418        return $q;
    419419}
     420
     421/**
     422 * Process xprofile_query parameters in BP_User_Query.
     423 *
     424 * @since BuddyPress (2.2.0)
     425 *
     426 * @param BP_User_Query Query object.
     427 */
     428function bp_xprofile_process_xprofile_query( $user_query ) {
     429        if ( empty( $user_query->query_vars['xprofile_query'] ) ) {
     430                return;
     431        }
     432
     433        $xprofile_query     = new BP_XProfile_Query( $user_query->query_vars['xprofile_query'] );
     434        $xprofile_query_sql = $xprofile_query->get_sql( 'xprofile', 'u', 'user_id' );
     435
     436        $user_query->xprofile_query = $xprofile_query;
     437
     438        $user_query->uid_clauses['select'] .= $xprofile_query_sql['join'];
     439        $user_query->uid_clauses['where']  .= $xprofile_query_sql['where'];
     440
     441        add_action( 'bp_user_query_populate_extras', array( $xprofile_query, 'populate_extras' ), 10, 2 );
     442}
     443add_action( 'bp_pre_user_query', 'bp_xprofile_process_xprofile_query' );
  • new file tests/phpunit/testcases/xprofile/class-bp-xprofile-query.php

    diff --git tests/phpunit/testcases/xprofile/class-bp-xprofile-query.php tests/phpunit/testcases/xprofile/class-bp-xprofile-query.php
    new file mode 100644
    index 0000000..ff5322d
    - +  
     1<?php
     2
     3/**
     4 * @group BP_XProfile_Query
     5 */
     6class BP_XProfile_Query_Tests extends BP_UnitTestCase {
     7        protected $users = array();
     8        protected $fields = array();
     9        protected $userdata = array();
     10
     11        protected $bpbd_query;
     12
     13        public function setUp() {
     14                parent::setUp();
     15
     16                // fields
     17                $g = $this->factory->xprofile_group->create();
     18                for ( $i = 1; $i <= 3; $i++ ) {
     19                        $this->fields[ $i ] = $this->factory->xprofile_field->create( array(
     20                                'field_group_id' => $g,
     21                                'type' => 'textarea',
     22                        ) );
     23                }
     24
     25                // users
     26                $now = time();
     27                for ( $i = 1; $i <= 5; $i++ ) {
     28                        $this->users[ $i ] = $this->create_user( array(
     29                                'last_activity' => date( 'Y-m-d H:i:s', $now - 60 * 60 * $i ), // $i hours ago
     30                        ) );
     31                }
     32        }
     33
     34        public function tearDown() {
     35                parent::tearDown();
     36        }
     37
     38        /**
     39         * @group BP_User_Query
     40         */
     41        public function test_query_single_clause_default_compare() {
     42                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     43                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     44                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     45
     46                $q = new BP_User_Query( array(
     47                        'xprofile_query' => array(
     48                                array(
     49                                        'field_id' => $this->fields[1],
     50                                        'value' => 'Bar',
     51                                ),
     52                        ),
     53                ) );
     54
     55                $found = wp_list_pluck( $q->results, 'ID' );
     56
     57                $expected = array(
     58                        $this->users[2] => $this->users[2],
     59                );
     60
     61                $this->assertEquals( $expected, $found );
     62        }
     63
     64        /**
     65         * @group BP_User_Query
     66         */
     67        public function test_query_single_clause_compare_equals() {
     68                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     69                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     70                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     71
     72                $q = new BP_User_Query( array(
     73                        'xprofile_query' => array(
     74                                array(
     75                                        'field_id' => $this->fields[1],
     76                                        'value' => 'Bar',
     77                                        'compare' => '=',
     78                                ),
     79                        ),
     80                ) );
     81
     82                $found = wp_list_pluck( $q->results, 'ID' );
     83
     84                $expected = array(
     85                        $this->users[2] => $this->users[2],
     86                );
     87
     88                $this->assertEquals( $expected, $found );
     89        }
     90
     91        /**
     92         * @group BP_User_Query
     93         */
     94        public function test_query_single_clause_compare_notequals() {
     95                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     96                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     97                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     98
     99                $q = new BP_User_Query( array(
     100                        'xprofile_query' => array(
     101                                array(
     102                                        'field_id' => $this->fields[1],
     103                                        'value' => 'Bar',
     104                                        'compare' => '!=',
     105                                ),
     106                        ),
     107                ) );
     108
     109                $found = wp_list_pluck( $q->results, 'ID' );
     110
     111                $expected = array(
     112                        $this->users[1] => $this->users[1],
     113                        $this->users[3] => $this->users[3],
     114                );
     115
     116                $this->assertEquals( $expected, $found );
     117        }
     118
     119        /**
     120         * @group BP_User_Query
     121         */
     122        public function test_query_single_clause_compare_greaterthan_type_numeric() {
     123                xprofile_set_field_data( $this->fields[1], $this->users[1], '10' );
     124                xprofile_set_field_data( $this->fields[1], $this->users[2], '100' );
     125                xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' );
     126
     127                $q = new BP_User_Query( array(
     128                        'xprofile_query' => array(
     129                                array(
     130                                        'field_id' => $this->fields[1],
     131                                        'value' => '101',
     132                                        'compare' => '>',
     133                                        'type' => 'NUMERIC',
     134                                ),
     135                        ),
     136                ) );
     137
     138                $found = wp_list_pluck( $q->results, 'ID' );
     139
     140                $expected = array(
     141                        $this->users[3] => $this->users[3],
     142                );
     143
     144                $this->assertEquals( $expected, $found );
     145        }
     146
     147        /**
     148         * @group BP_User_Query
     149         */
     150        public function test_query_single_clause_compare_greaterthanequalto_type_numeric() {
     151                xprofile_set_field_data( $this->fields[1], $this->users[1], '10' );
     152                xprofile_set_field_data( $this->fields[1], $this->users[2], '100' );
     153                xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' );
     154
     155                $q = new BP_User_Query( array(
     156                        'xprofile_query' => array(
     157                                array(
     158                                        'field_id' => $this->fields[1],
     159                                        'value' => '100',
     160                                        'compare' => '>=',
     161                                        'type' => 'NUMERIC',
     162                                ),
     163                        ),
     164                ) );
     165
     166                $found = wp_list_pluck( $q->results, 'ID' );
     167
     168                $expected = array(
     169                        $this->users[2] => $this->users[2],
     170                        $this->users[3] => $this->users[3],
     171                );
     172
     173                $this->assertEquals( $expected, $found );
     174        }
     175
     176        /**
     177         * @group BP_User_Query
     178         */
     179        public function test_query_single_clause_compare_lessthan_type_numeric() {
     180                xprofile_set_field_data( $this->fields[1], $this->users[1], '10' );
     181                xprofile_set_field_data( $this->fields[1], $this->users[2], '100' );
     182                xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' );
     183
     184                $q = new BP_User_Query( array(
     185                        'xprofile_query' => array(
     186                                array(
     187                                        'field_id' => $this->fields[1],
     188                                        'value' => '100',
     189                                        'compare' => '<',
     190                                        'type' => 'NUMERIC',
     191                                ),
     192                        ),
     193                ) );
     194
     195                $found = wp_list_pluck( $q->results, 'ID' );
     196
     197                $expected = array(
     198                        $this->users[1] => $this->users[1],
     199                );
     200
     201                $this->assertEquals( $expected, $found );
     202        }
     203
     204        /**
     205         * @group BP_User_Query
     206         */
     207        public function test_query_single_clause_compare_lessthanequalto_than_type_numeric() {
     208                xprofile_set_field_data( $this->fields[1], $this->users[1], '10' );
     209                xprofile_set_field_data( $this->fields[1], $this->users[2], '100' );
     210                xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' );
     211
     212                $q = new BP_User_Query( array(
     213                        'xprofile_query' => array(
     214                                array(
     215                                        'field_id' => $this->fields[1],
     216                                        'value' => '100',
     217                                        'compare' => '<=',
     218                                        'type' => 'NUMERIC',
     219                                ),
     220                        ),
     221                ) );
     222
     223                $found = wp_list_pluck( $q->results, 'ID' );
     224
     225                $expected = array(
     226                        $this->users[1] => $this->users[1],
     227                        $this->users[2] => $this->users[2],
     228                );
     229
     230                $this->assertEquals( $expected, $found );
     231        }
     232
     233        /**
     234         * @group BP_User_Query
     235         */
     236        public function test_query_single_clause_compare_like() {
     237                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     238                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     239                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     240
     241                $q = new BP_User_Query( array(
     242                        'xprofile_query' => array(
     243                                array(
     244                                        'field_id' => $this->fields[1],
     245                                        'value' => 'Bar',
     246                                        'compare' => 'LIKE',
     247                                ),
     248                        ),
     249                ) );
     250
     251                $found = wp_list_pluck( $q->results, 'ID' );
     252
     253                $expected = array(
     254                        $this->users[2] => $this->users[2],
     255                        $this->users[3] => $this->users[3],
     256                );
     257
     258                $this->assertEquals( $expected, $found );
     259        }
     260
     261        /**
     262         * @group BP_User_Query
     263         */
     264        public function test_query_single_clause_compare_notlike() {
     265                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     266                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     267                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     268
     269                $q = new BP_User_Query( array(
     270                        'xprofile_query' => array(
     271                                array(
     272                                        'field_id' => $this->fields[1],
     273                                        'value' => 'Bar',
     274                                        'compare' => 'NOT LIKE',
     275                                ),
     276                        ),
     277                ) );
     278
     279                $found = wp_list_pluck( $q->results, 'ID' );
     280
     281                $expected = array(
     282                        $this->users[1] => $this->users[1],
     283                );
     284
     285                $this->assertEquals( $expected, $found );
     286        }
     287
     288        /**
     289         * @group BP_User_Query
     290         */
     291        public function test_query_single_clause_compare_in_single_value() {
     292                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     293                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     294                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     295
     296                $q = new BP_User_Query( array(
     297                        'xprofile_query' => array(
     298                                array(
     299                                        'field_id' => $this->fields[1],
     300                                        'value' => 'Bar',
     301                                        'compare' => 'IN',
     302                                ),
     303                        ),
     304                ) );
     305
     306                $found = wp_list_pluck( $q->results, 'ID' );
     307
     308                $expected = array(
     309                        $this->users[2] => $this->users[2],
     310                );
     311
     312                $this->assertEquals( $expected, $found );
     313        }
     314
     315        /**
     316         * @group BP_User_Query
     317         */
     318        public function test_query_single_clause_compare_in() {
     319                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     320                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     321                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     322
     323                $q = new BP_User_Query( array(
     324                        'xprofile_query' => array(
     325                                array(
     326                                        'field_id' => $this->fields[1],
     327                                        'value' => array( 'Foo', 'Bar' ),
     328                                        'compare' => 'IN',
     329                                ),
     330                        ),
     331                ) );
     332
     333                $found = wp_list_pluck( $q->results, 'ID' );
     334
     335                $expected = array(
     336                        $this->users[1] => $this->users[1],
     337                        $this->users[2] => $this->users[2],
     338                );
     339
     340                $this->assertEquals( $expected, $found );
     341        }
     342
     343        /**
     344         * @group BP_User_Query
     345         */
     346        public function test_query_single_clause_compare_in_comma_sep_value() {
     347                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     348                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     349                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     350
     351                $q = new BP_User_Query( array(
     352                        'xprofile_query' => array(
     353                                array(
     354                                        'field_id' => $this->fields[1],
     355                                        'value' => 'Foo, Bar',
     356                                        'compare' => 'IN',
     357                                ),
     358                        ),
     359                ) );
     360
     361                $found = wp_list_pluck( $q->results, 'ID' );
     362
     363                $expected = array(
     364                        $this->users[1] => $this->users[1],
     365                        $this->users[2] => $this->users[2],
     366                );
     367
     368                $this->assertEquals( $expected, $found );
     369        }
     370        /**
     371         * @group BP_User_Query
     372         */
     373        public function test_query_single_clause_compare_notin_single_value() {
     374                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     375                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     376                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     377
     378                $q = new BP_User_Query( array(
     379                        'xprofile_query' => array(
     380                                array(
     381                                        'field_id' => $this->fields[1],
     382                                        'value' => 'Bar',
     383                                        'compare' => 'NOT IN',
     384                                ),
     385                        ),
     386                ) );
     387
     388                $found = wp_list_pluck( $q->results, 'ID' );
     389
     390                $expected = array(
     391                        $this->users[1] => $this->users[1],
     392                        $this->users[3] => $this->users[3],
     393                );
     394
     395                $this->assertEquals( $expected, $found );
     396        }
     397
     398        /**
     399         * @group BP_User_Query
     400         */
     401        public function test_query_single_clause_compare_notin() {
     402                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     403                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     404                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     405
     406                $q = new BP_User_Query( array(
     407                        'xprofile_query' => array(
     408                                array(
     409                                        'field_id' => $this->fields[1],
     410                                        'value' => array( 'Foo', 'Bar' ),
     411                                        'compare' => 'NOT IN',
     412                                ),
     413                        ),
     414                ) );
     415
     416                $found = wp_list_pluck( $q->results, 'ID' );
     417
     418                $expected = array(
     419                        $this->users[3] => $this->users[3],
     420                );
     421
     422                $this->assertEquals( $expected, $found );
     423        }
     424
     425        /**
     426         * @group BP_User_Query
     427         */
     428        public function test_query_single_clause_compare_between() {
     429                xprofile_set_field_data( $this->fields[1], $this->users[1], '10' );
     430                xprofile_set_field_data( $this->fields[1], $this->users[2], '100' );
     431                xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' );
     432
     433                $q = new BP_User_Query( array(
     434                        'xprofile_query' => array(
     435                                array(
     436                                        'field_id' => $this->fields[1],
     437                                        'value' => array( '99', '101' ),
     438                                        'compare' => 'BETWEEN',
     439                                        'type' => 'NUMERIC',
     440                                ),
     441                        ),
     442                ) );
     443
     444                $found = wp_list_pluck( $q->results, 'ID' );
     445
     446                $expected = array(
     447                        $this->users[2] => $this->users[2],
     448                );
     449
     450                $this->assertEquals( $expected, $found );
     451        }
     452
     453        /**
     454         * @group BP_User_Query
     455         */
     456        public function test_query_single_clause_compare_notbetween() {
     457                xprofile_set_field_data( $this->fields[1], $this->users[1], '10' );
     458                xprofile_set_field_data( $this->fields[1], $this->users[2], '100' );
     459                xprofile_set_field_data( $this->fields[1], $this->users[3], '1000' );
     460
     461                $q = new BP_User_Query( array(
     462                        'xprofile_query' => array(
     463                                array(
     464                                        'field_id' => $this->fields[1],
     465                                        'value' => array( '99', '101' ),
     466                                        'compare' => 'NOT BETWEEN',
     467                                        'type' => 'NUMERIC',
     468                                ),
     469                        ),
     470                ) );
     471
     472                $found = wp_list_pluck( $q->results, 'ID' );
     473
     474                $expected = array(
     475                        $this->users[1] => $this->users[1],
     476                        $this->users[3] => $this->users[3],
     477                );
     478
     479                $this->assertEquals( $expected, $found );
     480        }
     481
     482        /**
     483         * @group BP_User_Query
     484         */
     485        public function test_query_single_clause_compare_regexp() {
     486                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     487                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Foo Bar' );
     488                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Fooooo Bar' );
     489
     490                $q = new BP_User_Query( array(
     491                        'xprofile_query' => array(
     492                                array(
     493                                        'field_id' => $this->fields[1],
     494                                        'value' => '^Fo+ Bar',
     495                                        'compare' => 'REGEXP',
     496                                ),
     497                        ),
     498                ) );
     499
     500                $found = wp_list_pluck( $q->results, 'ID' );
     501
     502                $expected = array(
     503                        $this->users[2] => $this->users[2],
     504                        $this->users[3] => $this->users[3],
     505                );
     506
     507                $this->assertEquals( $expected, $found );
     508        }
     509
     510        /**
     511         * @group BP_User_Query
     512         */
     513        public function test_query_single_clause_compare_notregexp() {
     514                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     515                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Foo Bar' );
     516                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Fooooo Bar' );
     517
     518                $q = new BP_User_Query( array(
     519                        'xprofile_query' => array(
     520                                array(
     521                                        'field_id' => $this->fields[1],
     522                                        'value' => '^Fo+ Bar',
     523                                        'compare' => 'NOT REGEXP',
     524                                ),
     525                        ),
     526                ) );
     527
     528                $found = wp_list_pluck( $q->results, 'ID' );
     529
     530                $expected = array(
     531                        $this->users[1] => $this->users[1],
     532                );
     533
     534                $this->assertEquals( $expected, $found );
     535        }
     536
     537        /**
     538         * @group BP_User_Query
     539         */
     540        public function test_query_multiple_clauses_default_compare_default_relation() {
     541                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     542                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     543                xprofile_set_field_data( $this->fields[2], $this->users[2], 'Foo' );
     544                xprofile_set_field_data( $this->fields[2], $this->users[3], 'Bar' );
     545
     546                $q = new BP_User_Query( array(
     547                        'xprofile_query' => array(
     548                                array(
     549                                        'field_id' => $this->fields[1],
     550                                        'value' => 'Bar',
     551                                ),
     552                                array(
     553                                        'field_id' => $this->fields[2],
     554                                        'value' => 'Foo',
     555                                ),
     556                        ),
     557                ) );
     558
     559                $found = wp_list_pluck( $q->results, 'ID' );
     560
     561                $expected = array(
     562                        $this->users[2] => $this->users[2],
     563                );
     564
     565                $this->assertEquals( $expected, $found );
     566        }
     567
     568        /**
     569         * @group BP_User_Query
     570         */
     571        public function test_query_multiple_clauses_default_compare_relation_and() {
     572                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     573                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     574                xprofile_set_field_data( $this->fields[2], $this->users[2], 'Foo' );
     575                xprofile_set_field_data( $this->fields[2], $this->users[3], 'Bar' );
     576
     577                $q = new BP_User_Query( array(
     578                        'xprofile_query' => array(
     579                                'relation' => 'AND',
     580                                array(
     581                                        'field_id' => $this->fields[1],
     582                                        'value' => 'Bar',
     583                                ),
     584                                array(
     585                                        'field_id' => $this->fields[2],
     586                                        'value' => 'Foo',
     587                                ),
     588                        ),
     589                ) );
     590
     591                $found = wp_list_pluck( $q->results, 'ID' );
     592
     593                $expected = array(
     594                        $this->users[2] => $this->users[2],
     595                );
     596
     597                $this->assertEquals( $expected, $found );
     598        }
     599
     600        /**
     601         * @group BP_User_Query
     602         */
     603        public function test_query_multiple_clauses_default_compare_relation_or() {
     604                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     605                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     606                xprofile_set_field_data( $this->fields[2], $this->users[2], 'Foo' );
     607                xprofile_set_field_data( $this->fields[2], $this->users[3], 'Bar' );
     608                xprofile_set_field_data( $this->fields[2], $this->users[3], 'Barry' );
     609
     610                $q = new BP_User_Query( array(
     611                        'xprofile_query' => array(
     612                                'relation' => 'OR',
     613                                array(
     614                                        'field_id' => $this->fields[1],
     615                                        'value' => 'Foo',
     616                                ),
     617                                array(
     618                                        'field_id' => $this->fields[2],
     619                                        'value' => 'Barry',
     620                                ),
     621                        ),
     622                ) );
     623
     624                $found = wp_list_pluck( $q->results, 'ID' );
     625
     626                $expected = array(
     627                        $this->users[1] => $this->users[1],
     628                        $this->users[3] => $this->users[3],
     629                );
     630
     631                $this->assertEquals( $expected, $found );
     632        }
     633
     634        /**
     635         * @group BP_User_Query
     636         */
     637        public function test_query_multiple_clauses_compare_equals() {
     638                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     639                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     640                xprofile_set_field_data( $this->fields[2], $this->users[2], 'Foo' );
     641                xprofile_set_field_data( $this->fields[2], $this->users[3], 'Bar' );
     642
     643                $q = new BP_User_Query( array(
     644                        'xprofile_query' => array(
     645                                'relation' => 'AND',
     646                                array(
     647                                        'field_id' => $this->fields[1],
     648                                        'value' => 'Bar',
     649                                        'compare' => '=',
     650                                ),
     651                                array(
     652                                        'field_id' => $this->fields[2],
     653                                        'value' => 'Foo',
     654                                        'compare' => '=',
     655                                ),
     656                        ),
     657                ) );
     658
     659                $found = wp_list_pluck( $q->results, 'ID' );
     660
     661                $expected = array(
     662                        $this->users[2] => $this->users[2],
     663                );
     664
     665                $this->assertEquals( $expected, $found );
     666        }
     667
     668        /**
     669         * @group BP_User_Query
     670         */
     671        public function test_query_multiple_clauses_compare_mixed() {
     672                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     673                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     674                xprofile_set_field_data( $this->fields[2], $this->users[2], 'Foo' );
     675                xprofile_set_field_data( $this->fields[2], $this->users[3], 'Bar' );
     676
     677                $q = new BP_User_Query( array(
     678                        'xprofile_query' => array(
     679                                'relation' => 'AND',
     680                                array(
     681                                        'field_id' => $this->fields[1],
     682                                        'value' => 'B',
     683                                        'compare' => 'LIKE',
     684                                ),
     685                                array(
     686                                        'field_id' => $this->fields[2],
     687                                        'value' => '^F',
     688                                        'compare' => 'REGEXP',
     689                                ),
     690                        ),
     691                ) );
     692
     693                $found = wp_list_pluck( $q->results, 'ID' );
     694
     695                $expected = array(
     696                        $this->users[2] => $this->users[2],
     697                );
     698
     699                $this->assertEquals( $expected, $found );
     700        }
     701
     702        /**
     703         * @group BP_User_Query
     704         */
     705        public function test_query_single_clause_compare_exists() {
     706                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     707                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     708                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     709
     710                $q = new BP_User_Query( array(
     711                        'xprofile_query' => array(
     712                                array(
     713                                        'field_id' => $this->fields[1],
     714                                        'compare' => 'EXISTS',
     715                                ),
     716                        ),
     717                ) );
     718
     719                $found = wp_list_pluck( $q->results, 'ID' );
     720
     721                $expected = array(
     722                        $this->users[1] => $this->users[1],
     723                        $this->users[2] => $this->users[2],
     724                        $this->users[3] => $this->users[3],
     725                );
     726
     727                $this->assertEquals( $expected, $found );
     728        }
     729
     730        /**
     731         * @group BP_User_Query
     732         */
     733        public function test_query_single_clause_compare_exists_with_value() {
     734                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     735                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     736                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     737
     738                $q = new BP_User_Query( array(
     739                        'xprofile_query' => array(
     740                                array(
     741                                        'field_id' => $this->fields[1],
     742                                        'compare' => 'EXISTS',
     743                                        'value' => 'Foo',
     744                                ),
     745                        ),
     746                ) );
     747
     748                $found = wp_list_pluck( $q->results, 'ID' );
     749
     750                $expected = array(
     751                        $this->users[1] => $this->users[1],
     752                );
     753
     754                $this->assertEquals( $expected, $found );
     755        }
     756
     757        /**
     758         * @group BP_User_Query
     759         */
     760        public function test_query_single_clause_compare_notexists() {
     761                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     762                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     763                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     764
     765                $q = new BP_User_Query( array(
     766                        'xprofile_query' => array(
     767                                array(
     768                                        'field_id' => $this->fields[1],
     769                                        'compare' => 'NOT EXISTS',
     770                                ),
     771                        ),
     772                ) );
     773
     774                $found = wp_list_pluck( $q->results, 'ID' );
     775
     776                $expected = array(
     777                        $this->users[4] => $this->users[4],
     778                        $this->users[5] => $this->users[5],
     779                );
     780
     781                $this->assertEquals( $expected, $found );
     782        }
     783
     784        /**
     785         * @group BP_User_Query
     786         */
     787        public function test_query_single_clause_compare_notexists_with_value() {
     788                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     789                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     790                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     791
     792                $q = new BP_User_Query( array(
     793                        'xprofile_query' => array(
     794                                array(
     795                                        'field_id' => $this->fields[1],
     796                                        'compare' => 'NOT EXISTS',
     797                                        'value' => 'Foo',
     798                                ),
     799                        ),
     800                ) );
     801
     802                $found = wp_list_pluck( $q->results, 'ID' );
     803
     804                $expected = array(
     805                        $this->users[4] => $this->users[4],
     806                        $this->users[5] => $this->users[5],
     807                );
     808
     809                $this->assertEquals( $expected, $found );
     810        }
     811
     812        /**
     813         * @group BP_User_Query
     814         */
     815        public function test_query_single_clause_no_compare_no_value() {
     816                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     817                xprofile_set_field_data( $this->fields[1], $this->users[2], 'Bar' );
     818                xprofile_set_field_data( $this->fields[1], $this->users[3], 'Barry' );
     819
     820                $q = new BP_User_Query( array(
     821                        'xprofile_query' => array(
     822                                array(
     823                                        'field_id' => $this->fields[1],
     824                                ),
     825                        ),
     826                ) );
     827
     828                $found = wp_list_pluck( $q->results, 'ID' );
     829
     830                // Same as EXISTS
     831                $expected = array(
     832                        $this->users[1] => $this->users[1],
     833                        $this->users[2] => $this->users[2],
     834                        $this->users[3] => $this->users[3],
     835                );
     836
     837                $this->assertEquals( $expected, $found );
     838        }
     839
     840        /**
     841         * @group BP_User_Query
     842         */
     843        public function test_query_multiple_no_key_clauses() {
     844                xprofile_set_field_data( $this->fields[1], $this->users[1], 'Foo' );
     845                xprofile_set_field_data( $this->fields[2], $this->users[2], 'Bar' );
     846                xprofile_set_field_data( $this->fields[3], $this->users[3], 'Barry' );
     847
     848                $q = new BP_User_Query( array(
     849                        'xprofile_query' => array(
     850                                'relation' => 'OR',
     851                                array(
     852                                        'field_id' => $this->fields[1],
     853                                ),
     854                                array(
     855                                        'field_id' => $this->fields[2],
     856                                ),
     857                                array(
     858                                        'field_id' => $this->fields[3],
     859                                        'value' => 'Barry',
     860                                ),
     861                        ),
     862                ) );
     863
     864                $found = wp_list_pluck( $q->results, 'ID' );
     865
     866                // Same as EXISTS
     867                $expected = array(
     868                        $this->users[1] => $this->users[1],
     869                        $this->users[2] => $this->users[2],
     870                        $this->users[3] => $this->users[3],
     871                );
     872
     873                $this->assertEquals( $expected, $found );
     874        }
     875
     876        /**
     877         * @group BP_User_Query
     878         */
     879        public function test_query_translate_field_name() {
     880                $g = $this->factory->xprofile_group->create();
     881                $f = $this->factory->xprofile_field->create( array(
     882                        'field_group_id' => $g,
     883                        'name' => 'Foo Field',
     884                        'type' => 'textarea',
     885                ) );
     886
     887                xprofile_set_field_data( $f, $this->users[1], 'Foo' );
     888                xprofile_set_field_data( $f, $this->users[2], 'Bar' );
     889                xprofile_set_field_data( $f, $this->users[3], 'Barry' );
     890
     891                $q = new BP_User_Query( array(
     892                        'xprofile_query' => array(
     893                                array(
     894                                        'field_name' => 'Foo Field',
     895                                        'value' => 'Bar',
     896                                ),
     897                        ),
     898                ) );
     899
     900                $found = wp_list_pluck( $q->results, 'ID' );
     901
     902                $expected = array(
     903                        $this->users[2] => $this->users[2],
     904                );
     905
     906                $this->assertEquals( $expected, $found );
     907        }
     908
     909        /**
     910         * @group BP_User_Query
     911         */
     912        public function test_query_translate_field_name_with_invalid_field_name() {
     913                $g = $this->factory->xprofile_group->create();
     914                $f = $this->factory->xprofile_field->create( array(
     915                        'field_group_id' => $g,
     916                        'name' => 'Foo Field',
     917                        'type' => 'textarea',
     918                ) );
     919
     920                xprofile_set_field_data( $f, $this->users[1], 'Foo' );
     921                xprofile_set_field_data( $f, $this->users[2], 'Bar' );
     922                xprofile_set_field_data( $f, $this->users[3], 'Barry' );
     923
     924                // AND should return nothing
     925                $q = new BP_User_Query( array(
     926                        'xprofile_query' => array(
     927                                'relation' => 'AND',
     928                                array(
     929                                        'field_name' => 'Foo Field',
     930                                        'value' => 'Bar',
     931                                ),
     932                                array(
     933                                        'field_name' => 'Foo Field 2',
     934                                        'value' => 'Bar',
     935                                ),
     936                        ),
     937                ) );
     938
     939                $found = wp_list_pluck( $q->results, 'ID' );
     940                $this->assertEquals( array(), $found );
     941
     942                // OR should return hit for the legit field
     943                $q = new BP_User_Query( array(
     944                        'xprofile_query' => array(
     945                                'relation' => 'OR',
     946                                array(
     947                                        'field_name' => 'Foo Field',
     948                                        'value' => 'Bar',
     949                                ),
     950                                array(
     951                                        'field_name' => 'Foo Field 2',
     952                                        'value' => 'Bar',
     953                                ),
     954                        ),
     955                ) );
     956
     957                $found = wp_list_pluck( $q->results, 'ID' );
     958
     959                $expected = array(
     960                        $this->users[2] => $this->users[2],
     961                );
     962
     963                $this->assertEquals( $expected, $found );
     964        }
     965}