Skip to content

Commit

Permalink
MDL-37046 behat: Use behat test env
Browse files Browse the repository at this point in the history
Moving from phpunit test environment
to a specific behat test environment
  • Loading branch information
David Monllao committed Jan 29, 2013
1 parent b5c1300 commit 096858e
Show file tree
Hide file tree
Showing 17 changed files with 592 additions and 336 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -30,4 +30,4 @@ phpunit.xml
composer.phar
composer.lock
/vendor/
behat.yml
/behat.yml
135 changes: 118 additions & 17 deletions admin/tool/behat/cli/util.php
Expand Up @@ -15,23 +15,32 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* CLI tool
* CLI tool with utilities to manage Behat integration in Moodle
*
* All CLI utilities uses $CFG->behat_dataroot and $CFG->prefix_dataroot as
* $CFG->dataroot and $CFG->prefix
*
* @package tool_behat
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('CLI_SCRIPT', true);

require(__DIR__ . '/../../../../config.php');
require_once($CFG->libdir . '/clilib.php');
require_once($CFG->libdir . '/behat/behat_util.php');
if (isset($_SERVER['REMOTE_ADDR'])) {
die(); // No access from web!.
}

// Basic functions.
require_once(__DIR__ . '/../../../../lib/clilib.php');
require_once(__DIR__ . '/../../../../lib/behat/lib.php');


// CLI options.
list($options, $unrecognized) = cli_get_params(
array(
'help' => false,
'install' => false,
'drop' => false,
'enable' => false,
'disable' => false,
),
Expand All @@ -40,12 +49,16 @@
)
);


// Checking util.php CLI script usage.
$help = "
Behat tool
Behat utilities to manage the test environment
Options:
--enable Enables test environment and updates tests list
--disable Disables test environment
--install Installs the test environment for acceptance tests
--drop Drops the database tables and the dataroot contents
--enable Enables test environment and updates tests list
--disable Disables test environment
-h, --help Print out this help
Expand All @@ -60,22 +73,110 @@
exit(0);
}


// Checking $CFG->behat_* vars and values.
define('BEHAT_UTIL', true);
define('CLI_SCRIPT', true);
define('ABORT_AFTER_CONFIG', true);
define('NO_OUTPUT_BUFFERING', true);

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', '1');
ini_set('log_errors', '1');

require_once(__DIR__ . '/../../../../config.php');

// CFG->behat_prefix must be set and with value different than CFG->prefix and phpunit_prefix.
if (!isset($CFG->behat_prefix) ||
(isset($CFG->behat_prefix) &&
($CFG->behat_prefix == $CFG->prefix ||
$CFG->behat_prefix == $CFG->phpunit_prefix))) {
behat_error(BEHAT_EXITCODE_CONFIG,
'Define $CFG->behat_prefix in config.php with a value different than $CFG->prefix and $CFG->phpunit_prefix');
}

// CFG->behat_dataroot must be set and with value different than CFG->dataroot and phpunit_dataroot.
if (!isset($CFG->behat_dataroot) ||
(isset($CFG->behat_dataroot) &&
($CFG->behat_dataroot == $CFG->dataroot ||
$CFG->behat_dataroot == $CFG->phpunit_dataroot))) {
behat_error(BEHAT_EXITCODE_CONFIG,
'Define $CFG->behat_dataroot in config.php with a value different than $CFG->dataroot and $CFG->phpunit_dataroot');
}

// Create behat_dataroot if it doesn't exists.
if (!file_exists($CFG->behat_dataroot)) {
if (!mkdir($CFG->behat_dataroot, $CFG->directorypermissions)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, '$CFG->behat_dataroot directory can not be created');
}
}
if (!is_dir($CFG->behat_dataroot) || !is_writable($CFG->behat_dataroot)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, '$CFG->behat_dataroot directory has no permissions or is not a directory');
}

