Changeset 42343 for trunk/tests/phpunit/includes/testcase.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/includes/testcase.php
r41945 r42343 15 15 class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 16 16 17 protected static $forced_tickets = array();18 protected $expected_deprecated = array();19 protected $caught_deprecated = array();17 protected static $forced_tickets = array(); 18 protected $expected_deprecated = array(); 19 protected $caught_deprecated = array(); 20 20 protected $expected_doing_it_wrong = array(); 21 protected $caught_doing_it_wrong = array();21 protected $caught_doing_it_wrong = array(); 22 22 23 23 protected static $hooks_saved = array(); … … 26 26 function __isset( $name ) { 27 27 return 'factory' === $name; 28 28 } 29 29 30 30 function __get( $name ) { 31 31 if ( 'factory' === $name ) { 32 32 return self::factory(); 33 34 33 } 34 } 35 35 36 36 /** … … 56 56 // [0] WP_UnitTestCase::get_called_class() 57 57 // [1] WP_UnitTestCase::setUpBeforeClass() 58 if ( 'call_user_func' === 58 if ( 'call_user_func' === $backtrace[2]['function'] ) { 59 59 return $backtrace[2]['args'][0][0]; 60 60 } … … 66 66 67 67 $wpdb->suppress_errors = false; 68 $wpdb->show_errors = true;68 $wpdb->show_errors = true; 69 69 $wpdb->db_connect(); 70 ini_set( 'display_errors', 1 );70 ini_set( 'display_errors', 1 ); 71 71 72 72 parent::setUpBeforeClass(); … … 101 101 102 102 function setUp() { 103 set_time_limit( 0);103 set_time_limit( 0 ); 104 104 105 105 if ( ! self::$ignore_files ) { … … 160 160 } 161 161 $wp_query = new WP_Query(); 162 $wp = new WP();162 $wp = new WP(); 163 163 164 164 // Reset globals related to the post loop and `setup_postdata()`. … … 177 177 178 178 function clean_up_global_scope() { 179 $_GET = array();179 $_GET = array(); 180 180 $_POST = array(); 181 181 self::flush_cache(); … … 185 185 * Allow tests to be skipped on some automated runs 186 186 * 187 * For test runs on Travis for something other than trunk/master 187 * For test runs on Travis for something other than trunk/master 188 188 * we want to skip tests that only need to run for master. 189 189 */ … … 321 321 static function flush_cache() { 322 322 global $wp_object_cache; 323 $wp_object_cache->group_ops = array();324 $wp_object_cache->stats = array();323 $wp_object_cache->group_ops = array(); 324 $wp_object_cache->stats = array(); 325 325 $wp_object_cache->memcache_debug = array(); 326 $wp_object_cache->cache = array();326 $wp_object_cache->cache = array(); 327 327 if ( method_exists( $wp_object_cache, '__remoteset' ) ) { 328 328 $wp_object_cache->__remoteset(); … … 352 352 353 353 function _create_temporary_tables( $query ) { 354 if ( 'CREATE TABLE' === substr( trim( $query ), 0, 12 ) ) 354 if ( 'CREATE TABLE' === substr( trim( $query ), 0, 12 ) ) { 355 355 return substr_replace( trim( $query ), 'CREATE TEMPORARY TABLE', 0, 12 ); 356 } 356 357 return $query; 357 358 } 358 359 359 360 function _drop_temporary_tables( $query ) { 360 if ( 'DROP TABLE' === substr( trim( $query ), 0, 10 ) ) 361 if ( 'DROP TABLE' === substr( trim( $query ), 0, 10 ) ) { 361 362 return substr_replace( trim( $query ), 'DROP TEMPORARY TABLE', 0, 10 ); 363 } 362 364 return $query; 363 365 } … … 378 380 $annotations = $this->getAnnotations(); 379 381 foreach ( array( 'class', 'method' ) as $depth ) { 380 if ( ! empty( $annotations[ $depth ]['expectedDeprecated'] ) ) 382 if ( ! empty( $annotations[ $depth ]['expectedDeprecated'] ) ) { 381 383 $this->expected_deprecated = array_merge( $this->expected_deprecated, $annotations[ $depth ]['expectedDeprecated'] ); 382 if ( ! empty( $annotations[ $depth ]['expectedIncorrectUsage'] ) ) 384 } 385 if ( ! empty( $annotations[ $depth ]['expectedIncorrectUsage'] ) ) { 383 386 $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $annotations[ $depth ]['expectedIncorrectUsage'] ); 387 } 384 388 } 385 389 add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) ); … … 389 393 add_action( 'deprecated_function_trigger_error', '__return_false' ); 390 394 add_action( 'deprecated_argument_trigger_error', '__return_false' ); 391 add_action( 'deprecated_hook_trigger_error', 392 add_action( 'doing_it_wrong_trigger_error', 395 add_action( 'deprecated_hook_trigger_error', '__return_false' ); 396 add_action( 'doing_it_wrong_trigger_error', '__return_false' ); 393 397 } 394 398 … … 471 475 472 476 function deprecated_function_run( $function ) { 473 if ( ! in_array( $function, $this->caught_deprecated ) ) 477 if ( ! in_array( $function, $this->caught_deprecated ) ) { 474 478 $this->caught_deprecated[] = $function; 479 } 475 480 } 476 481 477 482 function doing_it_wrong_run( $function ) { 478 if ( ! in_array( $function, $this->caught_doing_it_wrong ) ) 483 if ( ! in_array( $function, $this->caught_doing_it_wrong ) ) { 479 484 $this->caught_doing_it_wrong[] = $function; 485 } 480 486 } 481 487 … … 503 509 504 510 function assertEqualFields( $object, $fields ) { 505 foreach ( $fields as $field_name => $field_value ) {511 foreach ( $fields as $field_name => $field_value ) { 506 512 if ( $object->$field_name != $field_value ) { 507 513 $this->fail(); … … 535 541 $this->assertNotEmpty( $array ); 536 542 537 foreach ( $array as $sub_array ) {543 foreach ( $array as $sub_array ) { 538 544 $this->assertTrue( is_array( $sub_array ) ); 539 545 $this->assertNotEmpty( $sub_array ); … … 563 569 // to run them more than once without very carefully clearing everything 564 570 $_GET = $_POST = array(); 565 foreach (array('query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow') as $v) { 566 if ( isset( $GLOBALS[$v] ) ) unset( $GLOBALS[$v] ); 567 } 568 $parts = parse_url($url); 569 if (isset($parts['scheme'])) { 571 foreach ( array( 'query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow' ) as $v ) { 572 if ( isset( $GLOBALS[ $v ] ) ) { 573 unset( $GLOBALS[ $v ] ); 574 } 575 } 576 $parts = parse_url( $url ); 577 if ( isset( $parts['scheme'] ) ) { 570 578 $req = isset( $parts['path'] ) ? $parts['path'] : ''; 571 if ( isset($parts['query'])) {579 if ( isset( $parts['query'] ) ) { 572 580 $req .= '?' . $parts['query']; 573 581 // parse the url query vars into $_GET 574 parse_str( $parts['query'], $_GET);582 parse_str( $parts['query'], $_GET ); 575 583 } 576 584 } else { … … 582 590 583 591 $_SERVER['REQUEST_URI'] = $req; 584 unset( $_SERVER['PATH_INFO']);592 unset( $_SERVER['PATH_INFO'] ); 585 593 586 594 self::flush_cache(); 587 unset( $GLOBALS['wp_query'], $GLOBALS['wp_the_query']);595 unset( $GLOBALS['wp_query'], $GLOBALS['wp_the_query'] ); 588 596 $GLOBALS['wp_the_query'] = new WP_Query(); 589 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];597 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; 590 598 591 599 $public_query_vars = $GLOBALS['wp']->public_query_vars; 592 600 $private_query_vars = $GLOBALS['wp']->private_query_vars; 593 601 594 $GLOBALS['wp'] = new WP();602 $GLOBALS['wp'] = new WP(); 595 603 $GLOBALS['wp']->public_query_vars = $public_query_vars; 596 604 $GLOBALS['wp']->private_query_vars = $private_query_vars; … … 598 606 _cleanup_query_vars(); 599 607 600 $GLOBALS['wp']->main( $parts['query']);608 $GLOBALS['wp']->main( $parts['query'] ); 601 609 } 602 610 … … 620 628 } 621 629 622 if ( WP_TESTS_FORCE_KNOWN_BUGS ) 630 if ( WP_TESTS_FORCE_KNOWN_BUGS ) { 623 631 return; 632 } 624 633 $tickets = PHPUnit_Util_Test::getTickets( get_class( $this ), $this->getName( false ) ); 625 634 foreach ( $tickets as $ticket ) { … … 628 637 } elseif ( 'Plugin' == substr( $ticket, 0, 6 ) ) { 629 638 $ticket = substr( $ticket, 6 ); 630 if ( $ticket && is_numeric( $ticket ) ) 639 if ( $ticket && is_numeric( $ticket ) ) { 631 640 $this->knownPluginBug( $ticket ); 641 } 632 642 } 633 643 } … … 638 648 */ 639 649 function knownWPBug( $ticket_id ) { 640 if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) ) 650 if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) ) { 641 651 return; 642 if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) ) 652 } 653 if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) ) { 643 654 $this->markTestSkipped( sprintf( 'WordPress Ticket #%d is not fixed', $ticket_id ) ); 655 } 644 656 } 645 657 … … 655 667 */ 656 668 function knownPluginBug( $ticket_id ) { 657 if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets ) ) 669 if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets ) ) { 658 670 return; 659 if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) ) 671 } 672 if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) ) { 660 673 $this->markTestSkipped( sprintf( 'WordPress Plugin Ticket #%d is not fixed', $ticket_id ) ); 674 } 661 675 } 662 676 … … 679 693 function temp_filename() { 680 694 $tmp_dir = ''; 681 $dirs = array( 'TMP', 'TMPDIR', 'TEMP' );682 foreach ( $dirs as $dir )683 if ( isset( $_ENV[ $dir] ) && !empty( $_ENV[$dir] ) ) {695 $dirs = array( 'TMP', 'TMPDIR', 'TEMP' ); 696 foreach ( $dirs as $dir ) { 697 if ( isset( $_ENV[ $dir ] ) && ! empty( $_ENV[ $dir ] ) ) { 684 698 $tmp_dir = $dir; 685 699 break; 686 700 } 701 } 687 702 if ( empty( $tmp_dir ) ) { 688 703 $tmp_dir = '/tmp'; … … 701 716 * @param string $prop,... Any number of WP_Query properties that are expected to be true for the current request. 702 717 */ 703 function assertQueryTrue( /* ... */) {718 function assertQueryTrue() { 704 719 global $wp_query; 705 $all = array(720 $all = array( 706 721 'is_404', 707 722 'is_admin', … … 739 754 } 740 755 741 $passed = true;756 $passed = true; 742 757 $message = ''; 743 758 … … 748 763 if ( ! $result ) { 749 764 $message .= $query_thing . ' is false but is expected to be true. ' . PHP_EOL; 750 $passed = false;765 $passed = false; 751 766 } 752 } else 767 } elseif ( $result ) { 753 768 $message .= $query_thing . ' is true but is expected to be false. ' . PHP_EOL; 754 $passed = false;769 $passed = false; 755 770 } 756 771 } … … 790 805 791 806 $iterator = new RecursiveDirectoryIterator( $dir ); 792 $objects = new RecursiveIteratorIterator( $iterator );807 $objects = new RecursiveIteratorIterator( $iterator ); 793 808 foreach ( $objects as $name => $object ) { 794 809 if ( is_file( $name ) ) { … … 807 822 808 823 $uploads = wp_upload_dir(); 809 $files = $this->files_in_dir( $uploads['basedir'] );824 $files = $this->files_in_dir( $uploads['basedir'] ); 810 825 return $files; 811 826 } … … 836 851 * Helper to Convert a microtime string into a float 837 852 */ 838 protected function _microtime_to_float( $microtime ){853 protected function _microtime_to_float( $microtime ) { 839 854 $time_array = explode( ' ', $microtime ); 840 855 return array_sum( $time_array ); … … 871 886 } 872 887 873 function _make_attachment( $upload, $parent_post_id = 0) {888 function _make_attachment( $upload, $parent_post_id = 0 ) { 874 889 $type = ''; 875 if ( ! empty($upload['type']) ) {890 if ( ! empty( $upload['type'] ) ) { 876 891 $type = $upload['type']; 877 892 } else { 878 893 $mime = wp_check_filetype( $upload['file'] ); 879 if ( $mime)894 if ( $mime ) { 880 895 $type = $mime['type']; 896 } 881 897 } 882 898 883 899 $attachment = array( 884 'post_title' => basename( $upload['file'] ),885 'post_content' => '',886 'post_type' => 'attachment',887 'post_parent' => $parent_post_id,900 'post_title' => basename( $upload['file'] ), 901 'post_content' => '', 902 'post_type' => 'attachment', 903 'post_parent' => $parent_post_id, 888 904 'post_mime_type' => $type, 889 'guid' => $upload[ 'url'],905 'guid' => $upload['url'], 890 906 ); 891 907 892 908 // Save the data 893 $id = wp_insert_attachment( $attachment, $upload[ 'file'], $parent_post_id );909 $id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id ); 894 910 wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); 895 911 return $id; … … 904 920 $wpdb->posts, 905 921 array( 906 'post_modified' => $date,922 'post_modified' => $date, 907 923 'post_modified_gmt' => $date, 908 924 ),
Note: See TracChangeset
for help on using the changeset viewer.