Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Merge branch 'w51_MDL-43213_m27_simplebehat' of https://github.com/sk…
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Dec 17, 2013
2 parents 37f59a6 + 60129d5 commit adfcdcb
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 234 deletions.
66 changes: 14 additions & 52 deletions admin/tool/behat/cli/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,42 +78,28 @@
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")) {
Expand All @@ -133,30 +119,6 @@
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');
Expand Down Expand Up @@ -185,7 +147,7 @@
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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ó
Expand All @@ -30,45 +30,13 @@
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.
Expand Down Expand Up @@ -103,7 +71,7 @@ public function test_merge_configs() {

$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']);

Expand Down Expand Up @@ -150,10 +118,8 @@ public function test_config_file_contents() {
$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);
Expand Down Expand Up @@ -194,3 +160,34 @@ public function test_config_file_contents() {

}

/**
* 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);
}
}
24 changes: 2 additions & 22 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,22 +584,12 @@
//=========================================================================
// 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
Expand Down Expand Up @@ -644,16 +634,6 @@
// )
// );
//
// 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
Expand Down
6 changes: 0 additions & 6 deletions lib/behat/classes/behat_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,6 @@ public final static function run($options = '') {
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.
Expand Down
7 changes: 5 additions & 2 deletions lib/behat/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ public static function install_site() {

// 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();
Expand Down Expand Up @@ -181,7 +184,7 @@ protected static function test_environment_problem() {
* 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
*/
Expand Down
90 changes: 57 additions & 33 deletions lib/behat/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Loading

0 comments on commit adfcdcb

Please sign in to comment.