mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
Merge branch 'w31_MDL-34538_m24_validatefloats' of git://github.com/skodak/moodle
This commit is contained in:
commit
a38514ac3f
@ -666,7 +666,8 @@ function optional_param_array($parname, $default, $type) {
|
||||
* @param string $type PARAM_ constant
|
||||
* @param bool $allownull are nulls valid value?
|
||||
* @param string $debuginfo optional debug information
|
||||
* @return mixed the $param value converted to PHP type or invalid_parameter_exception
|
||||
* @return mixed the $param value converted to PHP type
|
||||
* @throws invalid_parameter_exception if $param is not of given type
|
||||
*/
|
||||
function validate_param($param, $type, $allownull=NULL_NOT_ALLOWED, $debuginfo='') {
|
||||
if (is_null($param)) {
|
||||
@ -681,7 +682,15 @@ function validate_param($param, $type, $allownull=NULL_NOT_ALLOWED, $debuginfo='
|
||||
}
|
||||
|
||||
$cleaned = clean_param($param, $type);
|
||||
if ((string)$param !== (string)$cleaned) {
|
||||
|
||||
if ($type == PARAM_FLOAT) {
|
||||
// Do not detect precision loss here.
|
||||
if (is_float($param) or is_int($param)) {
|
||||
// These always fit.
|
||||
} else if (!is_numeric($param) or !preg_match('/^[\+-]?[0-9]*\.?[0-9]*(e[-+]?[0-9]+)?$/i', (string)$param)) {
|
||||
throw new invalid_parameter_exception($debuginfo);
|
||||
}
|
||||
} else if ((string)$param !== (string)$cleaned) {
|
||||
// conversion to string is usually lossless
|
||||
throw new invalid_parameter_exception($debuginfo);
|
||||
}
|
||||
|
@ -912,6 +912,56 @@ class moodlelib_testcase extends advanced_testcase {
|
||||
} catch (invalid_parameter_exception $ex) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
try {
|
||||
$param = validate_param('1.0', PARAM_FLOAT);
|
||||
$this->assertSame(1.0, $param);
|
||||
|
||||
// Make sure valid floats do not cause exception.
|
||||
validate_param(1.0, PARAM_FLOAT);
|
||||
validate_param(10, PARAM_FLOAT);
|
||||
validate_param('0', PARAM_FLOAT);
|
||||
validate_param('119813454.545464564564546564545646556564465465456465465465645645465645645645', PARAM_FLOAT);
|
||||
validate_param('011.1', PARAM_FLOAT);
|
||||
validate_param('11', PARAM_FLOAT);
|
||||
validate_param('+.1', PARAM_FLOAT);
|
||||
validate_param('-.1', PARAM_FLOAT);
|
||||
validate_param('1e10', PARAM_FLOAT);
|
||||
validate_param('.1e+10', PARAM_FLOAT);
|
||||
validate_param('1E-1', PARAM_FLOAT);
|
||||
$this->assertTrue(true);
|
||||
} catch (invalid_parameter_exception $ex) {
|
||||
$this->fail('Valid float notation not accepted');
|
||||
}
|
||||
try {
|
||||
$param = validate_param('1,2', PARAM_FLOAT);
|
||||
$this->fail('invalid_parameter_exception expected');
|
||||
} catch (invalid_parameter_exception $ex) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
try {
|
||||
$param = validate_param('', PARAM_FLOAT);
|
||||
$this->fail('invalid_parameter_exception expected');
|
||||
} catch (invalid_parameter_exception $ex) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
try {
|
||||
$param = validate_param('.', PARAM_FLOAT);
|
||||
$this->fail('invalid_parameter_exception expected');
|
||||
} catch (invalid_parameter_exception $ex) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
try {
|
||||
$param = validate_param('e10', PARAM_FLOAT);
|
||||
$this->fail('invalid_parameter_exception expected');
|
||||
} catch (invalid_parameter_exception $ex) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
try {
|
||||
$param = validate_param('abc', PARAM_FLOAT);
|
||||
$this->fail('invalid_parameter_exception expected');
|
||||
} catch (invalid_parameter_exception $ex) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
function test_shorten_text() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user