Make WordPress Core

Ticket #32315: 32315.2.diff

File 32315.2.diff, 5.9 KB (added by jdgrimes, 7 years ago)

Includes query() too, and additional unit tests

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

     
    12081208         * This method DOES NOT support sign, padding, alignment, width or precision specifiers.
    12091209         * This method DOES NOT support argument numbering or swapping.
    12101210         *
    1211          * Arguments may be passed as individual arguments to the method, or as a single array containing all arguments. A combination 
     1211         * Arguments may be passed as individual arguments to the method, or as a single array containing all arguments. A combination
    12121212         * of the two is not supported.
    12131213         *
    12141214         * Examples:
     
    17621762                        $this->flush();
    17631763                        if ( $stripped_query !== $query ) {
    17641764                                $this->insert_id = 0;
     1765                                $this->last_query = $query;
     1766                                $this->last_error = __( 'WordPress Database Error: Could not perform query because it contains invalid data.' );
    17651767                                return false;
    17661768                        }
    17671769                }
     
    20642066                $conditions = implode( ' AND ', $conditions );
    20652067
    20662068                $sql = "UPDATE `$table` SET $fields WHERE $conditions";
    2067                
     2069
    20682070                $this->check_current_query = false;
    20692071                return $this->query( $this->prepare( $sql, $values ) );
    20702072        }
     
    21562158                $converted_data = $this->strip_invalid_text( $data );
    21572159
    21582160                if ( $data !== $converted_data ) {
     2161                        $problem_fields = array();
     2162
     2163                        foreach ( $data as $field => $value ) {
     2164                                if ( $value !== $converted_data[ $field ] ) {
     2165                                        $problem_fields[] = $field;
     2166                                }
     2167                        }
     2168
     2169                        if ( count( $problem_fields ) === 1 ) {
     2170                                $message = __( 'WordPress Database Error: Processing the value for the following field failed, the supplied value may have been too long or contained invalid data: %s.' );
     2171                        } else {
     2172                                $message = __( 'WordPress Database Error: Processing the values for the following fields failed, the supplied values may have been too long or contained invalid data: %s.' );
     2173                        }
     2174
     2175                        $this->last_error = sprintf( $message, implode( ', ', $problem_fields ) );
    21592176                        return false;
    21602177                }
    21612178
  • tests/phpunit/tests/db.php

     
    3131                parent::setUp();
    3232                $this->_queries = array();
    3333                add_filter( 'query', array( $this, 'query_filter' ) );
     34                self::$_wpdb->last_error = null;
     35                $GLOBALS['wpdb']->last_error = null;
    3436        }
    3537
    3638        /**
     
    939941        }
    940942
    941943        /**
     944         * @ticket 32315
     945         *
     946         * @dataProvider data_process_fields_invalid_data
     947         */
     948        function test_process_fields_value_too_long_for_field( $data ) {
     949
     950                global $wpdb;
     951
     952                $this->assertFalse( self::$_wpdb->process_fields( $wpdb->posts, $data, null ) );
     953                $this->assertSame(
     954                        'WordPress Database Error: Processing the value for the following field failed, the supplied value may have been too long or contained invalid data: post_status.',
     955                        self::$_wpdb->last_error
     956                );
     957        }
     958
     959        function data_process_fields_invalid_data() {
     960                return array(
     961                        'too_long'      => array( array( 'post_status' => str_repeat( 'a', 21 ) ) ),
     962                        'invalid_chars' => array( array( 'post_status' => "\xF5" ) ),
     963                );
     964        }
     965
     966        /**
     967         * @ticket 32315
     968         */
     969        function test_process_fields_value_too_long_for_field_multiple() {
     970
     971                global $wpdb;
     972
     973                $data = array(
     974                        'post_status'  => str_repeat( 'a', 21 ),
     975                        'post_content' => "\xF5",
     976                );
     977
     978                $this->assertFalse( self::$_wpdb->process_fields( $wpdb->posts, $data, null ) );
     979                $this->assertSame(
     980                        'WordPress Database Error: Processing the values for the following fields failed, the supplied values may have been too long or contained invalid data: post_status, post_content.',
     981                        self::$_wpdb->last_error
     982                );
     983        }
     984
     985        /**
     986         * @ticket 32315
     987         *
     988         * @dataProvider data_process_fields_invalid_data
     989         */
     990        function test_insert_value_too_long_for_field( $data ) {
     991
     992                global $wpdb;
     993
     994                $this->assertFalse( $wpdb->insert( $wpdb->posts, $data ) );
     995                $this->assertSame(
     996                        'WordPress Database Error: Processing the value for the following field failed, the supplied value may have been too long or contained invalid data: post_status.',
     997                        $wpdb->last_error
     998                );
     999        }
     1000
     1001        /**
     1002         * @ticket 32315
     1003         *
     1004         * @dataProvider data_process_fields_invalid_data
     1005         */
     1006        function test_replace_value_too_long_for_field( $data ) {
     1007
     1008                global $wpdb;
     1009
     1010                $this->assertFalse( $wpdb->replace( $wpdb->posts, $data ) );
     1011                $this->assertSame(
     1012                        'WordPress Database Error: Processing the value for the following field failed, the supplied value may have been too long or contained invalid data: post_status.',
     1013                        $wpdb->last_error
     1014                );
     1015        }
     1016
     1017        /**
     1018         * @ticket 32315
     1019         *
     1020         * @dataProvider data_process_fields_invalid_data
     1021         */
     1022        function test_update_value_too_long_for_field( $data ) {
     1023
     1024                global $wpdb;
     1025
     1026                $this->assertFalse( $wpdb->update( $wpdb->posts, $data, array() ) );
     1027                $this->assertSame(
     1028                        'WordPress Database Error: Processing the value for the following field failed, the supplied value may have been too long or contained invalid data: post_status.',
     1029                        $wpdb->last_error
     1030                );
     1031        }
     1032
     1033        /**
     1034         * @ticket 32315
     1035         *
     1036         * @dataProvider data_process_fields_invalid_data
     1037         */
     1038        function test_delete_value_too_long_for_field( $data ) {
     1039
     1040                global $wpdb;
     1041
     1042                $this->assertFalse( $wpdb->delete( $wpdb->posts, $data, array() ) );
     1043                $this->assertSame(
     1044                        'WordPress Database Error: Processing the value for the following field failed, the supplied value may have been too long or contained invalid data: post_status.',
     1045                        $wpdb->last_error
     1046                );
     1047        }
     1048
     1049        /**
     1050         * @ticket 32315
     1051         */
     1052        function test_query_value_contains_invalid_chars() {
     1053
     1054                global $wpdb;
     1055
     1056                $this->assertFalse(
     1057                        $wpdb->query( "INSERT INTO {$wpdb->posts} (post_status) VALUES ('\xF5')" )
     1058                );
     1059                $this->assertSame(
     1060                        'WordPress Database Error: Could not perform query because it contains invalid data.',
     1061                        $wpdb->last_error
     1062                );
     1063        }
     1064
     1065        /**
    9421066         * @ticket 15158
    9431067         */
    9441068        function test_null_insert() {