Browse Source

Merge pull request #11599 from robertpustulka/validation-compare-fields

Add more field value comparison validation rules
Mark Story 8 years ago
parent
commit
dc27733fe2

+ 94 - 16
src/Validation/Validation.php

@@ -33,12 +33,50 @@ class Validation
 
     /**
      * Default locale
-     *
-     * @var string
      */
     const DEFAULT_LOCALE = 'en_US';
 
     /**
+     * Same as operator.
+     */
+    const COMPARE_SAME = '===';
+
+    /**
+     * Not same as comparison operator.
+     */
+    const COMPARE_NOT_SAME = '!==';
+
+    /**
+     * Equal to comparison operator.
+     */
+    const COMPARE_EQUAL = '==';
+
+    /**
+     * Not equal to comparison operator.
+     */
+    const COMPARE_NOT_EQUAL = '!=';
+
+    /**
+     * Greater than comparison operator.
+     */
+    const COMPARE_GREATER = '>';
+
+    /**
+     * Greater than or equal to comparison operator.
+     */
+    const COMPARE_GREATER_OR_EQUAL = '>=';
+
+    /**
+     * Less than comparison operator.
+     */
+    const COMPARE_LESS = '<';
+
+    /**
+     * Less than or equal to comparison operator.
+     */
+    const COMPARE_LESS_OR_EQUAL = '<=';
+
+    /**
      * Some complex patterns needed in multiple places
      *
      * @var array
@@ -257,44 +295,86 @@ class Validation
             return false;
         }
 
+        $message = 'Operator `%s` is deprecated, use constant `Validation::%s` instead.';
+
         $operator = str_replace([' ', "\t", "\n", "\r", "\0", "\x0B"], '', strtolower($operator));
         switch ($operator) {
             case 'isgreater':
-            case '>':
+                /*
+                 * @deprecated 3.6.0 Use Validation::COMPARE_GREATER instead.
+                 */
+                deprecationWarning(sprintf($message, $operator, 'COMPARE_GREATER'));
+                // no break
+            case static::COMPARE_GREATER:
                 if ($check1 > $check2) {
                     return true;
                 }
                 break;
             case 'isless':
-            case '<':
+                /*
+                 * @deprecated 3.6.0 Use Validation::COMPARE_LESS instead.
+                 */
+                deprecationWarning(sprintf($message, $operator, 'COMPARE_LESS'));
+                // no break
+            case static::COMPARE_LESS:
                 if ($check1 < $check2) {
                     return true;
                 }
                 break;
             case 'greaterorequal':
-            case '>=':
+                /*
+                 * @deprecated 3.6.0 Use Validation::COMPARE_GREATER_OR_EQUAL instead.
+                 */
+                deprecationWarning(sprintf($message, $operator, 'COMPARE_GREATER_OR_EQUAL'));
+                // no break
+            case static::COMPARE_GREATER_OR_EQUAL:
                 if ($check1 >= $check2) {
                     return true;
                 }
                 break;
             case 'lessorequal':
-            case '<=':
+                /*
+                 * @deprecated 3.6.0 Use Validation::COMPARE_LESS_OR_EQUAL instead.
+                 */
+                deprecationWarning(sprintf($message, $operator, 'COMPARE_LESS_OR_EQUAL'));
+                // no break
+            case static::COMPARE_LESS_OR_EQUAL:
                 if ($check1 <= $check2) {
                     return true;
                 }
                 break;
             case 'equalto':
-            case '==':
+                /*
+                 * @deprecated 3.6.0 Use Validation::COMPARE_EQUAL instead.
+                 */
+                deprecationWarning(sprintf($message, $operator, 'COMPARE_EQUAL'));
+                // no break
+            case static::COMPARE_EQUAL:
                 if ($check1 == $check2) {
                     return true;
                 }
                 break;
             case 'notequal':
-            case '!=':
+                /*
+                 * @deprecated 3.6.0 Use Validation::COMPARE_NOT_EQUAL instead.
+                 */
+                deprecationWarning(sprintf($message, $operator, 'COMPARE_NOT_EQUAL'));
+                // no break
+            case static::COMPARE_NOT_EQUAL:
                 if ($check1 != $check2) {
                     return true;
                 }
                 break;
+            case static::COMPARE_SAME:
+                if ($check1 === $check2) {
+                    return true;
+                }
+                break;
+            case static::COMPARE_NOT_SAME:
+                if ($check1 !== $check2) {
+                    return true;
+                }
+                break;
             default:
                 static::$errors[] = 'You must define the $operator parameter for Validation::comparison()';
         }
@@ -314,7 +394,7 @@ class Validation
      */
     public static function compareWith($check, $field, $context)
     {
-        return self::compareFields($check, $field, true, $context);
+        return self::compareFields($check, $field, static::COMPARE_SAME, $context);
     }
 
     /**
@@ -324,20 +404,18 @@ class Validation
      *
      * @param mixed $check The value to find in $field.
      * @param string $field The field to check $check against. This field must be present in $context.
-     * @param bool $result The expected result of field comparison.
+     * @param string $operator Comparison operator.
      * @param array $context The validation context.
      * @return bool
      * @since 3.6.0
      */
