Changeset 42343 for trunk/tests/phpunit/includes/utils.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/includes/utils.php
r41289 r42343 3 3 // misc help functions and utilities 4 4 5 function rand_str( $len=32) {6 return substr( md5(uniqid(rand())), 0, $len);5 function rand_str( $len = 32 ) { 6 return substr( md5( uniqid( rand() ) ), 0, $len ); 7 7 } 8 8 9 9 function rand_long_str( $length ) { 10 $chars = 'abcdefghijklmnopqrstuvwxyz';10 $chars = 'abcdefghijklmnopqrstuvwxyz'; 11 11 $string = ''; 12 12 13 13 for ( $i = 0; $i < $length; $i++ ) { 14 $rand = rand( 0, strlen( $chars ) - 1 );14 $rand = rand( 0, strlen( $chars ) - 1 ); 15 15 $string .= substr( $chars, $rand, 1 ); 16 16 } … … 20 20 21 21 // strip leading and trailing whitespace from each line in the string 22 function strip_ws( $txt) {23 $lines = explode("\n", $txt);22 function strip_ws( $txt ) { 23 $lines = explode( "\n", $txt ); 24 24 $result = array(); 25 foreach ($lines as $line) 26 if (trim($line)) 27 $result[] = trim($line); 28 29 return trim(join("\n", $result)); 25 foreach ( $lines as $line ) { 26 if ( trim( $line ) ) { 27 $result[] = trim( $line ); 28 } 29 } 30 31 return trim( join( "\n", $result ) ); 30 32 } 31 33 … … 51 53 52 54 function current_filter() { 53 if ( is_callable('current_filter'))55 if ( is_callable( 'current_filter' ) ) { 54 56 return current_filter(); 57 } 55 58 global $wp_actions; 56 return end($wp_actions); 57 } 58 59 function action($arg) { 60 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 61 $args = func_get_args(); 62 $this->events[] = array('action' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args); 59 return end( $wp_actions ); 60 } 61 62 function action( $arg ) { 63 if ( $this->debug ) { 64 dmp( __FUNCTION__, $this->current_filter() ); 65 } 66 $args = func_get_args(); 67 $this->events[] = array( 68 'action' => __FUNCTION__, 69 'tag' => $this->current_filter(), 70 'args' => $args, 71 ); 63 72 return $arg; 64 73 } 65 74 66 function action2($arg) { 67 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 68 69 $args = func_get_args(); 70 $this->events[] = array('action' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args); 75 function action2( $arg ) { 76 if ( $this->debug ) { 77 dmp( __FUNCTION__, $this->current_filter() ); 78 } 79 80 $args = func_get_args(); 81 $this->events[] = array( 82 'action' => __FUNCTION__, 83 'tag' => $this->current_filter(), 84 'args' => $args, 85 ); 71 86 return $arg; 72 87 } 73 88 74 function filter($arg) { 75 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 76 77 $args = func_get_args(); 78 $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args); 89 function filter( $arg ) { 90 if ( $this->debug ) { 91 dmp( __FUNCTION__, $this->current_filter() ); 92 } 93 94 $args = func_get_args(); 95 $this->events[] = array( 96 'filter' => __FUNCTION__, 97 'tag' => $this->current_filter(), 98 'args' => $args, 99 ); 79 100 return $arg; 80 101 } 81 102 82 function filter2($arg) { 83 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 84 85 $args = func_get_args(); 86 $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args); 103 function filter2( $arg ) { 104 if ( $this->debug ) { 105 dmp( __FUNCTION__, $this->current_filter() ); 106 } 107 108 $args = func_get_args(); 109 $this->events[] = array( 110 'filter' => __FUNCTION__, 111 'tag' => $this->current_filter(), 112 'args' => $args, 113 ); 87 114 return $arg; 88 115 } 89 116 90 function filter_append($arg) { 91 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 92 93 $args = func_get_args(); 94 $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args); 117 function filter_append( $arg ) { 118 if ( $this->debug ) { 119 dmp( __FUNCTION__, $this->current_filter() ); 120 } 121 122 $args = func_get_args(); 123 $this->events[] = array( 124 'filter' => __FUNCTION__, 125 'tag' => $this->current_filter(), 126 'args' => $args, 127 ); 95 128 return $arg . '_append'; 96 129 } 97 130 98 function filterall($tag, $arg=NULL) { 99 // this one doesn't return the result, so it's safe to use with the new 'all' filter 100 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 101 102 $args = func_get_args(); 103 $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$tag, 'args'=>array_slice($args, 1)); 131 function filterall( $tag, $arg = null ) { 132 // this one doesn't return the result, so it's safe to use with the new 'all' filter 133 if ( $this->debug ) { 134 dmp( __FUNCTION__, $this->current_filter() ); 135 } 136 137 $args = func_get_args(); 138 $this->events[] = array( 139 'filter' => __FUNCTION__, 140 'tag' => $tag, 141 'args' => array_slice( $args, 1 ), 142 ); 104 143 } 105 144 … … 110 149 111 150 // return a count of the number of times the action was called since the last reset 112 function get_call_count( $tag='') {113 if ( $tag) {151 function get_call_count( $tag = '' ) { 152 if ( $tag ) { 114 153 $count = 0; 115 foreach ( $this->events as $e)116 if ( $e['action'] == $tag)154 foreach ( $this->events as $e ) { 155 if ( $e['action'] == $tag ) { 117 156 ++$count; 157 } 158 } 118 159 return $count; 119 160 } 120 return count( $this->events);161 return count( $this->events ); 121 162 } 122 163 … … 124 165 function get_tags() { 125 166 $out = array(); 126 foreach ( $this->events as $e) {167 foreach ( $this->events as $e ) { 127 168 $out[] = $e['tag']; 128 169 } … … 133 174 function get_args() { 134 175 $out = array(); 135 foreach ( $this->events as $e)176 foreach ( $this->events as $e ) { 136 177 $out[] = $e['args']; 178 } 137 179 return $out; 138 180 } … … 150 192 function __construct( $in ) { 151 193 $this->xml = xml_parser_create(); 152 xml_set_object($this->xml, $this); 153 xml_parser_set_option($this->xml,XML_OPTION_CASE_FOLDING, 0); 154 xml_set_element_handler($this->xml, array($this, 'startHandler'), array($this, 'endHandler')); 155 xml_set_character_data_handler($this->xml, array($this, 'dataHandler')); 156 $this->parse($in); 157 } 158 159 function parse($in) { 160 $parse = xml_parse($this->xml, $in, true); 161 if (!$parse) { 162 trigger_error(sprintf("XML error: %s at line %d", 163 xml_error_string(xml_get_error_code($this->xml)), 164 xml_get_current_line_number($this->xml)), E_USER_ERROR); 165 xml_parser_free($this->xml); 194 xml_set_object( $this->xml, $this ); 195 xml_parser_set_option( $this->xml, XML_OPTION_CASE_FOLDING, 0 ); 196 xml_set_element_handler( $this->xml, array( $this, 'startHandler' ), array( $this, 'endHandler' ) ); 197 xml_set_character_data_handler( $this->xml, array( $this, 'dataHandler' ) ); 198 $this->parse( $in ); 199 } 200 201 function parse( $in ) { 202 $parse = xml_parse( $this->xml, $in, true ); 203 if ( ! $parse ) { 204 trigger_error( 205 sprintf( 206 'XML error: %s at line %d', 207 xml_error_string( xml_get_error_code( $this->xml ) ), 208 xml_get_current_line_number( $this->xml ) 209 ), E_USER_ERROR 210 ); 211 xml_parser_free( $this->xml ); 166 212 } 167 213 return true; 168 214 } 169 215 170 function startHandler( $parser, $name, $attributes) {216 function startHandler( $parser, $name, $attributes ) { 171 217 $data['name'] = $name; 172 if ($attributes) { $data['attributes'] = $attributes; } 218 if ( $attributes ) { 219 $data['attributes'] = $attributes; } 173 220 $this->data[] = $data; 174 221 } 175 222 176 function dataHandler( $parser, $data) {177 $index = count($this->data) - 1;178 @$this->data[ $index]['content'] .= $data;179 } 180 181 function endHandler( $parser, $name) {182 if ( count($this->data) > 1) {183 $data = array_pop($this->data);184 $index = count($this->data) - 1;185 $this->data[ $index]['child'][] = $data;186 } 187 } 188 } 189 190 function xml_to_array( $in) {191 $p = new testXMLParser( $in);223 function dataHandler( $parser, $data ) { 224 $index = count( $this->data ) - 1; 225 @$this->data[ $index ]['content'] .= $data; 226 } 227 228 function endHandler( $parser, $name ) { 229 if ( count( $this->data ) > 1 ) { 230 $data = array_pop( $this->data ); 231 $index = count( $this->data ) - 1; 232 $this->data[ $index ]['child'][] = $data; 233 } 234 } 235 } 236 237 function xml_to_array( $in ) { 238 $p = new testXMLParser( $in ); 192 239 return $p->data; 193 240 } 194 241 195 function xml_find( $tree /*, $el1, $el2, $el3, .. */) {196 $a = func_get_args();197 $a = array_slice($a, 1);198 $n = count($a);242 function xml_find( $tree /*, $el1, $el2, $el3, .. */ ) { 243 $a = func_get_args(); 244 $a = array_slice( $a, 1 ); 245 $n = count( $a ); 199 246 $out = array(); 200 247 201 if ( $n < 1)248 if ( $n < 1 ) { 202 249 return $out; 203 204 for ($i=0; $i<count($tree); $i++) { 205 # echo "checking '{$tree[$i][name]}' == '{$a[0]}'\n"; 206 # var_dump($tree[$i]['name'], $a[0]); 207 if ($tree[$i]['name'] == $a[0]) { 208 # echo "n == {$n}\n"; 209 if ($n == 1) 210 $out[] = $tree[$i]; 211 else { 212 $subtree =& $tree[$i]['child']; 213 $call_args = array($subtree); 214 $call_args = array_merge($call_args, array_slice($a, 1)); 215 $out = array_merge($out, call_user_func_array('xml_find', $call_args)); 250 } 251 252 for ( $i = 0; $i < count( $tree ); $i++ ) { 253 # echo "checking '{$tree[$i][name]}' == '{$a[0]}'\n"; 254 # var_dump($tree[$i]['name'], $a[0]); 255 if ( $tree[ $i ]['name'] == $a[0] ) { 256 # echo "n == {$n}\n"; 257 if ( $n == 1 ) { 258 $out[] = $tree[ $i ]; 259 } else { 260 $subtree =& $tree[ $i ]['child']; 261 $call_args = array( $subtree ); 262 $call_args = array_merge( $call_args, array_slice( $a, 1 ) ); 263 $out = array_merge( $out, call_user_func_array( 'xml_find', $call_args ) ); 216 264 } 217 265 } … … 221 269 } 222 270 223 function xml_join_atts( $atts) {271 function xml_join_atts( $atts ) { 224 272 $a = array(); 225 foreach ($atts as $k=>$v) 226 $a[] = $k.'="'.$v.'"'; 227 return join(' ', $a); 228 } 229 230 function xml_array_dumbdown(&$data) { 273 foreach ( $atts as $k => $v ) { 274 $a[] = $k . '="' . $v . '"'; 275 } 276 return join( ' ', $a ); 277 } 278 279 function xml_array_dumbdown( &$data ) { 231 280 $out = array(); 232 281 233 foreach (array_keys($data) as $i) { 234 $name = $data[$i]['name']; 235 if (!empty($data[$i]['attributes'])) 236 $name .= ' '.xml_join_atts($data[$i]['attributes']); 237 238 if (!empty($data[$i]['child'])) { 239 $out[$name][] = xml_array_dumbdown($data[$i]['child']); 240 } 241 else 242 $out[$name] = $data[$i]['content']; 282 foreach ( array_keys( $data ) as $i ) { 283 $name = $data[ $i ]['name']; 284 if ( ! empty( $data[ $i ]['attributes'] ) ) { 285 $name .= ' ' . xml_join_atts( $data[ $i ]['attributes'] ); 286 } 287 288 if ( ! empty( $data[ $i ]['child'] ) ) { 289 $out[ $name ][] = xml_array_dumbdown( $data[ $i ]['child'] ); 290 } else { 291 $out[ $name ] = $data[ $i ]['content']; 292 } 243 293 } 244 294 … … 249 299 $args = func_get_args(); 250 300 251 foreach ($args as $thing) 252 echo (is_scalar($thing) ? strval($thing) : var_export($thing, true)), "\n"; 253 } 254 255 function dmp_filter($a) { 256 dmp($a); 301 foreach ( $args as $thing ) { 302 echo ( is_scalar( $thing ) ? strval( $thing ) : var_export( $thing, true ) ), "\n"; 303 } 304 } 305 306 function dmp_filter( $a ) { 307 dmp( $a ); 257 308 return $a; 258 309 } 259 310 260 function get_echo( $callable, $args = array()) {311 function get_echo( $callable, $args = array() ) { 261 312 ob_start(); 262 call_user_func_array( $callable, $args);313 call_user_func_array( $callable, $args ); 263 314 return ob_get_clean(); 264 315 } 265 316 266 317 // recursively generate some quick assertEquals tests based on an array 267 function gen_tests_array( $name, $array) {318 function gen_tests_array( $name, $array ) { 268 319 $out = array(); 269 foreach ($array as $k=>$v) { 270 if (is_numeric($k)) 271 $index = strval($k); 272 else 273 $index = "'".addcslashes($k, "\n\r\t'\\")."'"; 274 275 if (is_string($v)) { 276 $out[] = '$this->assertEquals( \'' . addcslashes($v, "\n\r\t'\\") . '\', $'.$name.'['.$index.'] );'; 277 } 278 elseif (is_numeric($v)) { 279 $out[] = '$this->assertEquals( ' . $v . ', $'.$name.'['.$index.'] );'; 280 } 281 elseif (is_array($v)) { 282 $out[] = gen_tests_array("{$name}[{$index}]", $v); 283 } 284 } 285 return join("\n", $out)."\n"; 320 foreach ( $array as $k => $v ) { 321 if ( is_numeric( $k ) ) { 322 $index = strval( $k ); 323 } else { 324 $index = "'" . addcslashes( $k, "\n\r\t'\\" ) . "'"; 325 } 326 327 if ( is_string( $v ) ) { 328 $out[] = '$this->assertEquals( \'' . addcslashes( $v, "\n\r\t'\\" ) . '\', $' . $name . '[' . $index . '] );'; 329 } elseif ( is_numeric( $v ) ) { 330 $out[] = '$this->assertEquals( ' . $v . ', $' . $name . '[' . $index . '] );'; 331 } elseif ( is_array( $v ) ) { 332 $out[] = gen_tests_array( "{$name}[{$index}]", $v ); 333 } 334 } 335 return join( "\n", $out ) . "\n"; 286 336 } 287 337 … … 296 346 function drop_tables() { 297 347 global $wpdb; 298 $tables = $wpdb->get_col('SHOW TABLES;'); 299 foreach ($tables as $table) 300 $wpdb->query("DROP TABLE IF EXISTS {$table}"); 348 $tables = $wpdb->get_col( 'SHOW TABLES;' ); 349 foreach ( $tables as $table ) { 350 $wpdb->query( "DROP TABLE IF EXISTS {$table}" ); 351 } 301 352 } 302 353 … … 305 356 echo "Backtrace:\n"; 306 357 $i = 0; 307 foreach ($bt as $stack) { 308 echo ++$i, ": "; 309 if ( isset($stack['class']) ) 310 echo $stack['class'].'::'; 311 if ( isset($stack['function']) ) 312 echo $stack['function'].'() '; 358 foreach ( $bt as $stack ) { 359 echo ++$i, ': '; 360 if ( isset( $stack['class'] ) ) { 361 echo $stack['class'] . '::'; 362 } 363 if ( isset( $stack['function'] ) ) { 364 echo $stack['function'] . '() '; 365 } 313 366 echo "line {$stack[line]} in {$stack[file]}\n"; 314 367 } … … 317 370 318 371 // mask out any input fields matching the given name 319 function mask_input_value( $in, $name='_wpnonce') {320 return preg_replace( '@<input([^>]*) name="'.preg_quote($name).'"([^>]*) value="[^>]*" />@', '<input$1 name="'.preg_quote($name).'"$2 value="***" />', $in);321 } 322 323 if ( ! function_exists( 'str_getcsv' ) ) {324 function str_getcsv( $input, $delimiter = ',', $enclosure = '"', $escape = "\\") {372 function mask_input_value( $in, $name = '_wpnonce' ) { 373 return preg_replace( '@<input([^>]*) name="' . preg_quote( $name ) . '"([^>]*) value="[^>]*" />@', '<input$1 name="' . preg_quote( $name ) . '"$2 value="***" />', $in ); 374 } 375 376 if ( ! function_exists( 'str_getcsv' ) ) { 377 function str_getcsv( $input, $delimiter = ',', $enclosure = '"', $escape = '\\' ) { 325 378 $fp = fopen( 'php://temp/', 'r+' ); 326 379 fputs( $fp, $input ); … … 356 409 function _cleanup_query_vars() { 357 410 // clean out globals to stop them polluting wp and wp_query 358 foreach ( $GLOBALS['wp']->public_query_vars as $v ) 359 unset( $GLOBALS[$v] ); 360 361 foreach ( $GLOBALS['wp']->private_query_vars as $v ) 362 unset( $GLOBALS[$v] ); 363 364 foreach ( get_taxonomies( array() , 'objects' ) as $t ) { 365 if ( $t->publicly_queryable && ! empty( $t->query_var ) ) 411 foreach ( $GLOBALS['wp']->public_query_vars as $v ) { 412 unset( $GLOBALS[ $v ] ); 413 } 414 415 foreach ( $GLOBALS['wp']->private_query_vars as $v ) { 416 unset( $GLOBALS[ $v ] ); 417 } 418 419 foreach ( get_taxonomies( array(), 'objects' ) as $t ) { 420 if ( $t->publicly_queryable && ! empty( $t->query_var ) ) { 366 421 $GLOBALS['wp']->add_query_var( $t->query_var ); 367 } 368 369 foreach ( get_post_types( array() , 'objects' ) as $t ) { 370 if ( is_post_type_viewable( $t ) && ! empty( $t->query_var ) ) 422 } 423 } 424 425 foreach ( get_post_types( array(), 'objects' ) as $t ) { 426 if ( is_post_type_viewable( $t ) && ! empty( $t->query_var ) ) { 371 427 $GLOBALS['wp']->add_query_var( $t->query_var ); 428 } 372 429 } 373 430 } 374 431 375 432 function _clean_term_filters() { 376 remove_filter( 'get_terms', array( 'Featured_Content', 'hide_featured_term'), 10, 2 );433 remove_filter( 'get_terms', array( 'Featured_Content', 'hide_featured_term' ), 10, 2 ); 377 434 remove_filter( 'get_the_terms', array( 'Featured_Content', 'hide_the_featured_term' ), 10, 3 ); 378 435 } … … 384 441 public function __construct() { 385 442 global $wpdb; 386 $this->dbh = $wpdb->dbh;387 $this->use_mysqli = $wpdb->use_mysqli;388 $this->is_mysql = $wpdb->is_mysql;389 $this->ready = true;443 $this->dbh = $wpdb->dbh; 444 $this->use_mysqli = $wpdb->use_mysqli; 445 $this->is_mysql = $wpdb->is_mysql; 446 $this->ready = true; 390 447 $this->field_types = $wpdb->field_types; 391 $this->charset = $wpdb->charset;392 393 $this->dbuser = $wpdb->dbuser;448 $this->charset = $wpdb->charset; 449 450 $this->dbuser = $wpdb->dbuser; 394 451 $this->dbpassword = $wpdb->dbpassword; 395 $this->dbname = $wpdb->dbname;396 $this->dbhost = $wpdb->dbhost;452 $this->dbname = $wpdb->dbname; 453 $this->dbhost = $wpdb->dbhost; 397 454 } 398 455 … … 409 466 function benchmark_pcre_backtracking( $pattern, $subject, $strategy ) { 410 467 $saved_config = ini_get( 'pcre.backtrack_limit' ); 411 468 412 469 // Attempt to prevent PHP crashes. Adjust these lower when needed. 413 470 if ( version_compare( phpversion(), '5.4.8', '>' ) ) { … … 418 475 419 476 // Start with small numbers, so if a crash is encountered at higher numbers we can still debug the problem. 420 for ( $i = 4; $i <= $limit; $i *= 2 ) {477 for ( $i = 4; $i <= $limit; $i *= 2 ) { 421 478 422 479 ini_set( 'pcre.backtrack_limit', $i ); 423 424 switch ( $strategy ) {425 case 'split':426 preg_split( $pattern, $subject );427 break;428 case 'match':429 preg_match( $pattern, $subject );430 break;431 case 'match_all':432 $matches = array();433 preg_match_all( $pattern, $subject, $matches );434 break;480 481 switch ( $strategy ) { 482 case 'split': 483 preg_split( $pattern, $subject ); 484 break; 485 case 'match': 486 preg_match( $pattern, $subject ); 487 break; 488 case 'match_all': 489 $matches = array(); 490 preg_match_all( $pattern, $subject, $matches ); 491 break; 435 492 } 436 493 437 494 ini_set( 'pcre.backtrack_limit', $saved_config ); 438 495 439 switch ( preg_last_error() ) {440 case PREG_NO_ERROR:441 return $i;442 case PREG_BACKTRACK_LIMIT_ERROR:443 continue;444 case PREG_RECURSION_LIMIT_ERROR:445 trigger_error('PCRE recursion limit encountered before backtrack limit.');446 return;447 case PREG_BAD_UTF8_ERROR:448 trigger_error('UTF-8 error during PCRE benchmark.');449 return;450 case PREG_INTERNAL_ERROR:451 trigger_error('Internal error during PCRE benchmark.');452 return;453 default:454 trigger_error('Unexpected error during PCRE benchmark.');455 return;496 switch ( preg_last_error() ) { 497 case PREG_NO_ERROR: 498 return $i; 499 case PREG_BACKTRACK_LIMIT_ERROR: 500 continue; 501 case PREG_RECURSION_LIMIT_ERROR: 502 trigger_error( 'PCRE recursion limit encountered before backtrack limit.' ); 503 return; 504 case PREG_BAD_UTF8_ERROR: 505 trigger_error( 'UTF-8 error during PCRE benchmark.' ); 506 return; 507 case PREG_INTERNAL_ERROR: 508 trigger_error( 'Internal error during PCRE benchmark.' ); 509 return; 510 default: 511 trigger_error( 'Unexpected error during PCRE benchmark.' ); 512 return; 456 513 } 457 514 } … … 471 528 $name = explode( ':', $rel ); 472 529 473 $curie = wp_list_filter( $links['curies'], array( 'name' => $name[0] ) );474 $full_uri = str_replace( '{rel}', $name[1], $curie[0]['href'] );530 $curie = wp_list_filter( $links['curies'], array( 'name' => $name[0] ) ); 531 $full_uri = str_replace( '{rel}', $name[1], $curie[0]['href'] ); 475 532 $links[ $full_uri ] = $links_array; 476 533 unset( $links[ $rel ] );
Note: See TracChangeset
for help on using the changeset viewer.