Merge branch 'MDL-38007_master' of git://github.com/dmonllao/moodle

This commit is contained in:
Aparup Banerjee 2013-03-05 14:37:05 +08:00
commit 212e5ee909
6 changed files with 159 additions and 8 deletions

View file

@ -0,0 +1,98 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* CLI script to set up all the behat test environment.
*
* @package tool_behat
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (isset($_SERVER['REMOTE_ADDR'])) {
die(); // No access from web!
}
// Is not really necessary but adding it as is a CLI_SCRIPT.
define('CLI_SCRIPT', true);
// Basic functions.
require_once(__DIR__ . '/../../../../lib/clilib.php');
require_once(__DIR__ . '/../../../../lib/behat/lib.php');
// Changing the cwd to admin/tool/behat/cli.
chdir(__DIR__);
$output = null;
exec("php util.php --diag", $output, $code);
if ($code == 0) {
echo "Behat test environment already installed\n";
} else if ($code == BEHAT_EXITCODE_INSTALL) {
// Behat and dependencies are installed and we need to install the test site.
passthru("php util.php --install", $code);
if ($code != 0) {
exit($code);
}
} else if ($code == BEHAT_EXITCODE_REINSTALL) {
// Test site data is outdated.
passthru("php util.php --drop", $code);
if ($code != 0) {
exit($code);
}
passthru("php util.php --install", $code);
if ($code != 0) {
exit($code);
}
} else if ($code == BEHAT_EXITCODE_COMPOSER) {
// Missing Behat dependencies.
// Changing to moodle dirroot to run composer related commands at project level.
chdir(__DIR__ . '/../../../..');
if (!file_exists(__DIR__ . '/../../../../composer.phar')) {
passthru("curl http://getcomposer.org/install | php", $code);
if ($code != 0) {
exit($code);
}
}
passthru("php composer.phar install --dev", $code);
if ($code != 0) {
exit($code);
}
// Returning to admin/tool/behat/cli.
chdir(__DIR__);
passthru("php util.php --install", $code);
if ($code != 0) {
exit($code);
}
} else {
// Generic error, we just output it.
echo implode("\n", $output)."\n";
exit($code);
}
// Enable editing mode according to config.php vars.
passthru("php util.php --enable", $code);
if ($code != 0) {
exit($code);
}
exit(0);

View file

@ -43,6 +43,7 @@ list($options, $unrecognized) = cli_get_params(
'drop' => false,
'enable' => false,
'disable' => false,
'diag' => false
),
array(
'h' => 'help'
@ -59,6 +60,7 @@ Options:
--drop Drops the database tables and the dataroot contents
--enable Enables test environment and updates tests list
--disable Disables test environment
--diag Get behat test environment status code
-h, --help Print out this help
@ -183,6 +185,9 @@ if ($options['install']) {
} else if ($options['disable']) {
behat_util::stop_test_mode();
mtrace("Acceptance tests environment disabled");
} else if ($options['diag']) {
$code = behat_util::get_behat_status();
exit($code);
} else {
echo $help;
}

View file

@ -50,7 +50,7 @@ class tool_behat {
// We don't require the test environment to be enabled to list the steps definitions
// so test writers can more easily set up the environment.
behat_command::check_behat_setup();
behat_command::behat_setup_problem();
// The loaded steps depends on the component specified.
behat_config_manager::update_config_file($component, false);

View file

@ -101,9 +101,9 @@ class behat_command {
* the behat help command to ensure it works as expected
*
* @param bool $checkphp Extra check for the PHP version
* @return void
* @return int Error code or 0 if all ok
*/
public static function check_behat_setup($checkphp = false) {
public static function behat_setup_problem($checkphp = false) {
global $CFG;
// We don't check the PHP version if $CFG->behat_switchcompletely has been enabled.
@ -123,20 +123,27 @@ class behat_command {
$docslink = html_writer::tag('a', $docslink, array('href' => $docslink, 'target' => '_blank'));
}
$msg .= '. ' . get_string('moreinfoin', 'tool_behat', $docslink);
notice($msg);
self::output_msg($msg);
return BEHAT_EXITCODE_COMPOSER;
}
// Behat test command.
list($output, $code) = self::run(' --help');
if ($code != 0) {
notice(get_string('wrongbehatsetup', 'tool_behat'));
// Returning composer error code to avoid conflicts with behat and moodle error codes.
self::output_msg(get_string('wrongbehatsetup', 'tool_behat'));
return BEHAT_EXITCODE_COMPOSER;
}
// Checking behat dataroot existence otherwise notice about admin/tool/behat/cli/util.php.
// Checking behat dataroot existence otherwise echo about admin/tool/behat/cli/util.php.
if (empty($CFG->behat_dataroot) || !is_dir($CFG->behat_dataroot) || !is_writable($CFG->behat_dataroot)) {
notice(get_string('runclitool', 'tool_behat', 'php admin/tool/behat/cli/util.php'));
self::output_msg(get_string('runclitool', 'tool_behat', 'php admin/tool/behat/cli/util.php'));
return BEHAT_EXITCODE_CONFIG;
}
return 0;
}
/**
@ -150,4 +157,22 @@ class behat_command {
return true;
}
/**
* Outputs a message.
*
* Used in CLI + web UI methods. Stops the
* execution in web.
*
* @param string $msg
* @return void
*/
protected static function output_msg($msg) {
if (!CLI_SCRIPT) {
notice($msg);
} else {
echo $msg;
}
}
}

View file

@ -176,7 +176,9 @@ class behat_util extends testing_util {
}
// Checks the behat set up and the PHP version.
behat_command::check_behat_setup(true);
if ($errorcode = behat_command::behat_setup_problem(true)) {
exit($code);
}
// Check that test environment is correctly set up.
self::test_environment_problem();
@ -196,6 +198,26 @@ class behat_util extends testing_util {
}
}
/**
* Returns the status of the behat test environment
*
* @return int Error code
*/
public static function get_behat_status() {
if (!defined('BEHAT_UTIL')) {
throw new coding_exception('This method can be only used by Behat CLI tool');
}
// Checks the behat set up and the PHP version, returning an error code if something went wrong.
if ($errorcode = behat_command::behat_setup_problem(true)) {
return $code;
}
// Check that test environment is correctly set up, stops execution.
self::test_environment_problem();
}
/**
* Disables test mode
* @throws coding_exception

View file

@ -32,6 +32,7 @@ define('BEHAT_EXITCODE_REQUIREMENT', 251);
define('BEHAT_EXITCODE_PERMISSIONS', 252);
define('BEHAT_EXITCODE_REINSTALL', 253);
define('BEHAT_EXITCODE_INSTALL', 254);
define('BEHAT_EXITCODE_COMPOSER', 255);
/**
* Exits with an error code