Skip to content

Commit

Permalink
MDL-37046 behat: Using single config file on moodle side
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Jan 29, 2013
1 parent 603c95d commit df8dcf3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 38 deletions.
3 changes: 3 additions & 0 deletions admin/tool/behat/lang/en/tool_behat.php
Expand Up @@ -31,4 +31,7 @@
$string['nobehatpath'] = 'You must specify the path to moodle-acceptance-tests.';
$string['nostepsdefinitions'] = 'There aren\'t steps definitions matching this filter';
$string['pluginname'] = 'Acceptance testing';
$string['stepsdefinitions'] = 'Steps definitions which contains';
$string['stepsdefinitionsemptyfilter'] = 'all steps definitions if empty';
$string['viewsteps'] = 'View available steps definitions';
$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';
95 changes: 59 additions & 36 deletions admin/tool/behat/locallib.php
Expand Up @@ -63,11 +63,10 @@ public static function info() {
public static function stepsdefinitions($filter = false) {
global $CFG;

if (!CLI_SCRIPT) {
confirm_sesskey();
}
self::check_behat_setup();

self::update_config_file();

// Priority to the one specified as argument.
if (!$filter) {
$filter = optional_param('filter', false, PARAM_ALPHANUMEXT);
Expand All @@ -79,9 +78,14 @@ public static function stepsdefinitions($filter = false) {
$filteroption = ' -di';
}

$color = '';
if (CLI_SCRIPT) {
$color = '--ansi ';
}

$currentcwd = getcwd();
chdir($CFG->behatpath);
exec('bin/behat --ansi ' . $filteroption, $steps, $code);
exec('bin/behat ' . $color . ' --config="' . self::get_behat_config_filepath() . '" ' . $filteroption, $steps, $code);
chdir($currentcwd);

// Outputing steps.
Expand Down Expand Up @@ -124,10 +128,6 @@ public static function stepsdefinitions($filter = false) {
public static function switchenvironment($testenvironment = false) {
global $CFG;

if (!CLI_SCRIPT) {
confirm_sesskey();
}

// Priority to the one specified as argument.
if (!$testenvironment) {
$testenvironment = optional_param('testenvironment', 'enable', PARAM_ALPHA);
Expand All @@ -138,10 +138,6 @@ public static function switchenvironment($testenvironment = false) {
} else if ($testenvironment == 'disable') {
self::disable_test_environment();
}

if (!CLI_SCRIPT) {
redirect(get_login_url());
}
}

/**
Expand All @@ -152,9 +148,6 @@ public static function switchenvironment($testenvironment = false) {
public static function runtests($tags = false, $extra = false) {
global $CFG;

if (!CLI_SCRIPT) {
confirm_sesskey();
}
self::check_behat_setup();

self::update_config_file();
Expand All @@ -165,16 +158,13 @@ public static function runtests($tags = false, $extra = false) {
if (!$tags) {
$tags = optional_param('tags', false, PARAM_ALPHANUMEXT);
}
// $extra only passed as CLI option (to simplify web runner usage).

$tagsoption = '';
if ($tags) {
$tagsoption = ' --tags ' . $tags;
}

if (!$extra && !CLI_SCRIPT) {
$extra = ' --format html';
} else if(!$extra && CLI_SCRIPT) {
if (!$extra) {
$extra = '';
}

Expand All @@ -184,7 +174,7 @@ public static function runtests($tags = false, $extra = false) {

chdir($CFG->behatpath);
ob_start();
passthru('bin/behat --ansi ' . $tagsoption . ' ' .$extra, $code);
passthru('bin/behat --ansi --config="' . self::get_behat_config_filepath() .'" ' . $tagsoption . ' ' .$extra, $code);
$output = ob_get_contents();
ob_end_clean();

Expand All @@ -194,9 +184,6 @@ public static function runtests($tags = false, $extra = false) {

// Output.
echo self::get_header();
if (!CLI_SCRIPT) {
echo self::get_run_tests_form($tags);
}
echo $output;

// Dirty hack to avoid behat bad HTML structure when test execution throws an exception and there are skipped steps.
Expand All @@ -212,36 +199,64 @@ public static function runtests($tags = false, $extra = false) {
* @throws file_exception
*/
private static function update_config_file() {
global $CFG;

$contents = '';
$behatpath = rtrim($CFG->behatpath, '/');

// Basic behat dependencies.
$contents = 'default:
paths:
features: ' . $behatpath . '/features
bootstrap: ' . $behatpath . '/features/bootstrap
extensions:
Behat\MinkExtension\Extension:
base_url: ' . $CFG->wwwroot . '
goutte: ~
selenium2: ~
Sanpi\Behatch\Extension:
contexts:
browser: ~
system: ~
json: ~
table: ~
rest: ~
' . $behatpath . '/vendor/moodlehq/behat-extension/init.php:
';

// Gets all the components with features.
$components = tests_finder::get_components_with_tests('features');
if ($components) {
$featurespaths[] = '';
$featurespaths = array('');
foreach ($components as $componentname => $path) {
$path = self::clean_path($path) . self::$behat_tests_path;
if (empty($featurespaths[$path]) && file_exists($path)) {
$featurespaths[$path] = $path;
}
}
$contents = 'features:' . implode(PHP_EOL . ' - ', $featurespaths) . PHP_EOL;
$contents .= ' features:' . implode(PHP_EOL . ' - ', $featurespaths) . PHP_EOL;
}

// Gets all the components with steps definitions.
$components = tests_finder::get_components_with_tests('stepsdefinitions');
if ($components) {
$contents .= 'steps_definitions:' . PHP_EOL;
foreach ($components as $componentname => $filepath) {
$filepath = self::clean_path($path) . self::$behat_tests_path . '/behat_' . $componentname . '.php';
$contents .= ' ' . $componentname . ': ' . $filepath . PHP_EOL;
$stepsdefinitions = array('');
foreach ($components as $componentname => $componentpath) {
$componentpath = self::clean_path($componentpath);
$diriterator = new DirectoryIterator($componentpath . self::$behat_tests_path);
$regite = new RegexIterator($diriterator, '|behat_.*\.php$|');

// All behat_*.php inside self::$behat_tests_path are added as steps definitions files
foreach ($regite as $file) {
$key = $file->getBasename('.php');
$stepsdefinitions[$key] = $key . ': ' . $file->getPathname();
}
}
$contents .= ' steps_definitions:' . implode(PHP_EOL . ' ', $stepsdefinitions) . PHP_EOL;
}

// Stores the file.
$fullfilepath = self::get_behat_dir() . '/config.yml';
if (!file_put_contents($fullfilepath, $contents)) {
throw new file_exception('cannotcreatefile', $fullfilepath);
if (!file_put_contents(self::get_behat_config_filepath(), $contents)) {
throw new file_exception('cannotcreatefile', self::get_behat_config_filepath());
}

}
Expand Down Expand Up @@ -432,6 +447,14 @@ private static function get_behat_dir() {
return $behatdir;
}

/**
* Returns the behat config file path
* @return string
*/
private static function get_behat_config_filepath() {
return self::get_behat_dir() . '/behat.yml';
}

/**
* Returns header output
* @return string
Expand Down Expand Up @@ -524,10 +547,10 @@ private static function get_steps_definitions_form($filter = false) {
$html = $OUTPUT->box_start();
$html .= '<form method="get" action="index.php">';
$html .= '<fieldset class="invisiblefieldset">';
$html .= '<label for="id_filter">Steps definitions which contains</label> ';
$html .= '<input type="text" id="id_filter" value="' . $filter . '" name="filter"/> (all steps definitions if empty)';
$html .= '<label for="id_filter">' . get_string('stepsdefinitions', 'tool_behat') . '</label> ';
$html .= '<input type="text" id="id_filter" value="' . $filter . '" name="filter"/> (' . get_string('stepsdefinitionsemptyfilter', 'tool_behat') . ')';
$html .= '<p></p>';
$html .= '<input type="submit" value="View available steps definitions" />';
$html .= '<input type="submit" value="' . get_string('viewsteps', 'tool_behat') . '" />';
$html .= '<input type="hidden" name="action" value="stepsdefinitions" />';
$html .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
$html .= '</fieldset>';
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/behat/tests/behat/test_environment.feature
@@ -1,9 +1,9 @@
@tool_behat
Feature: Set up the testing environment
In order to execute automated acceptance tests
As a moodle developer
I need to use the test environment instead of the regular environment

@tool_behat
Scenario: Accessing the site
When I am on homepage
Then I should see "PHPUnit test site"
2 changes: 1 addition & 1 deletion admin/tool/behat/version.php
Expand Up @@ -24,6 +24,6 @@

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

$plugin->version = 2012092600; // The current plugin version (Date: YYYYMMDDXX)
$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)

0 comments on commit df8dcf3

Please sign in to comment.