Skip to content

Commit

Permalink
MDL-41592 behat: Allow alt wwwroot to be set for acceptance testing
Browse files Browse the repository at this point in the history
To set add $CFG->behat_wwwroot = 'http://alturl'; in your config.php
  • Loading branch information
Aaron Barnes authored and David Monllao committed Dec 2, 2013
1 parent ea04def commit 64c5119
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions config-dist.php
Expand Up @@ -589,8 +589,8 @@
// $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';
//
Expand Down
2 changes: 1 addition & 1 deletion lib/behat/classes/behat_command.php
Expand Up @@ -108,7 +108,7 @@ public static function behat_setup_problem($checkphp = false) {

// 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');
}

Expand Down
37 changes: 21 additions & 16 deletions lib/setup.php
Expand Up @@ -91,20 +91,11 @@
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
Expand All @@ -113,10 +104,24 @@

$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');
Expand Down

0 comments on commit 64c5119

Please sign in to comment.