This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
phpunit-plugin-bootstrap.php
90 lines (78 loc) · 3.15 KB
/
phpunit-plugin-bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
// Determine if we should update the content and plugin paths.
if ( file_exists( dirname( __DIR__ ) . '/wp-load.php' ) ) {
define( 'WP_CONTENT_DIR', dirname( __DIR__ ) . '/wp-content/' );
} else if ( file_exists( '../../../wp-content' ) ) {
define( 'WP_CONTENT_DIR', dirname( dirname( dirname( getcwd() ) ) ) . '/wp-content/' );
}
if ( defined( 'WP_CONTENT_DIR' ) && ! defined( 'WP_PLUGIN_DIR' ) ) {
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . 'plugins/' );
}
if ( file_exists( __DIR__ . '/../phpunit-plugin-bootstrap.project.php' ) ) {
require_once( __DIR__ . '/../phpunit-plugin-bootstrap.project.php' );
}
global $_plugin_file;
$_tests_dir = getenv( 'WP_TESTS_DIR' );
// Travis CI & Vagrant SSH tests directory.
if ( empty( $_tests_dir ) ) {
$_tests_dir = '/tmp/wordpress-tests';
}
// Relative path to Core tests directory.
if ( ! file_exists( $_tests_dir . '/includes/' ) ) {
$_tests_dir = '../../../../tests/phpunit';
}
if ( ! file_exists( $_tests_dir . '/includes/' ) ) {
trigger_error( 'Unable to locate wordpress-tests-lib', E_USER_ERROR );
}
require_once $_tests_dir . '/includes/functions.php';
$_plugin_dir = getcwd();
foreach ( glob( $_plugin_dir . '/*.php' ) as $_plugin_file_candidate ) {
// @codingStandardsIgnoreStart
$_plugin_file_src = file_get_contents( $_plugin_file_candidate );
// @codingStandardsIgnoreEnd
if ( preg_match( '/Plugin\s*Name\s*:/', $_plugin_file_src ) ) {
$_plugin_file = $_plugin_file_candidate;
break;
}
}
if ( ! isset( $_plugin_file ) ) {
trigger_error( 'Unable to locate a file containing a plugin metadata block.', E_USER_ERROR );
}
unset( $_plugin_dir, $_plugin_file_candidate, $_plugin_file_src );
/**
* Force plugins defined in a constant (supplied by phpunit.xml) to be active at runtime.
*
* @filter site_option_active_sitewide_plugins
* @filter option_active_plugins
*
* @param array $active_plugins
* @return array
*/
function xwp_filter_active_plugins_for_phpunit( $active_plugins ) {
$forced_active_plugins = array();
if ( file_exists( WP_CONTENT_DIR . '/themes/vip/plugins/vip-init.php' ) && defined( 'WP_TEST_VIP_QUICKSTART_ACTIVATED_PLUGINS' ) ) {
$forced_active_plugins = preg_split( '/\s*,\s*/', WP_TEST_VIP_QUICKSTART_ACTIVATED_PLUGINS );
} else if ( defined( 'WP_TEST_ACTIVATED_PLUGINS' ) ) {
$forced_active_plugins = preg_split( '/\s*,\s*/', WP_TEST_ACTIVATED_PLUGINS );
}
if ( ! empty( $forced_active_plugins ) ) {
foreach ( $forced_active_plugins as $forced_active_plugin ) {
$active_plugins[] = $forced_active_plugin;
}
}
return $active_plugins;
}
tests_add_filter( 'site_option_active_sitewide_plugins', 'xwp_filter_active_plugins_for_phpunit' );
tests_add_filter( 'option_active_plugins', 'xwp_filter_active_plugins_for_phpunit' );
function xwp_unit_test_load_plugin_file() {
global $_plugin_file;
// Force vip-init.php to be loaded on VIP quickstart
if ( file_exists( WP_CONTENT_DIR . '/themes/vip/plugins/vip-init.php' ) ) {
require_once( WP_CONTENT_DIR . '/themes/vip/plugins/vip-init.php' );
}
// Load this plugin
require_once $_plugin_file;
unset( $_plugin_file );
}
tests_add_filter( 'muplugins_loaded', 'xwp_unit_test_load_plugin_file' );
require $_tests_dir . '/includes/bootstrap.php';