MDL-37046 behat: Adding switch environment feature

This commit is contained in:
David Monllao
2013-01-29 08:40:35 +08:00
parent 7f541ea3ed
commit cc5446464e
4 changed files with 135 additions and 14 deletions
-4
View File
@@ -30,10 +30,6 @@ $action = optional_param('action', 'info', PARAM_ALPHAEXT);
admin_externalpage_setup('toolbehat');
$title = get_string('pluginname', 'tool_behat') . ' - ' . get_string('command' . $action, 'tool_behat');
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
call_user_func('tool_behat::' . $action);
echo $OUTPUT->footer();
+4
View File
@@ -27,8 +27,12 @@ $string['commandbuildconfigfile'] = 'Build config file';
$string['commandinfo'] = 'Info';
$string['commandruntests'] = 'Run tests';
$string['commandstepsdefinitions'] = 'Steps definitions list';
$string['commandswitchenvironment'] = 'Switch environment';
$string['finished'] = 'Process finished';
$string['nobehatpath'] = 'You must specify the path to moodle-acceptance-tests.';
$string['nostepsdefinitions'] = 'There aren\'t steps definitions matching this filter';
$string['pluginname'] = 'Acceptance test tool';
$string['switchenvironmentinfo'] = 'Caution! Use only in non-production sites, if you switch to the test environment all the logged users, including you, will be logged out.';
$string['switchenvironmentenable'] = 'Switch to test environment';
$string['switchenvironmentdisable'] = 'Return to regular environment';
$string['wrongbehatsetup'] = 'Something is wrong with the setup, check moodle-acceptance-tests runs well on CLI and check your \'behatpath\' setting value is pointing to the right directory';
+129 -10
View File
@@ -33,9 +33,12 @@ class tool_behat {
*/
public static function info() {
$html = tool_behat::get_info();
$html = tool_behat::get_header();
$html .= tool_behat::get_info();
$html .= tool_behat::get_switch_environment_form();
$html .= tool_behat::get_steps_definitions_form();
$html .= tool_behat::get_run_tests_form();
$html .= tool_behat::get_footer();
echo $html;
}
@@ -62,7 +65,8 @@ class tool_behat {
chdir($currentcwd);
// Outputing steps.
$html = tool_behat::get_steps_definitions_form($filter);
$html = tool_behat::get_header();
$html .= tool_behat::get_steps_definitions_form($filter);
$content = '';
if ($steps) {
@@ -80,9 +84,28 @@ class tool_behat {
}
$html .= html_writer::tag('div', $content, array('id' => 'steps-definitions'));
$html .= tool_behat::get_footer();
echo $html;
}
/**
* Switches from and to the regular environment to the testing environment
*/
public static function switchenvironment() {
global $CFG;
confirm_sesskey();
$testenvironment = optional_param('testenvironment', 'enable', PARAM_ALPHA);
if ($testenvironment == 'enable') {
tool_behat::enable_test_environment();
} else if ($testenvironment == 'disable') {
tool_behat::disable_test_environment();
}
redirect(get_login_url());
}
/**
* Creates a file listing all the moodle with features and steps definitions
@@ -100,6 +123,8 @@ class tool_behat {
confirm_sesskey();
tool_behat::check_behat_setup();
echo tool_behat::get_header();
@set_time_limit(0);
$tagsoption = '';
@@ -120,6 +145,8 @@ class tool_behat {
// Switching back to regular environment
tool_behat::disable_test_environment();
chdir($currentcwd);
echo tool_behat::get_footer();
}
@@ -173,12 +200,12 @@ class tool_behat {
/**
* Enables test mode checking the test environment setup
*
* Stores a file in dataroot/behat to allow Moodle swap to
* Stores a file in dataroot/behat to allow Moodle switch to
* test database and dataroot before the initial set up
*
* @throws file_exception
*/
public static function enable_test_environment() {
private static function enable_test_environment() {
global $CFG;
confirm_sesskey();
@@ -214,27 +241,32 @@ class tool_behat {
/**
* Disables test mode
*/
public static function disable_test_environment() {
global $CFG;
private static function disable_test_environment() {
confirm_sesskey();
$testenvfile = $CFG->dataroot . '/behat/test_environment_enabled.txt';
$testenvfile = tool_behat::get_test_filepath();
if (!tool_behat::is_test_environment_enabled()) {
debugging('Test environment was already disabled');
} else {
unlink($testenvfile);
if (!unlink($testenvfile)) {
throw new file_exception('cannotdeletetestenvironmentfile');
}
}
}
/**
* Checks whether test environment is enabled or disabled
*
* It does not return if the current script is running
* in test environment {@see tool_behat::is_test_environment_running()}
*
* @return bool
*/
public static function is_test_environment_enabled() {
global $CFG;
$testenvfile = $CFG->dataroot . '/behat/test_environment_enabled.txt';
$testenvfile = tool_behat::get_test_filepath();
if (file_exists($testenvfile)) {
return true;
}
@@ -242,6 +274,70 @@ class tool_behat {
return false;
}
/**
* Returns true if Moodle is currently running with the test database and dataroot
* @return bool
*/
public static function is_test_environment_running() {
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
*/
private static function get_test_filepath() {
global $CFG;
if (tool_behat::is_test_environment_running()) {
$testenvfile = $CFG->originaldataroot . '/behat/test_environment_enabled.txt';
} else {
$testenvfile = $CFG->dataroot . '/behat/test_environment_enabled.txt';
}
return $testenvfile;
}
/**
* Returns header output
* @return string
*/
private static function get_header() {
global $OUTPUT;
if (CLI_SCRIPT) {
return get_string('command' . $action, 'tool_behat');
}
$action = optional_param('action', 'info', PARAM_ALPHAEXT);
$title = get_string('pluginname', 'tool_behat') . ' - ' . get_string('command' . $action, 'tool_behat');
$html = $OUTPUT->header();
$html .= $OUTPUT->heading($title);
return $html;
}
/**
* Returns footer output
* @return string
*/
private static function get_footer() {
global $OUTPUT;
if (CLI_SCRIPT) {
return '';
}
return $OUTPUT->footer();
}
/**
* Returns the installation instructions
*
@@ -268,6 +364,29 @@ class tool_behat {
return $html;
}
/**
* Returns a button to switch between environments
* @return string
*/
private static function get_switch_environment_form() {
global $CFG, $OUTPUT;
if (tool_behat::is_test_environment_running()) {
$perform = 'disable';
} else {
$perform = 'enable';
}
$params = array('action' => 'switchenvironment', 'testenvironment' => $perform, 'sesskey' => sesskey());
$url = new moodle_url('/' . $CFG->admin . '/tool/behat/index.php', $params);
$html = $OUTPUT->box_start();
$html .= '<p>' . get_string('switchenvironmentinfo', 'tool_behat') . '</p>';
$html .= $OUTPUT->single_button($url, get_string('switchenvironment' . $perform, 'tool_behat'));
$html .= $OUTPUT->box_end();
return $html;
}
/**
* Returns the steps definitions form
* @param string $filter To filter the steps definitions list by keyword
+2
View File
@@ -92,6 +92,8 @@ if (!isset($CFG->wwwroot) or $CFG->wwwroot === 'http://example.com/moodle') {
// If acceptance testing mode is enabled use test database and dataroot
if (file_exists($CFG->dataroot . '/behat/test_environment_enabled.txt')) {
$CFG->passwordsaltmain = 'phpunit';
$CFG->originaldataroot = $CFG->dataroot;
$CFG->prefix = $CFG->phpunit_prefix;
$CFG->dataroot = $CFG->phpunit_dataroot;
}