Skip to content

Commit

Permalink
Added strict checks to Validation::comparison().
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Jan 4, 2018
1 parent 95b48c6 commit 37bc151
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/Validation/Validation.php
Expand Up @@ -295,6 +295,18 @@ public static function comparison($check1, $operator, $check2)
return true;
}
break;
case 'sameas':
case '===':
if ($check1 === $check2) {
return true;
}
break;
case 'notsameas':
case '!==':
if ($check1 !== $check2) {
return true;
}
break;
default:
static::$errors[] = 'You must define the $operator parameter for Validation::comparison()';
}
Expand All @@ -314,7 +326,7 @@ public static function comparison($check1, $operator, $check2)
*/
public static function compareWith($check, $field, $context)
{
return self::compareFields($check, $field, 'equalto', $context);
return self::compareFields($check, $field, '===', $context);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Validation/Validator.php
Expand Up @@ -983,7 +983,7 @@ public function sameAs($field, $secondField, $message = null, $when = null)
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'sameAs', $extra + [
'rule' => ['compareFields', $secondField, 'equalto']
'rule' => ['compareFields', $secondField, '===']
]);
}

Expand All @@ -1004,7 +1004,7 @@ public function notSameAs($field, $secondField, $message = null, $when = null)
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'notSameAs', $extra + [
'rule' => ['compareFields', $secondField, 'notequal']
'rule' => ['compareFields', $secondField, '!==']
]);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -855,6 +855,10 @@ public function testComparison()
$this->assertTrue(Validation::comparison(7, '==', 7));
$this->assertTrue(Validation::comparison(7, 'not equal', 6));
$this->assertTrue(Validation::comparison(7, '!=', 6));
$this->assertTrue(Validation::comparison(7, 'same as', 7));
$this->assertTrue(Validation::comparison(7, '===', 7));
$this->assertTrue(Validation::comparison(7, 'not same as', '7'));
$this->assertTrue(Validation::comparison(7, '!==', '7'));
$this->assertFalse(Validation::comparison(6, 'is greater', 7));
$this->assertFalse(Validation::comparison(6, '>', 7));
$this->assertFalse(Validation::comparison(7, 'is less', 6));
Expand All @@ -869,6 +873,10 @@ public function testComparison()
$this->assertFalse(Validation::comparison(7, '==', 6));
$this->assertFalse(Validation::comparison(7, 'not equal', 7));
$this->assertFalse(Validation::comparison(7, '!=', 7));
$this->assertFalse(Validation::comparison(7, 'same as', '7'));
$this->assertFalse(Validation::comparison(7, '===', '7'));
$this->assertFalse(Validation::comparison(7, 'not same as', 7));
$this->assertFalse(Validation::comparison(7, '!==', 7));

$this->assertTrue(Validation::comparison('6.5', '!=', 6));
$this->assertTrue(Validation::comparison('6.5', '<', 7));
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Validation/ValidatorTest.php
Expand Up @@ -1425,7 +1425,7 @@ public function testNotEquals()
public function testSameAs()
{
$validator = new Validator();
$this->assertProxyMethod($validator, 'sameAs', 'other', ['other', 'equalto'], 'compareFields');
$this->assertProxyMethod($validator, 'sameAs', 'other', ['other', '==='], 'compareFields');
$this->assertNotEmpty($validator->errors(['username' => 'foo']));
}

Expand All @@ -1437,7 +1437,7 @@ public function testSameAs()
public function testNotSameAs()
{
$validator = new Validator();
$this->assertProxyMethod($validator, 'notSameAs', 'other', ['other', 'notequal'], 'compareFields');
$this->assertProxyMethod($validator, 'notSameAs', 'other', ['other', '!=='], 'compareFields');
$this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'foo']));
}

Expand Down

0 comments on commit 37bc151

Please sign in to comment.