diff --git a/admin/tool/behat/tests/behat/fixtures/environment.php b/admin/tool/behat/tests/behat/fixtures/environment.php new file mode 100644 index 00000000000..7bbafda9346 --- /dev/null +++ b/admin/tool/behat/tests/behat/fixtures/environment.php @@ -0,0 +1,34 @@ +. + +/** + * Display environment used for running behat. + * + * This file is used for behat testing to ensure cli and apache + * version of environment is same. + * + * @package tool_behat + * @copyright 2016 onwards Rajesh Taneja + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__.'/../../../../../../config.php'); + +// Only continue for behat site. +defined('BEHAT_SITE_RUNNING') || die(); + +require_once($CFG->libdir.'/behat/classes/util.php'); +echo json_encode(behat_util::get_environment(), true); diff --git a/lib/behat/classes/util.php b/lib/behat/classes/util.php index a1f4696f3e6..80f59232581 100644 --- a/lib/behat/classes/util.php +++ b/lib/behat/classes/util.php @@ -138,20 +138,42 @@ class behat_util extends testing_util { } /** - * Checks if $CFG->behat_wwwroot is available + * Checks if $CFG->behat_wwwroot is available and using same versions for cli and web. * - * @return bool + * @return void */ - public static function is_server_running() { + public static function check_server_status() { global $CFG; - $request = new curl(); - $request->get($CFG->behat_wwwroot); + $url = $CFG->behat_wwwroot . '/admin/tool/behat/tests/behat/fixtures/environment.php'; - if ($request->get_errno() === 0) { - return true; + // Get web versions used by behat site. + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $result = curl_exec($ch); + curl_close($ch); + + if (empty($result)) { + + behat_error (BEHAT_EXITCODE_REQUIREMENT, $CFG->behat_wwwroot . ' is not available, ensure you specified ' . + 'correct url and that the server is set up and started.' . PHP_EOL . ' More info in ' . + behat_command::DOCS_URL . '#Running_tests' . PHP_EOL); + } + + // Check if cli version is same as web version. + $result = json_decode($result, true); + $clienv = self::get_environment(); + if ($result != $clienv) { + $output = 'Differences decteted between cli and webserver...'.PHP_EOL; + foreach ($result as $key => $version) { + if ($clienv[$key] != $version) { + $output .= ' ' . $key . ': ' . PHP_EOL; + $output .= ' - web server: ' . $version . PHP_EOL; + $output .= ' - cli: ' . $clienv[$key] . PHP_EOL; + } + } + echo $output; } - return false; } /** diff --git a/lib/testing/classes/util.php b/lib/testing/classes/util.php index dae5a4e149b..34c820c4350 100644 --- a/lib/testing/classes/util.php +++ b/lib/testing/classes/util.php @@ -778,34 +778,28 @@ abstract class testing_util { * @return string The site info */ public static function get_site_info() { - global $CFG, $DB; + global $CFG; $output = ''; // All developers have to understand English, do not localise! + $env = self::get_environment(); - $release = null; - require("$CFG->dirroot/version.php"); - - $output .= "Moodle $release"; + $output .= "Moodle ".$env['moodleversion']; if ($hash = self::get_git_hash()) { $output .= ", $hash"; } $output .= "\n"; // Add php version. - $phpversion = phpversion(); - $output .= "Php: ". $phpversion; + require_once($CFG->libdir.'/environmentlib.php'); + $output .= "Php: ". normalize_version($env['phpversion']); // Add database type and version. - $dbtype = $DB->get_dbvendor(); - $dbinfo = $DB->get_server_info(); - $dbversion = $dbinfo['version']; - $output .= ", " . ucfirst($dbtype) . ": " . $dbversion; + $output .= ", " . $env['dbtype'] . ": " . $env['dbversion']; // OS details. - $osdetails = php_uname('s') . " " . php_uname('r') . " " . php_uname('m'); - $output .= ", OS: " . $osdetails . "\n"; + $output .= ", OS: " . $env['os'] . "\n"; return $output; } @@ -989,4 +983,43 @@ abstract class testing_util { fclose($fp); } } + + /** + * Return list of environment versions on which tests will run. + * Environment includes: + * - moodleversion + * - phpversion + * - dbtype + * - dbversion + * - os + * + * @return array + */ + public static function get_environment() { + global $CFG, $DB; + + $env = array(); + + // Add moodle version. + $release = null; + require("$CFG->dirroot/version.php"); + $env['moodleversion'] = $release; + + // Add php version. + $phpversion = phpversion(); + $env['phpversion'] = $phpversion; + + // Add database type and version. + $dbtype = $DB->get_dbvendor(); + $dbinfo = $DB->get_server_info(); + $dbversion = $dbinfo['version']; + $env['dbtype'] = ucfirst($dbtype); + $env['dbversion'] = $dbversion; + + // OS details. + $osdetails = php_uname('s') . " " . php_uname('r') . " " . php_uname('m'); + $env['os'] = $osdetails; + + return $env; + } } diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index cddbe93c96f..03e7ef1e7b9 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -137,11 +137,8 @@ class behat_hooks extends behat_base { // If not done, then it can return apache error, while running tests. behat_util::reset_all_data(); - if (!behat_util::is_server_running()) { - throw new Exception($CFG->behat_wwwroot . - ' 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'); - } + // Check if server is running and using same version for cli and apache. + behat_util::check_server_status(); // Prevents using outdated data, upgrade script would start and tests would fail. if (!behat_util::is_test_data_updated()) { diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 683eeee2908..e2c2a0db212 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -8,6 +8,7 @@ information provided here is intended especially for developers. checking it against the parameter (PARAM_*) type of the target user field. MDL-52781 is going to add support to null/not null and choices validation, replacing the existing code to validate the user fields in different places in a common way. +* behat_util::is_server_running() is removed, please use behat_util::check_server_status() instead. === 3.0.1 ===