WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/24/2014 07:50:53 PM (7 years ago)
Author:
boonebgorges
Message:

Accept 'orderby=include' in WP_User_Query.

This lets the results of a user query be sorted manually by the value of the
'include' param.

Props jipmoors.
Fixes #30064.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/query.php

    r30008 r30016  
    133133
    134134        $this->assertEquals( $names, $values );
     135    }
     136
     137    /**
     138     * @ticket 30064
     139     */
     140    public function test_orderby_include_with_empty_include() {
     141        $q = new WP_User_Query( array(
     142            'orderby' => 'include',
     143        ) );
     144
     145        $this->assertContains( 'ORDER BY user_login', $q->query_orderby );
     146    }
     147
     148    /**
     149     * @ticket 30064
     150     */
     151    public function test_orderby_include() {
     152        global $wpdb;
     153
     154        $users = $this->factory->user->create_many( 4 );
     155        $q = new WP_User_Query( array(
     156            'orderby' => 'include',
     157            'include' => array( $users[1], $users[0], $users[3] ),
     158            'fields' => '',
     159        ) );
     160
     161        $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . $users[1] . ',' . $users[0] . ',' . $users[3] . ' )';
     162        $this->assertContains( $expected_orderby, $q->query_orderby );
     163
     164        // assertEquals() respects order but ignores type (get_results() returns numeric strings).
     165        $this->assertEquals( array( $users[1], $users[0], $users[3] ), $q->get_results() );
     166    }
     167
     168    /**
     169     * @ticket 30064
     170     */
     171    public function test_orderby_include_duplicate_values() {
     172        global $wpdb;
     173
     174        $users = $this->factory->user->create_many( 4 );
     175        $q = new WP_User_Query( array(
     176            'orderby' => 'include',
     177            'include' => array( $users[1], $users[0], $users[1], $users[3] ),
     178            'fields' => '',
     179        ) );
     180
     181        $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . $users[1] . ',' . $users[0] . ',' . $users[3] . ' )';
     182        $this->assertContains( $expected_orderby, $q->query_orderby );
     183
     184        // assertEquals() respects order but ignores type (get_results() returns numeric strings).
     185        $this->assertEquals( array( $users[1], $users[0], $users[3] ), $q->get_results() );
    135186    }
    136187
Note: See TracChangeset for help on using the changeset viewer.