Permalink
Browse files

added some tests to Recaptcha static code

  • Loading branch information...
bamarni committed Jan 12, 2013
1 parent 26cfb06 commit 5afbd797b4a3986bf353844030e88fcd348840e0
@@ -32,18 +32,16 @@ class ReCaptchaValidator implements EventSubscriberInterface
private $code;
/**
- * Constructs
- *
- * @param Request $request
- * @param string $privateKey
+ * @param Request $request
+ * @param string $privateKey
+ * @param null|string $code Predefined code to validate against (for testing)
*/
- public function __construct(Request $request, $privateKey, $code)
+ public function __construct(Request $request, $privateKey, $code = null)
{
- // predefined code to validate against (for testing)
$this->code = $code;
$this->request = $request;
- if(empty($code)) {
+ if (empty($code)) {
if (empty($privateKey)) {
throw new FormException('The child node "private_key" at path "genenu_form.captcha" must be configured.');
}
@@ -77,19 +75,14 @@ public function validate(DataEvent $event)
'remoteip' => $server->get('REMOTE_ADDR')
);
- if(empty($this->code)) {
+ 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')))) {
+ } elseif (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.";
- }
+ } elseif ($this->code != $datas['response']) {
+ $error = "The captcha is not valid.";
}
if (!empty($error)) {
@@ -12,6 +12,9 @@
namespace Genemu\Bundle\FormBundle\Tests\Form\Core\Type;
use Genemu\Bundle\FormBundle\Tests\Form\Type\TypeTestCase;
+use Genemu\Bundle\FormBundle\Form\Core\Type\ReCaptchaType;
+use Genemu\Bundle\FormBundle\Form\Core\Validator\ReCaptchaValidator;
+use Symfony\Component\HttpFoundation\Request;
/**
* @author Olivier Chauvel <olivier@generation-multiple.com>
@@ -55,4 +58,30 @@ public function testConfigs()
'timeout' => 30
), $form->getAttribute('option_validator'));
}
+
+ /**
+ * @dataProvider provideCodes
+ */
+ public function testCode($code, $isValid)
+ {
+ $request = new Request(array(), array('recaptcha_response_field' => $code));
+ $form = $this->factory->create(new ReCaptchaType(
+ new ReCaptchaValidator($request, 'privateKey', '1234'),
+ 'publicKey',
+ 'http://api.recaptcha.net',
+ array()
+ ));
+
+ $form->bind(null);
+
+ $this->assertEquals($isValid, $form->isValid());
+ }
+
+ public function provideCodes()
+ {
+ return array(
+ array('1234', true),
+ array('4321', false),
+ );
+ }
}
@@ -75,7 +75,7 @@ protected function loadTypes()
'folder' => '/upload'
), __DIR__.'/../../Fixtures'),
new Form\Core\Type\ReCaptchaType(
- new ReCaptchaValidator($this->request, 'privateKey', '1234'),
+ new ReCaptchaValidator($this->request, 'privateKey'),
'publicKey',
'http://api.recaptcha.net',
array()),

0 comments on commit 5afbd79

Please sign in to comment.