Make WordPress Core

source: trunk/tests/phpunit/includes/bootstrap.php @ 37475

Last change on this file since 37475 was 37266, checked in by jeremyfelt, 9 years ago

Tests: Allow override of MULTISITE and SUBDOMAIN_INSTALL constants

Props rmccue.
Fixes #36567.

File size: 5.9 KB
Line 
1<?php
2/**
3 * Installs WordPress for running the tests and loads WordPress and the test libraries
4 */
5
6
7$config_file_path = dirname( dirname( __FILE__ ) );
8if ( ! file_exists( $config_file_path . '/wp-tests-config.php' ) ) {
9        // Support the config file from the root of the develop repository.
10        if ( basename( $config_file_path ) === 'phpunit' && basename( dirname( $config_file_path ) ) === 'tests' )
11                $config_file_path = dirname( dirname( $config_file_path ) );
12}
13$config_file_path .= '/wp-tests-config.php';
14
15/*
16 * Globalize some WordPress variables, because PHPUnit loads this file inside a function
17 * See: https://github.com/sebastianbergmann/phpunit/issues/325
18 */
19global $wpdb, $current_site, $current_blog, $wp_rewrite, $shortcode_tags, $wp, $phpmailer;
20
21if ( !is_readable( $config_file_path ) ) {
22        die( "ERROR: wp-tests-config.php is missing! Please use wp-tests-config-sample.php to create a config file.\n" );
23}
24require_once $config_file_path;
25require_once dirname( __FILE__ ) . '/functions.php';
26
27tests_reset__SERVER();
28
29define( 'WP_TESTS_TABLE_PREFIX', $table_prefix );
30define( 'DIR_TESTDATA', dirname( __FILE__ ) . '/../data' );
31
32define( 'WP_LANG_DIR', DIR_TESTDATA . '/languages' );
33
34if ( ! defined( 'WP_TESTS_FORCE_KNOWN_BUGS' ) )
35        define( 'WP_TESTS_FORCE_KNOWN_BUGS', false );
36
37// Cron tries to make an HTTP request to the blog, which always fails, because tests are run in CLI mode only
38define( 'DISABLE_WP_CRON', true );
39
40define( 'WP_MEMORY_LIMIT', -1 );
41define( 'WP_MAX_MEMORY_LIMIT', -1 );
42
43$PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';
44
45// Should we run in multisite mode?
46$multisite = '1' == getenv( 'WP_MULTISITE' );
47$multisite = $multisite || ( defined( 'WP_TESTS_MULTISITE') && WP_TESTS_MULTISITE );
48$multisite = $multisite || ( defined( 'MULTISITE' ) && MULTISITE );
49
50// Override the PHPMailer
51require_once( dirname( __FILE__ ) . '/mock-mailer.php' );
52$phpmailer = new MockPHPMailer();
53
54system( WP_PHP_BINARY . ' ' . escapeshellarg( dirname( __FILE__ ) . '/install.php' ) . ' ' . escapeshellarg( $config_file_path ) . ' ' . $multisite );
55
56if ( $multisite ) {
57        echo "Running as multisite..." . PHP_EOL;
58        defined( 'MULTISITE' ) or define( 'MULTISITE', true );
59        defined( 'SUBDOMAIN_INSTALL' ) or define( 'SUBDOMAIN_INSTALL', false );
60        $GLOBALS['base'] = '/';
61} else {
62        echo "Running as single site... To run multisite, use -c tests/phpunit/multisite.xml" . PHP_EOL;
63}
64unset( $multisite );
65
66$GLOBALS['_wp_die_disabled'] = false;
67// Allow tests to override wp_die
68tests_add_filter( 'wp_die_handler', '_wp_die_handler_filter' );
69
70// Preset WordPress options defined in bootstrap file.
71// Used to activate themes, plugins, as well as  other settings.
72if(isset($GLOBALS['wp_tests_options'])) {
73        function wp_tests_options( $value ) {
74                $key = substr( current_filter(), strlen( 'pre_option_' ) );
75                return $GLOBALS['wp_tests_options'][$key];
76        }
77
78        foreach ( array_keys( $GLOBALS['wp_tests_options'] ) as $key ) {
79                tests_add_filter( 'pre_option_'.$key, 'wp_tests_options' );
80        }
81}
82
83// Load WordPress
84require_once ABSPATH . '/wp-settings.php';
85
86// Delete any default posts & related data
87_delete_all_posts();
88
89require dirname( __FILE__ ) . '/testcase.php';
90require dirname( __FILE__ ) . '/testcase-rest-api.php';
91require dirname( __FILE__ ) . '/testcase-xmlrpc.php';
92require dirname( __FILE__ ) . '/testcase-ajax.php';
93require dirname( __FILE__ ) . '/testcase-canonical.php';
94require dirname( __FILE__ ) . '/exceptions.php';
95require dirname( __FILE__ ) . '/utils.php';
96require dirname( __FILE__ ) . '/spy-rest-server.php';
97
98/**
99 * A child class of the PHP test runner.
100 *
101 * Used to access the protected longOptions property, to parse the arguments
102 * passed to the script.
103 *
104 * If it is determined that phpunit was called with a --group that corresponds
105 * to an @ticket annotation (such as `phpunit --group 12345` for bugs marked
106 * as #WP12345), then it is assumed that known bugs should not be skipped.
107 *
108 * If WP_TESTS_FORCE_KNOWN_BUGS is already set in wp-tests-config.php, then
109 * how you call phpunit has no effect.
110 */
111class WP_PHPUnit_Util_Getopt extends PHPUnit_Util_Getopt {
112        protected $longOptions = array(
113          'exclude-group=',
114          'group=',
115        );
116        function __construct( $argv ) {
117                array_shift( $argv );
118                $options = array();
119                while ( list( $i, $arg ) = each( $argv ) ) {
120                        try {
121                                if ( strlen( $arg ) > 1 && $arg[0] === '-' && $arg[1] === '-' ) {
122                                        PHPUnit_Util_Getopt::parseLongOption( substr( $arg, 2 ), $this->longOptions, $options, $argv );
123                                }
124                        }
125                        catch ( PHPUnit_Framework_Exception $e ) {
126                                // Enforcing recognized arguments or correctly formed arguments is
127                                // not really the concern here.
128                                continue;
129                        }
130                }
131
132                $skipped_groups = array(
133                        'ajax' => true,
134                        'ms-files' => true,
135                        'external-http' => true,
136                );
137
138                foreach ( $options as $option ) {
139                        switch ( $option[0] ) {
140                                case '--exclude-group' :
141                                        foreach ( $skipped_groups as $group_name => $skipped ) {
142                                                $skipped_groups[ $group_name ] = false;
143                                        }
144                                        continue 2;
145                                case '--group' :
146                                        $groups = explode( ',', $option[1] );
147                                        foreach ( $groups as $group ) {
148                                                if ( is_numeric( $group ) || preg_match( '/^(UT|Plugin)\d+$/', $group ) ) {
149                                                        WP_UnitTestCase::forceTicket( $group );
150                                                }
151                                        }
152
153                                        foreach ( $skipped_groups as $group_name => $skipped ) {
154                                                if ( in_array( $group_name, $groups ) ) {
155                                                        $skipped_groups[ $group_name ] = false;
156                                                }
157                                        }
158                                        continue 2;
159                        }
160                }
161
162                $skipped_groups = array_filter( $skipped_groups );
163                foreach ( $skipped_groups as $group_name => $skipped ) {
164                        echo sprintf( 'Not running %1$s tests. To execute these, use --group %1$s.', $group_name ) . PHP_EOL;
165                }
166
167                if ( ! isset( $skipped_groups['external-http'] ) ) {
168                        echo PHP_EOL;
169                        echo 'External HTTP skipped tests can be caused by timeouts.' . PHP_EOL;
170                        echo 'If this changeset includes changes to HTTP, make sure there are no timeouts.' . PHP_EOL;
171                        echo PHP_EOL;
172                }
173    }
174}
175new WP_PHPUnit_Util_Getopt( $_SERVER['argv'] );
Note: See TracBrowser for help on using the repository browser.