Ticket #42714: 42714.patch
File 42714.patch, 12.2 KB (added by , 7 years ago) |
---|
-
tests/phpunit/includes/bootstrap.php
diff --git tests/phpunit/includes/bootstrap.php tests/phpunit/includes/bootstrap.php index ac715d814c..2225235463 100644
require dirname( __FILE__ ) . '/testcase-xmlrpc.php'; 111 111 require dirname( __FILE__ ) . '/testcase-ajax.php'; 112 112 require dirname( __FILE__ ) . '/testcase-canonical.php'; 113 113 require dirname( __FILE__ ) . '/exceptions.php'; 114 require dirname( __FILE__ ) . '/class-mock-action.php'; 115 require dirname( __FILE__ ) . '/class-mock-class.php'; 116 require dirname( __FILE__ ) . '/class-test-xml-parser.php'; 117 require dirname( __FILE__ ) . '/class-wpdb-testing.php'; 114 118 require dirname( __FILE__ ) . '/utils.php'; 115 119 require dirname( __FILE__ ) . '/spy-rest-server.php'; 116 120 -
new file tests/phpunit/includes/class-mock-action.php
diff --git tests/phpunit/includes/class-mock-action.php tests/phpunit/includes/class-mock-action.php new file mode 100644 index 0000000000..0ad8aeeb46
- + 1 <?php 2 3 // helper class for testing code that involves actions and filters 4 // typical use: 5 // $ma = new MockAction(); 6 // add_action('foo', array(&$ma, 'action')); 7 class MockAction { 8 var $events; 9 var $debug; 10 11 /** 12 * PHP5 constructor. 13 */ 14 function __construct( $debug = 0 ) { 15 $this->reset(); 16 $this->debug = $debug; 17 } 18 19 function reset() { 20 $this->events = array(); 21 } 22 23 function current_filter() { 24 if (is_callable('current_filter')) 25 return current_filter(); 26 global $wp_actions; 27 return end($wp_actions); 28 } 29 30 function action($arg) { 31 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 32 $args = func_get_args(); 33 $this->events[] = array('action' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args); 34 return $arg; 35 } 36 37 function action2($arg) { 38 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 39 40 $args = func_get_args(); 41 $this->events[] = array('action' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args); 42 return $arg; 43 } 44 45 function filter($arg) { 46 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 47 48 $args = func_get_args(); 49 $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args); 50 return $arg; 51 } 52 53 function filter2($arg) { 54 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 55 56 $args = func_get_args(); 57 $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args); 58 return $arg; 59 } 60 61 function filter_append($arg) { 62 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 63 64 $args = func_get_args(); 65 $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$this->current_filter(), 'args'=>$args); 66 return $arg . '_append'; 67 } 68 69 function filterall($tag, $arg=NULL) { 70 // this one doesn't return the result, so it's safe to use with the new 'all' filter 71 if ($this->debug) dmp(__FUNCTION__, $this->current_filter()); 72 73 $args = func_get_args(); 74 $this->events[] = array('filter' => __FUNCTION__, 'tag'=>$tag, 'args'=>array_slice($args, 1)); 75 } 76 77 // return a list of all the actions, tags and args 78 function get_events() { 79 return $this->events; 80 } 81 82 // return a count of the number of times the action was called since the last reset 83 function get_call_count($tag='') { 84 if ($tag) { 85 $count = 0; 86 foreach ($this->events as $e) 87 if ($e['action'] == $tag) 88 ++$count; 89 return $count; 90 } 91 return count($this->events); 92 } 93 94 // return an array of the tags that triggered calls to this action 95 function get_tags() { 96 $out = array(); 97 foreach ($this->events as $e) { 98 $out[] = $e['tag']; 99 } 100 return $out; 101 } 102 103 // return an array of args passed in calls to this action 104 function get_args() { 105 $out = array(); 106 foreach ($this->events as $e) 107 $out[] = $e['args']; 108 return $out; 109 } 110 } -
new file tests/phpunit/includes/class-mock-class.php
diff --git tests/phpunit/includes/class-mock-class.php tests/phpunit/includes/class-mock-class.php new file mode 100644 index 0000000000..c8b152667a
- + 1 <?php 2 /** 3 * Use to create objects by yourself 4 */ 5 class MockClass {}; -
new file tests/phpunit/includes/class-test-xml-parser.php
diff --git tests/phpunit/includes/class-test-xml-parser.php tests/phpunit/includes/class-test-xml-parser.php new file mode 100644 index 0000000000..7e7a2e1360
- + 1 <?php 2 3 // convert valid xml to an array tree structure 4 // kinda lame but it works with a default php 4 installation 5 class testXMLParser { 6 var $xml; 7 var $data = array(); 8 9 /** 10 * PHP5 constructor. 11 */ 12 function __construct( $in ) { 13 $this->xml = xml_parser_create(); 14 xml_set_object($this->xml, $this); 15 xml_parser_set_option($this->xml,XML_OPTION_CASE_FOLDING, 0); 16 xml_set_element_handler($this->xml, array($this, 'startHandler'), array($this, 'endHandler')); 17 xml_set_character_data_handler($this->xml, array($this, 'dataHandler')); 18 $this->parse($in); 19 } 20 21 function parse($in) { 22 $parse = xml_parse($this->xml, $in, true); 23 if (!$parse) { 24 trigger_error(sprintf("XML error: %s at line %d", 25 xml_error_string(xml_get_error_code($this->xml)), 26 xml_get_current_line_number($this->xml)), E_USER_ERROR); 27 xml_parser_free($this->xml); 28 } 29 return true; 30 } 31 32 function startHandler($parser, $name, $attributes) { 33 $data['name'] = $name; 34 if ($attributes) { $data['attributes'] = $attributes; } 35 $this->data[] = $data; 36 } 37 38 function dataHandler($parser, $data) { 39 $index = count($this->data) - 1; 40 @$this->data[$index]['content'] .= $data; 41 } 42 43 function endHandler($parser, $name) { 44 if (count($this->data) > 1) { 45 $data = array_pop($this->data); 46 $index = count($this->data) - 1; 47 $this->data[$index]['child'][] = $data; 48 } 49 } 50 } -
new file tests/phpunit/includes/class-wpdb-testing.php
diff --git tests/phpunit/includes/class-wpdb-testing.php tests/phpunit/includes/class-wpdb-testing.php new file mode 100644 index 0000000000..711fa65f52
- + 1 <?php 2 /** 3 * Special class for exposing protected wpdb methods we need to access 4 */ 5 class wpdb_exposed_methods_for_testing extends wpdb { 6 public function __construct() { 7 global $wpdb; 8 $this->dbh = $wpdb->dbh; 9 $this->use_mysqli = $wpdb->use_mysqli; 10 $this->is_mysql = $wpdb->is_mysql; 11 $this->ready = true; 12 $this->field_types = $wpdb->field_types; 13 $this->charset = $wpdb->charset; 14 15 $this->dbuser = $wpdb->dbuser; 16 $this->dbpassword = $wpdb->dbpassword; 17 $this->dbname = $wpdb->dbname; 18 $this->dbhost = $wpdb->dbhost; 19 } 20 21 public function __call( $name, $arguments ) { 22 return call_user_func_array( array( $this, $name ), $arguments ); 23 } 24 } -
tests/phpunit/includes/utils.php
diff --git tests/phpunit/includes/utils.php tests/phpunit/includes/utils.php index 396edaebb3..514cabe95a 100644
function strip_ws($txt) { 29 29 return trim(join("\n", $result)); 30 30 } 31 31 32 // helper class for testing code that involves actions and filters33 // typical use:34 // $ma = new MockAction();35 // add_action('foo', array(&$ma, 'action'));36 class MockAction {37 var $events;38 var $debug;39 40 /**41 * PHP5 constructor.42 */43 function __construct( $debug = 0 ) {44 $this->reset();45 $this->debug = $debug;46 }47 48 function reset() {49 $this->events = array();50 }51 52 function current_filter() {53 if (is_callable('current_filter'))54 return current_filter();55 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);63 return $arg;64 }65 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);71 return $arg;72 }73 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);79 return $arg;80 }81 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);87 return $arg;88 }89 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);95 return $arg . '_append';96 }97 98 function filterall($tag, $arg=NULL) {99 // this one doesn't return the result, so it's safe to use with the new 'all' filter100 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));104 }105 106 // return a list of all the actions, tags and args107 function get_events() {108 return $this->events;109 }110 111 // return a count of the number of times the action was called since the last reset112 function get_call_count($tag='') {113 if ($tag) {114 $count = 0;115 foreach ($this->events as $e)116 if ($e['action'] == $tag)117 ++$count;118 return $count;119 }120 return count($this->events);121 }122 123 // return an array of the tags that triggered calls to this action124 function get_tags() {125 $out = array();126 foreach ($this->events as $e) {127 $out[] = $e['tag'];128 }129 return $out;130 }131 132 // return an array of args passed in calls to this action133 function get_args() {134 $out = array();135 foreach ($this->events as $e)136 $out[] = $e['args'];137 return $out;138 }139 }140 141 // convert valid xml to an array tree structure142 // kinda lame but it works with a default php 4 installation143 class testXMLParser {144 var $xml;145 var $data = array();146 147 /**148 * PHP5 constructor.149 */150 function __construct( $in ) {151 $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);166 }167 return true;168 }169 170 function startHandler($parser, $name, $attributes) {171 $data['name'] = $name;172 if ($attributes) { $data['attributes'] = $attributes; }173 $this->data[] = $data;174 }175 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 32 function xml_to_array($in) { 191 33 $p = new testXMLParser($in); 192 34 return $p->data; … … function gen_tests_array($name, $array) { 286 128 } 287 129 288 130 /** 289 * Use to create objects by yourself290 */291 class MockClass {};292 293 /**294 131 * Drops all tables from the WordPress database 295 132 */ 296 133 function drop_tables() { … … function _clean_term_filters() { 378 215 } 379 216 380 217 /** 381 * Special class for exposing protected wpdb methods we need to access382 */383 class wpdb_exposed_methods_for_testing extends wpdb {384 public function __construct() {385 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;390 $this->field_types = $wpdb->field_types;391 $this->charset = $wpdb->charset;392 393 $this->dbuser = $wpdb->dbuser;394 $this->dbpassword = $wpdb->dbpassword;395 $this->dbname = $wpdb->dbname;396 $this->dbhost = $wpdb->dbhost;397 }398 399 public function __call( $name, $arguments ) {400 return call_user_func_array( array( $this, $name ), $arguments );401 }402 }403 404 /**405 218 * Determine approximate backtrack count when running PCRE. 406 219 * 407 220 * @return int The backtrack count.