diff --git a/config-dist.php b/config-dist.php index 08a50a0a2d4..4912f73c6c9 100644 --- a/config-dist.php +++ b/config-dist.php @@ -589,8 +589,8 @@ $CFG->admin = 'admin'; // $CFG->behat_prefix = 'bht_'; // $CFG->behat_dataroot = '/home/example/bht_moodledata'; // -// Behat uses http://localhost:8000 as default URL to run -// the acceptance tests, you can override this value. +// To set a seperate wwwroot for Behat to use, set it below. +// This is set automatically elsewhere when using PHP's built in webserver. // Example: // $CFG->behat_wwwroot = 'http://192.168.1.250:8000'; // diff --git a/lib/behat/classes/behat_command.php b/lib/behat/classes/behat_command.php index fb149ad4ffa..a40a71911a6 100644 --- a/lib/behat/classes/behat_command.php +++ b/lib/behat/classes/behat_command.php @@ -108,7 +108,7 @@ class behat_command { // We don't check the PHP version if $CFG->behat_switchcompletely has been enabled. // Here we are in CLI. - if (empty($CFG->behat_switchcompletely) && $checkphp && version_compare(PHP_VERSION, '5.4.0', '<')) { + if (empty($CFG->behat_switchcompletely) && empty($CFG->behat_wwwroot) && $checkphp && version_compare(PHP_VERSION, '5.4.0', '<')) { behat_error(BEHAT_EXITCODE_REQUIREMENT, 'PHP 5.4 is required. See config-dist.php for possible alternatives'); } diff --git a/lib/setup.php b/lib/setup.php index 40ae817a86c..55656630fdf 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -91,20 +91,11 @@ if (!isset($CFG->wwwroot) or $CFG->wwwroot === 'http://example.com/moodle') { exit(1); } -// Ignore $CFG->behat_wwwroot and use the same wwwroot. -if (!empty($CFG->behat_switchcompletely)) { - $CFG->behat_wwwroot = $CFG->wwwroot; - -} else if (empty($CFG->behat_wwwroot)) { - // Default URL for acceptance testing, only accessible from localhost. - $CFG->behat_wwwroot = 'http://localhost:8000'; -} - - // Test environment is requested if: -// * Behat is running (constant set hooking the behat init process before requiring config.php). -// * If we are accessing though the built-in web server (cli-server). // * If $CFG->behat_switchcompletely has been set (maintains CLI scripts behaviour, which ATM is only preventive). +// * If we are accessing though the built-in web server (cli-server). +// * Behat is running (constant set hooking the behat init process before requiring config.php). +// * If $CFG->behat_wwwroot has been set and the hostname/port match what the page was requested with. // Test environment is enabled if: // * User has previously enabled through admin/tool/behat/cli/util.php --enable. // Both are required to switch to test mode @@ -113,10 +104,24 @@ if (!defined('BEHAT_SITE_RUNNING') && !empty($CFG->behat_dataroot) && $CFG->behat_dataroot = realpath($CFG->behat_dataroot); - $switchcompletely = !empty($CFG->behat_switchcompletely) && php_sapi_name() !== 'cli'; - $builtinserver = php_sapi_name() === 'cli-server'; - $behatrunning = defined('BEHAT_TEST'); - $testenvironmentrequested = $switchcompletely || $builtinserver || $behatrunning; + if (!empty($CFG->behat_switchcompletely) && php_sapi_name() !== 'cli') { + $behatswitchcompletely = true; + $CFG->behat_wwwroot = $CFG->wwwroot; + + } elseif (php_sapi_name() === 'cli-server') { + $behatbuiltinserver = true; + $CFG->behat_wwwroot = 'http://localhost:'.$_SERVER['SERVER_PORT']; + + } elseif (defined('BEHAT_TEST')) { + $behatrunning = true; + + } elseif (!empty($CFG->behat_wwwroot) && !empty($_SERVER['HTTP_HOST'])) { + $behaturl = parse_url($CFG->behat_wwwroot.'/'); + $behaturl['port'] = isset($behaturl['port']) ? $behaturl['port'] : 80; + $behataltwww = ($behaturl['host'] == $_SERVER['HTTP_HOST']) && ($behaturl['port'] == $_SERVER['SERVER_PORT']); + } + + $testenvironmentrequested = !empty($behatswitchcompletely) || !empty($behatbuiltinserver) || !empty($behatrunning) || !empty($behataltwww); // Only switch to test environment if it has been enabled. $testenvironmentenabled = file_exists($CFG->behat_dataroot . '/behat/test_environment_enabled.txt');