Skip to content

Commit

Permalink
Merge branch 'recaptcha_automated_testing' of github.com:vdroznik/Gen…
Browse files Browse the repository at this point in the history
…emuFormBundle into 2.1
  • Loading branch information
bamarni committed Jan 12, 2013
2 parents dddcff9 + 3a47825 commit 26cfb06
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 21 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ private function addRecaptcha(ArrayNodeDefinition $rootNode)
->end()
->end()
->variableNode('configs')->defaultValue(array())->end()
->scalarNode('code')->defaultNull()->end()
->end()
->end()
->end()
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/GenemuFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down
54 changes: 34 additions & 20 deletions Form/Core/Validator/ReCaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)) {
Expand Down
2 changes: 2 additions & 0 deletions Resources/config/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand All @@ -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">
Expand Down
11 changes: 11 additions & 0 deletions Resources/doc/recaptcha/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Tests/Form/Extension/TypeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down

0 comments on commit 26cfb06

Please sign in to comment.