-    public static function compareFields($check, $field, $result, $context)
+    public static function compareFields($check, $field, $operator, $context)
     {
         if (!isset($context['data'][$field])) {
             return false;
         }
 
-        $comparison = $context['data'][$field] === $check;
-
-        return $comparison === $result;
+        return static::comparison($check, $operator, $context['data'][$field]);
     }
 
     /**
@@ -1216,10 +1294,10 @@ class Validation
         if ($options['optional'] && $error === UPLOAD_ERR_NO_FILE) {
             return true;
         }
-        if (isset($options['minSize']) && !static::fileSize($file, '>=', $options['minSize'])) {
+        if (isset($options['minSize']) && !static::fileSize($file, static::COMPARE_GREATER_OR_EQUAL, $options['minSize'])) {
             return false;
         }
-        if (isset($options['maxSize']) && !static::fileSize($file, '<=', $options['maxSize'])) {
+        if (isset($options['maxSize']) && !static::fileSize($file, static::COMPARE_LESS_OR_EQUAL, $options['maxSize'])) {
             return false;
         }
         if (isset($options['types']) && !static::mimeType($file, $options['types'])) {

+ 140 - 14
src/Validation/Validator.php

@@ -861,7 +861,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         $extra = array_filter(['on' => $when, 'message' => $message]);
 
         return $this->add($field, 'greaterThan', $extra + [
-            'rule' => ['comparison', '>', $value]
+            'rule' => ['comparison', Validation::COMPARE_GREATER, $value]
         ]);
     }
 
@@ -881,7 +881,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         $extra = array_filter(['on' => $when, 'message' => $message]);
 
         return $this->add($field, 'greaterThanOrEqual', $extra + [
-            'rule' => ['comparison', '>=', $value]
+            'rule' => ['comparison', Validation::COMPARE_GREATER_OR_EQUAL, $value]
         ]);
     }
 
@@ -901,7 +901,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         $extra = array_filter(['on' => $when, 'message' => $message]);
 
         return $this->add($field, 'lessThan', $extra + [
-            'rule' => ['comparison', '<', $value]
+            'rule' => ['comparison', Validation::COMPARE_LESS, $value]
         ]);
     }
 
@@ -921,7 +921,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         $extra = array_filter(['on' => $when, 'message' => $message]);
 
         return $this->add($field, 'lessThanOrEqual', $extra + [
-            'rule' => ['comparison', '<=', $value]
+            'rule' => ['comparison', Validation::COMPARE_LESS_OR_EQUAL, $value]
         ]);
     }
 
@@ -941,7 +941,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         $extra = array_filter(['on' => $when, 'message' => $message]);
 
         return $this->add($field, 'equals', $extra + [
-            'rule' => ['comparison', '==', $value]
+            'rule' => ['comparison', Validation::COMPARE_EQUAL, $value]
         ]);
     }
 
@@ -961,7 +961,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         $extra = array_filter(['on' => $when, 'message' => $message]);
 
         return $this->add($field, 'notEquals', $extra + [
-            'rule' => ['comparison', '!=', $value]
+            'rule' => ['comparison', Validation::COMPARE_NOT_EQUAL, $value]
         ]);
     }
 
@@ -970,8 +970,8 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
      *
      * If both fields have the exact same value the rule will pass.
      *
-     * @param mixed $field The field you want to apply the rule to.
-     * @param mixed $secondField The field you want to compare against.
+     * @param string $field The field you want to apply the rule to.
+     * @param string $secondField The field you want to compare against.
      * @param string|null $message The error message when the rule fails.
      * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
      *   true when the validation rule should be applied.
@@ -983,15 +983,15 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         $extra = array_filter(['on' => $when, 'message' => $message]);
 
         return $this->add($field, 'sameAs', $extra + [
-            'rule' => ['compareFields', $secondField, true]
+            'rule' => ['compareFields', $secondField, Validation::COMPARE_SAME]
         ]);
     }
 
     /**
      * Add a rule to compare that two fields have different values.
      *
-     * @param mixed $field The field you want to apply the rule to.
-     * @param mixed $secondField The field you want to compare against.
+     * @param string $field The field you want to apply the rule to.
+     * @param string $secondField The field you want to compare against.
      * @param string|null $message The error message when the rule fails.
      * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
      *   true when the validation rule should be applied.
@@ -1004,7 +1004,133 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         $extra = array_filter(['on' => $when, 'message' => $message]);
 
         return $this->add($field, 'notSameAs', $extra + [
-            'rule' => ['compareFields', $secondField, false]
+            'rule' => ['compareFields', $secondField, Validation::COMPARE_NOT_SAME]
+        ]);
+    }
+
+    /**
+     * Add a rule to compare one field is equal to another.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $secondField The field you want to compare against.
+     * @param string|null $message The error message when the rule fails.
+     * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
+     *   true when the validation rule should be applied.
+     * @see \Cake\Validation\Validation::compareFields()
+     * @return $this
+     * @since 3.6.0
+     */
+    public function equalToField($field, $secondField, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+
+        return $this->add($field, 'equalToField', $extra + [
+            'rule' => ['compareFields', $secondField, Validation::COMPARE_EQUAL]
+        ]);
+    }
+
+    /**
+     * Add a rule to compare one field is not equal to another.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $secondField The field you want to compare against.
+     * @param string|null $message The error message when the rule fails.
+     * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
+     *   true when the validation rule should be applied.
+     * @see \Cake\Validation\Validation::compareFields()
+     * @return $this
+     * @since 3.6.0
+     */
+    public function notEqualToField($field, $secondField, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+
+        return $this->add($field, 'notEqualToField', $extra + [
+            'rule' => ['compareFields', $secondField, Validation::COMPARE_NOT_EQUAL]
+        ]);
+    }
+
+    /**
+     * Add a rule to compare one field is greater than another.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $secondField The field you want to compare against.
+     * @param string|null $message The error message when the rule fails.
+     * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
+     *   true when the validation rule should be applied.
+     * @see \Cake\Validation\Validation::compareFields()
+     * @return $this
+     * @since 3.6.0
+     */
+    public function greaterThanField($field, $secondField, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+
+        return $this->add($field, 'greaterThanField', $extra + [
+            'rule' => ['compareFields', $secondField, Validation::COMPARE_GREATER]
+        ]);
+    }
+
+    /**
+     * Add a rule to compare one field is greater than or equal to another.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $secondField The field you want to compare against.
+     * @param string|null $message The error message when the rule fails.
+     * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
+     *   true when the validation rule should be applied.
+     * @see \Cake\Validation\Validation::compareFields()
+     * @return $this
+     * @since 3.6.0
+     */
+    public function greaterThanOrEqualToField($field, $secondField, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+
+        return $this->add($field, 'greaterThanOrEqualToField', $extra + [
+            'rule' => ['compareFields', $secondField, Validation::COMPARE_GREATER_OR_EQUAL]
+        ]);
+    }
+
+    /**
+     * Add a rule to compare one field is less than another.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $secondField The field you want to compare against.
+     * @param string|null $message The error message when the rule fails.
+     * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
+     *   true when the validation rule should be applied.
+     * @see \Cake\Validation\Validation::compareFields()
+     * @return $this
+     * @since 3.6.0
+     */
+    public function lessThanField($field, $secondField, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+
+        return $this->add($field, 'lessThanField', $extra + [
+            'rule' => ['compareFields', $secondField, Validation::COMPARE_LESS]
+        ]);
+    }
+
+    /**
+     * Add a rule to compare one field is less than or equal to another.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $secondField The field you want to compare against.
+     * @param string|null $message The error message when the rule fails.
+     * @param string|callable|null $when Either 'create' or 'update' or a callable that returns
+     *   true when the validation rule should be applied.
+     * @see \Cake\Validation\Validation::compareFields()
+     * @return $this
+     * @since 3.6.0
+     */
+    public function lessThanOrEqualToField($field, $secondField, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+
+        return $this->add($field, 'lessThanOrEqualToField', $extra + [
+            'rule' => ['compareFields', $secondField, Validation::COMPARE_LESS_OR_EQUAL]
         ]);
     }
 
