/** * 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; }
/** * Should we switch to the test site data? * @return bool */ function behat_is_test_site() { global $CFG; if (defined('BEHAT_UTIL')) { // This is the framework 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; }
} elseif (php_sapi_name() === 'cli-server') { // If we are using the built-in server we use the provided $CFG->behat_wwwroot // value or the default one if $CFG->behat_wwwroot is not set, only if it matches // the requested URL. if (behat_is_requested_url($defaultbehatwwwroot)) { $behatwwwroot = $defaultbehatwwwroot; } } elseif (defined('BEHAT_TEST')) { // This is when moodle codebase runs through vendor/bin/behat, we "are not supposed" // to need a wwwroot, but before using the production one we should set something else // as an alternative. $behatwwwroot = $defaultbehatwwwroot; } elseif (!empty($CFG->behat_wwwroot) && !empty($_SERVER['HTTP_HOST'])) { // If $CFG->behat_wwwroot was set and matches the requested URL we // use $CFG->behat_wwwroot as our wwwroot. if (behat_is_requested_url($CFG->behat_wwwroot)) { $behatwwwroot = $CFG->behat_wwwroot; } } // If we found a proper behatwwwroot then we consider the behat test as requested. $testenvironmentrequested = !empty($behatwwwroot); // Only switch to test environment if it has been enabled. $CFG->behat_dataroot = realpath($CFG->behat_dataroot); $testenvironmentenabled = file_exists($CFG->behat_dataroot . '/behat/test_environment_enabled.txt'); if ($testenvironmentenabled && $testenvironmentrequested) { // Now we know which one will be our behat wwwroot. $CFG->behat_wwwroot = $behatwwwroot; // Checking the integrity of the provided $CFG->behat_* vars and the // selected wwwroot to prevent conflicts with production and phpunit environments. behat_check_config_vars(); // Constant used to inform that the behat test site is being used,