Changeset 47343 for branches/3.7/tests/phpunit/includes/testcase.php
- Timestamp:
- 02/22/2020 12:05:12 PM (5 years ago)
- Location:
- branches/3.7
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.7
- Property svn:mergeinfo changed
-
branches/3.7/tests/phpunit/includes/testcase.php
r42068 r47343 12 12 protected $caught_doing_it_wrong = array(); 13 13 14 protected static $hooks_saved = array(); 15 protected static $ignore_files; 16 14 17 /** 15 18 * @var WP_UnitTest_Factory … … 19 22 function setUp() { 20 23 set_time_limit(0); 24 25 if ( ! self::$ignore_files ) { 26 self::$ignore_files = $this->scan_user_uploads(); 27 } 28 29 if ( ! self::$hooks_saved ) { 30 $this->_backup_hooks(); 31 } 21 32 22 33 global $wpdb; … … 27 38 $this->factory = new WP_UnitTest_Factory; 28 39 $this->clean_up_global_scope(); 40 41 /* 42 * When running core tests, ensure that post types and taxonomies 43 * are reset for each test. We skip this step for non-core tests, 44 * given the large number of plugins that register post types and 45 * taxonomies at 'init'. 46 */ 47 if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) { 48 $this->reset_post_types(); 49 $this->reset_taxonomies(); 50 } 51 29 52 $this->start_transaction(); 30 53 $this->expectDeprecated(); 31 54 add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); 55 } 56 57 /** 58 * Unregister existing post types and register defaults. 59 * 60 * Run before each test in order to clean up the global scope, in case 61 * a test forgets to unregister a post type on its own, or fails before 62 * it has a chance to do so. 63 */ 64 protected function reset_post_types() { 65 foreach ( get_post_types() as $pt ) { 66 _unregister_post_type( $pt ); 67 } 68 create_initial_post_types(); 69 } 70 71 /** 72 * Unregister existing taxonomies and register defaults. 73 * 74 * Run before each test in order to clean up the global scope, in case 75 * a test forgets to unregister a taxonomy on its own, or fails before 76 * it has a chance to do so. 77 */ 78 protected function reset_taxonomies() { 79 foreach ( get_taxonomies() as $tax ) { 80 _unregister_taxonomy( $tax ); 81 } 82 create_initial_taxonomies(); 32 83 } 33 84 … … 36 87 $this->expectedDeprecated(); 37 88 $wpdb->query( 'ROLLBACK' ); 38 remove_filter( 'dbdelta_create_queries', array( $this, '_create_temporary_tables' ) ); 89 if ( is_multisite() ) { 90 while ( ms_is_switched() ) { 91 restore_current_blog(); 92 } 93 } 94 remove_filter( 'query', array( $this, '_create_temporary_tables' ) ); 39 95 remove_filter( 'query', array( $this, '_drop_temporary_tables' ) ); 40 96 remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); 97 $this->_restore_hooks(); 98 wp_set_current_user( 0 ); 41 99 } 42 100 … … 47 105 } 48 106 107 /** 108 * Allow tests to be skipped on some automated runs 109 * 110 * For test runs on Travis for something other than trunk/master 111 * we want to skip tests that only need to run for master. 112 */ 113 public function skipOnAutomatedBranches() { 114 // gentenv can be disabled 115 if ( ! function_exists( 'getenv' ) ) { 116 return false; 117 } 118 119 // https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables 120 $travis_branch = getenv( 'TRAVIS_BRANCH' ); 121 $travis_pull_request = getenv( 'TRAVIS_PULL_REQUEST' ); 122 123 if ( false !== $travis_pull_request && 'master' !== $travis_branch ) { 124 $this->markTestSkipped( 'For automated test runs, this test is only run on trunk/master' ); 125 } 126 } 127 128 /** 129 * Saves the action and filter-related globals so they can be restored later. 130 * 131 * Stores $merged_filters, $wp_actions, $wp_current_filter, and $wp_filter 132 * on a class variable so they can be restored on tearDown() using _restore_hooks(). 133 * 134 * @global array $merged_filters 135 * @global array $wp_actions 136 * @global array $wp_current_filter 137 * @global array $wp_filter 138 * @return void 139 */ 140 protected function _backup_hooks() { 141 $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' ); 142 foreach ( $globals as $key ) { 143 self::$hooks_saved[ $key ] = $GLOBALS[ $key ]; 144 } 145 } 146 147 /** 148 * Restores the hook-related globals to their state at setUp() 149 * so that future tests aren't affected by hooks set during this last test. 150 * 151 * @global array $merged_filters 152 * @global array $wp_actions 153 * @global array $wp_current_filter 154 * @global array $wp_filter 155 * @return void 156 */ 157 protected function _restore_hooks() { 158 $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' ); 159 foreach ( $globals as $key ) { 160 if ( isset( self::$hooks_saved[ $key ] ) ) { 161 $GLOBALS[ $key ] = self::$hooks_saved[ $key ]; 162 } 163 } 164 } 165 49 166 function flush_cache() { 50 167 global $wp_object_cache; … … 65 182 $wpdb->query( 'SET autocommit = 0;' ); 66 183 $wpdb->query( 'START TRANSACTION;' ); 67 add_filter( ' dbdelta_create_queries', array( $this, '_create_temporary_tables' ) );184 add_filter( 'query', array( $this, '_create_temporary_tables' ) ); 68 185 add_filter( 'query', array( $this, '_drop_temporary_tables' ) ); 69 186 } 70 187 71 function _create_temporary_tables( $queries ) { 72 return str_replace( 'CREATE TABLE', 'CREATE TEMPORARY TABLE', $queries ); 188 function _create_temporary_tables( $query ) { 189 if ( 'CREATE TABLE' === substr( trim( $query ), 0, 12 ) ) 190 return substr_replace( trim( $query ), 'CREATE TEMPORARY TABLE', 0, 12 ); 191 return $query; 73 192 } 74 193 75 194 function _drop_temporary_tables( $query ) { 76 if ( 'DROP TABLE' === substr( $query, 0, 10 ) )77 return 'DROP TEMPORARY TABLE ' . substr( $query, 10 );195 if ( 'DROP TABLE' === substr( trim( $query ), 0, 10 ) ) 196 return substr_replace( trim( $query ), 'DROP TEMPORARY TABLE', 0, 10 ); 78 197 return $query; 79 198 } … … 213 332 protected function checkRequirements() { 214 333 parent::checkRequirements(); 334 335 // Core tests no longer check against open Trac tickets, but others using WP_UnitTestCase may do so. 336 if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) { 337 return; 338 } 339 215 340 if ( WP_TESTS_FORCE_KNOWN_BUGS ) 216 341 return; … … 237 362 if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) ) 238 363 return; 239 if ( ! TracTickets::isTracTicketClosed( 'http ://core.trac.wordpress.org', $ticket_id ) )364 if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) ) 240 365 $this->markTestSkipped( sprintf( 'WordPress Ticket #%d is not fixed', $ticket_id ) ); 241 366 } … … 247 372 if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'UT' . $ticket_id, self::$forced_tickets ) ) 248 373 return; 249 if ( ! TracTickets::isTracTicketClosed( 'http ://unit-tests.trac.wordpress.org', $ticket_id ) )374 if ( ! TracTickets::isTracTicketClosed( 'https://unit-tests.trac.wordpress.org', $ticket_id ) ) 250 375 $this->markTestSkipped( sprintf( 'Unit Tests Ticket #%d is not fixed', $ticket_id ) ); 251 376 } … … 257 382 if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets ) ) 258 383 return; 259 if ( ! TracTickets::isTracTicketClosed( 'http ://plugins.trac.wordpress.org', $ticket_id ) )384 if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) ) 260 385 $this->markTestSkipped( sprintf( 'WordPress Plugin Ticket #%d is not fixed', $ticket_id ) ); 261 386 } … … 291 416 return tempnam( $tmp_dir, 'wpunit' ); 292 417 } 418 419 function unlink( $file ) { 420 $exists = is_file( $file ); 421 if ( $exists && ! in_array( $file, self::$ignore_files ) ) { 422 //error_log( $file ); 423 unlink( $file ); 424 } elseif ( ! $exists ) { 425 $this->fail( "Trying to delete a file that doesn't exist: $file" ); 426 } 427 } 428 429 function rmdir( $path ) { 430 $files = $this->files_in_dir( $path ); 431 foreach ( $files as $file ) { 432 if ( ! in_array( $file, self::$ignore_files ) ) { 433 $this->unlink( $file ); 434 } 435 } 436 } 437 438 function remove_added_uploads() { 439 // Remove all uploads. 440 $uploads = wp_upload_dir(); 441 $this->rmdir( $uploads['basedir'] ); 442 } 443 444 function files_in_dir( $dir ) { 445 $files = array(); 446 447 $iterator = new RecursiveDirectoryIterator( $dir ); 448 $objects = new RecursiveIteratorIterator( $iterator ); 449 foreach ( $objects as $name => $object ) { 450 if ( is_file( $name ) ) { 451 $files[] = $name; 452 } 453 } 454 455 return $files; 456 } 457 458 function scan_user_uploads() { 459 static $files = array(); 460 if ( ! empty( $files ) ) { 461 return $files; 462 } 463 464 $uploads = wp_upload_dir(); 465 $files = $this->files_in_dir( $uploads['basedir'] ); 466 return $files; 467 } 293 468 }
Note: See TracChangeset
for help on using the changeset viewer.