-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootstrap.php
246 lines (191 loc) · 5.2 KB
/
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* Tests Bootstrap
*
* @since 1.0.0
* @since Use `getcwd()` to set the `$plugin_dir` variable.
* @version 1.6.1
*/
require './vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
class LLMS_Tests_Bootstrap {
/**
* Path to the test assets directory
*
* @var string
*/
public $assets_dir;
/**
* __FILE__ reference, should be defined in the extending class
* @var [type]
*/
public $file = __FILE__;
/**
* Plugin Directory Path
* @var string
*/
public $plugin_dir;
/**
* Main PHP File for the plugin
* @var string
*/
public $plugin_main;
/**
* Name of the testing suite
* @var string
*/
public $suite_name = 'LifterLMS';
/**
* Tests Directory Path
* @var string
*/
public $tests_dir;
/**
* Determines if the LifterLMS core should be loaded
* @var bool
*/
public $use_core = true;
/**
* WP Tests Directory Path
* @var string
*/
public $wp_tests_dir;
/**
* Constructor
* @since 1.0.0
* @version 1.2.0
*/
public function __construct() {
echo 'Welcome to the ' . $this->suite_name . ' Test Suite' . PHP_EOL . PHP_EOL . PHP_EOL;
ini_set( 'display_errors','on' );
error_reporting( E_ALL );
// Ensure server variable is set for WP email functions.
if ( ! isset( $_SERVER['SERVER_NAME'] ) ) {
$_SERVER['SERVER_NAME'] = 'localhost';
}
$this->tests_dir = dirname( $this->file );
$this->plugin_dir = getcwd();
$this->wp_tests_dir = getenv( 'WP_TESTS_DIR' ) ? getenv( 'WP_TESTS_DIR' ) : 'tmp/tests/wordpress-tests-lib';
$this->assets_dir = $this->tests_dir . '/assets';
// load test function so tests_add_filter() is available
require_once $this->wp_tests_dir . '/includes/functions.php';
// Include llms-tests lib functions.
require_once dirname( __FILE__ ) . '/framework/functions-llms-tests.php';
// Include add-on tests functions (if they exist).
if ( file_exists( $this->tests_dir . '/framework/functions-llms-tests.php' ) ) {
require_once $this->tests_dir . '/framework/functions-llms-tests.php';
}
// Load the plugin.
tests_add_filter( 'muplugins_loaded', array( $this, 'load' ) );
// Install the plugin.
tests_add_filter( 'setup_theme', array( $this, 'install' ) );
tests_add_filter( 'setup_theme', array( $this, 'install_after' ) );
// Load the WP testing environment.
require_once( $this->wp_tests_dir . '/includes/bootstrap.php' );
// Load any includes.
$this->includes();
// Expose the bootstrap class.
global $_llms_tests_bootstrap;
$_llms_tests_bootstrap = $this;
}
/**
* Load test suite files/includes
*
* @since 1.0.0
*
* @return void
*/
public function includes() {
$dir = dirname( __FILE__ );
// Framework files included with llms-tests.
$files = array_merge(
glob( $dir . '/framework/traits/*.php' ),
glob( $dir . '/framework/*.php' ),
glob( $dir . '/framework/exceptions/*.php' ),
glob( $dir . '/framework/factory/*.php' )
);
// Framework files from the add-on.
if ( file_exists( $this->tests_dir . '/framework' ) ) {
// Top level files in the framework dir.
$files = array_merge( $files, glob( $this->tests_dir . '/framework/*.php' ) );
foreach ( glob( $this->tests_dir . '/framework/**', GLOB_ONLYDIR ) as $dir ) {
$files = array_merge( $files, glob( $dir . '/*.php' ) );
}
}
foreach ( $files as $file ) {
require_once $file;
}
}
/**
* Install the plugin
* @return void
* @since 1.0.0
* @version 1.1.0
*/
public function install() {
$this->uninstall();
echo 'Installing '. $this->suite_name .'...' . PHP_EOL;
if ( $this->use_core ) {
LLMS_Install::install();
}
}
/**
* Runs immediately after $this->install()
*
* @return void
* @since 1.0.0
* @version 1.0.0
*/
public function install_after() {
// Reload capabilities after install, see https://core.trac.wordpress.org/ticket/28374.
$GLOBALS['wp_roles'] = null;
wp_roles();
}
/**
* Load the plugin
* @return void
* @since 1.0.0
* @version 1.0.0
*/
public function load() {
if ( $this->use_core ) {
define( 'LLMS_USE_PHP_SESSIONS', true );
define( 'LLMS_PLUGIN_DIR', WP_PLUGIN_DIR . '/lifterlms/' );
$this->load_plugin( 'lifterlms', 'lifterlms.php' );
}
if ( $this->plugin_main ) {
require_once( $this->plugin_dir . '/' . $this->plugin_main );
}
}
/**
* Load a plugin dependency
*
* @param string $dir directory name for the plugin (eg lifterlms).
* @param string $file filename for the plugin (eg: lifterlms.php).
* @return void
* @since 1.0.0
* @version 1.0.0
*/
public function load_plugin( $dir, $file ) {
if ( file_exists( WP_PLUGIN_DIR . '/' . $dir ) ) {
require_once( WP_PLUGIN_DIR . '/' . $dir . '/' . $file );
}
}
/**
* Uninstall the plugin.
* @return void
* @since 1.0.0
* @version 1.0.0
*/
public function uninstall() {
echo 'Removing '. $this->suite_name .'...' . PHP_EOL;
define( 'WP_UNINSTALL_PLUGIN', true );
if ( $this->use_core ) {
define( 'LLMS_REMOVE_ALL_DATA', true );
include( WP_PLUGIN_DIR . '/lifterlms/uninstall.php' );
}
// Clean existing install first.
if ( file_exists( $this->plugin_dir . '/uninstall.php' ) ) {
require_once $this->plugin_dir . '/uninstall.php';
}
}
}