@@ -1727,7 +1853,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
                     $value = $value['_ids'];
                 }
 
-                return Validation::numElements($value, '>=', $count);
+                return Validation::numElements($value, Validation::COMPARE_GREATER_OR_EQUAL, $count);
             }
         ]);
     }
@@ -1754,7 +1880,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
                     $value = $value['_ids'];
                 }
 
-                return Validation::numElements($value, '<=', $count);
+                return Validation::numElements($value, Validation::COMPARE_LESS_OR_EQUAL, $count);
             }
         ]);
     }

+ 100 - 94
tests/TestCase/Validation/ValidationTest.php

@@ -841,37 +841,43 @@ class ValidationTest extends TestCase
     public function testComparison()
     {
         $this->assertFalse(Validation::comparison(7, null, 6));
-        $this->assertTrue(Validation::comparison(7, 'is greater', 6));
-        $this->assertTrue(Validation::comparison(7, '>', 6));
-        $this->assertTrue(Validation::comparison(6, 'is less', 7));
-        $this->assertTrue(Validation::comparison(6, '<', 7));
-        $this->assertTrue(Validation::comparison(7, 'greater or equal', 7));
-        $this->assertTrue(Validation::comparison(7, '>=', 7));
-        $this->assertTrue(Validation::comparison(7, 'greater or equal', 6));
-        $this->assertTrue(Validation::comparison(7, '>=', 6));
-        $this->assertTrue(Validation::comparison(6, 'less or equal', 7));
-        $this->assertTrue(Validation::comparison(6, '<=', 7));
-        $this->assertTrue(Validation::comparison(7, 'equal to', 7));
-        $this->assertTrue(Validation::comparison(7, '==', 7));
-        $this->assertTrue(Validation::comparison(7, 'not equal', 6));
-        $this->assertTrue(Validation::comparison(7, '!=', 6));
-        $this->assertFalse(Validation::comparison(6, 'is greater', 7));
-        $this->assertFalse(Validation::comparison(6, '>', 7));
-        $this->assertFalse(Validation::comparison(7, 'is less', 6));
-        $this->assertFalse(Validation::comparison(7, '<', 6));
-        $this->assertFalse(Validation::comparison(6, 'greater or equal', 7));
-        $this->assertFalse(Validation::comparison(6, '>=', 7));
-        $this->assertFalse(Validation::comparison(6, 'greater or equal', 7));
-        $this->assertFalse(Validation::comparison(6, '>=', 7));
-        $this->assertFalse(Validation::comparison(7, 'less or equal', 6));
-        $this->assertFalse(Validation::comparison(7, '<=', 6));
-        $this->assertFalse(Validation::comparison(7, 'equal to', 6));
-        $this->assertFalse(Validation::comparison(7, '==', 6));
-        $this->assertFalse(Validation::comparison(7, 'not equal', 7));
-        $this->assertFalse(Validation::comparison(7, '!=', 7));
-
-        $this->assertTrue(Validation::comparison('6.5', '!=', 6));
-        $this->assertTrue(Validation::comparison('6.5', '<', 7));
+        $this->assertTrue(Validation::comparison(7, Validation::COMPARE_GREATER, 6));
+        $this->assertTrue(Validation::comparison(6, Validation::COMPARE_LESS, 7));
+        $this->assertTrue(Validation::comparison(7, Validation::COMPARE_GREATER_OR_EQUAL, 7));
+        $this->assertTrue(Validation::comparison(7, Validation::COMPARE_GREATER_OR_EQUAL, 6));
+        $this->assertTrue(Validation::comparison(6, Validation::COMPARE_LESS_OR_EQUAL, 7));
+        $this->assertTrue(Validation::comparison(7, Validation::COMPARE_EQUAL, 7));
+        $this->assertTrue(Validation::comparison(7, Validation::COMPARE_NOT_EQUAL, 6));
+        $this->assertTrue(Validation::comparison(7, Validation::COMPARE_SAME, 7));
+        $this->assertTrue(Validation::comparison(7, Validation::COMPARE_NOT_SAME, '7'));
+        $this->assertFalse(Validation::comparison(6, Validation::COMPARE_GREATER, 7));
+        $this->assertFalse(Validation::comparison(7, Validation::COMPARE_LESS, 6));
+        $this->assertFalse(Validation::comparison(6, Validation::COMPARE_GREATER_OR_EQUAL, 7));
+        $this->assertFalse(Validation::comparison(6, Validation::COMPARE_GREATER_OR_EQUAL, 7));
+        $this->assertFalse(Validation::comparison(7, Validation::COMPARE_LESS_OR_EQUAL, 6));
+        $this->assertFalse(Validation::comparison(7, Validation::COMPARE_EQUAL, 6));
+        $this->assertFalse(Validation::comparison(7, Validation::COMPARE_NOT_EQUAL, 7));
+        $this->assertFalse(Validation::comparison(7, Validation::COMPARE_SAME, '7'));
+        $this->assertFalse(Validation::comparison(7, Validation::COMPARE_NOT_SAME, 7));
+        $this->assertTrue(Validation::comparison('6.5', Validation::COMPARE_NOT_EQUAL, 6));
+        $this->assertTrue(Validation::comparison('6.5', Validation::COMPARE_LESS, 7));
+
+        $this->deprecated(function () {
+            $this->assertTrue(Validation::comparison(7, 'is greater', 6));
+            $this->assertTrue(Validation::comparison(6, 'is less', 7));
+            $this->assertTrue(Validation::comparison(7, 'greater or equal', 7));
+            $this->assertTrue(Validation::comparison(7, 'greater or equal', 6));
+            $this->assertTrue(Validation::comparison(6, 'less or equal', 7));
+            $this->assertTrue(Validation::comparison(7, 'equal to', 7));
+            $this->assertTrue(Validation::comparison(7, 'not equal', 6));
+            $this->assertFalse(Validation::comparison(6, 'is greater', 7));
+            $this->assertFalse(Validation::comparison(7, 'is less', 6));
+            $this->assertFalse(Validation::comparison(6, 'greater or equal', 7));
+            $this->assertFalse(Validation::comparison(6, 'greater or equal', 7));
+            $this->assertFalse(Validation::comparison(7, 'less or equal', 6));
+            $this->assertFalse(Validation::comparison(7, 'equal to', 6));
+            $this->assertFalse(Validation::comparison(7, 'not equal', 7));
+        });
     }
 
     /**
@@ -881,14 +887,14 @@ class ValidationTest extends TestCase
      */
     public function testComparisonTypeChecks()
     {
-        $this->assertFalse(Validation::comparison('\x028', '>=', 1), 'hexish encoding fails');
-        $this->assertFalse(Validation::comparison('0b010', '>=', 1), 'binary string data fails');
-        $this->assertFalse(Validation::comparison('0x01', '>=', 1), 'hex string data fails');
-        $this->assertFalse(Validation::comparison('0x1', '>=', 1), 'hex string data fails');
+        $this->assertFalse(Validation::comparison('\x028', Validation::COMPARE_GREATER_OR_EQUAL, 1), 'hexish encoding fails');
+        $this->assertFalse(Validation::comparison('0b010', Validation::COMPARE_GREATER_OR_EQUAL, 1), 'binary string data fails');
+        $this->assertFalse(Validation::comparison('0x01', Validation::COMPARE_GREATER_OR_EQUAL, 1), 'hex string data fails');
+        $this->assertFalse(Validation::comparison('0x1', Validation::COMPARE_GREATER_OR_EQUAL, 1), 'hex string data fails');
 
-        $this->assertFalse(Validation::comparison('\x028', '>=', 1.5), 'hexish encoding fails');
-        $this->assertFalse(Validation::comparison('0b010', '>=', 1.5), 'binary string data fails');
-        $this->assertFalse(Validation::comparison('0x02', '>=', 1.5), 'hex string data fails');
+        $this->assertFalse(Validation::comparison('\x028', Validation::COMPARE_GREATER_OR_EQUAL, 1.5), 'hexish encoding fails');
+        $this->assertFalse(Validation::comparison('0b010', Validation::COMPARE_GREATER_OR_EQUAL, 1.5), 'binary string data fails');
+        $this->assertFalse(Validation::comparison('0x02', Validation::COMPARE_GREATER_OR_EQUAL, 1.5), 'hex string data fails');
     }
 
     /**
@@ -2593,15 +2599,15 @@ class ValidationTest extends TestCase
     public function testFileSize()
     {
         $image = TEST_APP . 'webroot/img/cake.power.gif';
-        $this->assertTrue(Validation::fileSize($image, '<', 1024));
-        $this->assertTrue(Validation::fileSize(['tmp_name' => $image], 'isless', 1024));
-        $this->assertTrue(Validation::fileSize($image, '<', '1KB'));
-        $this->assertTrue(Validation::fileSize($image, '>=', 200));
-        $this->assertTrue(Validation::fileSize($image, '==', 201));
-        $this->assertTrue(Validation::fileSize($image, '==', '201B'));
+        $this->assertTrue(Validation::fileSize($image, Validation::COMPARE_LESS, 1024));
+        $this->assertTrue(Validation::fileSize(['tmp_name' => $image], Validation::COMPARE_LESS, 1024));
+        $this->assertTrue(Validation::fileSize($image, Validation::COMPARE_LESS, '1KB'));
+        $this->assertTrue(Validation::fileSize($image, Validation::COMPARE_GREATER_OR_EQUAL, 200));
+        $this->assertTrue(Validation::fileSize($image, Validation::COMPARE_EQUAL, 201));
+        $this->assertTrue(Validation::fileSize($image, Validation::COMPARE_EQUAL, '201B'));
 
-        $this->assertFalse(Validation::fileSize($image, 'isgreater', 1024));
-        $this->assertFalse(Validation::fileSize(['tmp_name' => $image], '>', '1KB'));
+        $this->assertFalse(Validation::fileSize($image, Validation::COMPARE_GREATER, 1024));
+        $this->assertFalse(Validation::fileSize(['tmp_name' => $image], Validation::COMPARE_GREATER, '1KB'));
     }
 
     /**
@@ -2614,10 +2620,10 @@ class ValidationTest extends TestCase
         $image = TEST_APP . 'webroot/img/cake.power.gif';
         $file = new UploadedFile($image, 1000, UPLOAD_ERR_OK, 'cake.power.gif', 'image/gif');
 
-        $this->assertTrue(Validation::fileSize($file, '==', 201));
-        $this->assertTrue(Validation::fileSize($file, '<', 1024));
-        $this->assertFalse(Validation::fileSize($file, '>', 202));
-        $this->assertFalse(Validation::fileSize($file, '>', 1000));
+        $this->assertTrue(Validation::fileSize($file, Validation::COMPARE_EQUAL, 201));
+        $this->assertTrue(Validation::fileSize($file, Validation::COMPARE_LESS, 1024));
+        $this->assertFalse(Validation::fileSize($file, Validation::COMPARE_GREATER, 202));
+        $this->assertFalse(Validation::fileSize($file, Validation::COMPARE_GREATER, 1000));
     }
 
     /**
@@ -2742,53 +2748,53 @@ class ValidationTest extends TestCase
     }
 
     /**
-     * Test the compareFields method with true result.
+     * Test the compareFields method with equal result.
      *
      * @return void
      */
