Commit bd770617 authored by David Monllaó's avatar David Monllaó
Browse files

MDL-37046 behat: New setting to switch completely to test env.

parent f2f66ea4
......@@ -42,4 +42,4 @@ $string['theninfo'] = 'Then. Checkings to ensure the outcomes are the expected o
$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';
......@@ -194,7 +194,7 @@ class tool_behat {
),
'extensions' => array(
'Behat\MinkExtension\Extension' => array(
'base_url' => $CFG->test_wwwroot,
'base_url' => $CFG->behat_wwwroot,
'goutte' => null,
'selenium2' => null
),
......@@ -207,8 +207,8 @@ class tool_behat {
);
// 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);
......@@ -288,7 +288,7 @@ class tool_behat {
}
/**
* Checks if $CFG->test_wwwroot is available
* Checks if $CFG->behat_wwwroot is available
*
* @return boolean
*/
......@@ -296,7 +296,7 @@ class tool_behat {
global $CFG;
$request = new curl();
$request->get($CFG->test_wwwroot);
$request->get($CFG->behat_wwwroot);
return (true && !$request->get_errno());
}
......@@ -347,7 +347,8 @@ class tool_behat {
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'));
}
......@@ -384,7 +385,7 @@ class tool_behat {
* 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
*/
......@@ -407,7 +408,7 @@ class tool_behat {
$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)) {
......
......@@ -155,7 +155,7 @@ class tool_behat_testcase extends advanced_testcase {
public function test_config_file_contents() {
global $CFG;
unset($CFG->behatconfig);
unset($CFG->behat_config);
// List.
$features = array(
......@@ -174,7 +174,7 @@ class tool_behat_testcase extends advanced_testcase {
$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);
......
......@@ -548,15 +548,15 @@ $CFG->admin = 'admin';
// 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',
......@@ -568,6 +568,16 @@ $CFG->admin = 'admin';
// )
// );
//
// 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
......
......@@ -90,16 +90,24 @@ if (!isset($CFG->wwwroot) or $CFG->wwwroot === 'http://example.com/moodle') {
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;
......
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