2
0
Prechádzať zdrojové kódy

Removed _pubkey and _privkey from Zend_Captcha_ReCaptcha and simply let Zend_Service_ReCaptcha store them instead. Also added tests. Resolves ZF-6845

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16099 44c647ce-9c0f-0410-b52a-842ac1e357ba
cogo 16 rokov pred
rodič
commit
5a1e371f97

+ 41 - 54
library/Zend/Captcha/ReCaptcha.php

@@ -27,9 +27,9 @@ require_once 'Zend/Service/ReCaptcha.php';
 
 /**
  * ReCaptcha adapter
- * 
+ *
  * Allows to insert captchas driven by ReCaptcha service
- * 
+ *
  * @see http://recaptcha.net/apidocs/captcha/
  *
  * @category   Zend
@@ -39,30 +39,16 @@ require_once 'Zend/Service/ReCaptcha.php';
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  * @version    $Id: $
  */
-class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base 
+class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
 {
-    /**
-     * Recaptcha public key
-     *
-     * @var string
-     */
-    protected $_pubkey;
-
-    /**
-     * Recaptcha private key
-     *
-     * @var string
-     */
-    protected $_privkey;
-    
     /**@+
-     * ReCaptcha Field names 
+     * ReCaptcha Field names
      * @var string
      */
     protected $_CHALLENGE = 'recaptcha_challenge_field';
     protected $_RESPONSE  = 'recaptcha_response_field';
     /**@-*/
-     
+
     /**
      * Recaptcha service object
      *
@@ -72,7 +58,7 @@ class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
 
     /**
      * Parameters defined by the service
-     * 
+     *
      * @var array
      */
     protected $_serviceParams = array();
@@ -95,64 +81,64 @@ class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
         self::ERR_CAPTCHA   => 'Failed to validate captcha',
         self::BAD_CAPTCHA   => 'Captcha value is wrong: %value%',
     );
-    
+
     /**
      * Retrieve ReCaptcha Private key
      *
      * @return string
      */
-    public function getPrivkey() 
+    public function getPrivkey()
     {
-        return $this->_privkey;
+        return $this->getService()->getPrivateKey();
     }
-    
+
     /**
      * Retrieve ReCaptcha Public key
      *
      * @return string
      */
-    public function getPubkey() 
+    public function getPubkey()
     {
-        return $this->_pubkey;
+        return $this->getService()->getPublicKey();
     }
-    
+
     /**
      * Set ReCaptcha Private key
      *
-     * @param string $_privkey
+     * @param string $privkey
      * @return Zend_Captcha_ReCaptcha
      */
-    public function setPrivkey($privkey) 
+    public function setPrivkey($privkey)
     {
-        $this->_privkey = $privkey;
+        $this->getService()->setPrivateKey($privkey);
         return $this;
     }
-    
+
     /**
      * Set ReCaptcha public key
      *
-     * @param string $_pubkey
+     * @param string $pubkey
      * @return Zend_Captcha_ReCaptcha
      */
-    public function setPubkey($pubkey) 
+    public function setPubkey($pubkey)
     {
-        $this->_pubkey = $pubkey;
+        $this->getService()->setPublicKey($pubkey);
         return $this;
     }
-    
+
     /**
      * Constructor
      *
-     * @param  array|Zend_Config $options 
+     * @param  array|Zend_Config $options
      * @return void
      */
     public function __construct($options = null)
     {
-        parent::__construct($options);
-
-        $this->setService(new Zend_Service_ReCaptcha($this->getPubKey(), $this->getPrivKey()));
+        $this->setService(new Zend_Service_ReCaptcha());
         $this->_serviceParams = $this->getService()->getParams();
 
+        parent::__construct($options);
+
         if ($options instanceof Zend_Config) {
             $options = $options->toArray();
         }
@@ -163,8 +149,8 @@ class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
 
     /**
      * Set service object
-     * 
-     * @param  Zend_Service_ReCaptcha $service 
+     *
+     * @param  Zend_Service_ReCaptcha $service
      * @return Zend_Captcha_ReCaptcha
      */
     public function setService(Zend_Service_ReCaptcha $service)
@@ -175,7 +161,7 @@ class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
 
     /**
      * Retrieve ReCaptcha service object
-     * 
+     *
      * @return Zend_Service_ReCaptcha
      */
     public function getService()
@@ -187,9 +173,9 @@ class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
      * Set option
      *
      * If option is a service parameter, proxies to the service.
-     * 
-     * @param  string $key 
-     * @param  mixed $value 
+     *
+     * @param  string $key
+     * @param  mixed $value
      * @return Zend_Captcha_ReCaptcha
      */
     public function setOption($key, $value)
@@ -201,7 +187,7 @@ class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
         }
         return parent::setOption($key, $value);
     }
-    
+
     /**
      * Generate captcha
      *
@@ -226,6 +212,7 @@ class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
             $this->_error(self::MISSING_VALUE);
             return false;
         }
+
         if (!is_array($value) && is_array($context)) {
             $value = $context;
         }
@@ -236,14 +223,14 @@ class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
         }
 
         $service = $this->getService();
-        
-        $res = $service->verify($value[$this->_CHALLENGE], $value[$this->_RESPONSE]); 
-        
+
+        $res = $service->verify($value[$this->_CHALLENGE], $value[$this->_RESPONSE]);
+
         if (!$res) {
             $this->_error(self::ERR_CAPTCHA);
             return false;
         }
-        
+
         if (!$res->isValid()) {
             $this->_error(self::BAD_CAPTCHA, $res->getErrorCode());
             $service->setParam('error', $res->getErrorCode());
@@ -252,12 +239,12 @@ class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
 
         return true;
     }
-    
+
     /**
      * Render captcha
-     * 
-     * @param  Zend_View_Interface $view 
-     * @param  mixed $element 
+     *
+     * @param  Zend_View_Interface $view
+     * @param  mixed $element
      * @return string
      */
     public function render(Zend_View_Interface $view, $element = null)

+ 17 - 2
tests/Zend/Captcha/ReCaptchaTest.php

@@ -63,10 +63,10 @@ class Zend_Captcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
         }
 
         $this->element = new Zend_Form_Element_Captcha(
-            'captchaR', 
+            'captchaR',
             array(
                 'captcha' => array(
-                    'ReCaptcha', 
+                    'ReCaptcha',
                     'sessionClass' => 'Zend_Captcha_ReCaptchaTest_SessionContainer'
                 )
             )
@@ -114,6 +114,21 @@ class Zend_Captcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
         $captcha->setService($try);
         $this->assertSame($captcha->getService(), $try);
     }
+
+    public function testSetAndGetPublicAndPrivateKeys()
+    {
+        $captcha = new Zend_Captcha_ReCaptcha();
+        $pubKey = 'pubKey';
+        $privKey = 'privKey';
+        $captcha->setPubkey($pubKey)
+                ->setPrivkey($privKey);
+
+        $this->assertSame($pubKey, $captcha->getPubkey());
+        $this->assertSame($privKey, $captcha->getPrivkey());
+
+        $this->assertSame($pubKey, $captcha->getService()->getPublicKey());
+        $this->assertSame($privKey, $captcha->getService()->getPrivateKey());
+    }
 }
 
 class Zend_Captcha_ReCaptchaTest_SessionContainer