From 09dca04ece4e0887291bcbbb88d934a733c6157b Mon Sep 17 00:00:00 2001 From: Roberto Belotti Date: Mon, 21 Sep 2020 22:15:31 +0200 Subject: [PATCH 1/6] $attributes added to htmlFormSnippet method and helper (V2) --- src/ReCaptchaBuilderV2.php | 30 +++++++++++++++++++++++++++--- src/helpers.php | 7 ++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/ReCaptchaBuilderV2.php b/src/ReCaptchaBuilderV2.php index eab3ef7..6c82c15 100644 --- a/src/ReCaptchaBuilderV2.php +++ b/src/ReCaptchaBuilderV2.php @@ -1,4 +1,5 @@ tag + * + * @param null|array $attributes * @return string */ - public function htmlFormSnippet(): string + public function htmlFormSnippet(?array $attributes = []): string { $data_attributes = []; - $config_data_attributes = $this->getTagAttributes(); + $config_data_attributes = array_merge($this->getTagAttributes(), self::cleanAttributes($attributes)); foreach ($config_data_attributes as $k => $v) { if ($v) { @@ -93,4 +105,16 @@ public function getOnLoadCallback(): string return ""; } -} \ No newline at end of file + /** + * Compare given attributes with allowed attributes + * + * @param array|null $attributes + * @return array + */ + protected static function cleanAttributes(?array $attributes = []): array + { + return array_filter($attributes, function ($k) { + return in_array($k, self::$allowed_data_attribute); + }, ARRAY_FILTER_USE_KEY); + } +} diff --git a/src/helpers.php b/src/helpers.php index cfb56fd..51320b3 100755 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,4 +1,5 @@ Date: Thu, 24 Sep 2020 22:52:11 +0200 Subject: [PATCH 2/6] expired-callback attribute fixed --- src/ReCaptchaBuilderV2.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ReCaptchaBuilderV2.php b/src/ReCaptchaBuilderV2.php index 6c82c15..1f80bb5 100644 --- a/src/ReCaptchaBuilderV2.php +++ b/src/ReCaptchaBuilderV2.php @@ -26,7 +26,7 @@ class ReCaptchaBuilderV2 extends ReCaptchaBuilder "size", "tabindex", "callback", - "cexpired-allback", + "expired-callback", "error-callback", ]; From 707e6bed00ba7e8643c3581491a4e46b103a48c1 Mon Sep 17 00:00:00 2001 From: Roberto Belotti Date: Wed, 30 Sep 2020 22:04:10 +0200 Subject: [PATCH 3/6] Sort attributes in htmlFormSnippet method --- src/ReCaptchaBuilderV2.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ReCaptchaBuilderV2.php b/src/ReCaptchaBuilderV2.php index 1f80bb5..ca0de81 100644 --- a/src/ReCaptchaBuilderV2.php +++ b/src/ReCaptchaBuilderV2.php @@ -54,7 +54,7 @@ public function htmlFormSnippet(?array $attributes = []): string $data_attributes = []; $config_data_attributes = array_merge($this->getTagAttributes(), self::cleanAttributes($attributes)); - + ksort($config_data_attributes); foreach ($config_data_attributes as $k => $v) { if ($v) { $data_attributes[] = 'data-' . $k . '="' . $v . '"'; @@ -111,7 +111,7 @@ public function getOnLoadCallback(): string * @param array|null $attributes * @return array */ - protected static function cleanAttributes(?array $attributes = []): array + public static function cleanAttributes(?array $attributes = []): array { return array_filter($attributes, function ($k) { return in_array($k, self::$allowed_data_attribute); From 7d706facc3a80e85118e7f866212a4cca5822fd3 Mon Sep 17 00:00:00 2001 From: Roberto Belotti Date: Wed, 30 Sep 2020 22:04:58 +0200 Subject: [PATCH 4/6] Tests added for htmlFormSnippet method --- tests/ReCaptchaHelpersV2ExplicitTest.php | 22 ++++++---- tests/ReCaptchaHelpersV2Test.php | 51 +++++++++++++++++++++--- tests/ReCaptchaTest.php | 2 +- 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/tests/ReCaptchaHelpersV2ExplicitTest.php b/tests/ReCaptchaHelpersV2ExplicitTest.php index 8aa3642..10e23ca 100644 --- a/tests/ReCaptchaHelpersV2ExplicitTest.php +++ b/tests/ReCaptchaHelpersV2ExplicitTest.php @@ -1,4 +1,5 @@ getOnLoadCallback(); - $this->assertEquals('', - $callback); + $this->assertEquals( + '', + $callback + ); } /** @@ -37,8 +40,10 @@ public function testHtmlScriptTagJsApiHasJavascriptRenderFunction() $html = htmlScriptTagJsApi(); - $this->assertEquals('', - $html); + $this->assertEquals( + '', + $html + ); } /** @@ -85,9 +90,10 @@ public function testHtmlFormSnippet() /** @scrutinizer ignore-call */ $html_snippet = \recaptcha()->htmlFormSnippet(); - $this->assertEquals('
', - $html_snippet); - + $this->assertEquals( + '
', + $html_snippet + ); } /** @@ -112,4 +118,4 @@ protected function getEnvironmentSetUp($app) 'error-callback' => 'errorCallbackFunction', ]); } -} \ No newline at end of file +} diff --git a/tests/ReCaptchaHelpersV2Test.php b/tests/ReCaptchaHelpersV2Test.php index 774c1b6..7057120 100644 --- a/tests/ReCaptchaHelpersV2Test.php +++ b/tests/ReCaptchaHelpersV2Test.php @@ -1,4 +1,5 @@ with(["key" => "val"]); htmlScriptTagJsApi(["key" => "val"]); - } /** @@ -40,7 +40,6 @@ public function testHtmlFormSnippetCalledByFacade() ->once(); htmlFormSnippet(); - } /** @@ -95,9 +94,51 @@ public function testHtmlFormSnippet() { $html_snippet = \recaptcha()->htmlFormSnippet(); - $this->assertEquals('
', - $html_snippet); + $this->assertEquals( + '
', + $html_snippet + ); + } + + /** + * @test + */ + public function testHtmlFormSnippetOverridesDefaultAttributes() + { + $html_snippet = \recaptcha()->htmlFormSnippet([ + "theme" => "dark", + "size" => "compact", + "tabindex" => "2", + "callback" => "callbackFunction", + "expired-callback" => "expiredCallbackFunction", + "error-callback" => "errorCallbackFunction", + "not-allowed-attribute" => "error", + ]); + $this->assertEquals( + '
', + $html_snippet + ); + } + + public function testCleanAttributesReturnsOnlyAllowedAttributes() + { + $attributes = ReCaptchaBuilderV2::cleanAttributes([ + "theme" => "theme", + "size" => "size", + "tabindex" => "tabindex", + "callback" => "callback", + "expired-callback" => "expired-callback", + "error-callback" => "error-callback", + "not-allowed-attribute" => "error", + ]); + $this->assertArrayHasKey("theme", $attributes); + $this->assertArrayHasKey("size", $attributes); + $this->assertArrayHasKey("tabindex", $attributes); + $this->assertArrayHasKey("callback", $attributes); + $this->assertArrayHasKey("expired-callback", $attributes); + $this->assertArrayHasKey("error-callback", $attributes); + $this->assertArrayNotHasKey("not-allowed-attribute", $attributes); } /** @@ -121,4 +162,4 @@ protected function getEnvironmentSetUp($app) 'error-callback' => 'errorCallbackFunction', ]); } -} \ No newline at end of file +} diff --git a/tests/ReCaptchaTest.php b/tests/ReCaptchaTest.php index 2b55b86..c6675b3 100644 --- a/tests/ReCaptchaTest.php +++ b/tests/ReCaptchaTest.php @@ -85,7 +85,7 @@ public function testReCaptchaV2HtmlFormSnippet() $recaptcha = $this->recaptcha_v2; $html_snippet = $recaptcha->htmlFormSnippet(); - $this->assertEquals('
', $html_snippet); + $this->assertEquals('
', $html_snippet); } /** From 940207a1e07aa1d4d395189b94d7898dfb4fdfc2 Mon Sep 17 00:00:00 2001 From: Roberto Belotti Date: Wed, 30 Sep 2020 22:11:41 +0200 Subject: [PATCH 5/6] Tests improved overriding default config values in htmlFormSnippet method --- tests/ReCaptchaHelpersV2Test.php | 33 +++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/tests/ReCaptchaHelpersV2Test.php b/tests/ReCaptchaHelpersV2Test.php index 7057120..2b7c734 100644 --- a/tests/ReCaptchaHelpersV2Test.php +++ b/tests/ReCaptchaHelpersV2Test.php @@ -107,20 +107,23 @@ public function testHtmlFormSnippetOverridesDefaultAttributes() { $html_snippet = \recaptcha()->htmlFormSnippet([ - "theme" => "dark", - "size" => "compact", - "tabindex" => "2", - "callback" => "callbackFunction", - "expired-callback" => "expiredCallbackFunction", - "error-callback" => "errorCallbackFunction", + "theme" => "light", + "size" => "normal", + "tabindex" => "3", + "callback" => "callbackFunctionNew", + "expired-callback" => "expiredCallbackFunctionNew", + "error-callback" => "errorCallbackFunctionNew", "not-allowed-attribute" => "error", ]); $this->assertEquals( - '
', + '
', $html_snippet ); } + /** + * @test + */ public function testCleanAttributesReturnsOnlyAllowedAttributes() { $attributes = ReCaptchaBuilderV2::cleanAttributes([ @@ -141,6 +144,22 @@ public function testCleanAttributesReturnsOnlyAllowedAttributes() $this->assertArrayNotHasKey("not-allowed-attribute", $attributes); } + /** + * @test + */ + public function testHtmlFormSnippetKeepsDefaultConfigValuesUnlessOtherwiseStated() + { + $html_snippet = \recaptcha()->htmlFormSnippet([ + 'callback' => 'callbackFunction', + 'expired-callback' => 'expiredCallbackFunction', + 'error-callback' => 'errorCallbackFunction', + ]); + $this->assertEquals( + '
', + $html_snippet + ); + } + /** * Define environment setup. * From ff1913b8bf5af5161905308e22caf09d28d3d2f9 Mon Sep 17 00:00:00 2001 From: Roberto Belotti Date: Wed, 30 Sep 2020 22:12:00 +0200 Subject: [PATCH 6/6] Version changed in composer --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 099c5a5..1fedf35 100755 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "biscolab/laravel-recaptcha", "description": "Simple and painless Google reCAPTCHA package for Laravel 5.5 or greater", - "version": "4.2.0", + "version": "4.4.0", "license": "MIT", "type": "library", "keywords": [