Changeset 47329 for branches/3.9/tests/phpunit/includes/testcase.php
- Timestamp:
- 02/20/2020 05:04:42 PM (5 years ago)
- Location:
- branches/3.9
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.9
-
branches/3.9/tests/phpunit/includes/testcase.php
r42066 r47329 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' ); 89 if ( is_multisite() ) { 90 while ( ms_is_switched() ) { 91 restore_current_blog(); 92 } 93 } 38 94 $wp_query = new WP_Query(); 39 95 $post = null; … … 42 98 remove_filter( 'query', array( $this, '_drop_temporary_tables' ) ); 43 99 remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); 100 $this->_restore_hooks(); 101 wp_set_current_user( 0 ); 44 102 } 45 103 … … 71 129 } 72 130 131 /** 132 * Saves the action and filter-related globals so they can be restored later. 133 * 134 * Stores $merged_filters, $wp_actions, $wp_current_filter, and $wp_filter 135 * on a class variable so they can be restored on tearDown() using _restore_hooks(). 136 * 137 * @global array $merged_filters 138 * @global array $wp_actions 139 * @global array $wp_current_filter 140 * @global array $wp_filter 141 * @return void 142 */ 143 protected function _backup_hooks() { 144 $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' ); 145 foreach ( $globals as $key ) { 146 self::$hooks_saved[ $key ] = $GLOBALS[ $key ]; 147 } 148 } 149 150 /** 151 * Restores the hook-related globals to their state at setUp() 152 * so that future tests aren't affected by hooks set during this last test. 153 * 154 * @global array $merged_filters 155 * @global array $wp_actions 156 * @global array $wp_current_filter 157 * @global array $wp_filter 158 * @return void 159 */ 160 protected function _restore_hooks() { 161 $globals = array( 'merged_filters', 'wp_actions', 'wp_current_filter', 'wp_filter' ); 162 foreach ( $globals as $key ) { 163 if ( isset( self::$hooks_saved[ $key ] ) ) { 164 $GLOBALS[ $key ] = self::$hooks_saved[ $key ]; 165 } 166 } 167 } 168 73 169 function flush_cache() { 74 170 global $wp_object_cache; … … 232 328 protected function checkRequirements() { 233 329 parent::checkRequirements(); 330 331 // Core tests no longer check against open Trac tickets, but others using WP_UnitTestCase may do so. 332 if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) { 333 return; 334 } 335 234 336 if ( WP_TESTS_FORCE_KNOWN_BUGS ) 235 337 return; … … 354 456 $this->assertTrue( $passed, $message ); 355 457 } 458 459 function unlink( $file ) { 460 $exists = is_file( $file ); 461 if ( $exists && ! in_array( $file, self::$ignore_files ) ) { 462 //error_log( $file ); 463 unlink( $file ); 464 } elseif ( ! $exists ) { 465 $this->fail( "Trying to delete a file that doesn't exist: $file" ); 466 } 467 } 468 469 function rmdir( $path ) { 470 $files = $this->files_in_dir( $path ); 471 foreach ( $files as $file ) { 472 if ( ! in_array( $file, self::$ignore_files ) ) { 473 $this->unlink( $file ); 474 } 475 } 476 } 477 478 function remove_added_uploads() { 479 // Remove all uploads. 480 $uploads = wp_upload_dir(); 481 $this->rmdir( $uploads['basedir'] ); 482 } 483 484 function files_in_dir( $dir ) { 485 $files = array(); 486 487 $iterator = new RecursiveDirectoryIterator( $dir ); 488 $objects = new RecursiveIteratorIterator( $iterator ); 489 foreach ( $objects as $name => $object ) { 490 if ( is_file( $name ) ) { 491 $files[] = $name; 492 } 493 } 494 495 return $files; 496 } 497 498 function scan_user_uploads() { 499 static $files = array(); 500 if ( ! empty( $files ) ) { 501 return $files; 502 } 503 504 $uploads = wp_upload_dir(); 505 $files = $this->files_in_dir( $uploads['basedir'] ); 506 return $files; 507 } 356 508 }
Note: See TracChangeset
for help on using the changeset viewer.