Commit 60129d5d authored by Petr Škoda's avatar Petr Škoda
Browse files

MDL-43213 try to simplify behat config and init code

1/ always require 3 behat settings - $CFG->behat_wwwroot, $CFG->behat_dataroot and $CFG->behat_prefix
2/ cleanup init code
3/ do not require $CFG->wwwroot and $CFG->dataroot on test sites
4/ remove switch completely - the most confusing part for me
5/ print out behat_wwwwroot in init script so that you can test the test site manually
parent 2f4e0db7
......@@ -78,42 +78,28 @@ if (!empty($options['help'])) {
exit(0);
}
// Checking $CFG->behat_* vars and values.
// Describe this script.
define('BEHAT_UTIL', true);
define('CLI_SCRIPT', true);
define('ABORT_AFTER_CONFIG', true);
define('NO_OUTPUT_BUFFERING', true);
define('IGNORE_COMPONENT_CACHE', true);
error_reporting(E_ALL | E_STRICT);
// Only load CFG from config.php, stop ASAP in lib/setup.php.
define('ABORT_AFTER_CONFIG', true);
require_once(__DIR__ . '/../../../../config.php');
// Remove error handling overrides done in config.php.
$CFG->debug = (E_ALL | E_STRICT);
$CFG->debugdisplay = 1;
error_reporting($CFG->debug);
ini_set('display_errors', '1');
ini_set('log_errors', '1');
// Getting $CFG data.
require_once(__DIR__ . '/../../../../config.php');
// Finish moodle init.
define('ABORT_AFTER_CONFIG_CANCEL', true);
require("$CFG->dirroot/lib/setup.php");
// When we use the utilities we don't know how the site
// will be accessed, so if neither $CFG->behat_switchcompletely or
// $CFG->behat_wwwroot are set we must think that the site will
// be accessed using the built-in server which is set by default
// to localhost:8000. We need to do this to prevent uses of the production
// wwwroot when the site is being installed / dropped...
$CFG->behat_wwwroot = behat_get_wwwroot();
// Checking the integrity of the provided $CFG->behat_* vars
// to prevent conflicts with production and phpunit environments.
behat_check_config_vars();
// 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');
}
raise_memory_limit(MEMORY_HUGE);
// Check that the directory does not contains other things.
if (!file_exists("$CFG->behat_dataroot/behattestdir.txt")) {
......@@ -133,30 +119,6 @@ if (!file_exists("$CFG->behat_dataroot/behattestdir.txt")) {
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};
}
// Clean $CFG extra values before performing any action.
behat_clean_init_config();
$CFG->noemailever = true;
$CFG->passwordsaltmain = 'moodle';
$CFG->themerev = 1;
$CFG->jsrev = 1;
// Unset cache and temp directories to reset them again with the new $CFG->dataroot.
unset($CFG->cachedir);
unset($CFG->localcachedir);
unset($CFG->tempdir);
// 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');
......@@ -185,7 +147,7 @@ if ($options['install']) {
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);
mtrace("Acceptance tests environment enabled on $CFG->behat_wwwroot, to run the tests use:\n " . $runtestscommand);
} else if ($options['disable']) {
behat_util::stop_test_mode();
mtrace("Acceptance tests environment disabled");
......
......@@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for admin/tool/behat.
* Unit tests for behat manager.
*
* @package tool_behat
* @copyright 2012 David Monllaó
......@@ -30,45 +30,13 @@ 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.
*
* @package tool_behat
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class testable_behat_config_manager extends behat_config_manager {
/**
* Allow access to protected method
* @see parent::merge_config()
* @param mixed $config
* @param mixed $localconfig
* @return mixed
*/
public static function merge_config($config, $localconfig) {
return parent::merge_config($config, $localconfig);
}
/**
* Allow access to protected method
* @see parent::get_config_file_contents()
* @param array $features
* @param array $stepsdefinitions
* @return string
*/
public static function get_config_file_contents($features, $stepsdefinitions) {
return parent::get_config_file_contents($features, $stepsdefinitions);
}
}
/**
* Tool behat tests.
* Behat manager tests.
*
* @package tool_behat
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_behat_testcase extends advanced_testcase {
class tool_behat_manager_testcase extends advanced_testcase {
/**
* behat_config_manager tests.
......@@ -103,7 +71,7 @@ class tool_behat_testcase extends advanced_testcase {
$array = testable_behat_config_manager::merge_config($array1, $array2);
// Overriddes are applied.
// Overrides are applied.
$this->assertEquals('OVERRIDDEN1', $array['simple']);
$this->assertEquals('OVERRIDDEN2', $array['array']['one']);
......@@ -150,10 +118,8 @@ class tool_behat_testcase extends advanced_testcase {
$this->markTestSkipped('Behat not installed.');
}
// It is possible that it has no value.
if (empty($CFG->behat_wwwroot)) {
$CFG->behat_wwwroot = behat_get_wwwroot();
}
// Add some fake test url.
$CFG->behat_wwwroot = 'http://example.com/behat';
// To avoid user value at config.php level.
unset($CFG->behat_config);
......@@ -194,3 +160,34 @@ class tool_behat_testcase extends advanced_testcase {
}
/**
* Allows access to internal methods without exposing them.
*
* @package tool_behat
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class testable_behat_config_manager extends behat_config_manager {
/**
* Allow access to protected method
* @see parent::merge_config()
* @param mixed $config
* @param mixed $localconfig
* @return mixed
*/
public static function merge_config($config, $localconfig) {
return parent::merge_config($config, $localconfig);
}
/**
* Allow access to protected method
* @see parent::get_config_file_contents()
* @param array $features
* @param array $stepsdefinitions
* @return string
*/
public static function get_config_file_contents($features, $stepsdefinitions) {
return parent::get_config_file_contents($features, $stepsdefinitions);
}
}
......@@ -584,22 +584,12 @@ $CFG->admin = 'admin';
//=========================================================================
// 11. BEHAT SUPPORT
//=========================================================================
// Behat needs a separate data directory and unique database prefix:
// Behat test site needs a unique www root, data directory and database prefix:
//
// $CFG->behat_wwwroot = 'http://127.0.0.1/moodle';
// $CFG->behat_prefix = 'bht_';
// $CFG->behat_dataroot = '/home/example/bht_moodledata';
//
// To set a seperate wwwroot for Behat to use, use $CFG->behat_wwwroot; this is set automatically
// to http://localhost:8000 as it is the proposed PHP built-in server URL. Instead of that you can,
// for example, use an alias, add a host to /etc/hosts or add a new virtual host having a URL
// poiting to your production site and another one poiting to your test site. Note that you need
// to ensure that this URL is not accessible from the www as the behat test site uses "sugar"
// credentials (admin/admin) and can be easily hackable.
//
// Example:
// $CFG->behat_wwwroot = 'http://192.168.1.250:8000';
// $CFG->behat_wwwroot = 'http://localhost/moodlesitetesting';
//
// 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...
// These params would be merged with the default Moodle behat.yml, giving priority
......@@ -644,16 +634,6 @@ $CFG->admin = 'admin';
// )
// );
//
// 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
// tests, or in testing/development installations when you are developing in a pre-PHP 5.4 server.
// Note that with this setting enabled $CFG->behat_wwwroot is ignored and $CFG->behat_wwwroot
// value will be the regular $CFG->wwwroot value.
// Example:
// $CFG->behat_switchcompletely = true;
//
// You can force the browser session (not user's sessions) to restart after N seconds. This could
// be useful if you are using a cloud-based service with time restrictions in the browser side.
// Setting this value the browser session that Behat is using will be restarted. Set the time in
......
......@@ -106,12 +106,6 @@ class behat_command {
public static function behat_setup_problem($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) && empty($CFG->behat_wwwroot) && $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');
}
$clibehaterrorstr = "Behat dependencies not installed. Ensure you ran the composer installer. " . self::DOCS_URL . "#Installation\n";
// Moodle setting.
......
......@@ -102,7 +102,10 @@ class behat_util extends testing_util {
// Sets maximum debug level.
set_config('debug', DEBUG_DEVELOPER);
set_config('debugdisplay', true);
set_config('debugdisplay', 1);
// Disable some settings that are not wanted on test sites.
set_config('noemailever', 1);
// Keeps the current version of database and dataroot.
self::store_versions_hash();
......@@ -181,7 +184,7 @@ class behat_util extends testing_util {
* 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)
* to the test environment when using cli-server.
* @throws coding_exception
* @return void
*/
......
......@@ -180,56 +180,80 @@ function behat_clean_init_config() {
function behat_check_config_vars() {
global $CFG;
// CFG->behat_prefix must be set and with value different than CFG->prefix and phpunit_prefix.
if (empty($CFG->behat_prefix) ||
($CFG->behat_prefix == $CFG->prefix) ||
(!empty($CFG->phpunit_prefix) && $CFG->behat_prefix == $CFG->phpunit_prefix)) {
// Verify prefix value.
if (empty($CFG->behat_prefix)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'Define $CFG->behat_prefix in config.php with a value different than $CFG->prefix and $CFG->phpunit_prefix');
'Define $CFG->behat_prefix in config.php');
}
// $CFG->behat_wwwroot must be different than CFG->wwwroot if it is set, it may not be set as
// it can take the default value and we should also consider that will have the same value than
// $CFG->wwwroot if $CFG->behat_switchcompletely is set.
if (!empty($CFG->behat_wwwroot) && $CFG->behat_wwwroot == $CFG->wwwroot && empty($CFG->behat_switchcompletely)) {
if (!empty($CFG->prefix) and $CFG->behat_prefix == $CFG->prefix) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_prefix in config.php must be different from $CFG->prefix');
}
if (!empty($CFG->phpunit_prefix) and $CFG->behat_prefix == $CFG->phpunit_prefix) {
behat_error(BEHAT_EXITCODE_CONFIG,
'Define $CFG->behat_wwwroot in config.php with a value different than $CFG->wwwroot');
'$CFG->behat_prefix in config.php must be different from $CFG->phpunit_prefix');
}
// CFG->behat_dataroot must be set and with value different than CFG->dataroot and phpunit_dataroot.
$CFG->dataroot = realpath($CFG->dataroot);
if (!empty($CFG->behat_dataroot) && is_dir($CFG->behat_dataroot)) {
$CFG->behat_dataroot = realpath($CFG->behat_dataroot);
// Verify behat wwwroot value.
if (empty($CFG->behat_wwwroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'Define $CFG->behat_wwwroot in config.php');
}
if (empty($CFG->behat_dataroot) ||
($CFG->behat_dataroot == $CFG->dataroot) ||
(!empty($CFG->phpunit_dataroot) && is_dir($CFG->phpunit_dataroot)
&& $CFG->behat_dataroot == realpath($CFG->phpunit_dataroot))) {
if (!empty($CFG->wwwroot) and $CFG->behat_wwwroot == $CFG->wwwroot) {
behat_error(BEHAT_EXITCODE_CONFIG,
'Define $CFG->behat_dataroot in config.php with a value different than $CFG->dataroot and $CFG->phpunit_dataroot');
'$CFG->behat_wwwroot in config.php must be different from $CFG->wwwroot');
}
// Verify behat dataroot value.
if (empty($CFG->behat_dataroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'Define $CFG->behat_dataroot in config.php');
}
if (!file_exists($CFG->behat_dataroot)) {
$permissions = isset($CFG->directorypermissions) ? $CFG->directorypermissions : 02777;
if (!mkdir($CFG->behat_dataroot, $permissions, true)) {
behat_error(BEHAT_EXITCODE_PERMISSIONS, '$CFG->behat_dataroot directory can not be created');
}
}
$CFG->behat_dataroot = realpath($CFG->behat_dataroot);
if (empty($CFG->behat_dataroot) or !is_dir($CFG->behat_dataroot) or !is_writable($CFG->behat_dataroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_dataroot in config.php must point to an existing writable directory');
}
if (!empty($CFG->dataroot) and $CFG->behat_dataroot == realpath($CFG->dataroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_dataroot in config.php must be different from $CFG->dataroot');
}
if (!empty($CFG->phpunit_dataroot) and $CFG->behat_dataroot == realpath($CFG->phpunit_dataroot)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_dataroot in config.php must be different from $CFG->phpunit_dataroot');
}
}
/**
* Returns a URL based on the priorities between $CFG->behat_* vars.
*
* 1.- Switch completely wins and overwrites behat_wwwroot
* 2.- behat_wwwroot alternatively
* 3.- http://localhost:8000 if there is nothing else
*
* @return string
* Should we switch to the test site data?
* @return bool
*/
function behat_get_wwwroot() {
function behat_is_test_site() {
global $CFG;
if (!empty($CFG->behat_switchcompletely)) {
return $CFG->wwwroot;
} else if (!empty($CFG->behat_wwwroot)) {
return $CFG->behat_wwwroot;
if (defined('BEHAT_UTIL')) {
// This is the admin tool that installs/drops the test site install.
return true;
}
if (defined('BEHAT_TEST')) {
// This is the main vendor/bin/behat script.
return true;
}
if (empty($CFG->behat_wwwroot)) {
return false;
}
if (isset($_SERVER['REMOTE_ADDR']) and behat_is_requested_url($CFG->behat_wwwroot)) {
// Something is accessing the web server like a real browser.
return true;
}
return 'http://localhost:8000';
return false;
}
/**
......
......@@ -5696,6 +5696,11 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '',
return false;
}
if (defined('BEHAT_SITE_RUNNING')) {
// Fake email sending in behat.
return true;
}
if (!empty($CFG->noemailever)) {
// Hidden setting for development sites, set in config.php if needed.
debugging('Not sending email due to $CFG->noemailever config setting', DEBUG_NORMAL);
......
......@@ -59,6 +59,45 @@ if (!isset($CFG)) {
// it can not be anything else, there is no point in having this in config.php
$CFG->dirroot = dirname(dirname(__FILE__));
if (defined('BEHAT_SITE_RUNNING')) {
// We already switched to behat test site previously.
} else if (!empty($CFG->behat_wwwroot) or !empty($CFG->behat_dataroot) or !empty($CFG->behat_prefix)) {
// The behat is configured on this server, we need to find out if this is the behat test
// site based on the URL used for access.
require_once(__DIR__ . '/../lib/behat/lib.php');
if (behat_is_test_site()) {
// Checking the integrity of the provided $CFG->behat_* vars and the
// selected wwwroot to prevent conflicts with production and phpunit environments.
behat_check_config_vars();
if (!defined('BEHAT_UTIL') and !defined('BEHAT_TEST')) {
// Somebody tries to access test site directly, tell them if not enabled.
if (!file_exists($CFG->behat_dataroot . '/behat/test_environment_enabled.txt')) {
behat_error(BEHAT_EXITCODE_CONFIG, 'Behat is configured but not enabled on this test site.');
}
}
// Constant used to inform that the behat test site is being used,
// this includes all the processes executed by the behat CLI command like
// the site reset, the steps executed by the browser drivers when simulating
// a user session and a real session when browsing manually to $CFG->behat_wwwroot
// like the browser driver does automatically.
// Different from BEHAT_TEST as only this last one can perform CLI
// actions like reset the site or use data generators.
define('BEHAT_SITE_RUNNING', true);
// Clean extra config.php settings.
behat_clean_init_config();
// Now we can begin switching $CFG->X for $CFG->behat_X.
$CFG->wwwroot = $CFG->behat_wwwroot;
$CFG->passwordsaltmain = 'moodle';
$CFG->prefix = $CFG->behat_prefix;
$CFG->dataroot = $CFG->behat_dataroot;
}
}
// Normalise dataroot - we do not want any symbolic links, trailing / or any other weirdness there
if (!isset($CFG->dataroot)) {
if (isset($_SERVER['REMOTE_ADDR'])) {
......@@ -91,84 +130,6 @@ if (!isset($CFG->wwwroot) or $CFG->wwwroot === 'http://example.com/moodle') {
exit(1);
}
// Test environment is requested if:
// * If $CFG->behat_switchcompletely has been set (maintains CLI scripts behaviour, which ATM is only preventive).
// * If we are accessing though the built-in web server (cli-server).
// * Behat is running (constant set hooking the behat init process before requiring config.php).
// * If $CFG->behat_wwwroot has been set and the hostname/port match what the page was requested with.
// Test environment is enabled if:
// * User has previously enabled through admin/tool/behat/cli/util.php --enable or admin/tool/behat/cli/init.php
// Both are required to switch to test mode
if (!defined('BEHAT_SITE_RUNNING') && !empty($CFG->behat_dataroot) &&
!empty($CFG->behat_prefix) && file_exists($CFG->behat_dataroot)) {
// Only included if behat_* are set, it is not likely to be a production site.
require_once(__DIR__ . '/../lib/behat/lib.php');
$defaultbehatwwwroot = behat_get_wwwroot();
if (!empty($CFG->behat_switchcompletely) && php_sapi_name() !== 'cli') {
// Switch completely uses the production wwwroot as the test site URL.
$behatwwwroot = $defaultbehatwwwroot;
} elseif (php_sapi_name() === 'cli-server') {
// If we are using the built-in server we use the provided $CFG->behat_wwwroot
// value or the default one if $CFG->behat_wwwroot is not set, only if it matches
// the requested URL.
if (behat_is_requested_url($defaultbehatwwwroot)) {
$behatwwwroot = $defaultbehatwwwroot;
}
} elseif (defined('BEHAT_TEST')) {
// This is when moodle codebase runs through vendor/bin/behat, we "are not supposed"
// to need a wwwroot, but before using the production one we should set something else
// as an alternative.
$behatwwwroot = $defaultbehatwwwroot;
} elseif (!empty($CFG->behat_wwwroot) && !empty($_SERVER['HTTP_HOST'])) {
// If $CFG->behat_wwwroot was set and matches the requested URL we
// use $CFG->behat_wwwroot as our wwwroot.
if (behat_is_requested_url($CFG->behat_wwwroot)) {
$behatwwwroot = $CFG->behat_wwwroot;
}
}
// If we found a proper behatwwwroot then we consider the behat test as requested.
$testenvironmentrequested = !empty($behatwwwroot);
// Only switch to test environment if it has been enabled.
$CFG->behat_dataroot = realpath($CFG->behat_dataroot);
$testenvironmentenabled = file_exists($CFG->behat_dataroot . '/behat/test_environment_enabled.txt');
if ($testenvironmentenabled && $testenvironmentrequested) {
// Now we know which one will be our behat wwwroot.
$CFG->behat_wwwroot = $behatwwwroot;
// Checking the integrity of the provided $CFG->behat_* vars and the
// selected wwwroot to prevent conflicts with production and phpunit environments.
behat_check_config_vars();
// Constant used to inform that the behat test site is being used,
// this includes all the processes executed by the behat CLI command like
// the site reset, the steps executed by the browser drivers when simulating
// a user session and a real session when browsing manually to $CFG->behat_wwwroot
// like the browser driver does automatically.
// Different from BEHAT_TEST as only this last one can perform CLI
// actions like reset the site or use data generators.
define('BEHAT_SITE_RUNNING', true);
// Clean extra config.php settings.
behat_clean_init_config();
// Now we can begin switching $CFG->X for $CFG->behat_X.
$CFG->wwwroot = $CFG->behat_wwwroot;
$CFG->passwordsaltmain = 'moodle';
$CFG->prefix = $CFG->behat_prefix;
$CFG->dataroot = $CFG->behat_dataroot;
}
}
// Make sure there is some database table prefix.
if (!isset($CFG->prefix)) {
$CFG->prefix = '';
......
......@@ -107,7 +107,7 @@ class behat_hooks extends behat_base {
if (!behat_util::is_server_running()) {
throw new Exception($CFG->behat_wwwroot .
' is not available, ensure you started your PHP built-in server or your web server is correctly started and set up.' .
' is not available, ensure you specified correct url and that the server is set up and started.' .
' More info in ' . behat_command::DOCS_URL . '#Running_tests');
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment