From fb6cc24cc2c49342b32abe0548b791cae7e1eb94 Mon Sep 17 00:00:00 2001 From: prugala Date: Mon, 13 May 2019 10:28:37 +0200 Subject: [PATCH 1/5] feature / Support for reCAPTCHA v2 and some improvements --- .travis.yml | 4 ++++ README.md | 12 ++++++++++- composer.json | 8 ++++---- src/DependencyInjection/Configuration.php | 4 ++++ src/Resources/config/services.yml | 1 + src/Resources/views/Form/recaptcha.html.twig | 2 +- .../ContainsRecaptchaValidator.php | 20 ++++++++++++++----- 7 files changed, 40 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index cbf02d1..24027bb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,10 @@ cache: matrix: fast_finish: true include: + - php: 7.1 + env: SYMFONY=^3 + - php: 7.1 + env: SYMFONY=^4 - php: 7.2 env: SYMFONY=^3 - php: 7.2 diff --git a/README.md b/README.md index acdd6b7..8fd7858 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ pr_recaptcha: public_key: 'public key' secret_key: 'secret key' enabled: false # optional / default value: true - you can disable it for local or test env + version: 2 # optional / default value 3 - version of reCAPTCHA score_threshhold: 'score' # optional / default value: 0.5 hide_badge: true # optional / default value: false * host: 'www.google.com' # optional / default value: www.google.com ** @@ -36,6 +37,15 @@ Add field with type `RecaptchaType` to your form, example: `->add('captcha', RecaptchaType::class)` +###### Custom action name +If you want to use custom action name to analyze data from each form separately, set action name (default name: `form`) +``` +->add('captcha', RecaptchaType::class, [ + 'attr' => [ + 'action_name' => 'My form' + ] + ]) +``` + #### TODO -1. Support for version v2 2. Waiting for suggestions :) diff --git a/composer.json b/composer.json index f94da3a..2f57c91 100644 --- a/composer.json +++ b/composer.json @@ -15,13 +15,16 @@ } ], "require": { - "php": "^7.0", + "php": "^7.1", "google/recaptcha": "^1.1", "symfony/form": "^2.8 || ^3.0 || ^4.0", "twig/twig": "^2.0", "symfony/framework-bundle": "^2.8 || ^3.0 || ^4.0", "symfony/validator": "^2.8 || ^3.0 || ^4.0" }, + "require-dev": { + "phpunit/phpunit": "^7" + }, "autoload": { "psr-4": { "PR\\Bundle\\RecaptchaBundle\\": "src" @@ -31,8 +34,5 @@ "symfony": { "allow-contrib": "true" } - }, - "require-dev": { - "phpunit/phpunit": "^8" } } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index fdb47f2..addc57c 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -28,6 +28,10 @@ public function getConfigTreeBuilder() ->booleanNode('enabled') ->defaultValue(true) ->end() + ->enumNode('version') + ->values([2, 3]) + ->defaultValue(3) + ->end() ->floatNode('score_threshhold') ->defaultValue(0.5) ->end() diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index ba10fe7..3200f34 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -12,6 +12,7 @@ services: public: true arguments: - '%pr_recaptcha.enabled%' + - '%pr_recaptcha.version%' - '%pr_recaptcha.secret_key%' - '%pr_recaptcha.score_threshhold%' - '@request_stack' diff --git a/src/Resources/views/Form/recaptcha.html.twig b/src/Resources/views/Form/recaptcha.html.twig index 50e81c7..50d6894 100644 --- a/src/Resources/views/Form/recaptcha.html.twig +++ b/src/Resources/views/Form/recaptcha.html.twig @@ -7,7 +7,7 @@ + {% set options = attr.options %} - {% if form.vars.pr_recaptcha_hide_badge %} + {% if form.vars.pr_recaptcha_version == 2 %} + + {% else %} + + {% endif %} + + {% if form.vars.pr_recaptcha_hide_badge and form.vars.pr_recaptcha_version == 3 %} {% endif %} - + {% else %} + + + {% endif %} {{ form_label(form) }} {{ form_widget(form) }} diff --git a/tests/Form/Type/RecaptchaTypeTest.php b/tests/Form/Type/RecaptchaTypeTest.php index b8e04f7..3d0102d 100644 --- a/tests/Form/Type/RecaptchaTypeTest.php +++ b/tests/Form/Type/RecaptchaTypeTest.php @@ -18,7 +18,7 @@ final class RecaptchaTypeTest extends TestCase protected function setUp(): void { - $this->formType = new RecaptchaType('publicKey', true, 'www.google.com'); + $this->formType = new RecaptchaType(3,'publicKey', true, 'www.google.com'); } /** From 41944e9407cd6cfe775ca3b63bd56c8b0e2e79fd Mon Sep 17 00:00:00 2001 From: prugala Date: Mon, 13 May 2019 13:48:02 +0200 Subject: [PATCH 4/5] feature / Support for reCAPTCHA v2 and some improvements --- tests/Form/Type/RecaptchaTypeTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Form/Type/RecaptchaTypeTest.php b/tests/Form/Type/RecaptchaTypeTest.php index 3d0102d..c3df485 100644 --- a/tests/Form/Type/RecaptchaTypeTest.php +++ b/tests/Form/Type/RecaptchaTypeTest.php @@ -47,9 +47,9 @@ public function buildView(): void $this->formType->buildView($view, $form, []); - $this->assertContains('publicKey', $view->vars['pr_recaptcha_public_key']); + $this->assertEquals('publicKey', $view->vars['pr_recaptcha_public_key']); $this->assertTrue($view->vars['pr_recaptcha_hide_badge']); - $this->assertContains('www.google.com', $view->vars['pr_recaptcha_host']); + $this->assertEquals('www.google.com', $view->vars['pr_recaptcha_host']); } /** From f04364b569017fa40adc609b797b3ff18ba70919 Mon Sep 17 00:00:00 2001 From: prugala Date: Tue, 14 May 2019 06:40:09 +0200 Subject: [PATCH 5/5] feature / Support for reCAPTCHA v2 and some improvements --- composer.json | 2 +- phpunit.xml.dist | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 2f57c91..38472a1 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "symfony/validator": "^2.8 || ^3.0 || ^4.0" }, "require-dev": { - "phpunit/phpunit": "^7" + "phpunit/phpunit": "^7 || ^8" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 60a30bd..48436e5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,7 +16,4 @@ src - - -