Skip to content

Commit

Permalink
sync svn r24220 - ZF-10991: provide possibility for noscript ReCaptch…
Browse files Browse the repository at this point in the history
…a 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.
  • Loading branch information
sasezaki committed Nov 8, 2011
1 parent ed94797 commit 480d889
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
9 changes: 7 additions & 2 deletions library/Zend/Captcha/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,17 @@ public function isValid($value, $context = null)
/**
* Render captcha
*
* @param Zend_View_Interface $view
* @param Zend\View\Renderer $view
* @param mixed $element
* @return string
*/
public function render(\Zend\View\Renderer $view = null, $element = null)
{
return $this->getService()->getHTML();
$name = null;
if ($element instanceof \Zend\Form\Element) {
$name = $element->getBelongsTo();
}

return $this->getService()->getHTML($name);
}
}
13 changes: 10 additions & 3 deletions library/Zend/Service/ReCaptcha/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,11 @@ public function setPrivateKey($privateKey)
*
* 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) {
throw new Exception('Missing public key');
Expand Down Expand Up @@ -405,6 +406,12 @@ public function getHtml()
</script>
SCRIPT;
}
$challengeField = 'recaptcha_challenge_field';
$responseField = 'recaptcha_response_field';
if (!empty($name)) {
$challengeField = $name . '[' . $challengeField . ']';
$responseField = $name . '[' . $responseField . ']';
}

$return = $reCaptchaOptions;
$return .= <<<HTML
Expand All @@ -416,9 +423,9 @@ public function getHtml()
<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;
Expand Down
29 changes: 22 additions & 7 deletions tests/Zend/Captcha/ReCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,39 +106,54 @@ public function testSetAndGetPublicAndPrivateKeys()
$this->assertSame($privKey, $captcha->getService()->getPrivateKey());
}

/**@+
* Regression tests for ZF-7654
*/

/** @group ZF-7654 */
public function testConstructorShouldAllowSettingLangOptionOnServiceObject()
{
$options = array('lang'=>'fr');
$captcha = new Captcha\ReCaptcha($options);
$this->assertEquals('fr', $captcha->getService()->getOption('lang'));
}

/** @group ZF-7654 */
public function testConstructorShouldAllowSettingThemeOptionOnServiceObject()
{
$options = array('theme'=>'black');
$captcha = new Captcha\ReCaptcha($options);
$this->assertEquals('black', $captcha->getService()->getOption('theme'));
}

/** @group ZF-7654 */
public function testAllowsSettingLangOptionOnServiceObject()
{
$captcha = new Captcha\ReCaptcha;
$captcha->setOption('lang', 'fr');
$this->assertEquals('fr', $captcha->getService()->getOption('lang'));
}

/** @group ZF-7654 */
public function testAllowsSettingThemeOptionOnServiceObject()
{
$captcha = new Captcha\ReCaptcha;
$captcha->setOption('theme', 'black');
$this->assertEquals('black', $captcha->getService()->getOption('theme'));
}

/**@-
* End ZF-7654 tests
*/
/** @group ZF-10991 */
public function testRenderWillUseElementNameWhenRenderingNoScriptFields()
{
$captcha = new 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\PhpRenderer();
$html = $captcha->render($view, $element);
$this->assertContains('contact[recaptcha_challenge_field]', $html);
$this->assertContains('contact[recaptcha_response_field]', $html);
}

}
9 changes: 9 additions & 0 deletions tests/Zend/Service/ReCaptcha/ReCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ public function testGetHtml() {
$this->assertNotSame(false, strstr($html, 'src="' . ReCaptcha\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');

Expand Down

0 comments on commit 480d889

Please sign in to comment.