From cfcbc34a617696dedda237e3f42e3d2b23d89fd4 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Fri, 3 May 2013 14:03:49 +0800 Subject: [PATCH] MDL-39478 behat: New constant to detect when the test site is running Also removing an unused var and method. --- lib/behat/classes/util.php | 15 --------------- lib/sessionlib.php | 2 +- lib/setup.php | 18 ++++++++++++++---- lib/tests/behat/behat_hooks.php | 15 +++++++++------ 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/lib/behat/classes/util.php b/lib/behat/classes/util.php index 60d0ea0c4e9..2ac98ddb8e6 100644 --- a/lib/behat/classes/util.php +++ b/lib/behat/classes/util.php @@ -250,7 +250,6 @@ class behat_util extends testing_util { * To check is the current script is running in the test * environment * - * @see tool_behat::is_using_test_environment() * @return bool */ public static function is_test_mode_enabled() { @@ -263,20 +262,6 @@ class behat_util extends testing_util { 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 * @return string diff --git a/lib/sessionlib.php b/lib/sessionlib.php index 49288dca5ac..be19e443eee 100644 --- a/lib/sessionlib.php +++ b/lib/sessionlib.php @@ -1128,7 +1128,7 @@ function session_set_user($user) { } sesskey(); // init session key - if (PHPUNIT_TEST || defined('BEHAT_RUNNING')) { + if (PHPUNIT_TEST || defined('BEHAT_TEST')) { // phpunit tests use reversed reference global $USER; $USER = $_SESSION['USER']; diff --git a/lib/setup.php b/lib/setup.php index 213ac7030ed..242823cc185 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -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'; $builtinserver = php_sapi_name() === 'cli-server'; - $behatrunning = defined('BEHAT_RUNNING'); + $behatrunning = defined('BEHAT_TEST'); $testenvironmentrequested = $switchcompletely || $builtinserver || $behatrunning; // Only switch to test environment if it has been enabled. $testenvironmentenabled = file_exists($CFG->behat_dataroot . '/behat/test_environment_enabled.txt'); 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->passwordsaltmain = 'moodle'; - $CFG->originaldataroot = $CFG->dataroot; $CFG->prefix = $CFG->behat_prefix; $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); } -// Acceptance tests needs special output to capture the errors. -if (!empty($CFG->originaldataroot) && !defined('BEHAT_RUNNING')) { +// Acceptance tests needs special output to capture the errors, +// but not necessary for behat CLI command. +if (defined('BEHAT_SITE_RUNNING') && !defined('BEHAT_TEST')) { require_once(__DIR__ . '/behat/lib.php'); set_error_handler('behat_error_handler', E_ALL | E_STRICT); } diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 5b934b249db..3dfe6b51f3a 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -65,11 +65,14 @@ class behat_hooks extends behat_base { public static function before_suite($event) { global $CFG; - // To work with behat_dataroot and behat_prefix instead of the regular environment. - define('BEHAT_RUNNING', 1); + // Defined only when the behat CLI command is running, the moodle init setup process will + // 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); - // 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'); // Now that we are MOODLE_INTERNAL. @@ -109,11 +112,11 @@ class behat_hooks extends behat_base { global $DB, $SESSION, $CFG; // As many checks as we can. - if (!defined('BEHAT_RUNNING') || + if (!defined('BEHAT_TEST') || + !defined('BEHAT_SITE_RUNNING') || php_sapi_name() != 'cli' || !behat_util::is_test_mode_enabled() || - !behat_util::is_test_site() || - !isset($CFG->originaldataroot)) { + !behat_util::is_test_site()) { throw new coding_exception('Behat only can modify the test database and the test dataroot!'); }