Make WordPress Core

Changeset 44272


Ignore:
Timestamp:
12/17/2018 06:38:13 PM (6 years ago)
Author:
desrosj
Message:

WPDB: Check that $wpdb->last_result is countable before counting with it.

wpdb::get_col() iterates over $wpdb->last_result, which can be a non-countable value, should the preceding query have failed.

Props spacedmonkey, desrosj, pento.

Merges [43934] into trunk.

See #45299.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/wp-db.php

    r43571 r44272  
    25452545        $new_array = array();
    25462546        // Extract the column values
    2547         for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
    2548             $new_array[ $i ] = $this->get_var( null, $x, $i );
     2547        if ( $this->last_result ) {
     2548            for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
     2549                $new_array[ $i ] = $this->get_var( null, $x, $i );
     2550            }
    25492551        }
    25502552        return $new_array;
     
    25862588            // Return an array of row objects with keys from column 1
    25872589            // (Duplicates are discarded)
    2588             foreach ( $this->last_result as $row ) {
    2589                 $var_by_ref = get_object_vars( $row );
    2590                 $key        = array_shift( $var_by_ref );
    2591                 if ( ! isset( $new_array[ $key ] ) ) {
    2592                     $new_array[ $key ] = $row;
     2590            if ( $this->last_result ) {
     2591                foreach ( $this->last_result as $row ) {
     2592                    $var_by_ref = get_object_vars( $row );
     2593                    $key        = array_shift( $var_by_ref );
     2594                    if ( ! isset( $new_array[ $key ] ) ) {
     2595                        $new_array[ $key ] = $row;
     2596                    }
    25932597                }
    25942598            }
  • trunk/tests/phpunit/tests/db.php

    r43571 r44272  
    569569        $this->assertInternalType( 'object', $row );
    570570        $this->assertEquals( 'Walter Sobchak', $row->display_name );
     571    }
     572
     573    /**
     574     * Test the `get_col()` method.
     575     *
     576     * @param string|null        $query       The query to run.
     577     * @param string|array       $expected    The expected resulting value.
     578     * @param arrray|string|null $last_result The value to assign to `$wpdb->last_result`.
     579     * @param int|string         $column      The column index to retrieve.
     580     *
     581     * @dataProvider data_test_get_col
     582     *
     583     * @ticket 45299
     584     */
     585    function test_get_col( $query, $expected, $last_result, $column ) {
     586        global $wpdb;
     587
     588        $wpdb->last_result = $last_result;
     589
     590        $result = $wpdb->get_col( $query, $column );
     591
     592        if ( $query ) {
     593            $this->assertSame( $query, $wpdb->last_query );
     594        }
     595
     596        if ( is_array( $expected ) ) {
     597            $this->assertSame( $expected, $result );
     598        } else {
     599            $this->assertContains( $expected, $result );
     600        }
     601    }
     602
     603    /**
     604     * Data provider for testing `get_col()`.
     605     *
     606     * @return array {
     607     *     Arguments for testing `get_col()`.
     608     *
     609     *     @type string|null        $query       The query to run.
     610     *     @type string|array       $expected    The resulting expected value.
     611     *     @type arrray|string|null $last_result The value to assign to `$wpdb->last_result`.
     612     *     @type int|string         $column      The column index to retrieve.
     613     */
     614    function data_test_get_col() {
     615        global $wpdb;
     616
     617        return array(
     618            array(
     619                "SELECT display_name FROM $wpdb->users",
     620                'admin',
     621                array(),
     622                0,
     623            ),
     624            array(
     625                "SELECT user_login, user_email FROM $wpdb->users",
     626                'admin',
     627                array(),
     628                0,
     629            ),
     630            array(
     631                "SELECT user_login, user_email FROM $wpdb->users",
     632                'admin@example.org',
     633                array(),
     634                1,
     635            ),
     636            array(
     637                "SELECT user_login, user_email FROM $wpdb->users",
     638                'admin@example.org',
     639                array(),
     640                '1',
     641            ),
     642            array(
     643                "SELECT user_login, user_email FROM $wpdb->users",
     644                array( null ),
     645                array(),
     646                3,
     647            ),
     648            array(
     649                '',
     650                array(),
     651                null,
     652                0,
     653            ),
     654            array(
     655                null,
     656                array(),
     657                '',
     658                0,
     659            ),
     660        );
    571661    }
    572662
Note: See TracChangeset for help on using the changeset viewer.