bootstrap.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Installs WordPress for running the tests and loads WordPress and the test libraries
  4. */
  5. $config_file_path = dirname( dirname( __FILE__ ) );
  6. if ( ! file_exists( $config_file_path . '/wp-tests-config.php' ) ) {
  7. // Support the config file from the root of the develop repository.
  8. if ( basename( $config_file_path ) === 'phpunit' && basename( dirname( $config_file_path ) ) === 'tests' )
  9. $config_file_path = dirname( dirname( $config_file_path ) );
  10. }
  11. $config_file_path .= '/wp-tests-config.php';
  12. /*
  13. * Globalize some WordPress variables, because PHPUnit loads this file inside a function
  14. * See: https://github.com/sebastianbergmann/phpunit/issues/325
  15. */
  16. global $wpdb, $current_site, $current_blog, $wp_rewrite, $shortcode_tags, $wp, $phpmailer;
  17. if ( !is_readable( $config_file_path ) ) {
  18. die( "ERROR: wp-tests-config.php is missing! Please use wp-tests-config-sample.php to create a config file.\n" );
  19. }
  20. require_once $config_file_path;
  21. define( 'DIR_TESTDATA', dirname( __FILE__ ) . '/../data' );
  22. if ( ! defined( 'WP_TESTS_FORCE_KNOWN_BUGS' ) )
  23. define( 'WP_TESTS_FORCE_KNOWN_BUGS', false );
  24. // Cron tries to make an HTTP request to the blog, which always fails, because tests are run in CLI mode only
  25. define( 'DISABLE_WP_CRON', true );
  26. define( 'WP_MEMORY_LIMIT', -1 );
  27. define( 'WP_MAX_MEMORY_LIMIT', -1 );
  28. $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
  29. $_SERVER['HTTP_HOST'] = WP_TESTS_DOMAIN;
  30. $PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';
  31. if ( "1" == getenv( 'WP_MULTISITE' ) ||
  32. ( defined( 'WP_TESTS_MULTISITE') && WP_TESTS_MULTISITE ) ) {
  33. $multisite = true;
  34. } else {
  35. $multisite = false;
  36. }
  37. // Override the PHPMailer
  38. require_once( dirname( __FILE__ ) . '/mock-mailer.php' );
  39. $phpmailer = new MockPHPMailer();
  40. system( WP_PHP_BINARY . ' ' . escapeshellarg( dirname( __FILE__ ) . '/install.php' ) . ' ' . escapeshellarg( $config_file_path ) . ' ' . $multisite );
  41. if ( $multisite ) {
  42. echo "Running as multisite..." . PHP_EOL;
  43. define( 'MULTISITE', true );
  44. define( 'SUBDOMAIN_INSTALL', false );
  45. define( 'DOMAIN_CURRENT_SITE', WP_TESTS_DOMAIN );
  46. define( 'PATH_CURRENT_SITE', '/' );
  47. define( 'SITE_ID_CURRENT_SITE', 1 );
  48. define( 'BLOG_ID_CURRENT_SITE', 1 );
  49. $GLOBALS['base'] = '/';
  50. } else {
  51. echo "Running as single site... To run multisite, use -c tests/phpunit/multisite.xml" . PHP_EOL;
  52. }
  53. unset( $multisite );
  54. require_once dirname( __FILE__ ) . '/functions.php';
  55. // Preset WordPress options defined in bootstrap file.
  56. // Used to activate themes, plugins, as well as other settings.
  57. if(isset($GLOBALS['wp_tests_options'])) {
  58. function wp_tests_options( $value ) {
  59. $key = substr( current_filter(), strlen( 'pre_option_' ) );
  60. return $GLOBALS['wp_tests_options'][$key];
  61. }
  62. foreach ( array_keys( $GLOBALS['wp_tests_options'] ) as $key ) {
  63. tests_add_filter( 'pre_option_'.$key, 'wp_tests_options' );
  64. }
  65. }
  66. // Load WordPress
  67. require_once ABSPATH . '/wp-settings.php';
  68. // Delete any default posts & related data
  69. _delete_all_posts();
  70. require dirname( __FILE__ ) . '/testcase.php';
  71. require dirname( __FILE__ ) . '/testcase-xmlrpc.php';
  72. require dirname( __FILE__ ) . '/testcase-ajax.php';
  73. require dirname( __FILE__ ) . '/exceptions.php';
  74. require dirname( __FILE__ ) . '/utils.php';
  75. /**
  76. * A child class of the PHP test runner.
  77. *
  78. * Used to access the protected longOptions property, to parse the arguments
  79. * passed to the script.
  80. *
  81. * If it is determined that phpunit was called with a --group that corresponds
  82. * to an @ticket annotation (such as `phpunit --group 12345` for bugs marked
  83. * as #WP12345), then it is assumed that known bugs should not be skipped.
  84. *
  85. * If WP_TESTS_FORCE_KNOWN_BUGS is already set in wp-tests-config.php, then
  86. * how you call phpunit has no effect.
  87. */
  88. class WP_PHPUnit_Util_Getopt extends PHPUnit_Util_Getopt {
  89. protected $longOptions = array(
  90. 'exclude-group=',
  91. 'group=',
  92. );
  93. function __construct( $argv ) {
  94. array_shift( $argv );
  95. $options = array();
  96. while ( list( $i, $arg ) = each( $argv ) ) {
  97. try {
  98. if ( strlen( $arg ) > 1 && $arg[0] === '-' && $arg[1] === '-' ) {
  99. PHPUnit_Util_Getopt::parseLongOption( substr( $arg, 2 ), $this->longOptions, $options, $argv );
  100. }
  101. }
  102. catch ( PHPUnit_Framework_Exception $e ) {
  103. // Enforcing recognized arguments or correctly formed arguments is
  104. // not really the concern here.
  105. continue;
  106. }
  107. }
  108. $ajax_message = true;
  109. foreach ( $options as $option ) {
  110. switch ( $option[0] ) {
  111. case '--exclude-group' :
  112. $ajax_message = false;
  113. continue 2;
  114. case '--group' :
  115. $groups = explode( ',', $option[1] );
  116. foreach ( $groups as $group ) {
  117. if ( is_numeric( $group ) || preg_match( '/^(UT|Plugin)\d+$/', $group ) ) {
  118. WP_UnitTestCase::forceTicket( $group );
  119. }
  120. }
  121. $ajax_message = ! in_array( 'ajax', $groups );
  122. continue 2;
  123. }
  124. }
  125. if ( $ajax_message ) {
  126. echo "Not running ajax tests... To execute these, use --group ajax." . PHP_EOL;
  127. }
  128. }
  129. }
  130. new WP_PHPUnit_Util_Getopt( $_SERVER['argv'] );