Commit 7315791a authored by Elias Villiger's avatar Elias Villiger
Browse files

Feature #4542 - Add unit tests

parent 3bb338a2
......@@ -2448,6 +2448,9 @@ See also at specific *FormElement* definitions.
+------------------------+--------+----------------------------------------------------------------------------------------------------------+
| dateFormat | string | yyyy-mm-dd | dd.mm.yyyy |
+------------------------+--------+----------------------------------------------------------------------------------------------------------+
| decimalFormat | string | [precision,scale] Limits and formats input to a decimal number with the specified precision and scale. |
| | | If no precision and scale are specified, the decimal format is pulled from the table definition. |
+------------------------+--------+----------------------------------------------------------------------------------------------------------+
| showSeconds | string | 0|1 - Shows the seconds on form load. Default: 0 |
+------------------------+--------+----------------------------------------------------------------------------------------------------------+
| showZero | string | 0|1 - Empty timestamp: '0'(default) - nothing shown, '1' - the string '0000-00-00 00:00:00' is displayed |
......
......@@ -69,8 +69,6 @@ const SANITIZE_ALLOW_ALNUMX = "alnumx";
const SANITIZE_ALLOW_DIGIT = "digit";
const SANITIZE_ALLOW_NUMERICAL = "numerical";
const SANITIZE_ALLOW_EMAIL = "email";
const SANITIZE_ALLOW_MIN_MAX = "min|max";
const SANITIZE_ALLOW_MIN_MAX_DATE = "min|max date";
const SANITIZE_ALLOW_PATTERN = "pattern";
const SANITIZE_ALLOW_ALLBUT = "allbut";
const SANITIZE_ALLOW_ALL = "all";
......
......@@ -94,7 +94,7 @@ class Sanitize {
if ($decimalFormat != '' && $checkType !== SANITIZE_ALLOW_DIGIT) {
// overwrite pattern with decimalFormat pattern
$decimalFormatArray = explode(',', $decimalFormat);
$pattern = "^[0-9]{0,$decimalFormatArray[0]}(\.[0-9]{0,$decimalFormatArray[1]})?$";
$pattern = "^-?[0-9]{0," . ($decimalFormatArray[0] - $decimalFormatArray[1]) . "}(\.[0-9]{0,$decimalFormatArray[1]})?$";
}
return $pattern;
......
......@@ -782,7 +782,12 @@ class Support {
}
} else {
// Decimal format is defined in parameter field
if (!preg_match("/^[0-9]+,[0-9]+$/", $formElement[FE_DECIMAL_FORMAT]))
$isValidDecimalFormat = preg_match("/^[0-9]+,[0-9]+$/", $formElement[FE_DECIMAL_FORMAT]);
if ($isValidDecimalFormat) {
$decimalFormatArray = explode(',', $formElement[FE_DECIMAL_FORMAT]);
$isValidDecimalFormat = $decimalFormatArray[0] >= $decimalFormatArray[1];
}
if (!$isValidDecimalFormat)
throw new UserFormException("Invalid decimalFormat.", ERROR_INVALID_DECIMAL_FORMAT);
}
}
......
......@@ -125,11 +125,20 @@ class BuildFormPlainTest extends AbstractDatabaseTest {
$this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$formElement[FE_CHECK_TYPE] = '';
$formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
$formElement[FE_CHECK_PATTERN] = '';
// Explizit size
// Decimal format
$formElement[FE_DECIMAL_FORMAT] = '5,2';
$result = $build->buildInput($formElement, 'name:1', '', $json);
$this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^[0-9]{0,3},[0-9]{0,2}$" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$formElement[FE_DECIMAL_FORMAT] = '';
// Explicit size
$formElement['size'] = 40;
$formElement['maxLength'] = 40;
$result = $build->buildInput($formElement, 'name:1', '', $json);
......
......@@ -83,6 +83,25 @@ class SanitizeTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL), "SANITIZE_ALL fails");
$this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALLBUT), "SANITIZE_ALLBUT fails");
# Check Decimal Format
$val = '123.45';
$this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'));
$this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '10,3'));
$this->assertEquals('', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '4,2'));
$this->assertEquals('', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,1'));
$val = '-123.45';
$this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'));
$this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '10,3'));
$this->assertEquals('', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '4,2'));
$this->assertEquals('', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,1'));
$val = 'a.00';
$this->assertEquals('', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'));
$val = '-0.1e9';
$this->assertEquals('', Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'));
$val = '-4';
$this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'));
$val = '.42';
$this->assertEquals($val, Sanitize::sanitize($val, SANITIZE_ALLOW_ALL, '', '5,2'));
}
/**
......@@ -111,11 +130,11 @@ class SanitizeTest extends \PHPUnit_Framework_TestCase {
# Check min/max dates
$msg = "SANITIZE_MIN_MAX Date fails";
$val = "2010-05-01";
$this->assertEquals($val, Sanitize::checkMinMax($val, [ FE_MIN => "2010-01-01", FE_MAX => "2010-12-31" ]), $msg);
$this->assertEquals('', Sanitize::checkMinMax($val, [ FE_MIN => "2010-01-01", FE_MAX => "2010-04-30" ]), $msg);
$this->assertEquals('', Sanitize::checkMinMax($val, [ FE_MIN => "2010-01-01", FE_MAX => "2009-12-31" ]), $msg);
$this->assertEquals('', Sanitize::checkMinMax($val, [ FE_MIN => "2011-01-01", FE_MAX => "2009-12-31" ]), $msg);
$this->assertEquals($val, Sanitize::checkMinMax($val, [ FE_MIN => "2010-05-01", FE_MAX => "2010-05-01" ]), $msg);
$this->assertEquals($val, Sanitize::checkMinMax($val, "2010-01-01", "2010-12-31"), $msg);
$this->assertEquals('', Sanitize::checkMinMax($val, "2010-01-01", "2010-04-30"), $msg);
$this->assertEquals('', Sanitize::checkMinMax($val, "2010-01-01", "2009-12-31"), $msg);
$this->assertEquals('', Sanitize::checkMinMax($val, "2011-01-01", "2009-12-31"), $msg);
$this->assertEquals($val, Sanitize::checkMinMax($val, "2010-05-01", "2010-05-01"), $msg);
}
/**
......@@ -212,7 +231,7 @@ class SanitizeTest extends \PHPUnit_Framework_TestCase {
* @expectedException \qfq\UserFormException
*/
public function testSanitizeExceptionCheckFailed() {
Sanitize::sanitize('string', SANITIZE_ALLOW_DIGIT, '', SANITIZE_EXCEPTION);
Sanitize::sanitize('string', SANITIZE_ALLOW_DIGIT, '', '', SANITIZE_EXCEPTION);
}
/**
......
Markdown is supported
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