Commit 096858ed authored by David Monllaó's avatar David Monllaó
Browse files

MDL-37046 behat: Use behat test env

Moving from phpunit test environment
to a specific behat test environment
parent b5c13009
......@@ -30,4 +30,4 @@ phpunit.xml
composer.phar
composer.lock
/vendor/
behat.yml
/behat.yml
......@@ -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,
),
......@@ -40,12 +49,16 @@ list($options, $unrecognized) = cli_get_params(
)
);
// 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
......@@ -60,22 +73,110 @@ if (!empty($options['help'])) {
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);
......@@ -30,15 +30,11 @@ $string['newstepsinfo'] = 'Read {$a} for info about how to add new steps definit
$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';
......@@ -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');
......@@ -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.
......@@ -63,16 +66,15 @@ class tool_behat {
$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');
}
......
......@@ -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
......@@ -33,11 +36,6 @@ defined('MOODLE_INTERNAL') || die;
*/
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
*
......@@ -54,20 +52,28 @@ class tool_behat_renderer extends plugin_renderer_base {
$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.
......@@ -77,7 +83,7 @@ class tool_behat_renderer extends plugin_renderer_base {
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();
......
......@@ -23,7 +23,7 @@
*/
defined('MOODLE_INTERNAL') || die;
defined('MOODLE_INTERNAL') || die();
if ($hassiteconfig) {
$url = $CFG->wwwroot . '/' . $CFG->admin . '/tool/behat/index.php';
......
......@@ -37,6 +37,7 @@ class steps_definitions_form extends moodleform {
/**
* Form definition
* @return void
*/
public function definition() {
......
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;}
......@@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for admin/tool/behat
* Unit tests for admin/tool/behat.
*
* @package tool_behat
* @copyright 2012 David Monllaó
......@@ -26,11 +26,11 @@ defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/' . $CFG->admin .'/tool/behat/locallib.php');
require_once($CFG->libdir . '/behat/classes/behat_util.php');
require_once($CFG->libdir . '/behat/classes/util.php');
require_once($CFG->libdir . '/behat/classes/behat_config_manager.php');
/**
* Allows access to internal methods without exposing them
* Allows access to internal methods without exposing them.
*
* @package tool_behat
* @copyright 2012 David Monllaó
......@@ -57,13 +57,13 @@ class testable_behat_config_manager extends behat_config_manager {
* @param array $stepsdefinitions
* @return string
*/
public static function get_config_file_contents($prefix, $features, $stepsdefinitions) {
return parent::get_config_file_contents($prefix, $features, $stepsdefinitions);
public static function get_config_file_contents($features, $stepsdefinitions) {
return parent::get_config_file_contents($features, $stepsdefinitions);
}
}
/**
* Tool behat tests
* Tool behat tests.
*
* @package tool_behat
* @copyright 2012 David Monllaó
......@@ -72,37 +72,7 @@ class testable_behat_config_manager extends behat_config_manager {
class tool_behat_testcase extends advanced_testcase {
/**
* behat_util tests
*/
public function test_switch_environment() {
// Only run the tests if behat dependencies are installed.
// We don't need to pre-check PHPUnit initialisation because we are running on it.
if (version_compare(PHP_VERSION, '5.4.0', '>=') && behat_command::are_behat_dependencies_installed()) {
behat_util::switchenvironment('enable');
$this->assertTrue(behat_util::is_test_mode_enabled());
$this->assertFalse(behat_util::is_test_environment_running());
// We trigger a debugging() if it's already enabled.
behat_util::switchenvironment('enable');
$this->assertDebuggingCalled();
behat_util::switchenvironment('disable');
$this->assertFalse(behat_util::is_test_mode_enabled());
$this->assertFalse(behat_util::is_test_environment_running());
// We trigger a debugging() if it's already enabled.
behat_util::switchenvironment('disable');
$this->assertDebuggingCalled();
// Ensure all continues disabled.
$this->assertFalse(behat_util::is_test_mode_enabled());
$this->assertFalse(behat_util::is_test_environment_running());
}
}
/**
* behat_config_manager tests
* behat_config_manager tests.
*/
public function test_merge_configs() {
......@@ -170,11 +140,12 @@ class tool_behat_testcase extends advanced_testcase {
}
/**
* behat_config_manager tests
* behat_config_manager tests.
*/
public function test_config_file_contents() {
global $CFG;
// To avoid user value at config.php level.
unset($CFG->behat_config);
// List.
......@@ -190,9 +161,9 @@ class tool_behat_testcase extends advanced_testcase {
'anoche' => '/cuando/yo/dormia'
);
$contents = testable_behat_config_manager::get_config_file_contents('/i/am/a/prefix/', $features, $stepsdefinitions);
$contents = testable_behat_config_manager::get_config_file_contents($features, $stepsdefinitions);
$this->assertContains('features: /i/am/a/prefix/lib/behat/features', $contents);
$this->assertContains('features: ' . $CFG->dirroot . '/lib/behat/features', $contents);
$this->assertContains('micarro: /me/lo/robaron', $contents);
$this->assertContains('base_url: \'' . $CFG->behat_wwwroot . '\'', $contents);
$this->assertContains('class: behat_init_context', $contents);
......
......@@ -552,8 +552,8 @@ $CFG->admin = 'admin';
//
// You can override default Moodle configuration for Behat and add your own
// params; here you can add more profiles, use different Mink drivers than Selenium...
// This params would be merged with the default Moodle behat.yml, giving priority
// to the ones specified here. The array format is YAML, following the behat
// These params would be merged with the default Moodle behat.yml, giving priority
// to the ones specified here. The array format is YAML, following the Behat
// params hierarchy. More info: http://docs.behat.org/guides/7.config.html
// Example:
// $CFG->behat_config = array(
......@@ -568,7 +568,7 @@ $CFG->admin = 'admin';
// )
// );
//
// You can completely switch to test environment when "php admin/tool/behatcli/util --enable",
// You can completely switch to test environment when "php admin/tool/behat/cli/util --enable",
// this means that all the site accesses will be routed to the test environment instead of
// the regular one, so NEVER USE THIS SETTING IN PRODUCTION SITES. This setting is useful
// when working with cloud CI (continous integration) servers which requires public sites to run the
......
......@@ -23,7 +23,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../filestorage/file_exceptions.php');
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../lib.php');
/**
* Behat command related utils
......@@ -35,24 +37,28 @@ require_once(__DIR__ . '/../../filestorage/file_exceptions.php');
*/
class behat_command {
/**
* Docs url
*/
const DOCS_URL = 'http://docs.moodle.org/dev/Acceptance_testing';
/**
* Ensures the behat dir exists in moodledata
* @throws file_exception
* @return string Full path
*/
public static function get_behat_dir() {
global $CFG;
$behatdir = $CFG->dataroot . '/behat';
$behatdir = $CFG->behat_dataroot . '/behat';
if (!is_dir($behatdir)) {
if (!mkdir($behatdir, $CFG->directorypermissions, true)) {
throw new file_exception('storedfilecannotcreatefiledirs');
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' can not be created');
}
}
if (!is_writable($behatdir)) {
throw new file_exception('storedfilecannotcreatefiledirs');
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' is not writable');
}
return $behatdir;
......@@ -66,21 +72,44 @@ class behat_command {
return 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'behat';
}
/**
* Runs behat command with provided options
*
* Execution continues when the process finishes
*
* @param string $options Defaults to '' so tests would be executed
* @return array CLI command outputs [0] => string, [1] => integer
*/
public final static function run($options = '') {
global $CFG;
$currentcwd = getcwd();
chdir($CFG->dirroot);
exec(self::get_behat_command() . ' ' . $options, $output, $code);
chdir($currentcwd);
return array($output, $code);
}
/**
* Checks if behat is set up and working
*
* Uses notice() instead of behat_error() because is
* also called from web interface
*
* It checks behat dependencies have been installed and runs
* the behat help command to ensure it works as expected
*
* @throw Exception
* @param boolean $checkphp Extra check for the PHP version
* @param bool $checkphp Extra check for the PHP version
* @return void
*/
public static function check_behat_setup($checkphp = false) {
global $CFG;
// We don't check the PHP version if $CFG->behat_switchcompletely has been enabled.
// Here we are in CLI.
if (empty($CFG->behat_switchcompletely) && $checkphp && version_compare(PHP_VERSION, '5.4.0', '<')) {
throw new Exception(get_string('wrongphpversion', 'tool_behat'));
behat_error(BEHAT_EXITCODE_REQUIREMENT, 'PHP 5.4 is required. See config-dist.php for possible alternatives');
}
// Moodle setting.
......@@ -89,7 +118,7 @@ class behat_command {
$msg = get_string('wrongbehatsetup', 'tool_behat');
// With HTML.
$docslink = 'http://docs.moodle.org/dev/Acceptance_testing#Installation';
$docslink = self::DOCS_URL . '#Installation';
if (!CLI_SCRIPT) {
$docslink = html_writer::tag('a', $docslink, array('href' => $docslink, 'target' => '_blank'));
}
......@@ -98,10 +127,7 @@ class behat_command {
}
// Behat test command.
$currentcwd = getcwd();
chdir($CFG->dirroot);
exec(self::get_behat_command() . ' --help', $output, $code);
chdir($currentcwd);
list($output, $code) = self::run(' --help');
if ($code != 0) {
notice(get_string('wrongbehatsetup', 'tool_behat'));
......@@ -110,7 +136,7 @@ class behat_command {
/**
* Has the site installed composer with --dev option
* @return boolean
* @return bool
*/
public static function are_behat_dependencies_installed() {
if (!is_dir(__DIR__ . '/../../../vendor/behat')) {
......
......@@ -23,8 +23,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../lib.php');
require_once(__DIR__ . '/behat_command.php');
require_once(__DIR__ . '/../../filestorage/file_exceptions.php');
require_once(__DIR__ . '/../../testing/classes/tests_finder.php');
/**
......@@ -47,24 +49,21 @@ class behat_config_manager {
* config files to avoid problems with concurrent executions.
*
* The steps definitions list can be filtered by component so it's
* behat.yml can be different from the dirroot one.
* behat.yml is different from the $CFG->dirroot one.
*
* @param string $component Restricts the obtained steps definitions to the specified component
* @param string $testsrunner If the config file will be used to run tests
* @throws file_exception
* @param string $component Restricts the obtained steps definitions to the specified component
* @param string $testsrunner If the config file will be used to run tests
* @return void
*/
public static function update_config_file($component = '', $testsrunner = true) {
global $CFG;
// Behat must run with the whole set of features and steps definitions.
// Behat must have a separate behat.yml to have access to the whole set of features and steps definitions.
if ($testsrunner === true) {
$prefix = '';
$configfilepath = $CFG->dirroot . '/behat.yml';
// Alternative for steps definitions filtering.