Changeset 42060
- Timestamp:
- 10/31/2017 12:43:36 PM (7 years ago)
- Location:
- branches/4.5
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.5
- Property svn:mergeinfo changed
/trunk merged: 41662,42056
- Property svn:mergeinfo changed
-
branches/4.5/src/wp-includes/post.php
r37165 r42060 4233 4233 $page_path = str_replace('%20', ' ', $page_path); 4234 4234 $parts = explode( '/', trim( $page_path, '/' ) ); 4235 $parts = esc_sql( $parts );4236 4235 $parts = array_map( 'sanitize_title_for_query', $parts ); 4237 4238 $in_string = "'" . implode( "','", $parts ) . "'"; 4236 $escaped_parts = esc_sql( $parts ); 4237 4238 $in_string = "'" . implode( "','", $escaped_parts ) . "'"; 4239 4239 4240 4240 if ( is_array( $post_type ) ) { -
branches/4.5/src/wp-includes/wp-db.php
r41500 r42060 1125 1125 if ( $this->dbh ) { 1126 1126 if ( $this->use_mysqli ) { 1127 returnmysqli_real_escape_string( $this->dbh, $string );1127 $escaped = mysqli_real_escape_string( $this->dbh, $string ); 1128 1128 } else { 1129 return mysql_real_escape_string( $string, $this->dbh ); 1130 } 1131 } 1132 1133 $class = get_class( $this ); 1134 if ( function_exists( '__' ) ) { 1135 /* translators: %s: database access abstraction class, usually wpdb or a class extending wpdb */ 1136 _doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), E_USER_NOTICE ); 1129 $escaped = mysql_real_escape_string( $string, $this->dbh ); 1130 } 1137 1131 } else { 1138 _doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), E_USER_NOTICE ); 1139 } 1140 return addslashes( $string ); 1132 $class = get_class( $this ); 1133 if ( function_exists( '__' ) ) { 1134 /* translators: %s: database access abstraction class, usually wpdb or a class extending wpdb */ 1135 _doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), '3.6.0' ); 1136 } else { 1137 _doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), '3.6.0' ); 1138 } 1139 $escaped = addslashes( $string ); 1140 } 1141 1142 return $this->add_placeholder_escape( $escaped ); 1141 1143 } 1142 1144 … … 1213 1215 * Prepares a SQL query for safe execution. Uses sprintf()-like syntax. 1214 1216 * 1215 * The following directives can be used in the query formatstring:1217 * The following placeholders can be used in the query string: 1216 1218 * %d (integer) 1217 1219 * %f (float) 1218 1220 * %s (string) 1219 * %% (literal percentage sign - no argument needed) 1220 * 1221 * All of %d, %f, and %s are to be left unquoted in the query string and they need an argument passed for them. 1222 * Literals (%) as parts of the query must be properly written as %%. 1223 * 1224 * This function only supports a small subset of the sprintf syntax; it only supports %d (integer), %f (float), and %s (string). 1225 * Does not support sign, padding, alignment, width or precision specifiers. 1226 * Does not support argument numbering/swapping. 1227 * 1228 * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}. 1229 * 1230 * Both %d and %s should be left unquoted in the query string. 1231 * 1232 * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", 'foo', 1337 ) 1233 * wpdb::prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); 1234 * 1235 * @link http://php.net/sprintf Description of syntax. 1221 * 1222 * All placeholders MUST be left unquoted in the query string. A corresponding argument MUST be passed for each placeholder. 1223 * 1224 * For compatibility with old behavior, numbered or formatted string placeholders (eg, %1$s, %5s) will not have quotes 1225 * added by this function, so should be passed with appropriate quotes around them for your usage. 1226 * 1227 * Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards (for example, 1228 * to use in LIKE syntax) must be passed via a substitution argument containing the complete LIKE string, these 1229 * cannot be inserted directly in the query string. Also see {@see esc_like()}. 1230 * 1231 * Arguments may be passed as individual arguments to the method, or as a single array containing all arguments. A combination 1232 * of the two is not supported. 1233 * 1234 * Examples: 1235 * $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) ); 1236 * $wpdb->prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); 1237 * 1238 * @link https://secure.php.net/sprintf Description of syntax. 1236 1239 * @since 2.3.0 1237 1240 * 1238 1241 * @param string $query Query statement with sprintf()-like placeholders 1239 * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like 1240 * {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if 1241 * being called like {@link http://php.net/sprintf sprintf()}. 1242 * @param mixed $args,... further variables to substitute into the query's placeholders if being called like 1243 * {@link http://php.net/sprintf sprintf()}. 1242 * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called with an array of arguments, 1243 * or the first variable to substitute into the query's placeholders if being called with individual arguments. 1244 * @param mixed $args,... further variables to substitute into the query's placeholders if being called wih individual arguments. 1244 1245 * @return string|void Sanitized query string, if there is a query to prepare. 1245 1246 */ 1246 1247 public function prepare( $query, $args ) { 1247 if ( is_null( $query ) ) 1248 if ( is_null( $query ) ) { 1248 1249 return; 1250 } 1249 1251 1250 1252 // This is not meant to be foolproof -- but it will catch obviously incorrect usage. 1251 1253 if ( strpos( $query, '%' ) === false ) { 1252 _doing_it_wrong( 'wpdb::prepare', sprintf( __( 'The query argument of %s must have a placeholder.' ), 'wpdb::prepare()' ), '3.9' ); 1254 wp_load_translations_early(); 1255 _doing_it_wrong( 'wpdb::prepare', sprintf( __( 'The query argument of %s must have a placeholder.' ), 'wpdb::prepare()' ), '3.9.0' ); 1253 1256 } 1254 1257 … … 1256 1259 array_shift( $args ); 1257 1260 1258 // If args were passed as an array (as in vsprintf), move them up 1261 // If args were passed as an array (as in vsprintf), move them up. 1262 $passed_as_array = false; 1259 1263 if ( is_array( $args[0] ) && count( $args ) == 1 ) { 1264 $passed_as_array = true; 1260 1265 $args = $args[0]; 1261 1266 } … … 1263 1268 foreach ( $args as $arg ) { 1264 1269 if ( ! is_scalar( $arg ) && ! is_null( $arg ) ) { 1265 _doing_it_wrong( 'wpdb::prepare', sprintf( 'Unsupported value type (%s).', gettype( $arg ) ), '4.5.10' ); 1266 } 1267 } 1268 1269 $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it 1270 $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting 1271 $query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware 1272 $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s 1273 $query = preg_replace( '/%(?:%|$|([^dsF]))/', '%%\\1', $query ); // escape any unescaped percents 1270 wp_load_translations_early(); 1271 _doing_it_wrong( 'wpdb::prepare', sprintf( __( 'Unsupported value type (%s).' ), gettype( $arg ) ), '4.8.2' ); 1272 } 1273 } 1274 1275 /* 1276 * Specify the formatting allowed in a placeholder. The following are allowed: 1277 * 1278 * - Sign specifier. eg, $+d 1279 * - Numbered placeholders. eg, %1$s 1280 * - Padding specifier, including custom padding characters. eg, %05s, %'#5s 1281 * - Alignment specifier. eg, %05-s 1282 * - Precision specifier. eg, %.2f 1283 */ 1284 $allowed_format = '(?:[1-9][0-9]*[$])?[-+0-9]*(?: |0|\'.)?[-+0-9]*(?:\.[0-9]+)?'; 1285 1286 /* 1287 * If a %s placeholder already has quotes around it, removing the existing quotes and re-inserting them 1288 * ensures the quotes are consistent. 1289 * 1290 * For backwards compatibility, this is only applied to %s, and not to placeholders like %1$s, which are frequently 1291 * used in the middle of longer strings, or as table name placeholders. 1292 */ 1293 $query = str_replace( "'%s'", '%s', $query ); // Strip any existing single quotes. 1294 $query = str_replace( '"%s"', '%s', $query ); // Strip any existing double quotes. 1295 $query = preg_replace( '/(?<!%)%s/', "'%s'", $query ); // Quote the strings, avoiding escaped strings like %%s. 1296 1297 $query = preg_replace( "/(?<!%)(%($allowed_format)?f)/" , '%\\2F', $query ); // Force floats to be locale unaware. 1298 1299 $query = preg_replace( "/%(?:%|$|(?!($allowed_format)?[sdF]))/", '%%\\1', $query ); // Escape any unescaped percents. 1300 1301 // Count the number of valid placeholders in the query. 1302 $placeholders = preg_match_all( "/(^|[^%]|(%%)+)%($allowed_format)?[sdF]/", $query, $matches ); 1303 1304 if ( count( $args ) !== $placeholders ) { 1305 if ( 1 === $placeholders && $passed_as_array ) { 1306 // If the passed query only expected one argument, but the wrong number of arguments were sent as an array, bail. 1307 wp_load_translations_early(); 1308 _doing_it_wrong( 'wpdb::prepare', __( 'The query only expected one placeholder, but an array of multiple placeholders was sent.' ), '4.9.0' ); 1309 1310 return; 1311 } else { 1312 /* 1313 * If we don't have the right number of placeholders, but they were passed as individual arguments, 1314 * or we were expecting multiple arguments in an array, throw a warning. 1315 */ 1316 wp_load_translations_early(); 1317 _doing_it_wrong( 'wpdb::prepare', 1318 /* translators: 1: number of placeholders, 2: number of arguments passed */ 1319 sprintf( __( 'The query does not contain the correct number of placeholders (%1$d) for the number of arguments passed (%2$d).' ), 1320 $placeholders, 1321 count( $args ) ), 1322 '4.8.3' 1323 ); 1324 } 1325 } 1326 1274 1327 array_walk( $args, array( $this, 'escape_by_ref' ) ); 1275 return @vsprintf( $query, $args ); 1328 $query = @vsprintf( $query, $args ); 1329 1330 return $this->add_placeholder_escape( $query ); 1276 1331 } 1277 1332 … … 1828 1883 $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); 1829 1884 } 1885 } 1886 1887 /** 1888 * Generates and returns a placeholder escape string for use in queries returned by ::prepare(). 1889 * 1890 * @since 4.8.3 1891 * 1892 * @return string String to escape placeholders. 1893 */ 1894 public function placeholder_escape() { 1895 static $placeholder; 1896 1897 if ( ! $placeholder ) { 1898 // If ext/hash is not present, compat.php's hash_hmac() does not support sha256. 1899 $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; 1900 // Old WP installs may not have AUTH_SALT defined. 1901 $salt = defined( 'AUTH_SALT' ) ? AUTH_SALT : rand(); 1902 1903 $placeholder = '{' . hash_hmac( $algo, uniqid( $salt, true ), $salt ) . '}'; 1904 } 1905 1906 /* 1907 * Add the filter to remove the placeholder escaper. Uses priority 0, so that anything 1908 * else attached to this filter will recieve the query with the placeholder string removed. 1909 */ 1910 if ( ! has_filter( 'query', array( $this, 'remove_placeholder_escape' ) ) ) { 1911 add_filter( 'query', array( $this, 'remove_placeholder_escape' ), 0 ); 1912 } 1913 1914 return $placeholder; 1915 } 1916 1917 /** 1918 * Adds a placeholder escape string, to escape anything that resembles a printf() placeholder. 1919 * 1920 * @since 4.8.3 1921 * 1922 * @param string $query The query to escape. 1923 * @return string The query with the placeholder escape string inserted where necessary. 1924 */ 1925 public function add_placeholder_escape( $query ) { 1926 /* 1927 * To prevent returning anything that even vaguely resembles a placeholder, 1928 * we clobber every % we can find. 1929 */ 1930 return str_replace( '%', $this->placeholder_escape(), $query ); 1931 } 1932 1933 /** 1934 * Removes the placeholder escape strings from a query. 1935 * 1936 * @since 4.8.3 1937 * 1938 * @param string $query The query from which the placeholder will be removed. 1939 * @return string The query with the placeholder removed. 1940 */ 1941 public function remove_placeholder_escape( $query ) { 1942 return str_replace( $this->placeholder_escape(), '%', $query ); 1830 1943 } 1831 1944 -
branches/4.5/tests/phpunit/tests/comment/query.php
r36486 r42060 1223 1223 */ 1224 1224 public function test_search_int_0_should_not_be_ignored() { 1225 global $wpdb; 1225 1226 $q = new WP_Comment_Query(); 1226 1227 $q->query( array( 1227 1228 'search' => 0, 1228 1229 ) ); 1229 $this->assertContains( "comment_author LIKE '%0%'", $ q->request);1230 $this->assertContains( "comment_author LIKE '%0%'", $wpdb->remove_placeholder_escape( $q->request ) ); 1230 1231 } 1231 1232 … … 1234 1235 */ 1235 1236 public function test_search_string_0_should_not_be_ignored() { 1237 global $wpdb; 1236 1238 $q = new WP_Comment_Query(); 1237 1239 $q->query( array( 1238 1240 'search' => '0', 1239 1241 ) ); 1240 $this->assertContains( "comment_author LIKE '%0%'", $ q->request);1242 $this->assertContains( "comment_author LIKE '%0%'", $wpdb->remove_placeholder_escape( $q->request ) ); 1241 1243 } 1242 1244 -
branches/4.5/tests/phpunit/tests/date/query.php
r35242 r42060 513 513 */ 514 514 public function test_build_time_query_should_not_discard_hour_0() { 515 global $wpdb; 515 516 $q = new WP_Date_Query( array() ); 516 517 517 518 $found = $q->build_time_query( 'post_date', '=', 0, 10 ); 518 519 519 $this->assertContains( '%H', $ found);520 $this->assertContains( '%H', $wpdb->remove_placeholder_escape( $found ) ); 520 521 } 521 522 … … 613 614 614 615 public function test_build_time_query_hour_minute() { 616 global $wpdb; 615 617 $q = new WP_Date_Query( array() ); 616 618 … … 619 621 // $compare value is floating point - use regex to account for 620 622 // varying precision on different PHP installations 621 $this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i' \) = 5\.150*/", $ found);623 $this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i' \) = 5\.150*/", $wpdb->remove_placeholder_escape( $found ) ); 622 624 } 623 625 624 626 public function test_build_time_query_hour_minute_second() { 627 global $wpdb; 625 628 $q = new WP_Date_Query( array() ); 626 629 … … 629 632 // $compare value is floating point - use regex to account for 630 633 // varying precision on different PHP installations 631 $this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i%s' \) = 5\.15350*/", $ found);634 $this->assertRegExp( "/DATE_FORMAT\( post_date, '%H\.%i%s' \) = 5\.15350*/", $wpdb->remove_placeholder_escape( $found ) ); 632 635 } 633 636 634 637 public function test_build_time_query_minute_second() { 638 global $wpdb; 635 639 $q = new WP_Date_Query( array() ); 636 640 … … 639 643 // $compare value is floating point - use regex to account for 640 644 // varying precision on different PHP installations 641 $this->assertRegExp( "/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $ found);645 $this->assertRegExp( "/DATE_FORMAT\( post_date, '0\.%i%s' \) = 0\.15350*/", $wpdb->remove_placeholder_escape( $found ) ); 642 646 } 643 647 -
branches/4.5/tests/phpunit/tests/db.php
r41500 r42060 265 265 global $wpdb; 266 266 $sql = $wpdb->prepare( "UPDATE test_table SET string_column = '%%f is a float, %%d is an int %d, %%s is a string', field = %s", 3, '4' ); 267 $this->assertContains( $wpdb->placeholder_escape(), $sql ); 268 269 $sql = $wpdb->remove_placeholder_escape( $sql ); 267 270 $this->assertEquals( "UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql ); 268 271 } … … 370 373 } 371 374 372 373 375 function test_prepare_vsprintf() { 376 global $wpdb; 374 377 375 378 $prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, "admin" ) ); … … 388 391 $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( array( 1 ), "admin" ) ); 389 392 $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared ); 390 } 393 } 394 395 /** 396 * @ticket 42040 397 * @dataProvider data_prepare_incorrect_arg_count 398 * @expectedIncorrectUsage wpdb::prepare 399 */ 400 public function test_prepare_incorrect_arg_count( $query, $args, $expected ) { 401 global $wpdb; 402 403 // $query is the first argument to be passed to wpdb::prepare() 404 array_unshift( $args, $query ); 405 406 $prepared = @call_user_func_array( array( $wpdb, 'prepare' ), $args ); 407 $this->assertEquals( $expected, $prepared ); 408 } 409 410 public function data_prepare_incorrect_arg_count() { 411 global $wpdb; 412 413 return array( 414 array( 415 "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", // Query 416 array( 1, "admin", "extra-arg" ), // ::prepare() args, to be passed via call_user_func_array 417 "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", // Expected output 418 ), 419 array( 420 "SELECT * FROM $wpdb->users WHERE id = %%%d AND user_login = %s", 421 array( 1 ), 422 false, 423 ), 424 array( 425 "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 426 array( array( 1, "admin", "extra-arg" ) ), 427 "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", 428 ), 429 array( 430 "SELECT * FROM $wpdb->users WHERE id = %d AND %% AND user_login = %s", 431 array( 1, "admin", "extra-arg" ), 432 "SELECT * FROM $wpdb->users WHERE id = 1 AND {$wpdb->placeholder_escape()} AND user_login = 'admin'", 433 ), 434 array( 435 "SELECT * FROM $wpdb->users WHERE id = %%%d AND %F AND %f AND user_login = %s", 436 array( 1, 2.3, "4.5", "admin", "extra-arg" ), 437 "SELECT * FROM $wpdb->users WHERE id = {$wpdb->placeholder_escape()}1 AND 2.300000 AND 4.500000 AND user_login = 'admin'", 438 ), 439 array( 440 "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 441 array( array( 1 ), "admin", "extra-arg" ), 442 "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", 443 ), 444 array( 445 "SELECT * FROM $wpdb->users WHERE id = %d and user_nicename = %s and user_status = %d and user_login = %s", 446 array( 1, "admin", 0 ), 447 '', 448 ), 449 array( 450 "SELECT * FROM $wpdb->users WHERE id = %d and user_nicename = %s and user_status = %d and user_login = %s", 451 array( array( 1, "admin", 0 ) ), 452 '', 453 ), 454 array( 455 "SELECT * FROM $wpdb->users WHERE id = %d and %% and user_login = %s and user_status = %d and user_login = %s", 456 array( 1, "admin", "extra-arg" ), 457 '', 458 ), 459 ); 460 } 391 461 392 462 function test_db_version() { … … 999 1069 1000 1070 /** 1001 * 1002 */ 1003 function test_prepare_with_unescaped_percents() { 1004 global $wpdb; 1005 1006 $sql = $wpdb->prepare( '%d %1$d %%% %', 1 ); 1007 $this->assertEquals( '1 %1$d %% %', $sql ); 1071 * @dataProvider data_prepare_with_placeholders 1072 */ 1073 function test_prepare_with_placeholders_and_individual_args( $sql, $values, $incorrect_usage, $expected) { 1074 global $wpdb; 1075 1076 if ( $incorrect_usage ) { 1077 $this->setExpectedIncorrectUsage( 'wpdb::prepare' ); 1078 } 1079 1080 if ( ! is_array( $values ) ) { 1081 $values = array( $values ); 1082 } 1083 1084 array_unshift( $values, $sql ); 1085 1086 $sql = call_user_func_array( array( $wpdb, 'prepare' ), $values ); 1087 $this->assertEquals( $expected, $sql ); 1088 } 1089 1090 /** 1091 * @dataProvider data_prepare_with_placeholders 1092 */ 1093 function test_prepare_with_placeholders_and_array_args( $sql, $values, $incorrect_usage, $expected) { 1094 global $wpdb; 1095 1096 if ( $incorrect_usage ) { 1097 $this->setExpectedIncorrectUsage( 'wpdb::prepare' ); 1098 } 1099 1100 if ( ! is_array( $values ) ) { 1101 $values = array( $values ); 1102 } 1103 1104 $sql = call_user_func_array( array( $wpdb, 'prepare' ), array( $sql, $values ) ); 1105 $this->assertEquals( $expected, $sql ); 1106 } 1107 1108 function data_prepare_with_placeholders() { 1109 global $wpdb; 1110 1111 return array( 1112 array( 1113 '%5s', // SQL to prepare 1114 'foo', // Value to insert in the SQL 1115 false, // Whether to expect an incorrect usage error or not 1116 ' foo', // Expected output 1117 ), 1118 array( 1119 '%1$d %%% % %%1$d%% %%%1$d%%', 1120 1, 1121 true, 1122 "1 {$wpdb->placeholder_escape()}{$wpdb->placeholder_escape()} {$wpdb->placeholder_escape()} {$wpdb->placeholder_escape()}1\$d{$wpdb->placeholder_escape()} {$wpdb->placeholder_escape()}1{$wpdb->placeholder_escape()}", 1123 ), 1124 array( 1125 '%-5s', 1126 'foo', 1127 false, 1128 'foo ', 1129 ), 1130 array( 1131 '%05s', 1132 'foo', 1133 false, 1134 '00foo', 1135 ), 1136 array( 1137 "%'#5s", 1138 'foo', 1139 false, 1140 '##foo', 1141 ), 1142 array( 1143 '%.3s', 1144 'foobar', 1145 false, 1146 'foo', 1147 ), 1148 array( 1149 '%.3f', 1150 5.123456, 1151 false, 1152 '5.123', 1153 ), 1154 array( 1155 '%.3f', 1156 5.12, 1157 false, 1158 '5.120', 1159 ), 1160 array( 1161 '%s', 1162 ' %s ', 1163 false, 1164 "' {$wpdb->placeholder_escape()}s '", 1165 ), 1166 array( 1167 '%1$s', 1168 ' %s ', 1169 false, 1170 " {$wpdb->placeholder_escape()}s ", 1171 ), 1172 array( 1173 '%1$s', 1174 ' %1$s ', 1175 false, 1176 " {$wpdb->placeholder_escape()}1\$s ", 1177 ), 1178 array( 1179 '%d %1$d %%% %', 1180 1, 1181 true, 1182 "1 1 {$wpdb->placeholder_escape()}{$wpdb->placeholder_escape()} {$wpdb->placeholder_escape()}", 1183 ), 1184 array( 1185 '%d %2$s', 1186 array( 1, 'hello' ), 1187 false, 1188 "1 hello", 1189 ), 1190 array( 1191 "'%s'", 1192 'hello', 1193 false, 1194 "'hello'", 1195 ), 1196 array( 1197 '"%s"', 1198 'hello', 1199 false, 1200 "'hello'", 1201 ), 1202 array( 1203 "%s '%1\$s'", 1204 'hello', 1205 true, 1206 "'hello' 'hello'", 1207 ), 1208 array( 1209 "%s '%1\$s'", 1210 'hello', 1211 true, 1212 "'hello' 'hello'", 1213 ), 1214 array( 1215 '%s "%1$s"', 1216 'hello', 1217 true, 1218 "'hello' \"hello\"", 1219 ), 1220 array( 1221 "%%s %%'%1\$s'", 1222 'hello', 1223 false, 1224 "{$wpdb->placeholder_escape()}s {$wpdb->placeholder_escape()}'hello'", 1225 ), 1226 array( 1227 '%%s %%"%1$s"', 1228 'hello', 1229 false, 1230 "{$wpdb->placeholder_escape()}s {$wpdb->placeholder_escape()}\"hello\"", 1231 ), 1232 array( 1233 '%s', 1234 ' % s ', 1235 false, 1236 "' {$wpdb->placeholder_escape()} s '", 1237 ), 1238 array( 1239 '%%f %%"%1$f"', 1240 3, 1241 false, 1242 "{$wpdb->placeholder_escape()}f {$wpdb->placeholder_escape()}\"3.000000\"", 1243 ), 1244 array( 1245 'WHERE second=\'%2$s\' AND first=\'%1$s\'', 1246 array( 'first arg', 'second arg' ), 1247 false, 1248 "WHERE second='second arg' AND first='first arg'", 1249 ), 1250 array( 1251 'WHERE second=%2$d AND first=%1$d', 1252 array( 1, 2 ), 1253 false, 1254 "WHERE second=2 AND first=1", 1255 ), 1256 array( 1257 "'%'%%s", 1258 'hello', 1259 true, 1260 "'{$wpdb->placeholder_escape()}'{$wpdb->placeholder_escape()}s", 1261 ), 1262 array( 1263 "'%'%%s%s", 1264 'hello', 1265 false, 1266 "'{$wpdb->placeholder_escape()}'{$wpdb->placeholder_escape()}s'hello'", 1267 ), 1268 array( 1269 "'%'%%s %s", 1270 'hello', 1271 false, 1272 "'{$wpdb->placeholder_escape()}'{$wpdb->placeholder_escape()}s 'hello'", 1273 ), 1274 array( 1275 "'%-'#5s' '%'#-+-5s'", 1276 array( 'hello', 'foo' ), 1277 false, 1278 "'hello' 'foo##'", 1279 ), 1280 ); 1281 } 1282 1283 /** 1284 * @dataProvider data_escape_and_prepare 1285 */ 1286 function test_escape_and_prepare( $escape, $sql, $values, $incorrect_usage, $expected ) { 1287 global $wpdb; 1288 1289 if ( $incorrect_usage ) { 1290 $this->setExpectedIncorrectUsage( 'wpdb::prepare' ); 1291 } 1292 1293 $escape = esc_sql( $escape ); 1294 1295 $sql = str_replace( '{ESCAPE}', $escape, $sql ); 1296 1297 $actual = $wpdb->prepare( $sql, $values ); 1298 1299 $this->assertEquals( $expected, $actual ); 1300 } 1301 1302 function data_escape_and_prepare() { 1303 global $wpdb; 1304 return array( 1305 array( 1306 '%s', // String to pass through esc_url() 1307 ' {ESCAPE} ', // Query to insert the output of esc_url() into, replacing "{ESCAPE}" 1308 'foo', // Data to send to prepare() 1309 true, // Whether to expect an incorrect usage error or not 1310 " {$wpdb->placeholder_escape()}s ", // Expected output 1311 ), 1312 array( 1313 'foo%sbar', 1314 "SELECT * FROM bar WHERE foo='{ESCAPE}' OR baz=%s", 1315 array( ' SQLi -- -', 'pewpewpew' ), 1316 true, 1317 null, 1318 ), 1319 array( 1320 '%s', 1321 ' %s {ESCAPE} ', 1322 'foo', 1323 false, 1324 " 'foo' {$wpdb->placeholder_escape()}s ", 1325 ), 1326 ); 1327 } 1328 1329 /** 1330 * @expectedIncorrectUsage wpdb::prepare 1331 */ 1332 function test_double_prepare() { 1333 global $wpdb; 1334 1335 $part = $wpdb->prepare( ' AND meta_value = %s', ' %s ' ); 1336 $this->assertNotContains( '%s', $part ); 1337 $query = $wpdb->prepare( 'SELECT * FROM {$wpdb->postmeta} WHERE meta_key = %s $part', array( 'foo', 'bar' ) ); 1338 $this->assertNull( $query ); 1339 } 1340 1341 function test_prepare_numeric_placeholders_float_args() { 1342 global $wpdb; 1343 1344 $actual = $wpdb->prepare( 1345 'WHERE second=%2$f AND first=%1$f', 1346 1.1, 1347 2.2 1348 ); 1349 1350 /* Floats can be right padded, need to assert differently */ 1351 $this->assertContains( ' first=1.1', $actual ); 1352 $this->assertContains( ' second=2.2', $actual ); 1353 } 1354 1355 function test_prepare_numeric_placeholders_float_array() { 1356 global $wpdb; 1357 1358 $actual = $wpdb->prepare( 1359 'WHERE second=%2$f AND first=%1$f', 1360 array( 1.1, 2.2 ) 1361 ); 1362 1363 /* Floats can be right padded, need to assert differently */ 1364 $this->assertContains( ' first=1.1', $actual ); 1365 $this->assertContains( ' second=2.2', $actual ); 1366 } 1367 1368 function test_query_unescapes_placeholders() { 1369 global $wpdb; 1370 1371 $value = ' %s '; 1372 1373 $wpdb->query( "CREATE TABLE {$wpdb->prefix}test_placeholder( a VARCHAR(100) );" ); 1374 $sql = $wpdb->prepare( "INSERT INTO {$wpdb->prefix}test_placeholder VALUES(%s)", $value ); 1375 $wpdb->query( $sql ); 1376 1377 $actual = $wpdb->get_var( "SELECT a FROM {$wpdb->prefix}test_placeholder" ); 1378 1379 $wpdb->query( "DROP TABLE {$wpdb->prefix}test_placeholder" ); 1380 1381 $this->assertNotContains( '%s', $sql ); 1382 $this->assertEquals( $value, $actual ); 1383 } 1384 1385 function test_esc_sql_with_unsupported_placeholder_type() { 1386 global $wpdb; 1387 1388 $sql = $wpdb->prepare( ' %s %1$c ', 'foo' ); 1389 $sql = $wpdb->prepare( " $sql %s ", 'foo' ); 1390 1391 $this->assertEquals( " 'foo' {$wpdb->placeholder_escape()}1\$c 'foo' ", $sql ); 1008 1392 } 1009 1393 } -
branches/4.5/tests/phpunit/tests/term/meta.php
r36567 r42060 304 304 register_taxonomy( 'wptests_tax', 'post' ); 305 305 $t1 = wp_insert_term( 'Foo', 'wptests_tax' ); 306 add_term_meta( $t1 , 'foo', 'bar' );306 add_term_meta( $t1['term_id'], 'foo', 'bar' ); 307 307 308 308 register_taxonomy( 'wptests_tax_2', 'post' ); -
branches/4.5/wp-tests-config-sample.php
r36372 r42060 31 31 define( 'DB_COLLATE', '' ); 32 32 33 /**#@+ 34 * Authentication Unique Keys and Salts. 35 * 36 * Change these to different unique phrases! 37 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service} 38 */ 39 define('AUTH_KEY', 'put your unique phrase here'); 40 define('SECURE_AUTH_KEY', 'put your unique phrase here'); 41 define('LOGGED_IN_KEY', 'put your unique phrase here'); 42 define('NONCE_KEY', 'put your unique phrase here'); 43 define('AUTH_SALT', 'put your unique phrase here'); 44 define('SECURE_AUTH_SALT', 'put your unique phrase here'); 45 define('LOGGED_IN_SALT', 'put your unique phrase here'); 46 define('NONCE_SALT', 'put your unique phrase here'); 47 33 48 $table_prefix = 'wptests_'; // Only numbers, letters, and underscores please! 34 49
Note: See TracChangeset
for help on using the changeset viewer.