bootstrap.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Installs WordPress for running the tests and loads WordPress and the test libraries
  4. */
  5. /**
  6. * Compatibility with PHPUnit 6+
  7. */
  8. if ( class_exists( 'PHPUnit\Runner\Version' ) ) {
  9. require_once __DIR__ . '/phpunit6/compat.php';
  10. }
  11. if ( defined( 'WP_TESTS_CONFIG_FILE_PATH' ) ) {
  12. $config_file_path = WP_TESTS_CONFIG_FILE_PATH;
  13. } else {
  14. $config_file_path = dirname( __DIR__ );
  15. if ( ! file_exists( $config_file_path . '/wp-tests-config.php' ) ) {
  16. // Support the config file from the root of the develop repository.
  17. if ( basename( $config_file_path ) === 'phpunit' && basename( dirname( $config_file_path ) ) === 'tests' ) {
  18. $config_file_path = dirname( dirname( $config_file_path ) );
  19. }
  20. }
  21. $config_file_path .= '/wp-tests-config.php';
  22. }
  23. /*
  24. * Globalize some WordPress variables, because PHPUnit loads this file inside a function.
  25. * See: https://github.com/sebastianbergmann/phpunit/issues/325
  26. */
  27. global $wpdb, $current_site, $current_blog, $wp_rewrite, $shortcode_tags, $wp, $phpmailer, $wp_theme_directories;
  28. if ( ! is_readable( $config_file_path ) ) {
  29. echo "Error: wp-tests-config.php is missing! Please use wp-tests-config-sample.php to create a config file.\n";
  30. exit( 1 );
  31. }
  32. require_once $config_file_path;
  33. require_once __DIR__ . '/functions.php';
  34. if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS && ! is_dir( ABSPATH ) ) {
  35. echo "Error: The /build/ directory is missing! Please run `npm run build` prior to running PHPUnit.\n";
  36. exit( 1 );
  37. }
  38. $phpunit_version = tests_get_phpunit_version();
  39. if ( version_compare( $phpunit_version, '5.4', '<' ) || version_compare( $phpunit_version, '8.0', '>=' ) ) {
  40. printf(
  41. "Error: Looks like you're using PHPUnit %s. WordPress requires at least PHPUnit 5.4 and is currently only compatible with PHPUnit up to 7.x.\n",
  42. $phpunit_version
  43. );
  44. echo "Please use the latest PHPUnit version from the 7.x branch.\n";
  45. exit( 1 );
  46. }
  47. // If running core tests, check if all the required PHP extensions are loaded before running the test suite.
  48. if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
  49. $required_extensions = array(
  50. 'gd',
  51. );
  52. $missing_extensions = array();
  53. foreach ( $required_extensions as $extension ) {
  54. if ( ! extension_loaded( $extension ) ) {
  55. $missing_extensions[] = $extension;
  56. }
  57. }
  58. if ( $missing_extensions ) {
  59. printf(
  60. "Error: The following required PHP extensions are missing from the testing environment: %s.\n",
  61. implode( ', ', $missing_extensions )
  62. );
  63. echo "Please make sure they are installed and enabled.\n",
  64. exit( 1 );
  65. }
  66. }
  67. $required_constants = array(
  68. 'WP_TESTS_DOMAIN',
  69. 'WP_TESTS_EMAIL',
  70. 'WP_TESTS_TITLE',
  71. 'WP_PHP_BINARY',
  72. );
  73. $missing_constants = array();
  74. foreach ( $required_constants as $constant ) {
  75. if ( ! defined( $constant ) ) {
  76. $missing_constants[] = $constant;
  77. }
  78. }
  79. if ( $missing_constants ) {
  80. printf(
  81. "Error: The following required constants are not defined: %s.\n",
  82. implode( ', ', $missing_constants )
  83. );
  84. echo "Please check out `wp-tests-config-sample.php` for an example.\n",
  85. exit( 1 );
  86. }
  87. tests_reset__SERVER();
  88. define( 'WP_TESTS_TABLE_PREFIX', $table_prefix );
  89. define( 'DIR_TESTDATA', __DIR__ . '/../data' );
  90. define( 'DIR_TESTROOT', realpath( dirname( __DIR__ ) ) );
  91. define( 'WP_LANG_DIR', DIR_TESTDATA . '/languages' );
  92. if ( ! defined( 'WP_TESTS_FORCE_KNOWN_BUGS' ) ) {
  93. define( 'WP_TESTS_FORCE_KNOWN_BUGS', false );
  94. }
  95. /*
  96. * Cron tries to make an HTTP request to the site, which always fails,
  97. * because tests are run in CLI mode only.
  98. */
  99. define( 'DISABLE_WP_CRON', true );
  100. define( 'WP_MEMORY_LIMIT', -1 );
  101. define( 'WP_MAX_MEMORY_LIMIT', -1 );
  102. define( 'REST_TESTS_IMPOSSIBLY_HIGH_NUMBER', 99999999 );
  103. $PHP_SELF = '/index.php';
  104. $GLOBALS['PHP_SELF'] = '/index.php';
  105. $_SERVER['PHP_SELF'] = '/index.php';
  106. // Should we run in multisite mode?
  107. $multisite = ( '1' === getenv( 'WP_MULTISITE' ) );
  108. $multisite = $multisite || ( defined( 'WP_TESTS_MULTISITE' ) && WP_TESTS_MULTISITE );
  109. $multisite = $multisite || ( defined( 'MULTISITE' ) && MULTISITE );
  110. // Override the PHPMailer.
  111. require_once __DIR__ . '/mock-mailer.php';
  112. $phpmailer = new MockPHPMailer( true );
  113. if ( ! defined( 'WP_DEFAULT_THEME' ) ) {
  114. define( 'WP_DEFAULT_THEME', 'default' );
  115. }
  116. $wp_theme_directories = array();
  117. if ( file_exists( DIR_TESTDATA . '/themedir1' ) ) {
  118. $wp_theme_directories[] = DIR_TESTDATA . '/themedir1';
  119. }
  120. if ( '1' !== getenv( 'WP_TESTS_SKIP_INSTALL' ) ) {
  121. $core_tests = ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) ? 'run_core_tests' : 'no_core_tests';
  122. $ms_tests = $multisite ? 'run_ms_tests' : 'no_ms_tests';
  123. system( WP_PHP_BINARY . ' ' . escapeshellarg( __DIR__ . '/install.php' ) . ' ' . escapeshellarg( $config_file_path ) . ' ' . $ms_tests . ' ' . $core_tests, $retval );
  124. if ( 0 !== $retval ) {
  125. exit( $retval );
  126. }
  127. }
  128. if ( $multisite ) {
  129. echo 'Running as multisite...' . PHP_EOL;
  130. defined( 'MULTISITE' ) or define( 'MULTISITE', true );
  131. defined( 'SUBDOMAIN_INSTALL' ) or define( 'SUBDOMAIN_INSTALL', false );
  132. $GLOBALS['base'] = '/';
  133. } else {
  134. echo 'Running as single site... To run multisite, use -c tests/phpunit/multisite.xml' . PHP_EOL;
  135. }
  136. unset( $multisite );
  137. $GLOBALS['_wp_die_disabled'] = false;
  138. // Allow tests to override wp_die().
  139. tests_add_filter( 'wp_die_handler', '_wp_die_handler_filter' );
  140. // Use the Spy REST Server instead of default.
  141. tests_add_filter( 'wp_rest_server_class', '_wp_rest_server_class_filter' );
  142. // Prevent updating translations asynchronously.
  143. tests_add_filter( 'async_update_translation', '__return_false' );
  144. // Disable background updates.
  145. tests_add_filter( 'automatic_updater_disabled', '__return_true' );
  146. // Preset WordPress options defined in bootstrap file.
  147. // Used to activate themes, plugins, as well as other settings.
  148. if ( isset( $GLOBALS['wp_tests_options'] ) ) {
  149. function wp_tests_options( $value ) {
  150. $key = substr( current_filter(), strlen( 'pre_option_' ) );
  151. return $GLOBALS['wp_tests_options'][ $key ];
  152. }
  153. foreach ( array_keys( $GLOBALS['wp_tests_options'] ) as $key ) {
  154. tests_add_filter( 'pre_option_' . $key, 'wp_tests_options' );
  155. }
  156. }
  157. // Load WordPress.
  158. require_once ABSPATH . '/wp-settings.php';
  159. // Delete any default posts & related data.
  160. _delete_all_posts();
  161. if ( version_compare( tests_get_phpunit_version(), '7.0', '>=' ) ) {
  162. require __DIR__ . '/phpunit7/testcase.php';
  163. } else {
  164. require __DIR__ . '/testcase.php';
  165. }
  166. require __DIR__ . '/testcase-rest-api.php';
  167. require __DIR__ . '/testcase-rest-controller.php';
  168. require __DIR__ . '/testcase-rest-post-type-controller.php';
  169. require __DIR__ . '/testcase-xmlrpc.php';
  170. require __DIR__ . '/testcase-ajax.php';
  171. require __DIR__ . '/testcase-block-supports.php';
  172. require __DIR__ . '/testcase-canonical.php';
  173. require __DIR__ . '/testcase-xml.php';
  174. require __DIR__ . '/exceptions.php';
  175. require __DIR__ . '/utils.php';
  176. require __DIR__ . '/spy-rest-server.php';
  177. require __DIR__ . '/class-wp-rest-test-search-handler.php';
  178. require __DIR__ . '/class-wp-rest-test-configurable-controller.php';
  179. require __DIR__ . '/class-wp-fake-block-type.php';
  180. require __DIR__ . '/class-wp-sitemaps-test-provider.php';
  181. require __DIR__ . '/class-wp-sitemaps-empty-test-provider.php';
  182. require __DIR__ . '/class-wp-sitemaps-large-test-provider.php';
  183. /**
  184. * A class to handle additional command line arguments passed to the script.
  185. *
  186. * If it is determined that phpunit was called with a --group that corresponds
  187. * to an @ticket annotation (such as `phpunit --group 12345` for bugs marked
  188. * as #WP12345), then it is assumed that known bugs should not be skipped.
  189. *
  190. * If WP_TESTS_FORCE_KNOWN_BUGS is already set in wp-tests-config.php, then
  191. * how you call phpunit has no effect.
  192. */
  193. class WP_PHPUnit_Util_Getopt {
  194. function __construct( $argv ) {
  195. $skipped_groups = array(
  196. 'ajax' => true,
  197. 'ms-files' => true,
  198. 'external-http' => true,
  199. );
  200. while ( current( $argv ) ) {
  201. $option = current( $argv );
  202. $value = next( $argv );
  203. switch ( $option ) {
  204. case '--exclude-group':
  205. foreach ( $skipped_groups as $group_name => $skipped ) {
  206. $skipped_groups[ $group_name ] = false;
  207. }
  208. continue 2;
  209. case '--group':
  210. $groups = explode( ',', $value );
  211. foreach ( $groups as $group ) {
  212. if ( is_numeric( $group ) || preg_match( '/^(UT|Plugin)\d+$/', $group ) ) {
  213. WP_UnitTestCase::forceTicket( $group );
  214. }
  215. }
  216. foreach ( $skipped_groups as $group_name => $skipped ) {
  217. if ( in_array( $group_name, $groups, true ) ) {
  218. $skipped_groups[ $group_name ] = false;
  219. }
  220. }
  221. continue 2;
  222. }
  223. }
  224. $skipped_groups = array_filter( $skipped_groups );
  225. foreach ( $skipped_groups as $group_name => $skipped ) {
  226. echo sprintf( 'Not running %1$s tests. To execute these, use --group %1$s.', $group_name ) . PHP_EOL;
  227. }
  228. if ( ! isset( $skipped_groups['external-http'] ) ) {
  229. echo PHP_EOL;
  230. echo 'External HTTP skipped tests can be caused by timeouts.' . PHP_EOL;
  231. echo 'If this changeset includes changes to HTTP, make sure there are no timeouts.' . PHP_EOL;
  232. echo PHP_EOL;
  233. }
  234. }
  235. }
  236. new WP_PHPUnit_Util_Getopt( $_SERVER['argv'] );