Browse Source

ZF-10991: provide possibility for noscript ReCaptcha to be namespaced

- Added workflow for allowing <noscript> version of ReCaptcha to live in
  array notation (e.g. contact[recaptcha_challenge_field]). Note: does
  not solve issue when JS is in play, as hardcoded field names are used.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24220 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 14 years ago
parent
commit
02c26ab62a

+ 8 - 1
library/Zend/Captcha/ReCaptcha.php

@@ -224,6 +224,9 @@ class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
             $this->_error(self::MISSING_VALUE);
             return false;
         }
+echo "Validating ReCapthca\n";
+echo "    Value: " . var_export($value, 1) . "\n";
+echo "    Context " . var_export($context, 1) . "\n";
 
         if (!is_array($value) && is_array($context)) {
             $value = $context;
@@ -261,6 +264,10 @@ class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
      */
     public function render(Zend_View_Interface $view = null, $element = null)
     {
-        return $this->getService()->getHTML();
+        $name = null;
+        if ($element instanceof Zend_Form_Element) {
+            $name = $element->getBelongsTo();
+        }
+        return $this->getService()->getHTML($name);
     }
 }

+ 10 - 3
library/Zend/Service/ReCaptcha.php

@@ -373,10 +373,11 @@ class Zend_Service_ReCaptcha extends Zend_Service_Abstract
      *
      * This method uses the public key to fetch a recaptcha form.
      *
+     * @param  null|string $name Base name for recaptcha form elements
      * @return string
      * @throws Zend_Service_ReCaptcha_Exception
      */
-    public function getHtml()
+    public function getHtml($name = null)
     {
         if ($this->_publicKey === null) {
             /** @see Zend_Service_ReCaptcha_Exception */
@@ -415,6 +416,12 @@ class Zend_Service_ReCaptcha extends Zend_Service_Abstract
 </script>
 SCRIPT;
         }
+        $challengeField = 'recaptcha_challenge_field';
+        $responseField  = 'recaptcha_response_field';
+        if (!empty($name)) {
+            $challengeField = $name . '[' . $challengeField . ']';
+            $responseField  = $name . '[' . $responseField . ']';
+        }
 
         $return = $reCaptchaOptions;
         $return .= <<<HTML
@@ -426,9 +433,9 @@ HTML;
 <noscript>
    <iframe src="{$host}/noscript?k={$this->_publicKey}{$errorPart}"
        height="300" width="500" frameborder="0"></iframe>{$htmlBreak}
-   <textarea name="recaptcha_challenge_field" rows="3" cols="40">
+   <textarea name="{$challengeField}" rows="3" cols="40">
    </textarea>
-   <input type="hidden" name="recaptcha_response_field"
+   <input type="hidden" name="{$responseField}"
        value="manual_challenge"{$htmlInputClosing}
 </noscript>
 HTML;

+ 21 - 7
tests/Zend/Captcha/ReCaptchaTest.php

@@ -129,10 +129,7 @@ class Zend_Captcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
         $this->assertSame($privKey, $captcha->getService()->getPrivateKey());
     }
 
-    /**
-     * Regression tests for ZF-7654
-     */
-
+    /** @group ZF-7654 */
     public function testConstructorShouldAllowSettingLangOptionOnServiceObject()
     {
         $options = array('lang'=>'fr');
@@ -140,6 +137,7 @@ class Zend_Captcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('fr', $captcha->getService()->getOption('lang'));
     }
 
+    /** @group ZF-7654 */
     public function testConstructorShouldAllowSettingThemeOptionOnServiceObject()
     {
         $options = array('theme'=>'black');
@@ -147,6 +145,7 @@ class Zend_Captcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('black', $captcha->getService()->getOption('theme'));
     }
 
+    /** @group ZF-7654 */
     public function testAllowsSettingLangOptionOnServiceObject()
     {
         $captcha = new Zend_Captcha_ReCaptcha;
@@ -154,6 +153,7 @@ class Zend_Captcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('fr', $captcha->getService()->getOption('lang'));
     }
 
+    /** @group ZF-7654 */
     public function testAllowsSettingThemeOptionOnServiceObject()
     {
         $captcha = new Zend_Captcha_ReCaptcha;
@@ -161,9 +161,23 @@ class Zend_Captcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('black', $captcha->getService()->getOption('theme'));
     }
 
-    /**
-     * End ZF-7654 tests
-    */
+    /** @group ZF-10991 */
+    public function testRenderWillUseElementNameWhenRenderingNoScriptFields()
+    {
+        $captcha = new Zend_Captcha_ReCaptcha;
+        $pubKey  = 'pubKey';
+        $privKey = 'privKey';
+        $captcha->setPubkey($pubKey)
+                ->setPrivkey($privKey);
+        $element = new Zend_Form_Element_Captcha('captcha', array(
+            'captcha'   => $captcha,
+            'belongsTo' => 'contact',
+        ));
+        $view = new Zend_View();
+        $html = $captcha->render($view, $element);
+        $this->assertContains('contact[recaptcha_challenge_field]', $html);
+        $this->assertContains('contact[recaptcha_response_field]', $html);
+    }
 }
 
 class Zend_Captcha_ReCaptchaTest_SessionContainer

+ 9 - 0
tests/Zend/Service/ReCaptcha/ReCaptchaTest.php

@@ -254,6 +254,15 @@ class Zend_Service_ReCaptcha_ReCaptchaTest extends PHPUnit_Framework_TestCase
         $this->assertNotSame(false, strstr($html, 'src="' . Zend_Service_ReCaptcha::API_SECURE_SERVER . '/challenge?k=' . $this->_publicKey . '&error=' . $errorMsg . '"'));
     }
 
+    /** @group ZF-10991 */
+    public function testHtmlGenerationWillUseSuppliedNameForNoScriptElements()
+    {
+        $this->_reCaptcha->setPublicKey($this->_publicKey);
+        $html = $this->_reCaptcha->getHtml('contact');
+        $this->assertContains('contact[recaptcha_challenge_field]', $html);
+        $this->assertContains('contact[recaptcha_response_field]', $html);
+    }
+
     public function testVerifyWithMissingPrivateKey() {
         $this->setExpectedException('Zend_Service_ReCaptcha_Exception');