Skip to content

Commit

Permalink
MDL-37046 behat: New setting to switch completely to test env.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Jan 29, 2013
1 parent f2f66ea commit bd77061
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
2 changes: 1 addition & 1 deletion admin/tool/behat/lang/en/tool_behat.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@
$string['viewsteps'] = 'Filter';
$string['wheninfo'] = 'When. Actions that provokes an event';
$string['wrongphpversion'] = 'PHP 5.4.0 or higher is required to run acceptance tests';
$string['wrongbehatsetup'] = 'Something is wrong with the setup, ensure you ran the composer installer and /lib/behat/bin/behat file has execution permissions';
$string['wrongbehatsetup'] = 'Something is wrong with the setup, ensure you ran the composer installer and vendor/bin/behat file has execution permissions';
17 changes: 9 additions & 8 deletions admin/tool/behat/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected static function get_config_file_contents($prefix, $features, $stepsdef
),
'extensions' => array(
'Behat\MinkExtension\Extension' => array(
'base_url' => $CFG->test_wwwroot,
'base_url' => $CFG->behat_wwwroot,
'goutte' => null,
'selenium2' => null
),
Expand All @@ -207,8 +207,8 @@ protected static function get_config_file_contents($prefix, $features, $stepsdef
);

// In case user defined overrides respect them over our default ones.
if (!empty($CFG->behatconfig)) {
$config = self::merge_config($config, $CFG->behatconfig);
if (!empty($CFG->behat_config)) {
$config = self::merge_config($config, $CFG->behat_config);
}

return Symfony\Component\Yaml\Yaml::dump($config, 10, 2);
Expand Down Expand Up @@ -288,15 +288,15 @@ public static function get_components_steps_definitions() {
}

/**
* Checks if $CFG->test_wwwroot is available
* Checks if $CFG->behat_wwwroot is available
*
* @return boolean
*/
public static function is_server_running() {
global $CFG;

$request = new curl();
$request->get($CFG->test_wwwroot);
$request->get($CFG->behat_wwwroot);
return (true && !$request->get_errno());
}

Expand Down Expand Up @@ -347,7 +347,8 @@ protected static function test_environment_problem() {
protected static function check_behat_setup($checkphp = false) {
global $CFG;

if ($checkphp && version_compare(PHP_VERSION, '5.4.0', '<')) {
// We don't check the PHP version if $CFG->behat_switchcompletely has been enabled.
if (empty($CFG->behat_switchcompletely) && $checkphp && version_compare(PHP_VERSION, '5.4.0', '<')) {
throw new Exception(get_string('wrongphpversion', 'tool_behat'));
}

Expand Down Expand Up @@ -384,7 +385,7 @@ protected static function check_behat_setup($checkphp = false) {
* features and steps definitions.
*
* Stores a file in dataroot/behat to allow Moodle to switch
* to the test environment when using cli-server
* to the test environment when using cli-server (or $CFG->behat_switchcompletely)
*
* @throws file_exception
*/
Expand All @@ -407,7 +408,7 @@ protected static function start_test_mode() {

$behatdir = self::get_behat_dir();

$contents = '$CFG->test_wwwroot, $CFG->phpunit_prefix and $CFG->phpunit_dataroot' .
$contents = '$CFG->behat_wwwroot, $CFG->phpunit_prefix and $CFG->phpunit_dataroot' .
' are currently used as $CFG->wwwroot, $CFG->prefix and $CFG->dataroot';
$filepath = $behatdir . '/test_environment_enabled.txt';
if (!file_put_contents($filepath, $contents)) {
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/behat/tests/tool_behat_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function test_merge_configs() {
public function test_config_file_contents() {
global $CFG;

unset($CFG->behatconfig);
unset($CFG->behat_config);

// List.
$features = array(
Expand All @@ -174,7 +174,7 @@ public function test_config_file_contents() {

$this->assertContains('features: /i/am/a/prefix/lib/behat/features', $contents);
$this->assertContains('micarro: /me/lo/robaron', $contents);
$this->assertContains('base_url: \'' . $CFG->test_wwwroot . '\'', $contents);
$this->assertContains('base_url: \'' . $CFG->behat_wwwroot . '\'', $contents);
$this->assertContains('class: behat_init_context', $contents);
$this->assertContains('- feature1', $contents);
$this->assertContains('- feature3', $contents);
Expand Down
18 changes: 14 additions & 4 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,15 +548,15 @@
// Behat uses http://localhost:8000 as default URL to run
// the acceptance tests, you can override this value.
// Example:
// $CFG->test_wwwroot = 'http://192.168.1.250:8000';
// $CFG->behat_wwwroot = 'http://192.168.1.250:8000';
//
// 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 will be merged with the default Moodle behat.yml, giving priority
// to the ones specified here. The array format is a YAML following the behat
// 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
// params hierarchy. More info: http://docs.behat.org/guides/7.config.html
// Example:
// $CFG->behatconfig = array(
// $CFG->behat_config = array(
// 'default' => array(
// 'formatter' => array(
// 'name' => 'pretty',
Expand All @@ -568,6 +568,16 @@
// )
// );
//
// You can completely switch to test environment when "php admin/tool/behatcli/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;
//

//=========================================================================
// ALL DONE! To continue installation, visit your main page with a browser
Expand Down
28 changes: 18 additions & 10 deletions lib/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,24 @@
exit(1);
}

// Default URL for acceptance testing.
if (!isset($CFG->test_wwwroot)) {
$CFG->test_wwwroot = 'http://localhost:8000';
}

// Switch to test site only when test environment is enabled: Both when the
// acceptance tests are running and when Behat is requiring moodle codebase.
if ((php_sapi_name() === 'cli-server' || defined('BEHAT_RUNNING')) &&
file_exists($CFG->dataroot . '/behat/test_environment_enabled.txt')) {
$CFG->wwwroot = $CFG->test_wwwroot;
// Ignore $CFG->behat_wwwroot and use the same wwwroot in case test env. is enabled.
if (isset($CFG->behat_switchcompletely)) {
$CFG->behat_wwwroot = $CFG->wwwroot;

// Default URL for acceptance testing, only accessible from localhost.
} else if (!isset($CFG->behat_wwwroot)) {
$CFG->behat_wwwroot = 'http://localhost:8000';
}

// Test environment is requested if: Behat is running, if we are accessing though cli-server
// or if $CFG->behat_switchcompletely has been set (maintains CLI scripts behaviour).
$testenvironmentrequested = (isset($CFG->behat_switchcompletely) && php_sapi_name() !== 'cli') ||
php_sapi_name() === 'cli-server' ||
defined('BEHAT_RUNNING');
// Only switch to test environment if it has been enabled.
$testenvironmentenabled = file_exists($CFG->dataroot . '/behat/test_environment_enabled.txt');
if ($testenvironmentenabled && $testenvironmentrequested) {
$CFG->wwwroot = $CFG->behat_wwwroot;
$CFG->passwordsaltmain = 'phpunit';
$CFG->originaldataroot = $CFG->dataroot;
$CFG->prefix = $CFG->phpunit_prefix;
Expand Down

0 comments on commit bd77061

Please sign in to comment.