mirror of
https://github.com/moodle/moodle.git
synced 2025-08-08 10:26:40 +02:00
Merge branch 'MDL-37046_master' of git://github.com/dmonllao/moodle
This commit is contained in:
commit
68aaba6c84
22 changed files with 1673 additions and 8 deletions
148
lib/behat/classes/behat_command.php
Normal file
148
lib/behat/classes/behat_command.php
Normal file
|
@ -0,0 +1,148 @@
|
|||
<?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/>.
|
||||
|
||||
/**
|
||||
* Behat command utils
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2012 David Monllaó
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once(__DIR__ . '/../lib.php');
|
||||
|
||||
/**
|
||||
* Behat command related utils
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2013 David Monllaó
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class behat_command {
|
||||
|
||||
/**
|
||||
* Docs url
|
||||
*/
|
||||
const DOCS_URL = 'http://docs.moodle.org/dev/Acceptance_testing';
|
||||
|
||||
/**
|
||||
* Ensures the behat dir exists in moodledata
|
||||
* @return string Full path
|
||||
*/
|
||||
public static function get_behat_dir() {
|
||||
global $CFG;
|
||||
|
||||
$behatdir = $CFG->behat_dataroot . '/behat';
|
||||
|
||||
if (!is_dir($behatdir)) {
|
||||
if (!mkdir($behatdir, $CFG->directorypermissions, true)) {
|
||||
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' can not be created');
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_writable($behatdir)) {
|
||||
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' is not writable');
|
||||
}
|
||||
|
||||
return $behatdir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the executable path
|
||||
* @return string
|
||||
*/
|
||||
public final static function get_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
|
||||
*
|
||||
* @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', '<')) {
|
||||
behat_error(BEHAT_EXITCODE_REQUIREMENT, 'PHP 5.4 is required. See config-dist.php for possible alternatives');
|
||||
}
|
||||
|
||||
// Moodle setting.
|
||||
if (!self::are_behat_dependencies_installed()) {
|
||||
|
||||
$msg = get_string('wrongbehatsetup', 'tool_behat');
|
||||
|
||||
// With HTML.
|
||||
$docslink = self::DOCS_URL . '#Installation';
|
||||
if (!CLI_SCRIPT) {
|
||||
$docslink = html_writer::tag('a', $docslink, array('href' => $docslink, 'target' => '_blank'));
|
||||
}
|
||||
$msg .= '. ' . get_string('moreinfoin', 'tool_behat', $docslink);
|
||||
notice($msg);
|
||||
}
|
||||
|
||||
// Behat test command.
|
||||
list($output, $code) = self::run(' --help');
|
||||
|
||||
if ($code != 0) {
|
||||
notice(get_string('wrongbehatsetup', 'tool_behat'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the site installed composer with --dev option
|
||||
* @return bool
|
||||
*/
|
||||
public static function are_behat_dependencies_installed() {
|
||||
if (!is_dir(__DIR__ . '/../../../vendor/behat')) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
272
lib/behat/classes/behat_config_manager.php
Normal file
272
lib/behat/classes/behat_config_manager.php
Normal file
|
@ -0,0 +1,272 @@
|
|||
<?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/>.
|
||||
|
||||
/**
|
||||
* Utils to set Behat config
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2012 David Monllaó
|
||||
* @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__ . '/../../testing/classes/tests_finder.php');
|
||||
|
||||
/**
|
||||
* Behat configuration manager
|
||||
*
|
||||
* Creates/updates Behat config files getting tests
|
||||
* and steps from Moodle codebase
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2012 David Monllaó
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class behat_config_manager {
|
||||
|
||||
/**
|
||||
* Updates a config file
|
||||
*
|
||||
* The tests runner and the steps definitions list uses different
|
||||
* config files to avoid problems with concurrent executions.
|
||||
*
|
||||
* The steps definitions list can be filtered by component so it's
|
||||
* 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
|
||||
* @return void
|
||||
*/
|
||||
public static function update_config_file($component = '', $testsrunner = true) {
|
||||
global $CFG;
|
||||
|
||||
// Behat must have a separate behat.yml to have access to the whole set of features and steps definitions.
|
||||
if ($testsrunner === true) {
|
||||
$configfilepath = behat_command::get_behat_dir() . '/behat.yml';
|
||||
} else {
|
||||
// Alternative for steps definitions filtering, one for each user.
|
||||
$configfilepath = self::get_steps_list_config_filepath();
|
||||
}
|
||||
|
||||
// Gets all the components with features.
|
||||
$features = array();
|
||||
$components = tests_finder::get_components_with_tests('features');
|
||||
if ($components) {
|
||||
foreach ($components as $componentname => $path) {
|
||||
$path = self::clean_path($path) . self::get_behat_tests_path();
|
||||
if (empty($featurespaths[$path]) && file_exists($path)) {
|
||||
|
||||
// Standarizes separator (some dirs. comes with OS-dependant separator).
|
||||
$uniquekey = str_replace('\\', '/', $path);
|
||||
$featurespaths[$uniquekey] = $path;
|
||||
}
|
||||
}
|
||||
$features = array_values($featurespaths);
|
||||
}
|
||||
|
||||
// Gets all the components with steps definitions.
|
||||
$stepsdefinitions = array();
|
||||
$steps = self::get_components_steps_definitions();
|
||||
if ($steps) {
|
||||
foreach ($steps as $key => $filepath) {
|
||||
if ($component == '' || $component === $key) {
|
||||
$stepsdefinitions[$key] = $filepath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Behat config file specifing the main context class,
|
||||
// the required Behat extensions and Moodle test wwwroot.
|
||||
$contents = self::get_config_file_contents($features, $stepsdefinitions);
|
||||
|
||||
// Stores the file.
|
||||
if (!file_put_contents($configfilepath, $contents)) {
|
||||
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $configfilepath . ' can not be created');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of Moodle steps definitions
|
||||
*
|
||||
* Class name as a key and the filepath as value
|
||||
*
|
||||
* Externalized from update_config_file() to use
|
||||
* it from the steps definitions web interface
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_components_steps_definitions() {
|
||||
|
||||
$components = tests_finder::get_components_with_tests('stepsdefinitions');
|
||||
if (!$components) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$stepsdefinitions = array();
|
||||
foreach ($components as $componentname => $componentpath) {
|
||||
$componentpath = self::clean_path($componentpath);
|
||||
$diriterator = new DirectoryIterator($componentpath . self::get_behat_tests_path());
|
||||
$regite = new RegexIterator($diriterator, '|behat_.*\.php$|');
|
||||
|
||||
// All behat_*.php inside behat_config_manager::get_behat_tests_path() are added as steps definitions files.
|
||||
foreach ($regite as $file) {
|
||||
$key = $file->getBasename('.php');
|
||||
$stepsdefinitions[$key] = $file->getPathname();
|
||||
}
|
||||
}
|
||||
|
||||
return $stepsdefinitions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the behat config file path used by the steps definition list
|
||||
*
|
||||
* Note this can only be called from web-based scripts so it will return the
|
||||
* production dataroot not behat_dataroot. With this the steps definitions
|
||||
* list is accessible without having to install the behat test site.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_steps_list_config_filepath() {
|
||||
global $USER;
|
||||
|
||||
$userdir = behat_command::get_behat_dir() . '/users/' . $USER->id;
|
||||
make_writable_directory($userdir);
|
||||
|
||||
return $userdir . '/behat.yml';
|
||||
}
|
||||
|
||||
/**
|
||||
* Behat config file specifing the main context class,
|
||||
* the required Behat extensions and Moodle test wwwroot.
|
||||
*
|
||||
* @param array $features The system feature files
|
||||
* @param array $stepsdefinitions The system steps definitions
|
||||
* @return string
|
||||
*/
|
||||
protected static function get_config_file_contents($features, $stepsdefinitions) {
|
||||
global $CFG;
|
||||
|
||||
// We require here when we are sure behat dependencies are available.
|
||||
require_once($CFG->dirroot . '/vendor/autoload.php');
|
||||
|
||||
$basedir = $CFG->dirroot . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'behat';
|
||||
$config = array(
|
||||
'default' => array(
|
||||
'paths' => array(
|
||||
'features' => $basedir . DIRECTORY_SEPARATOR . 'features',
|
||||
'bootstrap' => $basedir . DIRECTORY_SEPARATOR . 'features' . DIRECTORY_SEPARATOR . 'bootstrap',
|
||||
),
|
||||
'context' => array(
|
||||
'class' => 'behat_init_context'
|
||||
),
|
||||
'extensions' => array(
|
||||
'Behat\MinkExtension\Extension' => array(
|
||||
'base_url' => $CFG->behat_wwwroot,
|
||||
'goutte' => null,
|
||||
'selenium2' => null
|
||||
),
|
||||
'Moodle\BehatExtension\Extension' => array(
|
||||
'features' => $features,
|
||||
'steps_definitions' => $stepsdefinitions
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// In case user defined overrides respect them over our default ones.
|
||||
if (!empty($CFG->behat_config)) {
|
||||
$config = self::merge_config($config, $CFG->behat_config);
|
||||
}
|
||||
|
||||
return Symfony\Component\Yaml\Yaml::dump($config, 10, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides default config with local config values
|
||||
*
|
||||
* array_merge does not merge completely the array's values
|
||||
*
|
||||
* @param mixed $config The node of the default config
|
||||
* @param mixed $localconfig The node of the local config
|
||||
* @return mixed The merge result
|
||||
*/
|
||||
protected static function merge_config($config, $localconfig) {
|
||||
|
||||
if (!is_array($config) && !is_array($localconfig)) {
|
||||
return $localconfig;
|
||||
}
|
||||
|
||||
// Local overrides also deeper default values.
|
||||
if (is_array($config) && !is_array($localconfig)) {
|
||||
return $localconfig;
|
||||
}
|
||||
|
||||
foreach ($localconfig as $key => $value) {
|
||||
|
||||
// If defaults are not as deep as local values let locals override.
|
||||
if (!is_array($config)) {
|
||||
unset($config);
|
||||
}
|
||||
|
||||
// Add the param if it doesn't exists or merge branches.
|
||||
if (empty($config[$key])) {
|
||||
$config[$key] = $value;
|
||||
} else {
|
||||
$config[$key] = self::merge_config($config[$key], $localconfig[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans the path returned by get_components_with_tests() to standarize it
|
||||
*
|
||||
* @see tests_finder::get_all_directories_with_tests() it returns the path including /tests/
|
||||
* @param string $path
|
||||
* @return string The string without the last /tests part
|
||||
*/
|
||||
protected final static function clean_path($path) {
|
||||
|
||||
$path = rtrim($path, DIRECTORY_SEPARATOR);
|
||||
|
||||
$parttoremove = DIRECTORY_SEPARATOR . 'tests';
|
||||
|
||||
$substr = substr($path, strlen($path) - strlen($parttoremove));
|
||||
if ($substr == $parttoremove) {
|
||||
$path = substr($path, 0, strlen($path) - strlen($parttoremove));
|
||||
}
|
||||
|
||||
return rtrim($path, DIRECTORY_SEPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* The relative path where components stores their behat tests
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected final static function get_behat_tests_path() {
|
||||
return DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'behat';
|
||||
}
|
||||
|
||||
}
|
262
lib/behat/classes/util.php
Normal file
262
lib/behat/classes/util.php
Normal file
|
@ -0,0 +1,262 @@
|
|||
<?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/>.
|
||||
|
||||
/**
|
||||
* Utils for behat-related stuff
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2012 David Monllaó
|
||||
* @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__ . '/../../testing/classes/util.php');
|
||||
require_once(__DIR__ . '/behat_command.php');
|
||||
require_once(__DIR__ . '/behat_config_manager.php');
|
||||
|
||||
require_once(__DIR__ . '/../../filelib.php');
|
||||
|
||||
/**
|
||||
* Init/reset utilities for Behat database and dataroot
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2013 David Monllaó
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class behat_util extends testing_util {
|
||||
|
||||
/**
|
||||
* @var array Files to skip when resetting dataroot folder
|
||||
*/
|
||||
protected static $datarootskiponreset = array('.', '..', 'behat', 'behattestdir.txt');
|
||||
|
||||
/**
|
||||
* @var array Files to skip when dropping dataroot folder
|
||||
*/
|
||||
protected static $datarootskipondrop = array('.', '..', 'lock');
|
||||
|
||||
/**
|
||||
* Installs a site using $CFG->dataroot and $CFG->prefix
|
||||
* @throws coding_exception
|
||||
* @return void
|
||||
*/
|
||||
public static function install_site() {
|
||||
global $DB;
|
||||
|
||||
if (!defined('BEHAT_UTIL')) {
|
||||
throw new coding_exception('This method can be only used by Behat CLI tool');
|
||||
}
|
||||
|
||||
// New dataroot.
|
||||
self::reset_dataroot();
|
||||
|
||||
$options = array();
|
||||
$options['adminuser'] = 'admin';
|
||||
$options['adminpass'] = 'admin';
|
||||
$options['fullname'] = 'Acceptance test site';
|
||||
|
||||
install_cli_database($options, false);
|
||||
|
||||
// Update admin user info.
|
||||
$user = $DB->get_record('user', array('username' => 'admin'));
|
||||
$user->email = 'moodle@moodlemoodle.com';
|
||||
$user->firstname = 'Admin';
|
||||
$user->lastname = 'User';
|
||||
$user->city = 'Perth';
|
||||
$user->country = 'AU';
|
||||
$DB->update_record('user', $user);
|
||||
|
||||
// Sets maximum debug level.
|
||||
set_config('debug', DEBUG_DEVELOPER);
|
||||
set_config('debugdisplay', true);
|
||||
|
||||
// Keeps the current version of database and dataroot.
|
||||
self::store_versions_hash();
|
||||
|
||||
// Stores the database contents for fast reset.
|
||||
self::store_database_state();
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops dataroot and remove test database tables
|
||||
* @throws coding_exception
|
||||
* @return void
|
||||
*/
|
||||
public static function drop_site() {
|
||||
|
||||
if (!defined('BEHAT_UTIL')) {
|
||||
throw new coding_exception('This method can be only used by Behat CLI tool');
|
||||
}
|
||||
|
||||
self::reset_dataroot();
|
||||
self::drop_dataroot();
|
||||
self::drop_database(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if $CFG->behat_wwwroot is available
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_server_running() {
|
||||
global $CFG;
|
||||
|
||||
$request = new curl();
|
||||
$request->get($CFG->behat_wwwroot);
|
||||
|
||||
if ($request->get_errno() === 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the test database and dataroot is ready
|
||||
* Stops execution if something went wrong
|
||||
* @throws coding_exception
|
||||
* @return void
|
||||
*/
|
||||
protected static function test_environment_problem() {
|
||||
global $CFG, $DB;
|
||||
|
||||
if (!defined('BEHAT_UTIL')) {
|
||||
throw new coding_exception('This method can be only used by Behat CLI tool');
|
||||
}
|
||||
|
||||
if (!self::is_test_site()) {
|
||||
behat_error(1, 'This is not a behat test site!');
|
||||
}
|
||||
|
||||
$tables = $DB->get_tables(false);
|
||||
if (empty($tables)) {
|
||||
behat_error(BEHAT_EXITCODE_INSTALL, '');
|
||||
}
|
||||
|
||||
if (!self::is_test_data_updated()) {
|
||||
behat_error(BEHAT_EXITCODE_REINSTALL, 'The test environment was initialised for a different version');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables test mode
|
||||
*
|
||||
* It uses CFG->behat_dataroot
|
||||
*
|
||||
* Starts the test mode checking the composer installation and
|
||||
* the test environment and updating the available
|
||||
* features and steps definitions.
|
||||
*
|
||||
* Stores a file in dataroot/behat to allow Moodle to switch
|
||||
* to the test environment when using cli-server (or $CFG->behat_switchcompletely)
|
||||
* @throws coding_exception
|
||||
* @return void
|
||||
*/
|
||||
public static function start_test_mode() {
|
||||
global $CFG;
|
||||
|
||||
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.
|
||||
behat_command::check_behat_setup(true);
|
||||
|
||||
// Check that test environment is correctly set up.
|
||||
self::test_environment_problem();
|
||||
|
||||
// Updates all the Moodle features and steps definitions.
|
||||
behat_config_manager::update_config_file();
|
||||
|
||||
if (self::is_test_mode_enabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$contents = '$CFG->behat_wwwroot, $CFG->behat_prefix and $CFG->behat_dataroot' .
|
||||
' are currently used as $CFG->wwwroot, $CFG->prefix and $CFG->dataroot';
|
||||
$filepath = self::get_test_file_path();
|
||||
if (!file_put_contents($filepath, $contents)) {
|
||||
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $filepath . ' can not be created');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables test mode
|
||||
* @throws coding_exception
|
||||
* @return void
|
||||
*/
|
||||
public static function stop_test_mode() {
|
||||
|
||||
if (!defined('BEHAT_UTIL')) {
|
||||
throw new coding_exception('This method can be only used by Behat CLI tool');
|
||||
}
|
||||
|
||||
$testenvfile = self::get_test_file_path();
|
||||
|
||||
if (!self::is_test_mode_enabled()) {
|
||||
echo "Test environment was already disabled\n";
|
||||
} else {
|
||||
if (!unlink($testenvfile)) {
|
||||
behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Can not delete test environment file');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether test environment is enabled or disabled
|
||||
*
|
||||
* To check is the current script is running in the test
|
||||
* environment
|
||||
*
|
||||
* @see tool_behat::is_using_test_environment()
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_test_mode_enabled() {
|
||||
|
||||
$testenvfile = self::get_test_file_path();
|
||||
if (file_exists($testenvfile)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if Moodle is currently running with the test database and dataroot
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_using_test_environment() {
|
||||
global $CFG;
|
||||
|
||||
if (!empty($CFG->originaldataroot)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path to the file which specifies if test environment is enabled
|
||||
* @return string
|
||||
*/
|
||||
protected final static function get_test_file_path() {
|
||||
return behat_command::get_behat_dir() . '/test_environment_enabled.txt';
|
||||
}
|
||||
|
||||
}
|
55
lib/behat/features/bootstrap/behat_init_context.php
Normal file
55
lib/behat/features/bootstrap/behat_init_context.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?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/>.
|
||||
|
||||
/**
|
||||
* Contexts initializer class
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2012 David Monllaó
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use Behat\Behat\Context\BehatContext,
|
||||
Behat\MinkExtension\Context\MinkContext,
|
||||
Moodle\BehatExtension\Context\MoodleContext;
|
||||
|
||||
/**
|
||||
* Loads main subcontexts
|
||||
*
|
||||
* Loading of moodle subcontexts is done by the Moodle extension
|
||||
*
|
||||
* Renamed from behat FeatureContext class according
|
||||
* to Moodle coding styles conventions
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2012 David Monllaó
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class behat_init_context extends BehatContext {
|
||||
|
||||
/**
|
||||
* Initializes subcontexts
|
||||
*
|
||||
* @param array $parameters context parameters (set them up through behat.yml)
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $parameters) {
|
||||
$this->useContext('moodle', new MoodleContext($parameters));
|
||||
}
|
||||
|
||||
}
|
71
lib/behat/lib.php
Normal file
71
lib/behat/lib.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?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/>.
|
||||
|
||||
/**
|
||||
* Behat basic functions
|
||||
*
|
||||
* It does not include MOODLE_INTERNAL because is part of the bootstrap
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2012 David Monllaó
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
require_once(__DIR__ . '/../testing/lib.php');
|
||||
|
||||
define('BEHAT_EXITCODE_CONFIG', 250);
|
||||
define('BEHAT_EXITCODE_REQUIREMENT', 251);
|
||||
define('BEHAT_EXITCODE_PERMISSIONS', 252);
|
||||
define('BEHAT_EXITCODE_REINSTALL', 253);
|
||||
define('BEHAT_EXITCODE_INSTALL', 254);
|
||||
|
||||
/**
|
||||
* Exits with an error code
|
||||
*
|
||||
* @param mixed $errorcode
|
||||
* @param string $text
|
||||
* @return void Stops execution with error code
|
||||
*/
|
||||
function behat_error($errorcode, $text = '') {
|
||||
|
||||
// Adding error prefixes.
|
||||
switch ($errorcode) {
|
||||
case BEHAT_EXITCODE_CONFIG:
|
||||
$text = 'Behat config error: ' . $text;
|
||||
break;
|
||||
case BEHAT_EXITCODE_REQUIREMENT:
|
||||
$text = 'Behat requirement not satisfied: ' . $text;
|
||||
break;
|
||||
case BEHAT_EXITCODE_PERMISSIONS:
|
||||
$text = 'Behat permissions problem: ' . $text . ', check the permissions';
|
||||
break;
|
||||
case BEHAT_EXITCODE_REINSTALL:
|
||||
$path = testing_cli_argument_path('/admin/tool/behat/cli/util.php');
|
||||
$text = "Reinstall Behat: ".$text.", use:\n php ".$path." --drop \n php ".$path." --install";
|
||||
break;
|
||||
case BEHAT_EXITCODE_INSTALL:
|
||||
$path = testing_cli_argument_path('/admin/tool/behat/cli/util.php');
|
||||
$text = "Install Behat before enabling it, use:\n php ".$path." --install";
|
||||
break;
|
||||
default:
|
||||
$text = 'Unknown error ' . $errorcode . ' ' . $text;
|
||||
break;
|
||||
}
|
||||
|
||||
testing_error($errorcode, $text);
|
||||
}
|
||||
|
|
@ -560,10 +560,10 @@ class plugin_manager {
|
|||
),
|
||||
|
||||
'tool' => array(
|
||||
'assignmentupgrade', 'capability', 'customlang', 'dbtransfer', 'generator',
|
||||
'health', 'innodb', 'langimport', 'multilangupgrade', 'phpunit', 'profiling',
|
||||
'qeupgradehelper', 'replace', 'spamcleaner', 'timezoneimport', 'unittest',
|
||||
'uploaduser', 'unsuproles', 'xmldb'
|
||||
'assignmentupgrade', 'behat', 'capability', 'customlang', 'dbtransfer',
|
||||
'generator', 'health', 'innodb', 'langimport', 'multilangupgrade', 'phpunit',
|
||||
'profiling', 'qeupgradehelper', 'replace', 'spamcleaner', 'timezoneimport',
|
||||
'unittest', 'uploaduser', 'unsuproles', 'xmldb'
|
||||
),
|
||||
|
||||
'webservice' => array(
|
||||
|
|
|
@ -90,6 +90,44 @@ if (!isset($CFG->wwwroot) or $CFG->wwwroot === 'http://example.com/moodle') {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
// Ignore $CFG->behat_wwwroot and use the same wwwroot.
|
||||
if (isset($CFG->behat_switchcompletely)) {
|
||||
$CFG->behat_wwwroot = $CFG->wwwroot;
|
||||
|
||||
} else if (!isset($CFG->behat_wwwroot)) {
|
||||
// Default URL for acceptance testing, only accessible from localhost.
|
||||
$CFG->behat_wwwroot = 'http://localhost:8000';
|
||||
}
|
||||
|
||||
|
||||
// Test environment is requested if:
|
||||
// * Behat is running (constant set hooking the behat init process before requiring config.php).
|
||||
// * If we are accessing though the built-in web server (cli-server).
|
||||
// * If $CFG->behat_switchcompletely has been set (maintains CLI scripts behaviour, which ATM is only preventive).
|
||||
// Test environment is enabled if:
|
||||
// * User has previously enabled through admin/tool/behat/cli/util.php --enable.
|
||||
// Both are required to switch to test mode
|
||||
if (isset($CFG->behat_dataroot) && isset($CFG->behat_prefix) && file_exists($CFG->behat_dataroot)) {
|
||||
|
||||
$CFG->behat_dataroot = realpath($CFG->behat_dataroot);
|
||||
|
||||
$switchcompletely = isset($CFG->behat_switchcompletely) && php_sapi_name() !== 'cli';
|
||||
$builtinserver = php_sapi_name() === 'cli-server';
|
||||
$behatrunning = defined('BEHAT_RUNNING');
|
||||
$testenvironmentrequested = $switchcompletely || $builtinserver || $behatrunning;
|
||||
|
||||
// Only switch to test environment if it has been enabled.
|
||||
$testenvironmentenabled = file_exists($CFG->behat_dataroot . '/behat/test_environment_enabled.txt');
|
||||
|
||||
if ($testenvironmentenabled && $testenvironmentrequested) {
|
||||
$CFG->wwwroot = $CFG->behat_wwwroot;
|
||||
$CFG->passwordsaltmain = 'moodle';
|
||||
$CFG->originaldataroot = $CFG->dataroot;
|
||||
$CFG->prefix = $CFG->behat_prefix;
|
||||
$CFG->dataroot = $CFG->behat_dataroot;
|
||||
}
|
||||
}
|
||||
|
||||
// Define admin directory
|
||||
if (!isset($CFG->admin)) { // Just in case it isn't defined in config.php
|
||||
$CFG->admin = 'admin'; // This is relative to the wwwroot and dirroot
|
||||
|
|
|
@ -181,7 +181,7 @@ class tests_finder {
|
|||
$regexp = '|'.$sep.'tests'.$sep.'behat'.$sep.'.*\.feature$|';
|
||||
break;
|
||||
case 'stepsdefinitions':
|
||||
$regexp = '|'.$sep.'tests'.$sep.'behat'.$sep.'.*\.php$|';
|
||||
$regexp = '|'.$sep.'tests'.$sep.'behat'.$sep.'behat_.*\.php$|';
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue