Browse Source

Merge pull request #8701 from cakephp/dereuromark-patch-1

notBlank rule should not false positive on numeric input
Mark Story 10 years ago
parent
commit
21012ccea0
2 changed files with 18 additions and 5 deletions
  1. 3 2
      src/Validation/Validation.php
  2. 15 3
      tests/TestCase/Validation/ValidationTest.php

+ 3 - 2
src/Validation/Validation.php

@@ -83,7 +83,7 @@ class Validation
      */
     public static function notBlank($check)
     {
-        if (empty($check) && $check !== '0') {
+        if (empty($check) && $check !== '0' && $check !== 0) {
             return false;
         }
         return static::_check($check, '/[^\s]+/m');
@@ -481,7 +481,8 @@ class Validation
         if (empty($methods[$type])) {
             throw new \InvalidArgumentException('Unsupported parser type given.');
         }
-        return (Time::{$methods[$type]}($check, $format) !== null);
+        $method = $methods[$type];
+        return (Time::$method($check, $format) !== null);
     }
 
     /**

+ 15 - 3
tests/TestCase/Validation/ValidationTest.php

@@ -31,6 +31,16 @@ class ValidationTest extends TestCase
 {
 
     /**
+     * @var string
+     */
+    public $locale;
+
+    /**
+     * @var string
+     */
+    protected $_appEncoding;
+
+    /**
      * setUp method
      *
      * @return void
@@ -60,7 +70,7 @@ class ValidationTest extends TestCase
      *
      * @return void
      */
-    public function testNotEmpty()
+    public function testNotBlank()
     {
         $this->assertTrue(Validation::notBlank('abcdefg'));
         $this->assertTrue(Validation::notBlank('fasdf '));
@@ -69,6 +79,8 @@ class ValidationTest extends TestCase
         $this->assertTrue(Validation::notBlank('José'));
         $this->assertTrue(Validation::notBlank('é'));
         $this->assertTrue(Validation::notBlank('π'));
+        $this->assertTrue(Validation::notBlank('0'));
+        $this->assertTrue(Validation::notBlank(0));
         $this->assertFalse(Validation::notBlank("\t "));
         $this->assertFalse(Validation::notBlank(""));
     }
@@ -78,7 +90,7 @@ class ValidationTest extends TestCase
      *
      * @return void
      */
-    public function testNotEmptyISO88591AppEncoding()
+    public function testNotBlankIso88591AppEncoding()
     {
         Configure::write('App.encoding', 'ISO-8859-1');
         $this->assertTrue(Validation::notBlank('abcdefg'));
@@ -1862,7 +1874,7 @@ class ValidationTest extends TestCase
      */
     public function testEmailDeep()
     {
-        $this->skipIf(gethostbynamel('example.abcd'), 'Your DNS service responds for non-existant domains, skipping deep email checks.');
+        $this->skipIf((bool)gethostbynamel('example.abcd'), 'Your DNS service responds for non-existant domains, skipping deep email checks.');
 
         $this->assertTrue(Validation::email('abc.efg@cakephp.org', true));
         $this->assertFalse(Validation::email('abc.efg@caphpkeinvalid.com', true));