2
0
Просмотр исходного кода

[ZF-9309] Zend_Validate:

- added type check for Sitemap

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21362 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 лет назад
Родитель
Сommit
1e4bbcaa84

+ 8 - 2
library/Zend/Validate/Sitemap/Changefreq.php

@@ -42,7 +42,8 @@ class Zend_Validate_Sitemap_Changefreq extends Zend_Validate_Abstract
      * Validation key for not valid
      *
      */
-    const NOT_VALID = 'invalidSitemapChangefreq';
+    const NOT_VALID = 'sitemapChangefreqNotValid';
+    const INVALID   = 'sitemapChangefreqInvalid';
 
     /**
      * Validation failure message template definitions
@@ -51,6 +52,7 @@ class Zend_Validate_Sitemap_Changefreq extends Zend_Validate_Abstract
      */
     protected $_messageTemplates = array(
         self::NOT_VALID => "'%value%' is no valid sitemap changefreq",
+        self::INVALID   => "Invalid type given, the value should be a string",
     );
 
     /**
@@ -73,8 +75,12 @@ class Zend_Validate_Sitemap_Changefreq extends Zend_Validate_Abstract
      */
     public function isValid($value)
     {
-        $this->_setValue($value);
+        if (!is_string($value)) {
+            $this->_error(self::INVALID);
+            return false;
+        }
 
+        $this->_setValue($value);
         if (!is_string($value)) {
             return false;
         }

+ 12 - 4
library/Zend/Validate/Sitemap/Lastmod.php

@@ -48,7 +48,8 @@ class Zend_Validate_Sitemap_Lastmod extends Zend_Validate_Abstract
      * Validation key for not valid
      *
      */
-    const NOT_VALID = 'invalidSitemapLastmod';
+    const NOT_VALID = 'sitemapLastmodNotValid';
+    const INVALID   = 'sitemapLastmodInvalid';
 
     /**
      * Validation failure message template definitions
@@ -57,6 +58,7 @@ class Zend_Validate_Sitemap_Lastmod extends Zend_Validate_Abstract
      */
     protected $_messageTemplates = array(
         self::NOT_VALID => "'%value%' is no valid sitemap lastmod",
+        self::INVALID   => "Invalid type given, the value should be a string",
     );
 
     /**
@@ -69,12 +71,18 @@ class Zend_Validate_Sitemap_Lastmod extends Zend_Validate_Abstract
      */
     public function isValid($value)
     {
-        $this->_setValue($value);
-
         if (!is_string($value)) {
+            $this->_error(self::INVALID);
+            return false;
+        }
+
+        $this->_setValue($value);
+        $result = @preg_match(self::LASTMOD_REGEX, $value);
+        if ($result != 1) {
+            $this->_error(self::NOT_VALID);
             return false;
         }
 
-        return @preg_match(self::LASTMOD_REGEX, $value) == 1;
+        return true;
     }
 }

+ 12 - 4
library/Zend/Validate/Sitemap/Loc.php

@@ -47,7 +47,8 @@ class Zend_Validate_Sitemap_Loc extends Zend_Validate_Abstract
      * Validation key for not valid
      *
      */
-    const NOT_VALID = 'invalidSitemapLoc';
+    const NOT_VALID = 'sitemapLocNotValid';
+    const INVALID   = 'sitemapLocInvalid';
 
     /**
      * Validation failure message template definitions
@@ -56,6 +57,7 @@ class Zend_Validate_Sitemap_Loc extends Zend_Validate_Abstract
      */
     protected $_messageTemplates = array(
         self::NOT_VALID => "'%value%' is no valid sitemap location",
+        self::INVALID   => "Invalid type given, the value should be a string",
     );
 
     /**
@@ -68,12 +70,18 @@ class Zend_Validate_Sitemap_Loc extends Zend_Validate_Abstract
      */
     public function isValid($value)
     {
-        $this->_setValue($value);
-
         if (!is_string($value)) {
+            $this->_error(self::INVALID);
+            return false;
+        }
+
+        $this->_setValue($value);
+        $result = Zend_Uri::check($value);
+        if ($result !== true) {
+            $this->_error(self::NOT_VALID);
             return false;
         }
 
-        return Zend_Uri::check($value);
+        return true;
     }
 }

+ 12 - 5
library/Zend/Validate/Sitemap/Priority.php

@@ -42,7 +42,8 @@ class Zend_Validate_Sitemap_Priority extends Zend_Validate_Abstract
      * Validation key for not valid
      *
      */