// Check that the directory does not contains other things.
if (!file_exists("$CFG->behat_dataroot/behattestdir.txt")) {
if ($dh = opendir($CFG->behat_dataroot)) {
while (($file = readdir($dh)) !== false) {
if ($file === 'behat' or $file === '.' or $file === '..' or $file === '.DS_Store') {
continue;
}
behat_error(BEHAT_EXITCODE_CONFIG, '$CFG->behat_dataroot directory is not empty, ensure this is the directory where you want to install behat test dataroot');
}
closedir($dh);
unset($dh);
unset($file);
}

// Now we create dataroot directory structure for behat tests.
testing_initdataroot($CFG->behat_dataroot, 'behat');
}

// Overrides vars with behat-test ones.
$vars = array('wwwroot', 'prefix', 'dataroot');
foreach ($vars as $var) {
$CFG->{$var} = $CFG->{'behat_' . $var};
}

$CFG->noemailever = true;
$CFG->passwordsaltmain = 'moodle';

// Continues setup.
define('ABORT_AFTER_CONFIG_CANCEL', true);
require("$CFG->dirroot/lib/setup.php");

require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/upgradelib.php');
require_once($CFG->libdir.'/clilib.php');
require_once($CFG->libdir.'/pluginlib.php');
require_once($CFG->libdir.'/installlib.php');

if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}

