diff --git a/admin/environment.xml b/admin/environment.xml index e596403a79d..29805fa16e4 100644 --- a/admin/environment.xml +++ b/admin/environment.xml @@ -1150,6 +1150,7 @@ + @@ -1273,6 +1274,7 @@ + diff --git a/lang/en/admin.php b/lang/en/admin.php index c59b42fc2b9..3626a7b4a76 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -1071,6 +1071,7 @@ $string['unsettheme'] = 'Unset theme'; $string['unsupported'] = 'Unsupported'; $string['unsupporteddbstorageengine'] = 'The database storage engine being used is no longer supported.'; $string['unsupporteddbtablerowformat'] = 'Your database has tables using Antelope as the file format. You are recommended to convert the tables to the Barracuda file format. See the documentation Administration via command line for details of a tool for converting InnoDB tables to Barracuda.'; +$string['unsupportedphpversion7'] = 'PHP version 7 is not supported.'; $string['unsuspenduser'] = 'Activate user account'; $string['updateaccounts'] = 'Update existing accounts'; $string['updatecomponent'] = 'Update component'; diff --git a/lib/environmentlib.php b/lib/environmentlib.php index 802ebef0dc8..cd251675d1b 100644 --- a/lib/environmentlib.php +++ b/lib/environmentlib.php @@ -1538,3 +1538,37 @@ function process_environment_result($element, &$result) { /// Process restrict, modifying $result if needed. process_environment_restrict($element, $result); } + +/** + * Check if the current PHP version is greater than or equal to + * PHP version 7. + * + * @param object $result an environment_results instance + * @return bool result of version check + */ +function restrict_php_version_7(&$result) { + return restrict_php_version($result, '7'); +} + +/** + * Check if the current PHP version is greater than or equal to an + * unsupported version. + * + * @param object $result an environment_results instance + * @param string $version the version of PHP that can't be used + * @return bool result of version check + */ +function restrict_php_version(&$result, $version) { + + // Get the current PHP version. + $currentversion = normalize_version(phpversion()); + + // Confirm we're using a supported PHP version. + if (version_compare($currentversion, $version, '<')) { + // Everything is ok, the restriction doesn't apply. + return false; + } else { + // We're using an unsupported PHP version, apply restriction. + return true; + } +} diff --git a/lib/tests/environment_test.php b/lib/tests/environment_test.php index 2554b0d243a..e59fbdbe3cd 100644 --- a/lib/tests/environment_test.php +++ b/lib/tests/environment_test.php @@ -128,4 +128,69 @@ END; $this->assertFalse(environment_verify_plugin('mod_someother', $plugin1['PLUGIN'])); $this->assertFalse(environment_verify_plugin('mod_someother', $plugin2['PLUGIN'])); } + + /** + * Test the restrict_php_version() function returns true if the current + * PHP version is greater than the restricted version + */ + public function test_restrict_php_version_greater_than_restricted_version() { + global $CFG; + require_once($CFG->libdir.'/environmentlib.php'); + + $result = new environment_results('php'); + $delimiter = '.'; + // Get the current PHP version. + $currentversion = explode($delimiter, normalize_version(phpversion())); + // Lets drop back one major version to ensure we trip the restriction. + $currentversion[0]--; + $restrictedversion = implode($delimiter, $currentversion); + + // Make sure the status is true before the test to see it flip to false. + $result->setStatus(true); + + $this->assertTrue(restrict_php_version($result, $restrictedversion), + 'restrict_php_version returns true if the current version exceeds the restricted version'); + } + + /** + * Test the restrict_php_version() function returns true if the current + * PHP version is equal to the restricted version + */ + public function test_restrict_php_version_equal_to_restricted_version() { + global $CFG; + require_once($CFG->libdir.'/environmentlib.php'); + + $result = new environment_results('php'); + // Get the current PHP version. + $currentversion = normalize_version(phpversion()); + + // Make sure the status is true before the test to see it flip to false. + $result->setStatus(true); + + $this->assertTrue(restrict_php_version($result, $currentversion), + 'restrict_php_version returns true if the current version is equal to the restricted version'); + } + + /** + * Test the restrict_php_version() function returns false if the current + * PHP version is less than the restricted version + */ + public function test_restrict_php_version_less_than_restricted_version() { + global $CFG; + require_once($CFG->libdir.'/environmentlib.php'); + + $result = new environment_results('php'); + $delimiter = '.'; + // Get the current PHP version. + $currentversion = explode($delimiter, normalize_version(phpversion())); + // Lets increase the major version to ensure don't trip the restriction. + $currentversion[0]++; + $restrictedversion = implode($delimiter, $currentversion); + + // Make sure the status is true before the test to see it flip to false. + $result->setStatus(true); + + $this->assertFalse(restrict_php_version($result, $restrictedversion), + 'restrict_php_version returns false if the current version is less than the restricted version'); + } }