Skip to content

Commit

Permalink
MDL-37046 behat: Requires Moodle 2.5
Browse files Browse the repository at this point in the history
Also more code comments and general cleanup
  • Loading branch information
David Monllao committed Jan 29, 2013
1 parent fbb5243 commit a09534f
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 55 deletions.
8 changes: 4 additions & 4 deletions admin/tool/behat/cli/util.php
Expand Up @@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* CLI script
* CLI tests runner
*
* @package tool_behat
* @copyright 2012 David Monllaó
Expand All @@ -28,13 +28,13 @@
require_once($CFG->libdir . '/clilib.php');
require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/behat/locallib.php');

// now get cli options
// CLI options.
list($options, $unrecognized) = cli_get_params(
array(
'help' => false,
'runtests' => false,
'tags' => false,
'extra' => false,
'extra' => '',
'with-javascript' => false,
'testenvironment' => false
),
Expand All @@ -57,7 +57,7 @@
Example from Moodle root directory:
\$ php admin/tool/behat/cli/util.php --runtests --tags=\"tool_behat\"
More info in http://docs.moodle.org/dev/Acceptance_testing#Usage
More info in http://docs.moodle.org/dev/Acceptance_testing#Running_tests
";

if (!empty($options['help'])) {
Expand Down
5 changes: 4 additions & 1 deletion admin/tool/behat/index.php
Expand Up @@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Behat web runner
* Web interface to list and filter steps
*
* @package tool_behat
* @copyright 2012 David Monllaó
Expand All @@ -40,9 +40,12 @@
'' => get_string('allavailablesteps', 'tool_behat'),
'nomoodle' => get_string('nomoodlesteps', 'tool_behat'),
);

// Complete the components list with the moodle steps definitions.
$components = tool_behat::get_components_steps_definitions();
if ($components) {
foreach ($components as $component => $filepath) {
// TODO Use a class static attribute instead of the class name.
$componentswithsteps[$component] = 'Moodle ' . substr($component, 6);
}
}
Expand Down
8 changes: 2 additions & 6 deletions admin/tool/behat/lang/en/tool_behat.php
Expand Up @@ -22,12 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['actionnotsupported'] = 'Action not supported';
$string['allavailablesteps'] = 'All the available steps';
$string['commandinfo'] = 'Info';
$string['commandruntests'] = 'Run tests';
$string['commandstepsdefinitions'] = 'Steps definitions list';
$string['commandswitchenvironment'] = 'Switch environment';
$string['finished'] = 'Process finished';
$string['giveninfo'] = 'Given. Processes to set up the environment';
$string['installinfo'] = 'for installation and tests execution info';
Expand All @@ -37,11 +32,12 @@
$string['nomoodlesteps'] = 'Generic web application steps';
$string['nostepsdefinitions'] = 'There aren\'t steps definitions matching this filter';
$string['pluginname'] = 'Acceptance testing';
$string['phpunitenvproblem'] = 'PHPUnit environment problem';
$string['stepsdefinitionscomponent'] = 'Area';
$string['stepsdefinitionscontains'] = 'Contains';
$string['stepsdefinitionsfilters'] = 'Steps definitions';
$string['stepsdefinitionstype'] = 'Type';
$string['theninfo'] = 'Then. Checkings to ensure the outcomes are the expected ones';
$string['viewsteps'] = 'Filter';
$string['wheninfo'] = 'When. Actions that provokes an event';
$string['wrongbehatsetup'] = 'Something is wrong with the setup, ensure you ran the composer installer and your /lib/behat/bin/behat file has execution permissions';
$string['wrongbehatsetup'] = 'Something is wrong with the setup, ensure you ran the composer installer and /lib/behat/bin/behat file has execution permissions';
54 changes: 24 additions & 30 deletions admin/tool/behat/locallib.php
Expand Up @@ -49,6 +49,7 @@ class tool_behat {
*/
private static $steps_types = array('given', 'when', 'then');

/** @var string Docu url */
public static $docsurl = 'http://docs.moodle.org/dev/Acceptance_testing';

/**
Expand All @@ -67,6 +68,7 @@ public static function stepsdefinitions($type, $component, $filter) {
// The loaded steps depends on the component specified.
self::update_config_file($component);

// The Moodle\BehatExtension\HelpPrinter\MoodleDefinitionsPrinter will parse this search format.
if ($type) {
$filter .= '&&' . $type;
}
Expand Down Expand Up @@ -118,13 +120,14 @@ public static function switchenvironment($testenvironment = false) {
/**
* Runs the acceptance tests
*
* It starts test mode and runs the built-in php
* CLI server and stops it all then it's done
* It starts test mode and runs the built-in PHP
* erver and stops it all then it's done
*
* @param boolean $withjavascript Include tests with javascript
* @param string $tags Restricts the executed tests to the ones that matches the tags
* @param string $extra Extra CLI behat options
*/
public static function runtests($withjavascript = false, $tags = false, $extra = false) {
public static function runtests($withjavascript = false, $tags = false, $extra = '') {
global $CFG;

// Checks that the behat reference is properly set up.
Expand All @@ -133,30 +136,27 @@ public static function runtests($withjavascript = false, $tags = false, $extra =
// Check that PHPUnit test environment is correctly set up.
self::test_environment_problem();

// Updates all the Moodle features and steps definitions.
self::update_config_file();

@set_time_limit(0);

// No javascript by default.
if (!$withjavascript && strstr($tags, 'javascript') == false) {
$jsstr = '~javascript';
$jsstr = '~@javascript';
}

// Adding javascript option to --tags.
$tagsoption = '';
if ($tags) {
if (!empty($jsstr)) {
$tags .= ',' . $jsstr;
$tags .= '&&' . $jsstr;
}
$tagsoption = ' --tags ' . $tags;
$tagsoption = " --tags '" . $tags . "'";

// No javascript by default.
} else if (!empty($jsstr)) {
$tagsoption = ' --tags ' . $jsstr;
}

if (!$extra) {
$extra = '';
$tagsoption = " --tags '" . $jsstr . "'";
}

// Starts built-in server and inits test mode.
Expand Down Expand Up @@ -196,7 +196,8 @@ private static function update_config_file($component = '') {
$loadbuiltincontexts = '1';
}

// Basic behat dependencies.
// Behat config file specifing the main context class,
// the required Behat extensions and Moodle test wwwroot.
$contents = 'default:
paths:
features: ' . $behatpath . '/features
Expand Down Expand Up @@ -246,7 +247,6 @@ class: behat_init_context

}


/**
* Gets the list of Moodle steps definitions
*
Expand Down Expand Up @@ -309,15 +309,12 @@ private static function clean_path($path) {
private static function test_environment_problem() {
global $CFG;

// phpunit --diag returns nothing if the test environment is set up correctly.
$currentcwd = getcwd();
chdir($CFG->dirroot . '/' . $CFG->admin . '/tool/phpunit/cli');
exec("php util.php --diag", $output, $code);
chdir($currentcwd);
// PHPUnit --diag returns nothing if the test environment is set up correctly.
exec('php ' . $CFG->dirroot . '/' . $CFG->admin . '/tool/phpunit/cli/util.php --diag', $output, $code);

// If something is not ready stop execution and display the CLI command output.
if ($code != 0) {
notice(implode(' ', $output));
notice(get_string('phpunitenvproblem', 'tool_behat') . ': ' . implode(' ', $output));
}
}

Expand All @@ -336,11 +333,9 @@ private static function check_behat_setup() {
$msg = get_string('wrongbehatsetup', 'tool_behat');

// With HTML.
$docslink = tool_behat::$docsurl;
$docslink = tool_behat::$docsurl . '#Installation';
if (!CLI_SCRIPT) {
$docslink = html_writer::tag('a', $docslink, array('href' => $docslink, 'target' => '_blank'));
$systempathsurl = $CFG->wwwroot . '/' . $CFG->admin . '/settings.php?section=systempaths';
$msg .= ' (' . html_writer::tag('a', get_string('systempaths', 'admin'), array('href' => $systempathsurl)) . ')';
}
$msg .= '. ' . get_string('moreinfoin', 'tool_behat') . ' ' . $docslink;
notice($msg);
Expand All @@ -360,12 +355,10 @@ private static function check_behat_setup() {
/**
* Enables test mode
*
* Stores a file in dataroot/behat to
* allow Moodle to switch to the test
* database and dataroot before the initial setup
* Stores a file in dataroot/behat to allow Moodle to switch
* to the test environment when using cli-server
*
* @throws file_exception
* @return array
*/
private static function start_test_mode() {
global $CFG;
Expand Down Expand Up @@ -413,6 +406,7 @@ private static function start_test_server() {

/**
* Disables test mode
* @throws file_exception
*/
private static function stop_test_mode() {

Expand Down Expand Up @@ -451,12 +445,12 @@ private static function stop_test_server($process, $pipes) {
/**
* 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()}
* To check is the current script is running in the test
* environment {@see tool_behat::is_test_environment_running()}
*
* @return bool
*/
private static function is_test_mode_enabled() {
public static function is_test_mode_enabled() {

$testenvfile = self::get_test_filepath();
if (file_exists($testenvfile)) {
Expand All @@ -473,7 +467,7 @@ private static function is_test_mode_enabled() {
private static function is_test_environment_running() {
global $CFG;

if (!empty($CFG->originaldataroot) && php_sapi_name() === 'cli-server') {
if (!empty($CFG->originaldataroot) || defined('BEHAT_RUNNING')) {
return true;
}

Expand Down
4 changes: 1 addition & 3 deletions admin/tool/behat/renderer.php
Expand Up @@ -22,9 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.');
}
defined('MOODLE_INTERNAL') || die;

/**
* Renderer for behat tool web features
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/behat/settings.php
Expand Up @@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* PHPunit integration
* Adds behat tests link in admin tree
*
* @package tool_behat
* @copyright 2012 David Monllaó
Expand Down
5 changes: 3 additions & 2 deletions admin/tool/behat/steps_definitions_form.php
Expand Up @@ -22,8 +22,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once($CFG->libdir.'/formslib.php');
defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir.'/formslib.php');

/**
* Form to display the available steps definitions
Expand All @@ -39,7 +40,7 @@ class steps_definitions_form extends moodleform {
*/
function definition() {

$mform =& $this->_form;
$mform = $this->_form;

$mform->addElement('header', 'filters', get_string('stepsdefinitionsfilters', 'tool_behat'));

Expand Down
4 changes: 1 addition & 3 deletions admin/tool/behat/styles.css
@@ -1,7 +1,5 @@
#region-main-box #behat .summary { position: relative;}
#page-admin-tool-behat-index{padding-top: 0px;}
div#steps-definitions{border-style:solid;border-width:1px;border-color:#BBB;padding:5px;margin:auto;width:50%;}
div#steps-definitions .step{margin: 10px 0px 10px 0px;}
div#steps-definitions .stepdescription{color:#bf8c12;}
div#steps-definitions .steptype{color:#1467a6;margin-right: 5px;}
div#steps-definitions .stepregex{color:#060;}
div#steps-definitions .stepregex{color:#060;}
6 changes: 3 additions & 3 deletions admin/tool/behat/version.php
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2012110400; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012061700; // Requires this Moodle version
$plugin->component = 'tool_behat'; // Full name of the plugin (used for diagnostics)
$plugin->version = 2012120700;
$plugin->requires = 2012120300; // Requires Moodle 2.5
$plugin->component = 'tool_behat';
9 changes: 8 additions & 1 deletion config-dist.php
Expand Up @@ -536,11 +536,18 @@
// 'otherplugin' => array('mysetting' => 'myvalue', 'thesetting' => 'thevalue'));
//
//=========================================================================
// 9. PHPUNIT SUPPORT
// 10. PHPUNIT SUPPORT
//=========================================================================
// $CFG->phpunit_prefix = 'phpu_';
// $CFG->phpunit_dataroot = '/home/example/phpu_moodledata';
// $CFG->phpunit_directorypermissions = 02777; // optional
//
//=========================================================================
// 11. BEHAT SUPPORT
//=========================================================================
// Behat uses http://localhost:8000 as default URL to run
// the acceptance tests, you can override this value
// $CFG->test_wwwroot = '';

//=========================================================================
// ALL DONE! To continue installation, visit your main page with a browser
Expand Down
3 changes: 2 additions & 1 deletion lib/setup.php
Expand Up @@ -95,7 +95,8 @@
$CFG->test_wwwroot = 'http://localhost:8000';
}

// Switch to test site only when test environment is enabled.
// Switch to test site only when test environment is enabled: Both when the
// acceptance tests are running and when Behat is requiring moodle codebase.
if ((php_sapi_name() === 'cli-server' || defined('BEHAT_RUNNING')) &&
file_exists($CFG->dataroot . '/behat/test_environment_enabled.txt')) {
$CFG->wwwroot = $CFG->test_wwwroot;
Expand Down

0 comments on commit a09534f

Please sign in to comment.