diff --git a/src/Object/Field.php b/src/Object/Field.php index 8ea5d7d..431c5eb 100644 --- a/src/Object/Field.php +++ b/src/Object/Field.php @@ -193,17 +193,17 @@ public function validate($value) $value, $matches ); - if (count($matches) !== 3) { + if (count($matches) !== 4) { $validationError->addError($this, $rule, $value); } else { - $strlen = strlen($matches[2]); + $strlen = strlen($matches[3]); if ($strlen !== 4) { $validationError->addError($this, $rule, $value); } else { - if ($matches[2] < 1 || $matches[2] > 4000) { + if ($matches[3] < 1 || $matches[3] > 4000) { $validationError->addError($this, $rule, $value); } else { - if (!checkdate($matches[0], $matches[1], $matches[2])) { + if (!checkdate($matches[1], $matches[2], $matches[3])) { $validationError->addError($this, $rule, $value); } } diff --git a/src/Object/Record.php b/src/Object/Record.php index eee423f..8dce1f4 100644 --- a/src/Object/Record.php +++ b/src/Object/Record.php @@ -286,7 +286,7 @@ public function setField($field, $value, $repetition = 0) $fieldFormat = $this->layout->getField($field)->result; if ($fieldFormat == 'date' || $fieldFormat == 'timestamp') { - $dateFormat = $this->fm->getProperty('dateFormat'); + $dateFormat = $this->fm->getProperty('dateFormat') === null ? 'm/d/Y' : $this->fm->getProperty('dateFormat'); try { if ($fieldFormat == 'date') { $convertedValue = DateFormat::convert($value, $dateFormat, 'm/d/Y'); diff --git a/tests/unit/src/Object/FieldTest.php b/tests/unit/src/Object/FieldTest.php index 624c795..a9f5fac 100644 --- a/tests/unit/src/Object/FieldTest.php +++ b/tests/unit/src/Object/FieldTest.php @@ -96,8 +96,8 @@ public function testGetRepetitionCount() public function testValidateNotEmpty() { //Valid values - $valideValues = ["0", "1", 0, 1, "toto", "06/21/2017"]; - foreach ($valideValues as $value) { + $validValues = ["0", "1", 0, 1, "toto", "06/21/2017"]; + foreach ($validValues as $value) { $this->record->setField('text_field', $value); $this->record->validate('text_field'); } @@ -120,8 +120,8 @@ public function testValidateNotEmpty() */ public function testValidateNumericOnly() { - $valideValues = ["0", "21", 0, 1]; - foreach ($valideValues as $value) { + $validValues = ["0", "21", 0, 1]; + foreach ($validValues as $value) { $this->record->setField('number_field', $value); $this->record->validate('number_field'); } @@ -147,6 +147,75 @@ public function testValidateNumericOnly() } } + /** + * @covers \airmoi\FileMaker\Object\Field::validate + */ + public function testValidateFourDigitDate() + { + //Test with date Field + //DateFormat auto convert 2 to 4 digits + $validValues = ["06/17/2017", "06/17/17"]; + foreach ($validValues as $value) { + $this->record->setField('dateFourDigit_field', $value); + $this->record->validate('dateFourDigit_field'); + } + + + /*$invalidValues = ["06/17/17"]; + foreach ($invalidValues as $value) { + $this->fm->dateFormat = 'm/d/y'; + $e = null; + try { + $this->record->setField('dateFourDigit_field', $value); + $this->record->validate('dateFourDigit_field'); + } catch (FileMakerValidationException $e) { + + } + $this->assertInstanceOf(FileMakerValidationException::class, $e); + }*/ + + //Test with Timestamp Field + //DateFormat auto convert 2 to 4 digits + $validValues = ["06/17/2017 00:00:00", "06/17/17 00:00:00"]; + foreach ($validValues as $value) { + $this->record->setField('timestamp_field', $value); + $this->record->validate('timestamp_field'); + } + + + /*$invalidValues = ["06/17/17 00:00:00"]; + foreach ($invalidValues as $value) { + $e = null; + try { + $this->record->setField('timestamp_field', $value); + $this->record->validate('timestamp_field'); + } catch (FileMakerValidationException $e) { + + } + $this->assertInstanceOf(FileMakerValidationException::class, $e); + }*/ + + //Test with text Fields + $validValues = ["06/17/2017"]; + foreach ($validValues as $value) { + $this->record->setField('textFourDigitDate_field', $value); + $this->record->validate('textFourDigitDate_field'); + } + + + $invalidValues = ["06/17/17"]; + foreach ($invalidValues as $value) { + $e = null; + try { + $this->record->setField('textFourDigitDate_field', $value); + $this->record->validate('textFourDigitDate_field'); + } catch (FileMakerValidationException $e) { + + } + $this->assertInstanceOf(FileMakerValidationException::class, $e); + } + } + /** * @covers \airmoi\FileMaker\Object\Field::getLocalValidationRules */ @@ -275,7 +344,7 @@ public function testCheckTimeStampFormatFourDigitYear() { $field = $this->record->layout->getField('timestamp_field'); //Valid values - $valideValues = [ + $validValues = [ "06/12/2004 00:00:00", "06-12/2004 00:00:00", "06-12\\2004 00:00:00", @@ -284,7 +353,7 @@ public function testCheckTimeStampFormatFourDigitYear() "06/12/2004 4:00 aM", "06/12/2004 4:00:00 Pm", ]; - foreach ($valideValues as $value) { + foreach ($validValues as $value) { $this->assertEquals(1, $field->checkTimeStampFormatFourDigitYear($value), 'value : ' . $value); } @@ -309,7 +378,7 @@ public function testCheckTimeStampFormat() { $field = $this->record->layout->getField('timestamp_field'); //Valid values - $valideValues = [ + $validValues = [ "06/12/04 00:00:00", "06-12-04 00:00:00", "06\\12\\04 00:00:00", @@ -318,7 +387,7 @@ public function testCheckTimeStampFormat() "06/12/4 4:00 aM", "06/12/2004 4:00:00 Pm", ]; - foreach ($valideValues as $value) { + foreach ($validValues as $value) { $this->assertEquals(1, $field->checkTimeStampFormat($value), 'value : ' . $value); } @@ -343,14 +412,14 @@ public function testCheckDateFormat() { $field = $this->record->layout->getField('timestamp_field'); //Valid values - $valideValues = [ + $validValues = [ "06/12/04", "06-12-04", "06\\12\\04", "06/12/004", "06/12/2004", ]; - foreach ($valideValues as $value) { + foreach ($validValues as $value) { $this->assertEquals(1, $field->checkDateFormat($value), 'value : ' . $value); } @@ -375,7 +444,7 @@ public function testCheckTimeFormat() { $field = $this->record->layout->getField('time_field'); //Valid values - $valideValues = [ + $validValues = [ "04:30", "04:01:53", "04:01:53 PM", @@ -385,7 +454,7 @@ public function testCheckTimeFormat() "02:30 AM", "02:30 aM", ]; - foreach ($valideValues as $value) { + foreach ($validValues as $value) { $this->assertEquals(1, $field->checkTimeFormat($value), 'value : ' . $value); } @@ -409,7 +478,7 @@ public function testCheckNumericOnly() { $field = $this->record->layout->getField('number_field'); //Valid values - $valideValues = [ + $validValues = [ 1, 205, 2E+003, @@ -420,7 +489,7 @@ public function testCheckNumericOnly() "0", 10^4, ]; - foreach ($valideValues as $value) { + foreach ($validValues as $value) { $this->assertFalse( $field->checkNumericOnly($value), 'value : ' . $value); } @@ -443,7 +512,7 @@ public function testCheckDateValidity() { $field = $this->record->layout->getField('timestamp_field'); //Valid values - $valideValues = [ + $validValues = [ "06/12/00", "06/12/04", "06-12-04", @@ -451,7 +520,7 @@ public function testCheckDateValidity() "01/27/004", "12/30/2017", ]; - foreach ($valideValues as $value) { + foreach ($validValues as $value) { $exception = new FileMakerValidationException($this->fm); $field->checkDateValidity($value, FileMaker::RULE_DATE_FIELD, $exception); $this->assertEquals(0, $exception->numErrors(), 'value : ' . $value); @@ -477,7 +546,7 @@ public function testCheckTimeValidity() { $field = $this->record->layout->getField('time_field'); //Valid values - $valideValues = [ + $validValues = [ "04:30" => false, "04:01:53" => false, "04:01:53 PM" => true, @@ -487,7 +556,7 @@ public function testCheckTimeValidity() "02:30 AM" => true, "02:30 aM" => true, ]; - foreach ($valideValues as $value => $shortFormat) { + foreach ($validValues as $value => $shortFormat) { $exception = new FileMakerValidationException($this->fm); $field->checkTimeValidity($value, FileMaker::RULE_DATE_FIELD, $exception, $shortFormat); $this->assertEquals(0, $exception->numErrors(), 'value : ' . $value);