-    const NOT_VALID = 'invalidSitemapPriority';
+    const NOT_VALID = 'sitemapPriorityNotValid';
+    const INVALID   = 'sitemapPriorityInvalid';
 
     /**
      * Validation failure message template definitions
@@ -51,6 +52,7 @@ class Zend_Validate_Sitemap_Priority extends Zend_Validate_Abstract
      */
     protected $_messageTemplates = array(
         self::NOT_VALID => "'%value%' is no valid sitemap priority",
+        self::INVALID   => "Invalid type given, the value should be a integer, a float or a numeric string",
     );
 
     /**
@@ -63,13 +65,18 @@ class Zend_Validate_Sitemap_Priority extends Zend_Validate_Abstract
      */
     public function isValid($value)
     {
-        $this->_setValue($value);
-
         if (!is_numeric($value)) {
+            $this->_error(self::INVALID);
+            return false;
+        }
+
+        $this->_setValue($value);
+        $value = (float) $value;
+        if ($value < 0 || $value > 1) {
+            $this->_error(self::NOT_VALID);
             return false;
         }
 
-        $value = (float)$value;
-        return $value >= 0 && $value <= 1;
+        return true;
     }
 }

+ 4 - 0
tests/Zend/Validate/Sitemap/ChangefreqTest.php

@@ -88,6 +88,8 @@ class Zend_Validate_Sitemap_ChangefreqTest extends PHPUnit_Framework_TestCase
 
         foreach ($values as $value) {
             $this->assertSame(false, $this->_validator->isValid($value));
+            $messages = $this->_validator->getMessages();
+            $this->assertContains('is no valid', current($messages));
         }
     }
 
@@ -103,6 +105,8 @@ class Zend_Validate_Sitemap_ChangefreqTest extends PHPUnit_Framework_TestCase
 
         foreach ($values as $value) {
             $this->assertSame(false, $this->_validator->isValid($value));
+            $messages = $this->_validator->getMessages();
+            $this->assertContains('should be a string', current($messages));
         }
     }
 }

+ 4 - 0
tests/Zend/Validate/Sitemap/LastmodTest.php

@@ -100,6 +100,8 @@ class Zend_Validate_Sitemap_LastmodTest extends PHPUnit_Framework_TestCase
 
         foreach ($values as $value) {
             $this->assertSame(false, $this->_validator->isValid($value));
+            $messages = $this->_validator->getMessages();
+            $this->assertContains('is no valid', current($messages));
         }
     }
 
@@ -115,6 +117,8 @@ class Zend_Validate_Sitemap_LastmodTest extends PHPUnit_Framework_TestCase
 
         foreach ($values as $value) {
             $this->assertSame(false, $this->_validator->isValid($value));
+            $messages = $this->_validator->getMessages();
+            $this->assertContains('should be a string', current($messages));
         }
     }
 }

+ 20 - 3
tests/Zend/Validate/Sitemap/LocTest.php

@@ -89,15 +89,32 @@ class Zend_Validate_Sitemap_LocTest extends PHPUnit_Framework_TestCase
             'www.example.com',
             '/news/',
             '#',
-            new stdClass(),
-            42,
             'http:/example.com/',
-            null,
             'https://www.exmaple.com/?foo="bar\'&bar=<bat>'
         );
 
         foreach ($values as $value) {
             $this->assertSame(false, $this->_validator->isValid($value));
+            $messages = $this->_validator->getMessages();
+            $this->assertContains('is no valid', current($messages));
         }
     }
+
+    /**
+     * Tests values that are not strings
+     *
+     */
+    public function testNotStrings()
+    {
+        $values = array(
+            1, 1.4, null, new stdClass(), true, false
+        );
+
+        foreach ($values as $value) {
+            $this->assertSame(false, $this->_validator->isValid($value));
+            $messages = $this->_validator->getMessages();
+            $this->assertContains('should be a string', current($messages));
+        }
+    }
+
 }

+ 20 - 4
tests/Zend/Validate/Sitemap/PriorityTest.php

@@ -82,14 +82,30 @@ class Zend_Validate_Sitemap_PriorityTest extends PHPUnit_Framework_TestCase
     public function testInvalidPriorities()
     {
         $values = array(
-            'alwayz',  '_hourly', 'Daily', 'wEekly',
-            'mönthly ', ' yearly ', 'never ', 'rofl',
-            '0,0', '1.1', '02', '3', '01.4', '0.f',
-            1.1, -0.001, 1.0001
+            -1, -0.1, 1.1, 100, 10, 2, '3', '-4',
         );
 
         foreach ($values as $value) {
             $this->assertSame(false, $this->_validator->isValid($value));
+            $messages = $this->_validator->getMessages();
+            $this->assertContains('is no valid', current($messages));
+        }
+    }
+
+    /**
+     * Tests values that are no numbers
+     *
+     */
+    public function testNotNumbers()
+    {
+        $values = array(
+            null, new stdClass(), true, false, 'abcd',
+        );
+
+        foreach ($values as $value) {
+            $this->assertSame(false, $this->_validator->isValid($value));
+            $messages = $this->_validator->getMessages();
+            $this->assertContains('should be a integer', current($messages));
         }
     }
 }