Skip to content

Commit

Permalink
Merge branch 'release/2.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
airmoi committed Aug 3, 2017
2 parents 6b43d22 + d3ca039 commit 60d40eb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/Object/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Object/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
105 changes: 87 additions & 18 deletions tests/unit/src/Object/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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');
}
Expand All @@ -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
*/
Expand Down Expand Up @@ -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",
Expand All @@ -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);
}

Expand All @@ -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",
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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",
Expand All @@ -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);
}

Expand All @@ -409,7 +478,7 @@ public function testCheckNumericOnly()
{
$field = $this->record->layout->getField('number_field');
//Valid values
$valideValues = [
$validValues = [
1,
205,
2E+003,
Expand All @@ -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);
}

Expand All @@ -443,15 +512,15 @@ public function testCheckDateValidity()
{
$field = $this->record->layout->getField('timestamp_field');
//Valid values
$valideValues = [
$validValues = [
"06/12/00",
"06/12/04",
"06-12-04",
"06\\12\\04",
"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);
Expand All @@ -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,
Expand All @@ -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);
Expand Down

0 comments on commit 60d40eb

Please sign in to comment.