Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Merged
merged 2 commits into from

2 participants

@vdroznik
# config_test.yml
genemu_form:
    recaptcha:
        code: 1234

this will make validator to wait 1234 as answer to captcha which is useful for automated testing

@bamarni
Collaborator

Thanks, could you add a small note in the docs about it? I've just done it for captcha (@dddcff9).

Apart a few CS issues it looks good, but nevermind I'll run a cs fixer tool on the bundle some day :+1:

@vdroznik

When do you think this PR could be merged?

@bamarni bamarni merged commit 3a47825 into genemu:2.1
@bamarni
Collaborator

merged, when you add a commit to a PR you should leave a message to ping people because github doesn't ;)

@vdroznik

Thanks, I didn't know about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 3, 2013
  1. @vdroznik
Commits on Jan 4, 2013
  1. @vdroznik

    added documentation

    vdroznik authored
This page is out of date. Refresh to see the latest.
View
1  DependencyInjection/Configuration.php
@@ -148,6 +148,7 @@ private function addRecaptcha(ArrayNodeDefinition $rootNode)
->end()
->end()
->variableNode('configs')->defaultValue(array())->end()
+ ->scalarNode('code')->defaultNull()->end()
->end()
->end()
->end()
View
1  DependencyInjection/GenemuFormExtension.php
@@ -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']);
}
View
54 Form/Core/Validator/ReCaptchaValidator.php
@@ -29,6 +29,7 @@ class ReCaptchaValidator implements EventSubscriberInterface
private $httpRequest;
private $request;
private $privateKey;
+ private $code;
/**
* Constructs
@@ -36,23 +37,29 @@ class ReCaptchaValidator implements EventSubscriberInterface
* @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)) {
View
2  Resources/config/form.xml
@@ -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">
View
11 Resources/doc/recaptcha/index.md
@@ -10,6 +10,17 @@ genemu_form:
private_key: `your private key is required`
```
+## Hardcoding the captcha value (for testing)
+
+You can define a static code for your test environment:
+
+``` yml
+# app/config/config_test.yml
+genemu_form:
+ recaptcha:
+ code: 1234
+```
+
## Default Usage:
``` php
View
2  Tests/Form/Extension/TypeExtensionTest.php
@@ -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()),
Something went wrong with that request. Please try again.