Skip to content
This repository
Browse code

added some tests to Recaptcha static code

  • Loading branch information...
commit 5afbd797b4a3986bf353844030e88fcd348840e0 1 parent 26cfb06
Bilal Amarni authored
25  Form/Core/Validator/ReCaptchaValidator.php
@@ -32,18 +32,16 @@ class ReCaptchaValidator implements EventSubscriberInterface
32 32
     private $code;
33 33
 
34 34
     /**
35  
-     * Constructs
36  
-     *
37  
-     * @param Request $request
38  
-     * @param string  $privateKey
  35
+     * @param Request      $request
  36
+     * @param string       $privateKey
  37
+     * @param null|string  $code       Predefined code to validate against (for testing)
39 38
      */
40  
-    public function __construct(Request $request, $privateKey, $code)
  39
+    public function __construct(Request $request, $privateKey, $code = null)
41 40
     {
42  
-        // predefined code to validate against (for testing)
43 41
         $this->code = $code;
44 42
         $this->request = $request;
45 43
 
46  
-        if(empty($code)) {
  44
+        if (empty($code)) {
47 45
             if (empty($privateKey)) {
48 46
                 throw new FormException('The child node "private_key" at path "genenu_form.captcha" must be configured.');
49 47
             }
@@ -77,19 +75,14 @@ public function validate(DataEvent $event)
77 75
             'remoteip' => $server->get('REMOTE_ADDR')
78 76
         );
79 77
 
80  
-        if(empty($this->code)) {
  78
+        if (empty($this->code)) {
81 79
             if (empty($datas['challenge']) || empty($datas['response'])) {
82 80
                 $error = 'The captcha is not valid.';
83  
-            }
84  
-
85  
-            if (true !== ($answer = $this->check($datas, $form->getAttribute('option_validator')))) {
  81
+            } elseif (true !== ($answer = $this->check($datas, $form->getAttribute('option_validator')))) {
86 82
                 $error = sprintf('Unable to check the captcha from the server. (%s)', $answer);
87 83
             }
88  
-        }
89  
-        else {
90  
-            if($this->code != $datas['response']) {
91  
-                $error = "The captcha is not valid.";
92  
-            }
  84
+        } elseif ($this->code != $datas['response']) {
  85
+            $error = "The captcha is not valid.";
93 86
         }
94 87
 
95 88
         if (!empty($error)) {
29  Tests/Form/Core/Type/ReCaptchaTypeTest.php
@@ -12,6 +12,9 @@
12 12
 namespace Genemu\Bundle\FormBundle\Tests\Form\Core\Type;
13 13
 
14 14
 use Genemu\Bundle\FormBundle\Tests\Form\Type\TypeTestCase;
  15
+use Genemu\Bundle\FormBundle\Form\Core\Type\ReCaptchaType;
  16
+use Genemu\Bundle\FormBundle\Form\Core\Validator\ReCaptchaValidator;
  17
+use Symfony\Component\HttpFoundation\Request;
15 18
 
16 19
 /**
17 20
  * @author Olivier Chauvel <olivier@generation-multiple.com>
@@ -55,4 +58,30 @@ public function testConfigs()
55 58
             'timeout' => 30
56 59
         ), $form->getAttribute('option_validator'));
57 60
     }
  61
+    
  62
+    /**
  63
+     * @dataProvider provideCodes
  64
+     */
  65
+    public function testCode($code, $isValid)
  66
+    {
  67
+        $request = new Request(array(), array('recaptcha_response_field' => $code));
  68
+        $form = $this->factory->create(new ReCaptchaType(
  69
+            new ReCaptchaValidator($request, 'privateKey', '1234'),
  70
+            'publicKey',
  71
+            'http://api.recaptcha.net',
  72
+            array()
  73
+        ));
  74
+
  75
+        $form->bind(null);
  76
+
  77
+        $this->assertEquals($isValid, $form->isValid());
  78
+    }
  79
+
  80
+    public function provideCodes()
  81
+    {
  82
+        return array(
  83
+            array('1234', true),
  84
+            array('4321', false),
  85
+        );
  86
+    }
58 87
 }
2  Tests/Form/Extension/TypeExtensionTest.php
@@ -75,7 +75,7 @@ protected function loadTypes()
75 75
                 'folder' => '/upload'
76 76
             ), __DIR__.'/../../Fixtures'),
77 77
             new Form\Core\Type\ReCaptchaType(
78  
-                new ReCaptchaValidator($this->request, 'privateKey', '1234'),
  78
+                new ReCaptchaValidator($this->request, 'privateKey'),
79 79
                 'publicKey',
80 80
                 'http://api.recaptcha.net',
81 81
                 array()),

0 notes on commit 5afbd79

Please sign in to comment.
Something went wrong with that request. Please try again.