Commit 4195ca48 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Merge branch '5064-date-validation' into 'master'

5064 date validation

See merge request !34
parents 84fd1a0c f9cbc848
......@@ -187,6 +187,7 @@ const ERROR_MAX_FILE_SIZE_TOO_BIG = 1082;
const ERROR_SMALLER_THAN_MIN = 1083;
const ERROR_LARGER_THAN_MAX = 1084;
const ERROR_INVALID_DECIMAL_FORMAT = 1085;
const ERROR_INVALID_DATE = 1086;
// Subrecord
const ERROR_SUBRECORD_MISSING_COLUMN_ID = 1100;
......
......@@ -302,7 +302,7 @@ class FillStoreForm {
}
/**
* Check $value as date/datime/time value and convert it to FORMAT_DATE_INTERNATIONAL.
* Check $value as date/datetime/time value and convert it to FORMAT_DATE_INTERNATIONAL.
*
* @param array $formElement - if not set, set $formElement[FE_DATE_FORMAT]
* @param string $value - date/datetime/time value in format FORMAT_DATE_INTERNATIONAL or FORMAT_DATE_GERMAN
......@@ -310,7 +310,7 @@ class FillStoreForm {
* @return string - checked datetime string
* @throws UserFormException
*/
private function doDateTime(array &$formElement, $value) {
public function doDateTime(array &$formElement, $value) {
$regexp = Support::dateTimeRegexp($formElement[FE_TYPE], $formElement[FE_DATE_FORMAT]);
......@@ -319,9 +319,17 @@ class FillStoreForm {
throw new UserFormException("DateTime format not recognized: $placeholder / $value ", ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
}
$showTime = $formElement[FE_TYPE] == 'date' ? '0' : '1';
$showTime = $formElement[FE_TYPE] == FE_TYPE_DATE ? '0' : '1';
$value = Support::convertDateTime($value, FORMAT_DATE_INTERNATIONAL, '1', $showTime, $formElement[FE_SHOW_SECONDS]);
if ($formElement[FE_TYPE] !== FE_TYPE_TIME) {
// Validate date (e.g. 2010-02-31)
$dateValue = explode(' ', $value)[0];
$dateParts = explode('-', $dateValue);
if (!checkdate($dateParts[1], $dateParts[2], $dateParts[0]))
throw new UserFormException("$dateValue is not a valid date.", ERROR_INVALID_DATE);
}
return $value;
}
}
\ No newline at end of file
......@@ -19,23 +19,44 @@ class FillStoreFormTest extends \PHPUnit_Framework_TestCase {
* @throws CodeException
* @throws UserFormException
*/
public function testFake() {
# Violates SANITIZE class: SANITIZE string is always an empty string.
# Access are cached: use new variables for every test.
public function testDoDateTime() {
$formElement = [ FE_TYPE => FE_TYPE_DATE,
FE_DATE_FORMAT => FORMAT_DATE_INTERNATIONAL,
FE_SHOW_SECONDS => 0 ];
$msg = 'doDateTime fails';
$val = '2010-03-31';
$this->assertEquals($val, FillStoreForm::doDateTime($formElement, $val), $msg);
$val = '2010-02-28';
$this->assertEquals($val, FillStoreForm::doDateTime($formElement, $val), $msg);
$val = '2012-02-29';
$this->assertEquals($val, FillStoreForm::doDateTime($formElement, $val), $msg);
$formElement[FE_DATE_FORMAT] = FORMAT_DATE_GERMAN;
$this->assertEquals($val, FillStoreForm::doDateTime($formElement, '29.02.2012'), $msg);
}
# Check ''
// $this->assertEquals('', Sanitize::sanitize('', SANITIZE_ALLOW_ALNUMX), "SANITIZE_ALNUMX fails");
$this->assertEquals('', '');
/**
* @expectedException \qfq\UserFormException
*/
public function testDoDateTimeInvalidDate() {
$formElement = [ FE_TYPE => FE_TYPE_DATE,
FE_DATE_FORMAT => FORMAT_DATE_INTERNATIONAL,
FE_SHOW_SECONDS => 0 ];
$val = '2010-02-29';
$this->assertEquals($val, FillStoreForm::doDateTime($formElement, $val), "doDateTime is passing when it shouldn't");
}
/**
* @expectedException \qfq\UserFormException
*/
public function testDoDateTimeInvalidDateWithTime() {
$formElement = [ FE_TYPE => FE_TYPE_DATE,
FE_DATE_FORMAT => FORMAT_DATE_INTERNATIONAL,
FE_SHOW_SECONDS => 0 ];
$val = '2010-02-31 23:25';
$this->assertEquals($val, FillStoreForm::doDateTime($formElement, $val), "doDateTime is passing when it shouldn't");
}
//
// /**
// * @expectedException \qfq\UserFormException
// */
// public function testSanitizeExceptionMinMaxMissingMin() {
// Sanitize::sanitize(56, SANITIZE_ALLOW_MIN_MAX, '|45');
// }
//
}
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