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": [ diff --git a/src/ReCaptchaBuilderV2.php b/src/ReCaptchaBuilderV2.php index eab3ef7..ca0de81 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)); + ksort($config_data_attributes); foreach ($config_data_attributes as $k => $v) { if ($v) { $data_attributes[] = 'data-' . $k . '="' . $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 + */ + public 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 @@ 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..2b7c734 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,70 @@ public function testHtmlFormSnippet() { $html_snippet = \recaptcha()->htmlFormSnippet(); - $this->assertEquals('', - $html_snippet); + $this->assertEquals( + '', + $html_snippet + ); + } + + /** + * @test + */ + public function testHtmlFormSnippetOverridesDefaultAttributes() + { + $html_snippet = \recaptcha()->htmlFormSnippet([ + "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([ + "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); + } + + /** + * @test + */ + public function testHtmlFormSnippetKeepsDefaultConfigValuesUnlessOtherwiseStated() + { + $html_snippet = \recaptcha()->htmlFormSnippet([ + 'callback' => 'callbackFunction', + 'expired-callback' => 'expiredCallbackFunction', + 'error-callback' => 'errorCallbackFunction', + ]); + $this->assertEquals( + '', + $html_snippet + ); } /** @@ -121,4 +181,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); } /**