From cd9c0539e0fa687dea031bc84cc9d0dc2b69a2c6 Mon Sep 17 00:00:00 2001 From: Jake Gully Date: Wed, 2 Aug 2017 13:44:18 +0100 Subject: [PATCH 1/3] Handle preg_match returning array with 4 elements --- src/Object/Field.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Object/Field.php b/src/Object/Field.php index 912410e..1daef3b 100644 --- a/src/Object/Field.php +++ b/src/Object/Field.php @@ -187,17 +187,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); } } From 58becc5be83a01ec4caf3e160beac6e635e3679d Mon Sep 17 00:00:00 2001 From: Romain Dunand Date: Thu, 3 Aug 2017 12:03:59 +0200 Subject: [PATCH 2/3] UnitTest for 4 digit date format validation --- tests/unit/src/Object/FieldTest.php | 61 ++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/tests/unit/src/Object/FieldTest.php b/tests/unit/src/Object/FieldTest.php index 624c795..ff4155b 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,31 @@ public function testValidateNumericOnly() } } + /** + * @covers \airmoi\FileMaker\Object\Field::validate + */ + public function testValidateFourDigitDate() + { + $validValues = ["06/17/2017"]; + foreach ($validValues as $value) { + $this->record->setField('dateFourDigit_field', $value); + $this->record->validate('dateFourDigit_field'); + } + + + $invalidValues = ["06/17/17"]; + foreach ($invalidValues as $value) { + $e = null; + try { + $this->record->setField('dateFourDigit_field', $value); + $this->record->validate('dateFourDigit_field'); + } catch (FileMakerValidationException $e) { + + } + $this->assertInstanceOf(FileMakerValidationException::class, $e); + } + } + /** * @covers \airmoi\FileMaker\Object\Field::getLocalValidationRules */ @@ -275,7 +300,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 +309,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 +334,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 +343,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 +368,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 +400,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 +410,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 +434,7 @@ public function testCheckNumericOnly() { $field = $this->record->layout->getField('number_field'); //Valid values - $valideValues = [ + $validValues = [ 1, 205, 2E+003, @@ -420,7 +445,7 @@ public function testCheckNumericOnly() "0", 10^4, ]; - foreach ($valideValues as $value) { + foreach ($validValues as $value) { $this->assertFalse( $field->checkNumericOnly($value), 'value : ' . $value); } @@ -443,7 +468,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 +476,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 +502,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 +512,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); From d3ca039a9bd336ffed56046effe4f36900e583dc Mon Sep 17 00:00:00 2001 From: Romain Dunand Date: Thu, 3 Aug 2017 12:28:19 +0200 Subject: [PATCH 3/3] Fix setField Issue on timestamps when no dateFormat was set --- src/Object/Record.php | 2 +- tests/unit/src/Object/FieldTest.php | 48 +++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) 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 ff4155b..a9f5fac 100644 --- a/tests/unit/src/Object/FieldTest.php +++ b/tests/unit/src/Object/FieldTest.php @@ -152,21 +152,65 @@ public function testValidateNumericOnly() */ public function testValidateFourDigitDate() { - $validValues = ["06/17/2017"]; + //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"]; + /*$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); }