MDL-39478 behat: New constant to detect when the test site is running

Also removing an unused var and method.
This commit is contained in:
David Monllao 2013-05-03 14:03:49 +08:00
parent 081bfdc5ff
commit cfcbc34a61
4 changed files with 24 additions and 26 deletions

View File

@ -250,7 +250,6 @@ class behat_util extends testing_util {
* To check is the current script is running in the test * To check is the current script is running in the test
* environment * environment
* *
* @see tool_behat::is_using_test_environment()
* @return bool * @return bool
*/ */
public static function is_test_mode_enabled() { public static function is_test_mode_enabled() {
@ -263,20 +262,6 @@ class behat_util extends testing_util {
return false; return false;
} }
/**
* Returns true if Moodle is currently running with the test database and dataroot
* @return bool
*/
public static function is_using_test_environment() {
global $CFG;
if (!empty($CFG->originaldataroot)) {
return true;
}
return false;
}
/** /**
* Returns the path to the file which specifies if test environment is enabled * Returns the path to the file which specifies if test environment is enabled
* @return string * @return string

View File

@ -1128,7 +1128,7 @@ function session_set_user($user) {
} }
sesskey(); // init session key sesskey(); // init session key
if (PHPUNIT_TEST || defined('BEHAT_RUNNING')) { if (PHPUNIT_TEST || defined('BEHAT_TEST')) {
// phpunit tests use reversed reference // phpunit tests use reversed reference
global $USER; global $USER;
$USER = $_SESSION['USER']; $USER = $_SESSION['USER'];

View File

@ -113,16 +113,25 @@ if (!empty($CFG->behat_dataroot) && !empty($CFG->behat_prefix) && file_exists($C
$switchcompletely = !empty($CFG->behat_switchcompletely) && php_sapi_name() !== 'cli'; $switchcompletely = !empty($CFG->behat_switchcompletely) && php_sapi_name() !== 'cli';
$builtinserver = php_sapi_name() === 'cli-server'; $builtinserver = php_sapi_name() === 'cli-server';
$behatrunning = defined('BEHAT_RUNNING'); $behatrunning = defined('BEHAT_TEST');
$testenvironmentrequested = $switchcompletely || $builtinserver || $behatrunning; $testenvironmentrequested = $switchcompletely || $builtinserver || $behatrunning;
// Only switch to test environment if it has been enabled. // Only switch to test environment if it has been enabled.
$testenvironmentenabled = file_exists($CFG->behat_dataroot . '/behat/test_environment_enabled.txt'); $testenvironmentenabled = file_exists($CFG->behat_dataroot . '/behat/test_environment_enabled.txt');
if ($testenvironmentenabled && $testenvironmentrequested) { if ($testenvironmentenabled && $testenvironmentrequested) {
// Constant used to inform that the behat test site is being used,
// this includes all the processes executed by the behat CLI command like
// the site reset, the steps executed by the browser drivers when simulating
// a user session and a real session when browsing manually to $CFG->behat_wwwroot
// like the browser driver does automatically.
// Different from BEHAT_TEST as only this last one can perform CLI
// actions like reset the site or use data generators.
define('BEHAT_SITE_RUNNING', true);
$CFG->wwwroot = $CFG->behat_wwwroot; $CFG->wwwroot = $CFG->behat_wwwroot;
$CFG->passwordsaltmain = 'moodle'; $CFG->passwordsaltmain = 'moodle';
$CFG->originaldataroot = $CFG->dataroot;
$CFG->prefix = $CFG->behat_prefix; $CFG->prefix = $CFG->behat_prefix;
$CFG->dataroot = $CFG->behat_dataroot; $CFG->dataroot = $CFG->behat_dataroot;
} }
@ -455,8 +464,9 @@ if (!PHPUNIT_TEST or PHPUNIT_UTIL) {
set_error_handler('default_error_handler', E_ALL | E_STRICT); set_error_handler('default_error_handler', E_ALL | E_STRICT);
} }
// Acceptance tests needs special output to capture the errors. // Acceptance tests needs special output to capture the errors,
if (!empty($CFG->originaldataroot) && !defined('BEHAT_RUNNING')) { // but not necessary for behat CLI command.
if (defined('BEHAT_SITE_RUNNING') && !defined('BEHAT_TEST')) {
require_once(__DIR__ . '/behat/lib.php'); require_once(__DIR__ . '/behat/lib.php');
set_error_handler('behat_error_handler', E_ALL | E_STRICT); set_error_handler('behat_error_handler', E_ALL | E_STRICT);
} }

View File

@ -65,11 +65,14 @@ class behat_hooks extends behat_base {
public static function before_suite($event) { public static function before_suite($event) {
global $CFG; global $CFG;
// To work with behat_dataroot and behat_prefix instead of the regular environment. // Defined only when the behat CLI command is running, the moodle init setup process will
define('BEHAT_RUNNING', 1); // read this value and switch to $CFG->behat_dataroot and $CFG->behat_prefix instead of
// the normal site.
define('BEHAT_TEST', 1);
define('CLI_SCRIPT', 1); define('CLI_SCRIPT', 1);
// With BEHAT_RUNNING we will be using $CFG->behat_* instead of $CFG->dataroot, $CFG->prefix and $CFG->wwwroot. // With BEHAT_TEST we will be using $CFG->behat_* instead of $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.
require_once(__DIR__ . '/../../../config.php'); require_once(__DIR__ . '/../../../config.php');
// Now that we are MOODLE_INTERNAL. // Now that we are MOODLE_INTERNAL.
@ -109,11 +112,11 @@ class behat_hooks extends behat_base {
global $DB, $SESSION, $CFG; global $DB, $SESSION, $CFG;
// As many checks as we can. // As many checks as we can.
if (!defined('BEHAT_RUNNING') || if (!defined('BEHAT_TEST') ||
!defined('BEHAT_SITE_RUNNING') ||
php_sapi_name() != 'cli' || php_sapi_name() != 'cli' ||
!behat_util::is_test_mode_enabled() || !behat_util::is_test_mode_enabled() ||
!behat_util::is_test_site() || !behat_util::is_test_site()) {
!isset($CFG->originaldataroot)) {
throw new coding_exception('Behat only can modify the test database and the test dataroot!'); throw new coding_exception('Behat only can modify the test database and the test dataroot!');
} }