-    public function testCompareFieldsTrue()
+    public function testCompareFieldsEqualTo()
     {
         $context = [
             'data' => [
                 'other' => 'a value'
             ]
         ];
-        $this->assertTrue(Validation::compareFields('a value', 'other', true, $context));
+        $this->assertTrue(Validation::compareFields('a value', 'other', Validation::COMPARE_EQUAL, $context));
 
         $context = [
             'data' => [
                 'other' => 'different'
             ]
         ];
-        $this->assertFalse(Validation::compareFields('a value', 'other', true, $context));
+        $this->assertFalse(Validation::compareFields('a value', 'other', Validation::COMPARE_EQUAL, $context));
 
         $context = [];
-        $this->assertFalse(Validation::compareFields('a value', 'other', true, $context));
+        $this->assertFalse(Validation::compareFields('a value', 'other', Validation::COMPARE_EQUAL, $context));
     }
 
     /**
-     * Test the compareFields method with false result.
+     * Test the compareFields method with not equal result.
      *
      * @return void
      */
-    public function testCompareFieldsFalse()
+    public function testCompareFieldsNotEqual()
     {
         $context = [
             'data' => [
                 'other' => 'different'
             ]
         ];
-        $this->assertTrue(Validation::compareFields('a value', 'other', false, $context));
+        $this->assertTrue(Validation::compareFields('a value', 'other', Validation::COMPARE_NOT_EQUAL, $context));
 
         $context = [
             'data' => [
                 'other' => 'a value'
             ]
         ];
-        $this->assertFalse(Validation::compareFields('a value', 'other', false, $context));
+        $this->assertFalse(Validation::compareFields('a value', 'other', Validation::COMPARE_NOT_EQUAL, $context));
 
         $context = [];
-        $this->assertFalse(Validation::compareFields('a value', 'other', false, $context));
+        $this->assertFalse(Validation::compareFields('a value', 'other', Validation::COMPARE_NOT_EQUAL, $context));
     }
 
     /**
@@ -3036,19 +3042,19 @@ class ValidationTest extends TestCase
     public function testNumElements()
     {
         $array = ['cake', 'php'];
-        $this->assertTrue(Validation::numElements($array, '==', 2));
-        $this->assertFalse(Validation::numElements($array, '>', 3));
-        $this->assertFalse(Validation::numElements($array, '<', 1));
+        $this->assertTrue(Validation::numElements($array, Validation::COMPARE_EQUAL, 2));
+        $this->assertFalse(Validation::numElements($array, Validation::COMPARE_GREATER, 3));
+        $this->assertFalse(Validation::numElements($array, Validation::COMPARE_LESS, 1));
 
         $callable = function () {
             return '';
         };
 
-        $this->assertFalse(Validation::numElements(null, '==', 0));
-        $this->assertFalse(Validation::numElements(new stdClass(), '==', 0));
-        $this->assertFalse(Validation::numElements($callable, '==', 0));
-        $this->assertFalse(Validation::numElements(false, '==', 0));
-        $this->assertFalse(Validation::numElements(true, '==', 0));
+        $this->assertFalse(Validation::numElements(null, Validation::COMPARE_EQUAL, 0));
+        $this->assertFalse(Validation::numElements(new stdClass(), Validation::COMPARE_EQUAL, 0));
+        $this->assertFalse(Validation::numElements($callable, Validation::COMPARE_EQUAL, 0));
+        $this->assertFalse(Validation::numElements(false, Validation::COMPARE_EQUAL, 0));
+        $this->assertFalse(Validation::numElements(true, Validation::COMPARE_EQUAL, 0));
     }
 
     /**
@@ -3075,38 +3081,38 @@ class ValidationTest extends TestCase
         ];
 
         $this->assertTrue(Validation::imageSize($upload, [
-            'width' => ['>', 100],
-            'height' => ['>', 100],
+            'width' => [Validation::COMPARE_GREATER, 100],
+            'height' => [Validation::COMPARE_GREATER, 100],
         ]));
 
         $this->assertFalse(Validation::imageSize($upload, [
-            'width' => ['>', 100],
-            'height' => ['<', 100],
+            'width' => [Validation::COMPARE_GREATER, 100],
+            'height' => [Validation::COMPARE_LESS, 100],
         ]));
 
         $this->assertFalse(Validation::imageSize($upload, [
-            'width' => ['==', 100],
-            'height' => ['==', 300],
+            'width' => [Validation::COMPARE_EQUAL, 100],
+            'height' => [Validation::COMPARE_EQUAL, 300],
         ]));
 
         $this->assertTrue(Validation::imageSize($upload, [
-            'width' => ['>=', 300],
-            'height' => ['>=', 300],
+            'width' => [Validation::COMPARE_GREATER_OR_EQUAL, 300],
+            'height' => [Validation::COMPARE_GREATER_OR_EQUAL, 300],
         ]));
 
         $this->assertTrue(Validation::imageSize($upload, [
-            'width' => ['<=', 300],
-            'height' => ['<=', 300],
+            'width' => [Validation::COMPARE_LESS_OR_EQUAL, 300],
+            'height' => [Validation::COMPARE_LESS_OR_EQUAL, 300],
         ]));
 
         $this->assertTrue(Validation::imageSize($upload, [
-            'width' => ['<=', 300],
-            'height' => ['>=', 300],
+            'width' => [Validation::COMPARE_LESS_OR_EQUAL, 300],
+            'height' => [Validation::COMPARE_GREATER_OR_EQUAL, 300],
         ]));
 
         $this->assertFalse(Validation::imageSize($upload, [
-            'width' => ['<=', 299],
-            'height' => ['>=', 300],
+            'width' => [Validation::COMPARE_LESS_OR_EQUAL, 299],
+            'height' => [Validation::COMPARE_GREATER_OR_EQUAL, 300],
         ]));
     }
 
@@ -3122,13 +3128,13 @@ class ValidationTest extends TestCase
             'tmp_name' => $image
         ];
 
-        $this->assertTrue(Validation::imageHeight($upload, '>', 100));
-        $this->assertTrue(Validation::imageHeight($upload, '<', 2000));
-        $this->assertTrue(Validation::imageHeight($upload, '==', 300));
+        $this->assertTrue(Validation::imageHeight($upload, Validation::COMPARE_GREATER, 100));
+        $this->assertTrue(Validation::imageHeight($upload, Validation::COMPARE_LESS, 2000));
+        $this->assertTrue(Validation::imageHeight($upload, Validation::COMPARE_EQUAL, 300));
 
-        $this->assertFalse(Validation::imageHeight($upload, '<', 100));
-        $this->assertFalse(Validation::imageHeight($upload, '>', 2000));
-        $this->assertFalse(Validation::imageHeight($upload, '==', 3000));
+        $this->assertFalse(Validation::imageHeight($upload, Validation::COMPARE_LESS, 100));
+        $this->assertFalse(Validation::imageHeight($upload, Validation::COMPARE_GREATER, 2000));
+        $this->assertFalse(Validation::imageHeight($upload, Validation::COMPARE_EQUAL, 3000));
     }
 
     /**
@@ -3143,13 +3149,13 @@ class ValidationTest extends TestCase
             'tmp_name' => $image
         ];
 
-        $this->assertTrue(Validation::imageWidth($upload, '>', 100));
-        $this->assertTrue(Validation::imageWidth($upload, '<', 2000));
-        $this->assertTrue(Validation::imageWidth($upload, '==', 300));
+        $this->assertTrue(Validation::imageWidth($upload, Validation::COMPARE_GREATER, 100));
+        $this->assertTrue(Validation::imageWidth($upload, Validation::COMPARE_LESS, 2000));
+        $this->assertTrue(Validation::imageWidth($upload, Validation::COMPARE_EQUAL, 300));
 
-        $this->assertFalse(Validation::imageWidth($upload, '<', 100));
-        $this->assertFalse(Validation::imageWidth($upload, '>', 2000));
-        $this->assertFalse(Validation::imageWidth($upload, '==', 3000));
+        $this->assertFalse(Validation::imageWidth($upload, Validation::COMPARE_LESS, 100));
+        $this->assertFalse(Validation::imageWidth($upload, Validation::COMPARE_GREATER, 2000));
+        $this->assertFalse(Validation::imageWidth($upload, Validation::COMPARE_EQUAL, 3000));
     }
 
     /**

+ 85 - 8
tests/TestCase/Validation/ValidatorTest.php

@@ -15,6 +15,7 @@
 namespace Cake\Test\TestCase\Validation;
 
 use Cake\TestSuite\TestCase;
+use Cake\Validation\Validation;
 use Cake\Validation\ValidationSet;
 use Cake\Validation\Validator;
 
@@ -1352,7 +1353,7 @@ class ValidatorTest extends TestCase
     public function testGreaterThan()
     {
         $validator = new Validator();
-        $this->assertProxyMethod($validator, 'greaterThan', 5, ['>', 5], 'comparison');
+        $this->assertProxyMethod($validator, 'greaterThan', 5, [Validation::COMPARE_GREATER, 5], 'comparison');
         $this->assertNotEmpty($validator->errors(['username' => 2]));
     }
 
@@ -1364,7 +1365,7 @@ class ValidatorTest extends TestCase
     public function testGreaterThanOrEqual()
     {
         $validator = new Validator();
-        $this->assertProxyMethod($validator, 'greaterThanOrEqual', 5, ['>=', 5], 'comparison');
+        $this->assertProxyMethod($validator, 'greaterThanOrEqual', 5, [Validation::COMPARE_GREATER_OR_EQUAL, 5], 'comparison');
         $this->assertNotEmpty($validator->errors(['username' => 2]));
     }
 
@@ -1376,7 +1377,7 @@ class ValidatorTest extends TestCase
     public function testLessThan()
     {
         $validator = new Validator();
-        $this->assertProxyMethod($validator, 'lessThan', 5, ['<', 5], 'comparison');
+        $this->assertProxyMethod($validator, 'lessThan', 5, [Validation::COMPARE_LESS, 5], 'comparison');
         $this->assertNotEmpty($validator->errors(['username' => 5]));
     }
 
@@ -1388,7 +1389,7 @@ class ValidatorTest extends TestCase
     public function testLessThanOrEqual()
     {
         $validator = new Validator();
-        $this->assertProxyMethod($validator, 'lessThanOrEqual', 5, ['<=', 5], 'comparison');
+        $this->assertProxyMethod($validator, 'lessThanOrEqual', 5, [Validation::COMPARE_LESS_OR_EQUAL, 5], 'comparison');
         $this->assertNotEmpty($validator->errors(['username' => 6]));
     }
 
@@ -1400,7 +1401,7 @@ class ValidatorTest extends TestCase
     public function testEquals()
     {
         $validator = new Validator();
-        $this->assertProxyMethod($validator, 'equals', 5, ['==', 5], 'comparison');
+        $this->assertProxyMethod($validator, 'equals', 5, [Validation::COMPARE_EQUAL, 5], 'comparison');
         $this->assertEmpty($validator->errors(['username' => 5]));
         $this->assertNotEmpty($validator->errors(['username' => 6]));
     }
@@ -1413,7 +1414,7 @@ class ValidatorTest extends TestCase
     public function testNotEquals()
     {
         $validator = new Validator();
-        $this->assertProxyMethod($validator, 'notEquals', 5, ['!=', 5], 'comparison');
+        $this->assertProxyMethod($validator, 'notEquals', 5, [Validation::COMPARE_NOT_EQUAL, 5], 'comparison');
         $this->assertNotEmpty($validator->errors(['username' => 5]));
     }
 
@@ -1425,8 +1426,9 @@ class ValidatorTest extends TestCase
     public function testSameAs()
     {
         $validator = new Validator();
-        $this->assertProxyMethod($validator, 'sameAs', 'other', ['other', true], 'compareFields');
+        $this->assertProxyMethod($validator, 'sameAs', 'other', ['other', Validation::COMPARE_SAME], 'compareFields');
         $this->assertNotEmpty($validator->errors(['username' => 'foo']));
+        $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => '1']));
     }
 
     /**
@@ -1437,11 +1439,86 @@ class ValidatorTest extends TestCase
     public function testNotSameAs()
     {
         $validator = new Validator();
-        $this->assertProxyMethod($validator, 'notSameAs', 'other', ['other', false], 'compareFields');
+        $this->assertProxyMethod($validator, 'notSameAs', 'other', ['other', Validation::COMPARE_NOT_SAME], 'compareFields');
         $this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'foo']));
     }
 
     /**
+     * Tests the equalToField proxy method
+     *
+     * @return void
+     */
+    public function testEqualToField()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'equalToField', 'other', ['other', Validation::COMPARE_EQUAL], 'compareFields');
+        $this->assertNotEmpty($validator->errors(['username' => 'foo']));
+        $this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'bar']));
+    }
+
+    /**
+     * Tests the notEqualToField proxy method
+     *
+     * @return void
+     */
+    public function testNotEqualToField()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'notEqualToField', 'other', ['other', Validation::COMPARE_NOT_EQUAL], 'compareFields');
+        $this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'foo']));
+    }
+
+    /**
+     * Tests the greaterThanField proxy method
+     *
+     * @return void
+     */
+    public function testGreaterThanField()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'greaterThanField', 'other', ['other', Validation::COMPARE_GREATER], 'compareFields');
+        $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 1]));
+        $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 2]));
+    }
+
+    /**
+     * Tests the greaterThanOrEqualToField proxy method
+     *
+     * @return void
+     */
+    public function testGreaterThanOrEqualToField()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'greaterThanOrEqualToField', 'other', ['other', Validation::COMPARE_GREATER_OR_EQUAL], 'compareFields');
+        $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 2]));
+    }
+
+    /**
+     * Tests the lessThanField proxy method
+     *
+     * @return void
+     */
+    public function testLessThanField()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'lessThanField', 'other', ['other', Validation::COMPARE_LESS], 'compareFields');
+        $this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 1]));
+        $this->assertNotEmpty($validator->errors(['username' => 2, 'other' => 1]));
+    }
+
+    /**
+     * Tests the lessThanOrEqualToField proxy method
+     *
+     * @return void
+     */
+    public function testLessThanOrEqualToField()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'lessThanOrEqualToField', 'other', ['other', Validation::COMPARE_LESS_OR_EQUAL], 'compareFields');
+        $this->assertNotEmpty($validator->errors(['username' => 2, 'other' => 1]));
+    }
+
+    /**
      * Tests the containsNonAlphaNumeric proxy method
      *
      * @return void