-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
451 lines (404 loc) · 16.1 KB
/
lib.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Behat basic functions
*
* It does not include MOODLE_INTERNAL because is part of the bootstrap.
*
* This script should not be usually included, neither any of its functions
* used, within mooodle code at all. It's for exclusive use of behat and
* moodle setup.php. For places requiring a different/special behavior
* needing to check if are being run as part of behat tests, use:
* if (defined('BEHAT_SITE_RUNNING')) { ...
*
* @package core
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../testing/lib.php');
define('BEHAT_EXITCODE_CONFIG', 250);
define('BEHAT_EXITCODE_REQUIREMENT', 251);
define('BEHAT_EXITCODE_PERMISSIONS', 252);
define('BEHAT_EXITCODE_REINSTALL', 253);
define('BEHAT_EXITCODE_INSTALL', 254);
define('BEHAT_EXITCODE_INSTALLED', 256);
/**
* The behat test site fullname and shortname.
*/
define('BEHAT_PARALLEL_SITE_NAME', "behatrun");
/**
* Exits with an error code
*
* @param mixed $errorcode
* @param string $text
* @return void Stops execution with error code
*/
function behat_error($errorcode, $text = '') {
// Adding error prefixes.
switch ($errorcode) {
case BEHAT_EXITCODE_CONFIG:
$text = 'Behat config error: ' . $text;
break;
case BEHAT_EXITCODE_REQUIREMENT:
$text = 'Behat requirement not satisfied: ' . $text;
break;
case BEHAT_EXITCODE_PERMISSIONS:
$text = 'Behat permissions problem: ' . $text . ', check the permissions';
break;
case BEHAT_EXITCODE_REINSTALL:
$path = testing_cli_argument_path('/admin/tool/behat/cli/init.php');
$text = "Reinstall Behat: ".$text.", use:\n php ".$path;
break;
case BEHAT_EXITCODE_INSTALL:
$path = testing_cli_argument_path('/admin/tool/behat/cli/init.php');
$text = "Install Behat before enabling it, use:\n php ".$path;
break;
case BEHAT_EXITCODE_INSTALLED:
$text = "The Behat site is already installed";
break;
default:
$text = 'Unknown error ' . $errorcode . ' ' . $text;
break;
}
testing_error($errorcode, $text);
}
/**
* PHP errors handler to use when running behat tests.
*
* Adds specific CSS classes to identify
* the messages.
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @param array $errcontext
* @return bool
*/
function behat_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
// If is preceded by an @ we don't show it.
if (!error_reporting()) {
return true;
}
// This error handler receives E_ALL | E_STRICT, running the behat test site the debug level is
// set to DEVELOPER and will always include E_NOTICE,E_USER_NOTICE... as part of E_ALL, if the current
// error_reporting() value does not include one of those levels is because it has been forced through
// the moodle code (see fix_utf8() for example) in that cases we respect the forced error level value.
$respect = array(E_NOTICE, E_USER_NOTICE, E_STRICT, E_WARNING, E_USER_WARNING);
foreach ($respect as $respectable) {
// If the current value does not include this kind of errors and the reported error is
// at that level don't print anything.
if ($errno == $respectable && !(error_reporting() & $respectable)) {
return true;
}
}
// Using the default one in case there is a fatal catchable error.
default_error_handler($errno, $errstr, $errfile, $errline, $errcontext);
switch ($errno) {
case E_USER_ERROR:
$errnostr = 'Fatal error';
break;
case E_WARNING:
case E_USER_WARNING:
$errnostr = 'Warning';
break;
case E_NOTICE:
case E_USER_NOTICE:
case E_STRICT:
$errnostr = 'Notice';
break;
case E_RECOVERABLE_ERROR:
$errnostr = 'Catchable';
break;
default:
$errnostr = 'Unknown error type';
}
// Wrapping the output.
echo '<div class="phpdebugmessage" data-rel="phpdebugmessage">' . PHP_EOL;
echo "$errnostr: $errstr in $errfile on line $errline" . PHP_EOL;
echo '</div>';
// Also use the internal error handler so we keep the usual behaviour.
return false;
}
/**
* Restrict the config.php settings allowed.
*
* When running the behat features the config.php
* settings should not affect the results.
*
* @return void
*/
function behat_clean_init_config() {
global $CFG;
$allowed = array_flip(array(
'wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
'umaskpermissions', 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix',
'dboptions', 'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword',
'proxybypass', 'theme', 'pathtogs', 'pathtoclam', 'pathtodu', 'aspellpath', 'pathtodot', 'skiplangupgrade',
'altcacheconfigpath'
));
// Add extra allowed settings.
if (!empty($CFG->behat_extraallowedsettings)) {
$allowed = array_merge($allowed, array_flip($CFG->behat_extraallowedsettings));
}
// Also allowing behat_ prefixed attributes.
foreach ($CFG as $key => $value) {
if (!isset($allowed[$key]) && strpos($key, 'behat_') !== 0) {
unset($CFG->{$key});
}
}
}
/**
* Checks that the behat config vars are properly set.
*
* @return void Stops execution with error code if something goes wrong.
*/
function behat_check_config_vars() {
global $CFG;
// Verify prefix value.
if (empty($CFG->behat_prefix)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'Define $CFG->behat_prefix in config.php');
}
if (!empty($CFG->prefix) and $CFG->behat_prefix == $CFG->prefix) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_prefix in config.php must be different from $CFG->prefix');
}
if (!empty($CFG->phpunit_prefix) and $CFG->behat_prefix == $CFG->phpunit_prefix) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_prefix in config.php must be different from $CFG->phpunit_prefix');
}
// Verify behat wwwroot value.
if (empty($CFG->behat_wwwroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'Define $CFG->behat_wwwroot in config.php');
}
if (!empty($CFG->wwwroot) and $CFG->behat_wwwroot == $CFG->wwwroot) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_wwwroot in config.php must be different from $CFG->wwwroot');
}
// Verify behat dataroot value.
if (empty($CFG->behat_dataroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'Define $CFG->behat_dataroot in config.php');
}
clearstatcache();
if (!file_exists($CFG->behat_dataroot)) {
$permissions = isset($CFG->directorypermissions) ? $CFG->directorypermissions : 02777;
umask(0);
if (!mkdir($CFG->behat_dataroot, $permissions, true)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, '$CFG->behat_dataroot directory can not be created');
}
}
$CFG->behat_dataroot = realpath($CFG->behat_dataroot);
if (empty($CFG->behat_dataroot) or !is_dir($CFG->behat_dataroot) or !is_writable($CFG->behat_dataroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_dataroot in config.php must point to an existing writable directory');
}
if (!empty($CFG->dataroot) and $CFG->behat_dataroot == realpath($CFG->dataroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_dataroot in config.php must be different from $CFG->dataroot');
}
if (!empty($CFG->phpunit_dataroot) and $CFG->behat_dataroot == realpath($CFG->phpunit_dataroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_dataroot in config.php must be different from $CFG->phpunit_dataroot');
}
}
/**
* Should we switch to the test site data?
* @return bool
*/
function behat_is_test_site() {
global $CFG;
if (defined('BEHAT_UTIL')) {
// This is the admin tool that installs/drops the test site install.
return true;
}
if (defined('BEHAT_TEST')) {
// This is the main vendor/bin/behat script.
return true;
}
if (empty($CFG->behat_wwwroot)) {
return false;
}
if (isset($_SERVER['REMOTE_ADDR']) and behat_is_requested_url($CFG->behat_wwwroot)) {
// Something is accessing the web server like a real browser.
return true;
}
return false;
}
/**
* Fix variables for parallel behat testing.
* - behat_wwwroot = behat_wwwroot{behatrunprocess}
* - behat_dataroot = behat_dataroot{behatrunprocess}
* - behat_prefix = behat_prefix.{behatrunprocess}_ (For oracle it will be firstletter of prefix and behatrunprocess)
**/
function behat_update_vars_for_process() {
global $CFG;
$allowedconfigoverride = array('dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'behat_prefix',
'behat_wwwroot', 'behat_dataroot');
$behatrunprocess = behat_get_run_process();
$CFG->behatrunprocess = $behatrunprocess;
if ($behatrunprocess) {
if (empty($CFG->behat_parallel_run[$behatrunprocess - 1]['behat_wwwroot'])) {
// Set www root for run process.
if (isset($CFG->behat_wwwroot) &&
!preg_match("#/" . BEHAT_PARALLEL_SITE_NAME . $behatrunprocess . "\$#", $CFG->behat_wwwroot)) {
$CFG->behat_wwwroot .= "/" . BEHAT_PARALLEL_SITE_NAME . $behatrunprocess;
}
}
if (empty($CFG->behat_parallel_run[$behatrunprocess - 1]['behat_dataroot'])) {
// Set behat_dataroot.
if (!preg_match("#" . $behatrunprocess . "\$#", $CFG->behat_dataroot)) {
$CFG->behat_dataroot .= $behatrunprocess;
}
}
// Set behat_prefix for db, just suffix run process number, to avoid max length exceed.
// For oracle only 2 letter prefix is possible.
// NOTE: This will not work for parallel process > 9.
if ($CFG->dbtype === 'oci') {
$CFG->behat_prefix = substr($CFG->behat_prefix, 0, 1);
$CFG->behat_prefix .= "{$behatrunprocess}";
} else {
$CFG->behat_prefix .= "{$behatrunprocess}_";
}
if (!empty($CFG->behat_parallel_run[$behatrunprocess - 1])) {
// Override allowed config vars.
foreach ($allowedconfigoverride as $config) {
if (isset($CFG->behat_parallel_run[$behatrunprocess - 1][$config])) {
$CFG->$config = $CFG->behat_parallel_run[$behatrunprocess - 1][$config];
}
}
}
}
}
/**
* Checks if the URL requested by the user matches the provided argument
*
* @param string $url
* @return bool Returns true if it matches.
*/
function behat_is_requested_url($url) {
$parsedurl = parse_url($url . '/');
$parsedurl['port'] = isset($parsedurl['port']) ? $parsedurl['port'] : 80;
$parsedurl['path'] = rtrim($parsedurl['path'], '/');
// Removing the port.
$pos = strpos($_SERVER['HTTP_HOST'], ':');
if ($pos !== false) {
$requestedhost = substr($_SERVER['HTTP_HOST'], 0, $pos);
} else {
$requestedhost = $_SERVER['HTTP_HOST'];
}
// The path should also match.
if (empty($parsedurl['path'])) {
$matchespath = true;
} else if (strpos($_SERVER['SCRIPT_NAME'], $parsedurl['path']) === 0) {
$matchespath = true;
}
// The host and the port should match
if ($parsedurl['host'] == $requestedhost && $parsedurl['port'] == $_SERVER['SERVER_PORT'] && !empty($matchespath)) {
return true;
}
return false;
}
/**
* Get behat run process from either $_SERVER or command config.
*
* @return bool|int false if single run, else run process number.
*/
function behat_get_run_process() {
global $argv, $CFG;
$behatrunprocess = false;
// Get behat run process, if set.
if (defined('BEHAT_CURRENT_RUN') && BEHAT_CURRENT_RUN) {
$behatrunprocess = BEHAT_CURRENT_RUN;
} else if (!empty($_SERVER['REMOTE_ADDR'])) {
// Try get it from config if present.
if (!empty($CFG->behat_parallel_run)) {
foreach ($CFG->behat_parallel_run as $run => $behatconfig) {
if (isset($behatconfig['behat_wwwroot']) && behat_is_requested_url($behatconfig['behat_wwwroot'])) {
$behatrunprocess = $run + 1; // We start process from 1.
break;
}
}
}
// Check if parallel site prefix is used.
if (empty($behatrunprocess) && preg_match('#/' . BEHAT_PARALLEL_SITE_NAME . '(.+?)/#', $_SERVER['REQUEST_URI'])) {
$dirrootrealpath = str_replace("\\", "/", realpath($CFG->dirroot));
$serverrealpath = str_replace("\\", "/", realpath($_SERVER['SCRIPT_FILENAME']));
$afterpath = str_replace($dirrootrealpath.'/', '', $serverrealpath);
if (!$behatrunprocess = preg_filter("#.*/" . BEHAT_PARALLEL_SITE_NAME . "(.+?)/$afterpath#", '$1',
$_SERVER['SCRIPT_FILENAME'])) {
throw new Exception("Unable to determine behat process [afterpath=" . $afterpath .
", scriptfilename=" . $_SERVER['SCRIPT_FILENAME'] . "]!");
}
}
} else if (defined('BEHAT_TEST') || defined('BEHAT_UTIL')) {
if ($match = preg_filter('#--run=(.+)#', '$1', $argv)) {
$behatrunprocess = reset($match);
} else if ($k = array_search('--config', $argv)) {
$behatconfig = str_replace("\\", "/", $argv[$k + 1]);
// Try get it from config if present.
if (!empty($CFG->behat_parallel_run)) {
foreach ($CFG->behat_parallel_run as $run => $parallelconfig) {
if (!empty($parallelconfig['behat_dataroot']) &&
$parallelconfig['behat_dataroot'] . '/behat/behat.yml' == $behatconfig) {
$behatrunprocess = $run + 1; // We start process from 1.
break;
}
}
}
// Check if default behat datroot increment was done.
if (empty($behatrunprocess)) {
$behatdataroot = str_replace("\\", "/", $CFG->behat_dataroot);
$behatrunprocess = preg_filter("#^{$behatdataroot}" . "(.+?)[/|\\\]behat[/|\\\]behat\.yml#", '$1',
$behatconfig);
}
}
}
return $behatrunprocess;
}
/**
* Execute commands in parallel.
*
* @param array $cmds list of commands to be executed.
* @param string $cwd absolute path of working directory.
* @return array list of processes.
*/
function cli_execute_parallel($cmds, $cwd = null) {
require_once(__DIR__ . "/../../vendor/autoload.php");
$processes = array();
// Create child process.
foreach ($cmds as $name => $cmd) {
$process = new Symfony\Component\Process\Process($cmd);
$process->setWorkingDirectory($cwd);
$process->setTimeout(null);
$processes[$name] = $process;
$processes[$name]->start();
// If error creating process then exit.
if ($processes[$name]->getStatus() !== 'started') {
echo "Error starting process: $name";
foreach ($processes[$name] as $process) {
if ($process) {
$process->signal(SIGKILL);
}
}
exit(1);
}
}
return $processes;
}