Permalink
Browse files

[ReCaptcha] added a way for automated testing (issue #215)

  • Loading branch information...
1 parent 4ad71b4 commit b48e37e0edf7174fcd70539143ac0201ca9bcde2 @vdroznik vdroznik committed Jan 3, 2013
@@ -148,6 +148,7 @@ private function addRecaptcha(ArrayNodeDefinition $rootNode)
->end()
->end()
->variableNode('configs')->defaultValue(array())->end()
+ ->scalarNode('code')->defaultNull()->end()
->end()
->end()
->end()
@@ -125,6 +125,7 @@ private function registerRecaptchaConfiguration(array $configs, ContainerBuilder
$container->setParameter('genemu.form.recaptcha.server_url', $serverUrl);
$container->setParameter('genemu.form.recaptcha.private_key', $configs['private_key']);
$container->setParameter('genemu.form.recaptcha.public_key', $configs['public_key']);
+ $container->setParameter('genemu.form.recaptcha.code', $configs['code']);
$container->setParameter('genemu.form.recaptcha.options', $configs['configs']);
}
@@ -29,30 +29,37 @@ class ReCaptchaValidator implements EventSubscriberInterface
private $httpRequest;
private $request;
private $privateKey;
+ private $code;
/**
* Constructs
*
* @param Request $request
* @param string $privateKey
*/
- public function __construct(Request $request, $privateKey)
+ public function __construct(Request $request, $privateKey, $code)
{
- if (empty($privateKey)) {
- throw new FormException('The child node "private_key" at path "genenu_form.captcha" must be configured.');
- }
-
+ // predefined code to validate against (for testing)
+ $this->code = $code;
$this->request = $request;
- $this->privateKey = $privateKey;
-
- $this->httpRequest = array(
- 'POST %s HTTP/1.0',
- 'Host: %s',
- 'Content-Type: application/x-www-form-urlencoded',
- 'Content-Length: %d',
- 'User-Agent: reCAPTCHA/PHP'
- );
- $this->httpRequest = implode("\r\n", $this->httpRequest)."\r\n\r\n%s";
+
+ if(empty($code)) {
+ if (empty($privateKey)) {
+ throw new FormException('The child node "private_key" at path "genenu_form.captcha" must be configured.');
+ }
+
+ $this->request = $request;
+ $this->privateKey = $privateKey;
+
+ $this->httpRequest = array(
+ 'POST %s HTTP/1.0',
+ 'Host: %s',
+ 'Content-Type: application/x-www-form-urlencoded',
+ 'Content-Length: %d',
+ 'User-Agent: reCAPTCHA/PHP'
+ );
+ $this->httpRequest = implode("\r\n", $this->httpRequest)."\r\n\r\n%s";
+ }
}
public function validate(DataEvent $event)
@@ -70,12 +77,19 @@ public function validate(DataEvent $event)
'remoteip' => $server->get('REMOTE_ADDR')
);
- if (empty($datas['challenge']) || empty($datas['response'])) {
- $error = 'The captcha is not valid.';
- }
+ if(empty($this->code)) {
+ if (empty($datas['challenge']) || empty($datas['response'])) {
+ $error = 'The captcha is not valid.';
+ }
- if (true !== ($answer = $this->check($datas, $form->getAttribute('option_validator')))) {
- $error = sprintf('Unable to check the captcha from the server. (%s)', $answer);
+ if (true !== ($answer = $this->check($datas, $form->getAttribute('option_validator')))) {
+ $error = sprintf('Unable to check the captcha from the server. (%s)', $answer);
+ }
+ }
+ else {
+ if($this->code != $datas['response']) {
+ $error = "The captcha is not valid.";
+ }
}
if (!empty($error)) {
@@ -8,6 +8,7 @@
<parameter key="genemu.form.recaptcha.public_key" />
<parameter key="genemu.form.recaptcha.private_key" />
<parameter key="genemu.form.recaptcha.server_url" />
+ <parameter key="genemu.form.recaptcha.code" />
<parameter key="genemu.form.recaptcha.options" type="collection" />
<parameter key="genemu.form.captcha.options" type="collection" />
@@ -18,6 +19,7 @@
<service id="genemu.form.type_recaptcha.validator" class="Genemu\Bundle\FormBundle\Form\Core\Validator\ReCaptchaValidator">
<argument type="service" id="request" strict="false" />
<argument>%genemu.form.recaptcha.private_key%</argument>
+ <argument>%genemu.form.recaptcha.code%</argument>
</service>
<service id="genemu.form.core.type.recaptcha" class="Genemu\Bundle\FormBundle\Form\Core\Type\ReCaptchaType">
@@ -75,7 +75,7 @@ protected function loadTypes()
'folder' => '/upload'
), __DIR__.'/../../Fixtures'),
new Form\Core\Type\ReCaptchaType(
- new ReCaptchaValidator($this->request, 'privateKey'),
+ new ReCaptchaValidator($this->request, 'privateKey', '1234'),
'publicKey',
'http://api.recaptcha.net',
array()),

0 comments on commit b48e37e

Please sign in to comment.