// Run command.
if ($options['enable']) {
$action = 'enable';
// Behat utilities.
require_once($CFG->libdir . '/behat/classes/util.php');
require_once($CFG->libdir . '/behat/classes/behat_command.php');

// Run command (only one per time).
if ($options['install']) {
behat_util::install_site();
mtrace("Acceptance tests site installed");
} else if ($options['drop']) {
behat_util::drop_site();
mtrace("Acceptance tests site dropped");
} else if ($options['enable']) {
behat_util::start_test_mode();
$runtestscommand = behat_command::get_behat_command() . ' --config '
. $CFG->behat_dataroot . DIRECTORY_SEPARATOR . 'behat' . DIRECTORY_SEPARATOR . 'behat.yml';
mtrace("Acceptance tests environment enabled, to run the tests use:\n " . $runtestscommand);
} else if ($options['disable']) {
$action = 'disable';
behat_util::stop_test_mode();
mtrace("Acceptance tests environment disabled");
} else {
echo $help;
exit(0);
}

behat_util::switchenvironment($action);

mtrace(get_string('testenvironment' . $action, 'tool_behat'));

exit(0);
4 changes: 0 additions & 4 deletions admin/tool/behat/lang/en/tool_behat.php
Expand Up @@ -30,15 +30,11 @@
$string['newtestsinfo'] = 'Read {$a} for info about how to write new tests';
$string['nostepsdefinitions'] = 'There aren\'t steps definitions matching this filters';
$string['pluginname'] = 'Acceptance testing';
$string['phpunitenvproblem'] = 'PHPUnit environment problem';
$string['stepsdefinitionscomponent'] = 'Area';
$string['stepsdefinitionscontains'] = 'Contains';
$string['stepsdefinitionsfilters'] = 'Steps definitions';
$string['stepsdefinitionstype'] = 'Type';
$string['testenvironmentenable'] = 'Test environment enabled';
$string['testenvironmentdisable'] = 'Test environment disabled';
$string['theninfo'] = 'Then. Checkings to ensure the outcomes are the expected ones';
$string['viewsteps'] = 'Filter';
$string['wheninfo'] = 'When. Actions that provokes an event';
$string['wrongphpversion'] = 'PHP 5.4 or higher is required to run acceptance tests. See config-dist.php for alternatives.';
$string['wrongbehatsetup'] = 'Something is wrong with the setup, ensure you ran the composer installer and vendor/bin/behat file has execution permissions';
14 changes: 8 additions & 6 deletions admin/tool/behat/locallib.php
Expand Up @@ -22,6 +22,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

global $CFG;
require_once($CFG->libdir . '/behat/classes/behat_command.php');
require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
Expand All @@ -45,8 +47,9 @@ class tool_behat {
* @return string
*/
public static function stepsdefinitions($type, $component, $filter) {
global $CFG;

// 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();

// The loaded steps depends on the component specified.
Expand All @@ -63,16 +66,15 @@ public static function stepsdefinitions($type, $component, $filter) {
$filteroption = ' -di';
}

$currentcwd = getcwd();
chdir($CFG->dirroot);
exec(behat_command::get_behat_command() . ' --config="'.behat_config_manager::get_steps_list_config_filepath(). '" '.$filteroption, $steps, $code);
chdir($currentcwd);
// Get steps definitions from Behat.
$options = ' --config="'.behat_config_manager::get_steps_list_config_filepath(). '" '.$filteroption;
list($steps, $code) = behat_command::run($options);

if ($steps) {
$stepshtml = implode('', $steps);
}

if (!isset($stepshtml) || $stepshtml == '') {
if (empty($stepshtml)) {
$stepshtml = get_string('nostepsdefinitions', 'tool_behat');
}

Expand Down
28 changes: 17 additions & 11 deletions admin/tool/behat/renderer.php
Expand Up @@ -22,7 +22,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

global $CFG;
require_once($CFG->libdir . '/behat/classes/behat_command.php');

/**
* Renderer for behat tool web features
Expand All @@ -33,11 +36,6 @@
*/
class tool_behat_renderer extends plugin_renderer_base {

/**
* @var string Docs url
*/
protected $docsurl = 'http://docs.moodle.org/dev/Acceptance_testing';

/**
* Renders the list of available steps according to the submitted filters
*
Expand All @@ -54,20 +52,28 @@ public function render_stepsdefinitions($stepsdefinitions, $form) {
$html .= $this->output->heading($title);

// Info.
$installurl = $this->docsurl . '#Installation';
$installurl = behat_command::DOCS_URL . '#Installation';
$installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank'));
$writetestsurl = $this->docsurl . '#Writting_features';
$writetestsurl = behat_command::DOCS_URL . '#Writting_features';
$writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank'));
$writestepsurl = $this->docsurl . '#Adding_steps_definitions';
$writestepsurl = behat_command::DOCS_URL . '#Adding_steps_definitions';
$writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank'));
$infos = array(
get_string('installinfo', 'tool_behat', $installlink),
get_string('newtestsinfo', 'tool_behat', $writetestslink),
get_string('newstepsinfo', 'tool_behat', $writestepslink)
);

// List of steps
$html .= $this->output->box_start();
$html .= html_writer::tag('h1', 'Info');
$html .= html_writer::tag('div', '<ul><li>' . implode('</li><li>', $infos) . '</li></ul>');
$html .= html_writer::empty_tag('div');
$html .= html_writer::empty_tag('ul');
$html .= html_writer::empty_tag('li');
$html .= implode(html_writer::end_tag('li') . html_writer::empty_tag('li'), $infos);
$html .= html_writer::end_tag('li');
$html .= html_writer::end_tag('ul');
$html .= html_writer::end_tag('div');
$html .= $this->output->box_end();

// Form.
Expand All @@ -77,7 +83,7 @@ public function render_stepsdefinitions($stepsdefinitions, $form) {
ob_end_clean();

// Steps definitions.
$html .= html_writer::tag('div', $stepsdefinitions, array('id' => 'steps-definitions'));
$html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));

$html .= $this->output->footer();

Expand Down
2 changes: 1 addition & 1 deletion admin/tool/behat/settings.php
Expand Up @@ -23,7 +23,7 @@
*/


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

if ($hassiteconfig) {
$url = $CFG->wwwroot . '/' . $CFG->admin . '/tool/behat/index.php';
Expand Down
1 change: 1 addition & 0 deletions admin/tool/behat/steps_definitions_form.php
Expand Up @@ -37,6 +37,7 @@ class steps_definitions_form extends moodleform {

/**
* Form definition
* @return void
*/
public function definition() {

Expand Down
10 changes: 5 additions & 5 deletions admin/tool/behat/styles.css
@@ -1,5 +1,5 @@
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;}
.steps-definitions{border-style:solid;border-width:1px;border-color:#BBB;padding:5px;margin:auto;width:50%;}
.steps-definitions .step{margin: 10px 0px 10px 0px;}
.steps-definitions .stepdescription{color:#bf8c12;}
.steps-definitions .steptype{color:#1467a6;margin-right: 5px;}
.steps-definitions .stepregex{color:#060;}

0 comments on commit 096858e

Please sign in to comment.