Make WordPress Core

Changeset 32186


Ignore:
Timestamp:
04/20/2015 11:31:22 AM (10 years ago)
Author:
pento
Message:

Merge the query sanity checks from #21212 to the 3.8 branch.

Props pento, nacin, mdawaffe, DrewAPicture.

Location:
branches/3.8
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/3.8/src/wp-admin/includes/post.php

    r28114 r32186  
    167167 */
    168168function edit_post( $post_data = null ) {
     169    global $wpdb;
    169170
    170171    if ( empty($post_data) )
     
    296297    update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
    297298
    298     wp_update_post( $post_data );
     299    $success = wp_update_post( $post_data );
     300    // If the save failed, see if we can sanity check the main fields and try again
     301    if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) {
     302        $fields = array( 'post_title', 'post_content', 'post_excerpt' );
     303
     304        foreach( $fields as $field ) {
     305            if ( isset( $post_data[ $field ] ) ) {
     306                $post_data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $post_data[ $field ] );
     307            }
     308        }
     309
     310        wp_update_post( $post_data );
     311    }
    299312
    300313    // Now that we have an ID we can fix any attachment anchor hrefs
  • branches/3.8/src/wp-includes/wp-db.php

    r26512 r32186  
    141141
    142142    /**
     143     * Cached column info, for sanity checking data before inserting
     144     *
     145     * @since 4.2.0
     146     * @access protected
     147     * @var array
     148     */
     149    protected $col_meta = array();
     150
     151    /**
     152     * Calculated character sets on tables
     153     *
     154     * @since 4.2.0
     155     * @access protected
     156     * @var array
     157     */
     158    protected $table_charset = array();
     159
     160    /**
     161     * Whether text fields in the current query need to be sanity checked.
     162     *
     163     * @since 4.2.0
     164     * @access protected
     165     * @var bool
     166     */
     167    protected $check_current_query = true;
     168
     169    /**
     170     * Flag to ensure we don't run into recursion problems when checking the collation.
     171     *
     172     * @since 4.2.0
     173     * @access private
     174     * @see wpdb::check_safe_collation()
     175     * @var boolean
     176     */
     177    private $checking_collation = false;
     178
     179    /**
    143180     * Saved info on the table column
    144181     *
     
    576613     */
    577614    function __set( $name, $value ) {
     615        $protected_members = array(
     616            'col_meta',
     617            'table_charset',
     618            'check_current_query',
     619        );
     620        if (  in_array( $name, $protected_members, true ) ) {
     621            return;
     622        }
    578623        $this->$name = $value;
    579624    }
     
    11871232     */
    11881233    function query( $query ) {
    1189         if ( ! $this->ready )
     1234        if ( ! $this->ready ) {
     1235            $this->check_current_query = true;
    11901236            return false;
     1237        }
     1238
    11911239        /**
    11921240         * Filter the database query.
     
    12051253        $this->func_call = "\$db->query(\"$query\")";
    12061254
     1255        // If we're writing to the database, make sure the query will write safely.
     1256        if ( $this->check_current_query && ! $this->check_ascii( $query ) ) {
     1257            $stripped_query = $this->strip_invalid_text_from_query( $query );
     1258            // strip_invalid_text_from_query() can perform queries, so we need
     1259            // to flush again, just to make sure everything is clear.
     1260            $this->flush();
     1261            if ( $stripped_query !== $query ) {
     1262                $this->insert_id = 0;
     1263                return false;
     1264            }
     1265        }
     1266
     1267        $this->check_current_query = true;
     1268
    12071269        // Keep track of the last query for debug..
    12081270        $this->last_query = $query;
     
    13181380     */
    13191381    function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
    1320         if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
     1382        if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) {
    13211383            return false;
     1384        }
     1385
     1386        $data = $this->process_fields( $table, $data, $format );
     1387        if ( false === $data ) {
     1388            return false;
     1389        }
     1390
     1391        $formats = $values = array();
     1392        foreach ( $data as $value ) {
     1393            $formats[] = $value['format'];
     1394            $values[]  = $value['value'];
     1395        }
     1396
     1397        $fields  = '`' . implode( '`, `', array_keys( $data ) ) . '`';
     1398        $formats = implode( ', ', $formats );
     1399
     1400        $sql = "$type INTO `$table` ($fields) VALUES ($formats)";
     1401
    13221402        $this->insert_id = 0;
    1323         $formats = $format = (array) $format;
    1324         $fields = array_keys( $data );
    1325         $formatted_fields = array();
    1326         foreach ( $fields as $field ) {
    1327             if ( !empty( $format ) )
    1328                 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
    1329             elseif ( isset( $this->field_types[$field] ) )
    1330                 $form = $this->field_types[$field];
    1331             else
    1332                 $form = '%s';
    1333             $formatted_fields[] = $form;
    1334         }
    1335         $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")";
    1336         return $this->query( $this->prepare( $sql, $data ) );
     1403        $this->check_current_query = false;
     1404        return $this->query( $this->prepare( $sql, $values ) );
    13371405    }
    13381406
     
    13591427     */
    13601428    function update( $table, $data, $where, $format = null, $where_format = null ) {
    1361         if ( ! is_array( $data ) || ! is_array( $where ) )
     1429        if ( ! is_array( $data ) || ! is_array( $where ) ) {
    13621430            return false;
    1363 
    1364         $formats = $format = (array) $format;
    1365         $bits = $wheres = array();
    1366         foreach ( (array) array_keys( $data ) as $field ) {
    1367             if ( !empty( $format ) )
    1368                 $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
    1369             elseif ( isset($this->field_types[$field]) )
    1370                 $form = $this->field_types[$field];
    1371             else
    1372                 $form = '%s';
    1373             $bits[] = "`$field` = {$form}";
    1374         }
    1375 
    1376         $where_formats = $where_format = (array) $where_format;
    1377         foreach ( (array) array_keys( $where ) as $field ) {
    1378             if ( !empty( $where_format ) )
    1379                 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
    1380             elseif ( isset( $this->field_types[$field] ) )
    1381                 $form = $this->field_types[$field];
    1382             else
    1383                 $form = '%s';
    1384             $wheres[] = "`$field` = {$form}";
    1385         }
    1386 
    1387         $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
    1388         return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
     1431        }
     1432
     1433        $data = $this->process_fields( $table, $data, $format );
     1434        if ( false === $data ) {
     1435            return false;
     1436        }
     1437        $where = $this->process_fields( $table, $where, $where_format );
     1438        if ( false === $where ) {
     1439            return false;
     1440        }
     1441
     1442        $fields = $conditions = $values = array();
     1443        foreach ( $data as $field => $value ) {
     1444            $fields[] = "`$field` = " . $value['format'];
     1445            $values[] = $value['value'];
     1446        }
     1447        foreach ( $where as $field => $value ) {
     1448            $conditions[] = "`$field` = " . $value['format'];
     1449            $values[] = $value['value'];
     1450        }
     1451
     1452        $fields = implode( ', ', $fields );
     1453        $conditions = implode( ' AND ', $conditions );
     1454
     1455        $sql = "UPDATE `$table` SET $fields WHERE $conditions";
     1456
     1457        $this->check_current_query = false;
     1458        return $this->query( $this->prepare( $sql, $values ) );
    13891459    }
    13901460
     
    14081478     */
    14091479    function delete( $table, $where, $where_format = null ) {
    1410         if ( ! is_array( $where ) )
     1480        if ( ! is_array( $where ) ) {
    14111481            return false;
    1412 
    1413         $bits = $wheres = array();
    1414 
    1415         $where_formats = $where_format = (array) $where_format;
    1416 
    1417         foreach ( array_keys( $where ) as $field ) {
    1418             if ( !empty( $where_format ) ) {
    1419                 $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
     1482        }
     1483
     1484        $where = $this->process_fields( $table, $where, $where_format );
     1485        if ( false === $where ) {
     1486            return false;
     1487        }
     1488
     1489        $conditions = $values = array();
     1490        foreach ( $where as $field => $value ) {
     1491            $conditions[] = "`$field` = " . $value['format'];
     1492            $values[] = $value['value'];
     1493        }
     1494
     1495        $conditions = implode( ' AND ', $conditions );
     1496
     1497        $sql = "DELETE FROM `$table` WHERE $conditions";
     1498
     1499        $this->check_current_query = false;
     1500        return $this->query( $this->prepare( $sql, $values ) );
     1501    }
     1502
     1503
     1504    /**
     1505     * Processes arrays of field/value pairs and field formats.
     1506     *
     1507     * This is a helper method for wpdb's CRUD methods, which take field/value
     1508     * pairs for inserts, updates, and where clauses. This method first pairs
     1509     * each value with a format. Then it determines the charset of that field,
     1510     * using that to determine if any invalid text would be stripped. If text is
     1511     * stripped, then field processing is rejected and the query fails.
     1512     *
     1513     * @since 4.2.0
     1514     * @access protected
     1515     *
     1516     * @param string $table  Table name.
     1517     * @param array  $data   Field/value pair.
     1518     * @param mixed  $format Format for each field.
     1519     * @return array|bool Returns an array of fields that contain paired values
     1520     *                    and formats. Returns false for invalid values.
     1521     */
     1522    protected function process_fields( $table, $data, $format ) {
     1523        $data = $this->process_field_formats( $data, $format );
     1524        $data = $this->process_field_charsets( $data, $table );
     1525        if ( false === $data ) {
     1526            return false;
     1527        }
     1528
     1529        $converted_data = $this->strip_invalid_text( $data );
     1530
     1531        if ( $data !== $converted_data ) {
     1532            return false;
     1533        }
     1534
     1535        return $data;
     1536    }
     1537
     1538    /**
     1539     * Prepares arrays of value/format pairs as passed to wpdb CRUD methods.
     1540     *
     1541     * @since 4.2.0
     1542     * @access protected
     1543     *
     1544     * @param array $data   Array of fields to values.
     1545     * @param mixed $format Formats to be mapped to the values in $data.
     1546     * @return array Array, keyed by field names with values being an array
     1547     *               of 'value' and 'format' keys.
     1548     */
     1549    protected function process_field_formats( $data, $format ) {
     1550        $formats = $original_formats = (array) $format;
     1551
     1552        foreach ( $data as $field => $value ) {
     1553            $value = array(
     1554                'value'  => $value,
     1555                'format' => '%s',
     1556            );
     1557
     1558            if ( ! empty( $format ) ) {
     1559                $value['format'] = array_shift( $formats );
     1560                if ( ! $value['format'] ) {
     1561                    $value['format'] = reset( $original_formats );
     1562                }
    14201563            } elseif ( isset( $this->field_types[ $field ] ) ) {
    1421                 $form = $this->field_types[ $field ];
     1564                $value['format'] = $this->field_types[ $field ];
     1565            }
     1566
     1567            $data[ $field ] = $value;
     1568        }
     1569
     1570        return $data;
     1571    }
     1572
     1573    /**
     1574     * Adds field charsets to field/value/format arrays generated by
     1575     * the {@see wpdb::process_field_formats()} method.
     1576     *
     1577     * @since 4.2.0
     1578     * @access protected
     1579     *
     1580     * @param array  $data  As it comes from the {@see wpdb::process_field_formats()} method.
     1581     * @param string $table Table name.
     1582     * @return The same array as $data with additional 'charset' keys.
     1583     */
     1584    protected function process_field_charsets( $data, $table ) {
     1585        foreach ( $data as $field => $value ) {
     1586            if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
     1587                // We can skip this field if we know it isn't a string.
     1588                // This checks %d/%f versus ! %s because it's sprintf() could take more.
     1589                $value['charset'] = false;
     1590            } elseif ( $this->check_ascii( $value['value'] ) ) {
     1591                // If it's ASCII, then we don't need the charset. We can skip this field.
     1592                $value['charset'] = false;
    14221593            } else {
    1423                 $form = '%s';
    1424             }
    1425 
    1426             $wheres[] = "$field = $form";
    1427         }
    1428 
    1429         $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres );
    1430         return $this->query( $this->prepare( $sql, $where ) );
    1431     }
    1432 
     1594                $value['charset'] = $this->get_col_charset( $table, $field );
     1595                if ( is_wp_error( $value['charset'] ) ) {
     1596                    return false;
     1597                }
     1598
     1599                // This isn't ASCII. Don't have strip_invalid_text() re-check.
     1600                $value['ascii'] = false;
     1601            }
     1602
     1603            $data[ $field ] = $value;
     1604        }
     1605
     1606        return $data;
     1607    }
    14331608
    14341609    /**
     
    14481623    function get_var( $query = null, $x = 0, $y = 0 ) {
    14491624        $this->func_call = "\$db->get_var(\"$query\", $x, $y)";
     1625
     1626        if ( $this->check_safe_collation( $query ) ) {
     1627            $this->check_current_query = false;
     1628        }
     1629
    14501630        if ( $query )
    14511631            $this->query( $query );
     
    14751655    function get_row( $query = null, $output = OBJECT, $y = 0 ) {
    14761656        $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
     1657
     1658        if ( $this->check_safe_collation( $query ) ) {
     1659            $this->check_current_query = false;
     1660        }
     1661
    14771662        if ( $query )
    14781663            $this->query( $query );
     
    15081693     */
    15091694    function get_col( $query = null , $x = 0 ) {
     1695        if ( $this->check_safe_collation( $query ) ) {
     1696            $this->check_current_query = false;
     1697        }
     1698
    15101699        if ( $query )
    15111700            $this->query( $query );
     
    15341723    function get_results( $query = null, $output = OBJECT ) {
    15351724        $this->func_call = "\$db->get_results(\"$query\", $output)";
     1725
     1726        if ( $this->check_safe_collation( $query ) ) {
     1727            $this->check_current_query = false;
     1728        }
    15361729
    15371730        if ( $query )
     
    15721765    }
    15731766
     1767
     1768    /**
     1769     * Retrieves the character set for the given table.
     1770     *
     1771     * @since 4.2.0
     1772     * @access protected
     1773     *
     1774     * @param string $table Table name.
     1775     * @return string|WP_Error Table character set, {@see WP_Error} object if it couldn't be found.
     1776     */
     1777    protected function get_table_charset( $table ) {
     1778        $tablekey = strtolower( $table );
     1779
     1780        /**
     1781         * Filter the table charset value before the DB is checked.
     1782         *
     1783         * Passing a non-null value to the filter will effectively short-circuit
     1784         * checking the DB for the charset, returning that value instead.
     1785         *
     1786         * @since 4.2.0
     1787         *
     1788         * @param string $charset The character set to use. Default null.
     1789         * @param string $table   The name of the table being checked.
     1790         */
     1791        $charset = apply_filters( 'pre_get_table_charset', null, $table );
     1792        if ( null !== $charset ) {
     1793            return $charset;
     1794        }
     1795
     1796        if ( isset( $this->table_charset[ $tablekey ] ) ) {
     1797            return $this->table_charset[ $tablekey ];
     1798        }
     1799
     1800        $charsets = $columns = array();
     1801        $results = $this->get_results( "SHOW FULL COLUMNS FROM `$table`" );
     1802        if ( ! $results ) {
     1803            return new WP_Error( 'wpdb_get_table_charset_failure' );
     1804        }
     1805
     1806        foreach ( $results as $column ) {
     1807            $columns[ strtolower( $column->Field ) ] = $column;
     1808        }
     1809
     1810        $this->col_meta[ $tablekey ] = $columns;
     1811
     1812        foreach ( $columns as $column ) {
     1813            if ( ! empty( $column->Collation ) ) {
     1814                list( $charset ) = explode( '_', $column->Collation );
     1815                $charsets[ strtolower( $charset ) ] = true;
     1816            }
     1817
     1818            list( $type ) = explode( '(', $column->Type );
     1819
     1820            // A binary/blob means the whole query gets treated like this.
     1821            if ( in_array( strtoupper( $type ), array( 'BINARY', 'VARBINARY', 'TINYBLOB', 'MEDIUMBLOB', 'BLOB', 'LONGBLOB' ) ) ) {
     1822                $this->table_charset[ $tablekey ] = 'binary';
     1823                return 'binary';
     1824            }
     1825        }
     1826
     1827        // utf8mb3 is an alias for utf8.
     1828        if ( isset( $charsets['utf8mb3'] ) ) {
     1829            $charsets['utf8'] = true;
     1830            unset( $charsets['utf8mb3'] );
     1831        }
     1832
     1833        // Check if we have more than one charset in play.
     1834        $count = count( $charsets );
     1835        if ( 1 === $count ) {
     1836            $charset = key( $charsets );
     1837        } elseif ( 0 === $count ) {
     1838            // No charsets, assume this table can store whatever.
     1839            $charset = false;
     1840        } else {
     1841            // More than one charset. Remove latin1 if present and recalculate.
     1842            unset( $charsets['latin1'] );
     1843            $count = count( $charsets );
     1844            if ( 1 === $count ) {
     1845                // Only one charset (besides latin1).
     1846                $charset = key( $charsets );
     1847            } elseif ( 2 === $count && isset( $charsets['utf8'], $charsets['utf8mb4'] ) ) {
     1848                // Two charsets, but they're utf8 and utf8mb4, use utf8.
     1849                $charset = 'utf8';
     1850            } else {
     1851                // Two mixed character sets. ascii.
     1852                $charset = 'ascii';
     1853            }
     1854        }
     1855
     1856        $this->table_charset[ $tablekey ] = $charset;
     1857        return $charset;
     1858    }
     1859
     1860    /**
     1861     * Retrieves the character set for the given column.
     1862     *
     1863     * @since 4.2.0
     1864     * @access public
     1865     *
     1866     * @param string $table  Table name.
     1867     * @param string $column Column name.
     1868     * @return mixed Column character set as a string. False if the column has no
     1869     *               character set. {@see WP_Error} object if there was an error.
     1870     */
     1871    public function get_col_charset( $table, $column ) {
     1872        $tablekey = strtolower( $table );
     1873        $columnkey = strtolower( $column );
     1874
     1875        /**
     1876         * Filter the column charset value before the DB is checked.
     1877         *
     1878         * Passing a non-null value to the filter will short-circuit
     1879         * checking the DB for the charset, returning that value instead.
     1880         *
     1881         * @since 4.2.0
     1882         *
     1883         * @param string $charset The character set to use. Default null.
     1884         * @param string $table   The name of the table being checked.
     1885         * @param string $column  The name of the column being checked.
     1886         */
     1887        $charset = apply_filters( 'pre_get_col_charset', null, $table, $column );
     1888        if ( null !== $charset ) {
     1889            return $charset;
     1890        }
     1891
     1892        // Skip this entirely if this isn't a MySQL database.
     1893        if ( false === $this->is_mysql ) {
     1894            return false;
     1895        }
     1896
     1897        if ( empty( $this->table_charset[ $tablekey ] ) ) {
     1898            // This primes column information for us.
     1899            $table_charset = $this->get_table_charset( $table );
     1900            if ( is_wp_error( $table_charset ) ) {
     1901                return $table_charset;
     1902            }
     1903        }
     1904
     1905        // If still no column information, return the table charset.
     1906        if ( empty( $this->col_meta[ $tablekey ] ) ) {
     1907            return $this->table_charset[ $tablekey ];
     1908        }
     1909
     1910        // If this column doesn't exist, return the table charset.
     1911        if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) {
     1912            return $this->table_charset[ $tablekey ];
     1913        }
     1914
     1915        // Return false when it's not a string column.
     1916        if ( empty( $this->col_meta[ $tablekey ][ $columnkey ]->Collation ) ) {
     1917            return false;
     1918        }
     1919
     1920        list( $charset ) = explode( '_', $this->col_meta[ $tablekey ][ $columnkey ]->Collation );
     1921        return $charset;
     1922    }
     1923
     1924    /**
     1925     * Check if a string is ASCII.
     1926     *
     1927     * The negative regex is faster for non-ASCII strings, as it allows
     1928     * the search to finish as soon as it encounters a non-ASCII character.
     1929     *
     1930     * @since 4.2.0
     1931     * @access protected
     1932     *
     1933     * @param string $string String to check.
     1934     * @return bool True if ASCII, false if not.
     1935     */
     1936    protected function check_ascii( $string ) {
     1937        if ( function_exists( 'mb_check_encoding' ) ) {
     1938            if ( mb_check_encoding( $string, 'ASCII' ) ) {
     1939                return true;
     1940            }
     1941        } elseif ( ! preg_match( '/[^\x00-\x7F]/', $string ) ) {
     1942            return true;
     1943        }
     1944
     1945        return false;
     1946    }
     1947
     1948    /**
     1949     * Check if the query is accessing a collation considered safe on the current version of MySQL.
     1950     *
     1951     * @since 4.2.0
     1952     * @access protected
     1953     *
     1954     * @param string $query The query to check.
     1955     * @return bool True if the collation is safe, false if it isn't.
     1956     */
     1957    protected function check_safe_collation( $query ) {
     1958        if ( $this->checking_collation ) {
     1959            return true;
     1960        }
     1961        $table = $this->get_table_from_query( $query );
     1962        if ( ! $table ) {
     1963            return false;
     1964        }
     1965
     1966        $this->checking_collation = true;
     1967        $this->get_table_charset( $table );
     1968        $this->checking_collation = false;
     1969
     1970        $table = strtolower( $table );
     1971        if ( empty( $this->col_meta[ $table ] ) ) {
     1972            return false;
     1973        }
     1974
     1975        foreach( $this->col_meta[ $table ] as $col ) {
     1976            if ( empty( $col->Collation ) ) {
     1977                continue;
     1978            }
     1979
     1980            if ( ! in_array( $col->Collation, array( 'utf8_general_ci', 'utf8_bin', 'utf8mb4_general_ci', 'utf8mb4_bin' ), true ) ) {
     1981                return false;
     1982            }
     1983        }
     1984
     1985        return true;
     1986    }
     1987
     1988    /**
     1989     * Strips any invalid characters based on value/charset pairs.
     1990     *
     1991     * @since 4.2.0
     1992     * @access protected
     1993     *
     1994     * @param array $data Array of value arrays. Each value array has the keys
     1995     *                    'value' and 'charset'. An optional 'ascii' key can be
     1996     *                    set to false to avoid redundant ASCII checks.
     1997     * @return array|WP_Error The $data parameter, with invalid characters removed from
     1998     *                        each value. This works as a passthrough: any additional keys
     1999     *                        such as 'field' are retained in each value array. If we cannot
     2000     *                        remove invalid characters, a {@see WP_Error} object is returned.
     2001     */
     2002    protected function strip_invalid_text( $data ) {
     2003        // Some multibyte character sets that we can check in PHP.
     2004        $mb_charsets = array(
     2005            'ascii'   => 'ASCII',
     2006            'big5'    => 'BIG-5',
     2007            'eucjpms' => 'eucJP-win',
     2008            'gb2312'  => 'EUC-CN',
     2009            'ujis'    => 'EUC-JP',
     2010            'utf32'   => 'UTF-32',
     2011        );
     2012
     2013        $supported_charsets = array();
     2014        if ( function_exists( 'mb_list_encodings' ) ) {
     2015            $supported_charsets = mb_list_encodings();
     2016        }
     2017
     2018        $db_check_string = false;
     2019
     2020        foreach ( $data as &$value ) {
     2021            $charset = $value['charset'];
     2022
     2023            // Column isn't a string, or is latin1, which will will happily store anything.
     2024            if ( false === $charset || 'latin1' === $charset ) {
     2025                continue;
     2026            }
     2027
     2028            if ( ! is_string( $value['value'] ) ) {
     2029                continue;
     2030            }
     2031
     2032            // ASCII is always OK.
     2033            if ( ! isset( $value['ascii'] ) && $this->check_ascii( $value['value'] ) ) {
     2034                continue;
     2035            }
     2036
     2037            // Convert the text locally.
     2038            if ( $supported_charsets ) {
     2039                if ( isset( $mb_charsets[ $charset ] ) && in_array( $mb_charsets[ $charset ], $supported_charsets ) ) {
     2040                    $value['value'] = mb_convert_encoding( $value['value'], $mb_charsets[ $charset ], $mb_charsets[ $charset ] );
     2041                    continue;
     2042                }
     2043            }
     2044
     2045            // utf8 can be handled by regex, which is a bunch faster than a DB lookup.
     2046            if ( 'utf8' === $charset || 'utf8mb3' === $charset || 'utf8mb4' === $charset ) {
     2047                $regex = '/
     2048                    (
     2049                        (?: [\x00-\x7F]                  # single-byte sequences   0xxxxxxx
     2050                        |   [\xC2-\xDF][\x80-\xBF]       # double-byte sequences   110xxxxx 10xxxxxx
     2051                        |   \xE0[\xA0-\xBF][\x80-\xBF]   # triple-byte sequences   1110xxxx 10xxxxxx * 2
     2052                        |   [\xE1-\xEC][\x80-\xBF]{2}
     2053                        |   \xED[\x80-\x9F][\x80-\xBF]
     2054                        |   [\xEE-\xEF][\x80-\xBF]{2}';
     2055
     2056                if ( 'utf8mb4' === $charset) {
     2057                    $regex .= '
     2058                        |    \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences   11110xxx 10xxxxxx * 3
     2059                        |    [\xF1-\xF3][\x80-\xBF]{3}
     2060                        |    \xF4[\x80-\x8F][\x80-\xBF]{2}
     2061                    ';
     2062                }
     2063
     2064                $regex .= '){1,50}                          # ...one or more times
     2065                    )
     2066                    | .                                  # anything else
     2067                    /x';
     2068                $value['value'] = preg_replace( $regex, '$1', $value['value'] );
     2069                continue;
     2070            }
     2071
     2072            // We couldn't use any local conversions, send it to the DB.
     2073            $value['db'] = $db_check_string = true;
     2074        }
     2075        unset( $value ); // Remove by reference.
     2076
     2077        if ( $db_check_string ) {
     2078            $queries = array();
     2079            foreach ( $data as $col => $value ) {
     2080                if ( ! empty( $value['db'] ) ) {
     2081                    if ( ! isset( $queries[ $value['charset'] ] ) ) {
     2082                        $queries[ $value['charset'] ] = array();
     2083                    }
     2084
     2085                    // Split the CONVERT() calls by charset, so we can make sure the connection is right
     2086                    $queries[ $value['charset'] ][ $col ] = $this->prepare( "CONVERT( %s USING {$value['charset']} )", $value['value'] );
     2087                }
     2088            }
     2089
     2090            $connection_charset = $this->charset;
     2091            foreach ( $queries as $charset => $query ) {
     2092                if ( ! $query ) {
     2093                    continue;
     2094                }
     2095
     2096                // Change the charset to match the string(s) we're converting
     2097                if ( $charset !== $connection_charset ) {
     2098                    $connection_charset = $charset;
     2099                    $this->set_charset( $this->dbh, $charset );
     2100                }
     2101
     2102                $this->check_current_query = false;
     2103
     2104                $row = $this->get_row( "SELECT " . implode( ', ', $query ), ARRAY_N );
     2105                if ( ! $row ) {
     2106                    $this->set_charset( $this->dbh, $connection_charset );
     2107                    return new WP_Error( 'wpdb_strip_invalid_text_failure' );
     2108                }
     2109
     2110                $cols = array_keys( $query );
     2111                $col_count = count( $cols );
     2112                for ( $ii = 0; $ii < $col_count; $ii++ ) {
     2113                    $data[ $cols[ $ii ] ]['value'] = $row[ $ii ];
     2114                }
     2115            }
     2116
     2117            // Don't forget to change the charset back!
     2118            if ( $connection_charset !== $this->charset ) {
     2119                $this->set_charset( $this->dbh );
     2120            }
     2121        }
     2122
     2123        return $data;
     2124    }
     2125
     2126    /**
     2127     * Strips any invalid characters from the query.
     2128     *
     2129     * @since 4.2.0
     2130     * @access protected
     2131     *
     2132     * @param string $query Query to convert.
     2133     * @return string|WP_Error The converted query, or a {@see WP_Error} object if the conversion fails.
     2134     */
     2135    protected function strip_invalid_text_from_query( $query ) {
     2136        $table = $this->get_table_from_query( $query );
     2137        if ( $table ) {
     2138            $charset = $this->get_table_charset( $table );
     2139            if ( is_wp_error( $charset ) ) {
     2140                return $charset;
     2141            }
     2142
     2143            // We can't reliably strip text from tables containing binary/blob columns
     2144            if ( 'binary' === $charset ) {
     2145                return $query;
     2146            }
     2147        } else {
     2148            $charset = $this->charset;
     2149        }
     2150
     2151        $data = array(
     2152            'value'   => $query,
     2153            'charset' => $charset,
     2154            'ascii'   => false,
     2155        );
     2156
     2157        $data = $this->strip_invalid_text( array( $data ) );
     2158        if ( is_wp_error( $data ) ) {
     2159            return $data;
     2160        }
     2161
     2162        return $data[0]['value'];
     2163    }
     2164
     2165    /**
     2166     * Strips any invalid characters from the string for a given table and column.
     2167     *
     2168     * @since 4.2.0
     2169     * @access public
     2170     *
     2171     * @param string $table  Table name.
     2172     * @param string $column Column name.
     2173     * @param string $value  The text to check.
     2174     * @return string|WP_Error The converted string, or a `WP_Error` object if the conversion fails.
     2175     */
     2176    public function strip_invalid_text_for_column( $table, $column, $value ) {
     2177        if ( ! is_string( $value ) || $this->check_ascii( $value ) ) {
     2178            return $value;
     2179        }
     2180
     2181        $charset = $this->get_col_charset( $table, $column );
     2182        if ( ! $charset ) {
     2183            // Not a string column.
     2184            return $value;
     2185        } elseif ( is_wp_error( $charset ) ) {
     2186            // Bail on real errors.
     2187            return $charset;
     2188        }
     2189
     2190        $data = array(
     2191            $column => array(
     2192                'value'   => $value,
     2193                'charset' => $charset,
     2194                'ascii'   => false,
     2195            )
     2196        );
     2197
     2198        $data = $this->strip_invalid_text( $data );
     2199        if ( is_wp_error( $data ) ) {
     2200            return $data;
     2201        }
     2202
     2203        return $data[ $column ]['value'];
     2204    }
     2205
     2206    /**
     2207     * Find the first table name referenced in a query.
     2208     *
     2209     * @since 4.2.0
     2210     * @access protected
     2211     *
     2212     * @param string $query The query to search.
     2213     * @return string|false $table The table name found, or false if a table couldn't be found.
     2214     */
     2215    protected function get_table_from_query( $query ) {
     2216        // Remove characters that can legally trail the table name.
     2217        $query = rtrim( $query, ';/-#' );
     2218
     2219        // Allow (select...) union [...] style queries. Use the first query's table name.
     2220        $query = ltrim( $query, "\r\n\t (" );
     2221
     2222        /*
     2223         * Strip everything between parentheses except nested selects and use only 1,000
     2224         * chars of the query.
     2225         */
     2226        $query = preg_replace( '/\((?!\s*select)[^(]*?\)/is', '()', substr( $query, 0, 1000 ) );
     2227
     2228        // Quickly match most common queries.
     2229        if ( preg_match( '/^\s*(?:'
     2230                . 'SELECT.*?\s+FROM'
     2231                . '|INSERT(?:\s+LOW_PRIORITY|\s+DELAYED|\s+HIGH_PRIORITY)?(?:\s+IGNORE)?(?:\s+INTO)?'
     2232                . '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?'
     2233                . '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?'
     2234                . '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:\s+FROM)?'
     2235                . ')\s+`?([\w-]+)`?/is', $query, $maybe ) ) {
     2236            return $maybe[1];
     2237        }
     2238
     2239        // SHOW TABLE STATUS and SHOW TABLES
     2240        if ( preg_match( '/^\s*(?:'
     2241                . 'SHOW\s+TABLE\s+STATUS.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
     2242                . '|SHOW\s+(?:FULL\s+)?TABLES.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
     2243                . ')\W([\w-]+)\W/is', $query, $maybe ) ) {
     2244            return $maybe[1];
     2245        }
     2246
     2247        // Big pattern for the rest of the table-related queries.
     2248        if ( preg_match( '/^\s*(?:'
     2249                . '(?:EXPLAIN\s+(?:EXTENDED\s+)?)?SELECT.*?\s+FROM'
     2250                . '|DESCRIBE|DESC|EXPLAIN|HANDLER'
     2251                . '|(?:LOCK|UNLOCK)\s+TABLE(?:S)?'
     2252                . '|(?:RENAME|OPTIMIZE|BACKUP|RESTORE|CHECK|CHECKSUM|ANALYZE|REPAIR).*\s+TABLE'
     2253                . '|TRUNCATE(?:\s+TABLE)?'
     2254                . '|CREATE(?:\s+TEMPORARY)?\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?'
     2255                . '|ALTER(?:\s+IGNORE)?\s+TABLE'
     2256                . '|DROP\s+TABLE(?:\s+IF\s+EXISTS)?'
     2257                . '|CREATE(?:\s+\w+)?\s+INDEX.*\s+ON'
     2258                . '|DROP\s+INDEX.*\s+ON'
     2259                . '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE'
     2260                . '|(?:GRANT|REVOKE).*ON\s+TABLE'
     2261                . '|SHOW\s+(?:.*FROM|.*TABLE)'
     2262                . ')\s+\(*\s*`?([\w-]+)`?\s*\)*/is', $query, $maybe ) ) {
     2263            return $maybe[1];
     2264        }
     2265
     2266        return false;
     2267    }
     2268
    15742269    /**
    15752270     * Load the column metadata from the last query.
  • branches/3.8/tests/phpunit/includes/utils.php

    r26187 r32186  
    388388    remove_filter( 'get_the_terms', array( 'Featured_Content', 'hide_the_featured_term' ), 10, 3 );
    389389}
     390
     391/**
     392 * Special class for exposing protected wpdb methods we need to access
     393 */
     394class wpdb_exposed_methods_for_testing extends wpdb {
     395    public function __construct() {
     396        global $wpdb;
     397        $this->dbh = $wpdb->dbh;
     398        $this->ready = true;
     399        $this->field_types = $wpdb->field_types;
     400        $this->charset = $wpdb->charset;
     401    }
     402
     403    public function __call( $name, $arguments ) {
     404        return call_user_func_array( array( $this, $name ), $arguments );
     405    }
     406}
  • branches/3.8/tests/phpunit/tests/db.php

    r25374 r32186  
    1515
    1616    /**
     17     * Our special WPDB
     18     * @var resource
     19     */
     20    protected static $_wpdb;
     21
     22    public static function setUpBeforeClass() {
     23        self::$_wpdb = new wpdb_exposed_methods_for_testing();
     24    }
     25
     26    /**
    1727     * Set up the test fixture
    1828     */
     
    2737     */
    2838    public function tearDown() {
     39        remove_filter( 'query', array( $this, 'query_filter' ) );
    2940        parent::tearDown();
    30         remove_filter( 'query', array( $this, 'query_filter' ) );
    3141    }
    3242
     
    105115
    106116    /**
     117     * @ticket 21212
     118     */
     119    function test_wpdb_actually_protected_properties() {
     120        global $wpdb;
     121
     122        $new_meta = "HAHA I HOPE THIS DOESN'T WORK";
     123
     124        $col_meta = $wpdb->col_meta;
     125        $wpdb->col_meta = $new_meta;
     126
     127        $this->assertNotEquals( $col_meta, $new_meta );
     128        $this->assertEquals( $col_meta, $wpdb->col_meta );
     129    }
     130
     131    /**
    107132     * @ticket 18510
    108133     */
     
    127152        $this->assertEquals( "UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql );
    128153    }
     154
     155    function test_db_version() {
     156        global $wpdb;
     157
     158        $this->assertTrue( version_compare( $wpdb->db_version(), '5.0', '>=' ) );
     159    }
     160
     161    function test_get_caller() {
     162        global $wpdb;
     163        $str = $wpdb->get_caller();
     164        $calls = explode( ', ', $str );
     165        $called = join( '->', array( __CLASS__, __FUNCTION__ ) );
     166        $this->assertEquals( $called, end( $calls ) );
     167    }
     168
     169    function test_has_cap() {
     170        global $wpdb;
     171        $this->assertTrue( $wpdb->has_cap( 'collation' ) );
     172        $this->assertTrue( $wpdb->has_cap( 'group_concat' ) );
     173        $this->assertTrue( $wpdb->has_cap( 'subqueries' ) );
     174        $this->assertTrue( $wpdb->has_cap( 'COLLATION' ) );
     175        $this->assertTrue( $wpdb->has_cap( 'GROUP_CONCAT' ) );
     176        $this->assertTrue( $wpdb->has_cap( 'SUBQUERIES' ) );
     177        $this->assertEquals(
     178            version_compare( $wpdb->db_version(), '5.0.7', '>=' ),
     179            $wpdb->has_cap( 'set_charset' )
     180        );
     181        $this->assertEquals(
     182            version_compare( $wpdb->db_version(), '5.0.7', '>=' ),
     183            $wpdb->has_cap( 'SET_CHARSET' )
     184        );
     185    }
     186
     187    /**
     188     * @expectedDeprecated supports_collation
     189     */
     190    function test_supports_collation() {
     191        global $wpdb;
     192        $this->assertTrue( $wpdb->supports_collation() );
     193    }
     194
     195    function test_check_database_version() {
     196        global $wpdb;
     197        $this->assertEmpty( $wpdb->check_database_version() );
     198    }
     199
     200    /**
     201     * @expectedException WPDieException
     202     */
     203    function test_bail() {
     204        global $wpdb;
     205        $wpdb->bail( 'Database is dead.' );
     206    }
     207
     208    function test_timers() {
     209        global $wpdb;
     210
     211        $wpdb->timer_start();
     212        usleep( 5 );
     213        $stop = $wpdb->timer_stop();
     214
     215        $this->assertNotEquals( $wpdb->time_start, $stop );
     216        $this->assertGreaterThan( $stop, $wpdb->time_start );
     217    }
     218
     219    function test_get_col_info() {
     220        global $wpdb;
     221
     222        $wpdb->get_results( "SELECT ID FROM $wpdb->users" );
     223
     224        $this->assertEquals( array( 'ID' ), $wpdb->get_col_info() );
     225        $this->assertEquals( array( $wpdb->users ), $wpdb->get_col_info( 'table' ) );
     226        $this->assertEquals( $wpdb->users, $wpdb->get_col_info( 'table', 0 ) );
     227    }
     228
     229    function test_query_and_delete() {
     230        global $wpdb;
     231        $rows = $wpdb->query( "INSERT INTO $wpdb->users (display_name) VALUES ('Walter Sobchak')" );
     232        $this->assertEquals( 1, $rows );
     233        $this->assertNotEmpty( $wpdb->insert_id );
     234        $d_rows = $wpdb->delete( $wpdb->users, array( 'ID' => $wpdb->insert_id ) );
     235        $this->assertEquals( 1, $d_rows );
     236    }
     237
     238    function test_get_row() {
     239        global $wpdb;
     240        $rows = $wpdb->query( "INSERT INTO $wpdb->users (display_name) VALUES ('Walter Sobchak')" );
     241        $this->assertEquals( 1, $rows );
     242        $this->assertNotEmpty( $wpdb->insert_id );
     243
     244        $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d", $wpdb->insert_id ) );
     245        $this->assertInternalType( 'object', $row );
     246        $this->assertEquals( 'Walter Sobchak', $row->display_name );
     247    }
     248
     249    function test_replace() {
     250        global $wpdb;
     251        $rows1 = $wpdb->insert( $wpdb->users, array( 'display_name' => 'Walter Sobchak' ) );
     252        $this->assertEquals( 1, $rows1 );
     253        $this->assertNotEmpty( $wpdb->insert_id );
     254        $last = $wpdb->insert_id;
     255
     256        $rows2 = $wpdb->replace( $wpdb->users, array( 'ID' => $last, 'display_name' => 'Walter Replace Sobchak' ) );
     257        $this->assertEquals( 2, $rows2 );
     258        $this->assertNotEmpty( $wpdb->insert_id );
     259
     260        $this->assertEquals( $last, $wpdb->insert_id );
     261
     262        $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d", $last ) );
     263        $this->assertEquals( 'Walter Replace Sobchak', $row->display_name );
     264    }
     265
     266    /**
     267     * wpdb::update() requires a WHERE condition.
     268     *
     269     * @ticket 26106
     270     */
     271    function test_empty_where_on_update() {
     272        global $wpdb;
     273        $suppress = $wpdb->suppress_errors( true );
     274        $wpdb->update( $wpdb->posts, array( 'post_name' => 'burrito' ), array() );
     275
     276        $expected1 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito' WHERE ";
     277        $this->assertNotEmpty( $wpdb->last_error );
     278        $this->assertEquals( $expected1, $wpdb->last_query );
     279
     280        $wpdb->update( $wpdb->posts, array( 'post_name' => 'burrito' ), array( 'post_status' => 'taco' ) );
     281
     282        $expected2 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito' WHERE `post_status` = 'taco'";
     283        $this->assertEmpty( $wpdb->last_error );
     284        $this->assertEquals( $expected2, $wpdb->last_query );
     285        $wpdb->suppress_errors( $suppress );
     286    }
     287
     288    /**
     289     * @ticket 21212
     290     */
     291    function data_get_table_from_query() {
     292        $table = 'a_test_table_name';
     293
     294        $queries = array(
     295            // Basic
     296            "SELECT * FROM $table",
     297            "SELECT * FROM `$table`",
     298
     299            "INSERT $table",
     300            "INSERT IGNORE $table",
     301            "INSERT IGNORE INTO $table",
     302            "INSERT INTO $table",
     303            "INSERT LOW_PRIORITY $table",
     304            "INSERT DELAYED $table",
     305            "INSERT HIGH_PRIORITY $table",
     306            "INSERT LOW_PRIORITY IGNORE $table",
     307            "INSERT LOW_PRIORITY INTO $table",
     308            "INSERT LOW_PRIORITY IGNORE INTO $table",
     309
     310            "REPLACE $table",
     311            "REPLACE INTO $table",
     312            "REPLACE LOW_PRIORITY $table",
     313            "REPLACE DELAYED $table",
     314            "REPLACE LOW_PRIORITY INTO $table",
     315
     316            "UPDATE LOW_PRIORITY $table",
     317            "UPDATE LOW_PRIORITY IGNORE $table",
     318
     319            "DELETE $table",
     320            "DELETE IGNORE $table",
     321            "DELETE IGNORE FROM $table",
     322            "DELETE FROM $table",
     323            "DELETE LOW_PRIORITY $table",
     324            "DELETE QUICK $table",
     325            "DELETE IGNORE $table",
     326            "DELETE LOW_PRIORITY FROM $table",
     327
     328            // STATUS
     329            "SHOW TABLE STATUS LIKE '$table'",
     330            "SHOW TABLE STATUS WHERE NAME='$table'",
     331
     332            "SHOW TABLES LIKE '$table'",
     333            "SHOW FULL TABLES LIKE '$table'",
     334            "SHOW TABLES WHERE NAME='$table'",
     335
     336            // Extended
     337            "EXPLAIN SELECT * FROM $table",
     338            "EXPLAIN EXTENDED SELECT * FROM $table",
     339            "EXPLAIN EXTENDED SELECT * FROM `$table`",
     340
     341            "DESCRIBE $table",
     342            "DESC $table",
     343            "EXPLAIN $table",
     344            "HANDLER $table",
     345
     346            "LOCK TABLE $table",
     347            "LOCK TABLES $table",
     348            "UNLOCK TABLE $table",
     349
     350            "RENAME TABLE $table",
     351            "OPTIMIZE TABLE $table",
     352            "BACKUP TABLE $table",
     353            "RESTORE TABLE $table",
     354            "CHECK TABLE $table",
     355            "CHECKSUM TABLE $table",
     356            "ANALYZE TABLE $table",
     357            "REPAIR TABLE $table",
     358
     359            "TRUNCATE $table",
     360            "TRUNCATE TABLE $table",
     361
     362            "CREATE TABLE $table",
     363            "CREATE TEMPORARY TABLE $table",
     364            "CREATE TABLE IF NOT EXISTS $table",
     365
     366            "ALTER TABLE $table",
     367            "ALTER IGNORE TABLE $table",
     368
     369            "DROP TABLE $table",
     370            "DROP TABLE IF EXISTS $table",
     371
     372            "CREATE INDEX foo(bar(20)) ON $table",
     373            "CREATE UNIQUE INDEX foo(bar(20)) ON $table",
     374            "CREATE FULLTEXT INDEX foo(bar(20)) ON $table",
     375            "CREATE SPATIAL INDEX foo(bar(20)) ON $table",
     376
     377            "DROP INDEX foo ON $table",
     378
     379            "LOAD DATA INFILE 'wp.txt' INTO TABLE $table",
     380            "LOAD DATA LOW_PRIORITY INFILE 'wp.txt' INTO TABLE $table",
     381            "LOAD DATA CONCURRENT INFILE 'wp.txt' INTO TABLE $table",
     382            "LOAD DATA LOW_PRIORITY LOCAL INFILE 'wp.txt' INTO TABLE $table",
     383            "LOAD DATA INFILE 'wp.txt' REPLACE INTO TABLE $table",
     384            "LOAD DATA INFILE 'wp.txt' IGNORE INTO TABLE $table",
     385
     386            "GRANT ALL ON TABLE $table",
     387            "REVOKE ALL ON TABLE $table",
     388
     389            "SHOW COLUMNS FROM $table",
     390            "SHOW FULL COLUMNS FROM $table",
     391            "SHOW CREATE TABLE $table",
     392            "SHOW INDEX FROM $table",
     393        );
     394
     395        foreach ( $queries as &$query ) {
     396            $query = array( $query, $table );
     397        }
     398        return $queries;
     399    }
     400
     401    /**
     402     * @dataProvider data_get_table_from_query
     403     * @ticket 21212
     404     */
     405    function test_get_table_from_query( $query, $table ) {
     406        $this->assertEquals( $table, self::$_wpdb->get_table_from_query( $query ) );
     407    }
     408
     409    function data_get_table_from_query_false() {
     410        $table = 'a_test_table_name';
     411        return array(
     412            array( "LOL THIS ISN'T EVEN A QUERY $table" ),
     413        );
     414    }
     415
     416    /**
     417     * @dataProvider data_get_table_from_query_false
     418     * @ticket 21212
     419     */
     420    function test_get_table_from_query_false( $query ) {
     421        $this->assertFalse( self::$_wpdb->get_table_from_query( $query ) );
     422    }
     423
     424    /**
     425     * @ticket 21212
     426     */
     427    function data_process_field_formats() {
     428        $core_db_fields_no_format_specified = array(
     429            array( 'post_content' => 'foo', 'post_parent' => 0 ),
     430            null,
     431            array(
     432                'post_content' => array( 'value' => 'foo', 'format' => '%s' ),
     433                'post_parent' => array( 'value' => 0, 'format' => '%d' ),
     434            )
     435        );
     436
     437        $core_db_fields_formats_specified = array(
     438            array( 'post_content' => 'foo', 'post_parent' => 0 ),
     439            array( '%d', '%s' ), // These override core field_types
     440            array(
     441                'post_content' => array( 'value' => 'foo', 'format' => '%d' ),
     442                'post_parent' => array( 'value' => 0, 'format' => '%s' ),
     443            )
     444        );
     445
     446        $misc_fields_no_format_specified = array(
     447            array( 'this_is_not_a_core_field' => 'foo', 'this_is_not_either' => 0 ),
     448            null,
     449            array(
     450                'this_is_not_a_core_field' => array( 'value' => 'foo', 'format' => '%s' ),
     451                'this_is_not_either' => array( 'value' => 0, 'format' => '%s' ),
     452            )
     453        );
     454
     455        $misc_fields_formats_specified = array(
     456            array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 1.2 ),
     457            array( '%d', '%f' ),
     458            array(
     459                'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ),
     460                'this_is_not_either' => array( 'value' => 1.2, 'format' => '%f' ),
     461            )
     462        );
     463
     464        $misc_fields_insufficient_formats_specified = array(
     465            array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 's', 'nor_this' => 1 ),
     466            array( '%d', '%s' ), // The first format is used for the third
     467            array(
     468                'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ),
     469                'this_is_not_either' => array( 'value' => 's', 'format' => '%s' ),
     470                'nor_this' => array( 'value' => 1, 'format' => '%d' ),
     471            )
     472        );
     473
     474        $vars = get_defined_vars();
     475        // Push the variable name onto the end for assertSame $message
     476        foreach ( $vars as $var_name => $var ) {
     477            $vars[ $var_name ][] = $var_name;
     478        }
     479        return array_values( $vars );
     480    }
     481
     482    /**
     483     * @dataProvider data_process_field_formats
     484     * @ticket 21212
     485     */
     486    function test_process_field_formats( $data, $format, $expected, $message ) {
     487        $actual = self::$_wpdb->process_field_formats( $data, $format );
     488        $this->assertSame( $expected, $actual, $message );
     489    }
     490
     491    /**
     492     * @ticket 21212
     493     */
     494    function test_process_fields() {
     495        global $wpdb;
     496
     497        if ( $wpdb->charset ) {
     498            $expected_charset = $wpdb->charset;
     499        } else {
     500            $expected_charset = $wpdb->get_col_charset( $wpdb->posts, 'post_content' );
     501        }
     502
     503        if ( ! in_array( $expected_charset, array( 'utf8', 'utf8mb4', 'latin1' ) ) ) {
     504            $this->markTestSkipped( "This test only works with utf8, utf8mb4 or latin1 character sets" );
     505        }
     506
     507        $data = array( 'post_content' => '¡foo foo foo!' );
     508        $expected = array(
     509            'post_content' => array(
     510                'value' => '¡foo foo foo!',
     511                'format' => '%s',
     512                'charset' => $expected_charset,
     513                'ascii' => false,
     514            )
     515        );
     516
     517        $this->assertSame( $expected, self::$_wpdb->process_fields( $wpdb->posts, $data, null ) );
     518    }
     519
     520    /**
     521     * @ticket 21212
     522     * @depends test_process_fields
     523     */
     524    function test_process_fields_on_nonexistent_table( $data ) {
     525        self::$_wpdb->suppress_errors( true );
     526        $data = array( 'post_content' => '¡foo foo foo!' );
     527        $this->assertFalse( self::$_wpdb->process_fields( 'nonexistent_table', $data, null ) );
     528        self::$_wpdb->suppress_errors( false );
     529    }
     530
     531    /**
     532     * @ticket 21212
     533     */
     534    function test_pre_get_table_charset_filter() {
     535        add_filter( 'pre_get_table_charset', array( $this, 'filter_pre_get_table_charset' ), 10, 2 );
     536        $charset = self::$_wpdb->get_table_charset( 'some_table' );
     537        remove_filter( 'pre_get_table_charset', array( $this, 'filter_pre_get_table_charset' ), 10 );
     538
     539        $this->assertEquals( $charset, 'fake_charset' );
     540    }
     541    function filter_pre_get_table_charset( $charset, $table ) {
     542        return 'fake_charset';
     543    }
     544
     545    /**
     546     * @ticket 21212
     547     */
     548    function test_pre_get_col_charset_filter() {
     549        add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset' ), 10, 3 );
     550        $charset = self::$_wpdb->get_col_charset( 'some_table', 'some_col' );
     551        remove_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset' ), 10 );
     552
     553        $this->assertEquals( $charset, 'fake_col_charset' );
     554    }
     555    function filter_pre_get_col_charset( $charset, $table, $column ) {
     556        return 'fake_col_charset';
     557    }
    129558}
     559
  • branches/3.8/tests/phpunit/tests/post.php

    r25975 r32186  
    830830        return $counts;
    831831    }
     832
     833    /**
     834     * @ticket 21212
     835     */
     836    function test_utf8mb3_post_saves_with_emoji() {
     837        global $wpdb;
     838        $_wpdb = new wpdb_exposed_methods_for_testing();
     839
     840        if ( 'utf8' !== $_wpdb->get_col_charset( $wpdb->posts, 'post_title' ) ) {
     841            $this->markTestSkipped( 'This test is only useful with the utf8 character set' );
     842        }
     843
     844        require_once( ABSPATH . '/wp-admin/includes/post.php' );
     845
     846        $post_id = $this->factory->post->create();
     847
     848        $data = array(
     849            'post_ID'      => $post_id,
     850            'post_title'   => "foo\xf0\x9f\x98\x88bar",
     851            'post_content' => "foo\xf0\x9f\x98\x8ebaz",
     852            'post_excerpt' => "foo\xf0\x9f\x98\x90bat"
     853        );
     854
     855        $expected = array(
     856            'post_title'   => "foobar",
     857            'post_content' => "foobaz",
     858            'post_excerpt' => "foobat"
     859        );
     860
     861        edit_post( $data );
     862
     863        $post = get_post( $post_id );
     864
     865        foreach( $expected as $field => $value ) {
     866            $this->assertEquals( $post->$field, $value );
     867        }
     868    }
    832869}
Note: See TracChangeset for help on using the changeset viewer.