Skip to content
This repository was archived by the owner on Jun 10, 2024. It is now read-only.

Commit 998da06

Browse files
committed
Merge branch 'feature/htmlFormSnippet_attributes' into develop
2 parents 4991b28 + ff1913b commit 998da06

6 files changed

+113
-22
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "biscolab/laravel-recaptcha",
33
"description": "Simple and painless Google reCAPTCHA package for Laravel 5.5 or greater",
4-
"version": "4.2.0",
4+
"version": "4.4.0",
55
"license": "MIT",
66
"type": "library",
77
"keywords": [

src/ReCaptchaBuilderV2.php

+28-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (c) 2017 - present
45
* LaravelGoogleRecaptcha - ReCaptchaBuilderV2.php
@@ -20,6 +21,15 @@
2021
class ReCaptchaBuilderV2 extends ReCaptchaBuilder
2122
{
2223

24+
protected static $allowed_data_attribute = [
25+
"theme",
26+
"size",
27+
"tabindex",
28+
"callback",
29+
"expired-callback",
30+
"error-callback",
31+
];
32+
2333
/**
2434
* ReCaptchaBuilderV2 constructor.
2535
*
@@ -35,14 +45,16 @@ public function __construct(string $api_site_key, string $api_secret_key)
3545
/**
3646
* Write ReCAPTCHA HTML tag in your FORM
3747
* Insert before </form> tag
48+
*
49+
* @param null|array $attributes
3850
* @return string
3951
*/
40-
public function htmlFormSnippet(): string
52+
public function htmlFormSnippet(?array $attributes = []): string
4153
{
4254

4355
$data_attributes = [];
44-
$config_data_attributes = $this->getTagAttributes();
45-
56+
$config_data_attributes = array_merge($this->getTagAttributes(), self::cleanAttributes($attributes));
57+
ksort($config_data_attributes);
4658
foreach ($config_data_attributes as $k => $v) {
4759
if ($v) {
4860
$data_attributes[] = 'data-' . $k . '="' . $v . '"';
@@ -93,4 +105,16 @@ public function getOnLoadCallback(): string
93105
return "<script>var biscolabOnloadCallback = function() {grecaptcha.render('recaptcha-element', " . json_encode($attributes) . ");};</script>";
94106
}
95107

96-
}
108+
/**
109+
* Compare given attributes with allowed attributes
110+
*
111+
* @param array|null $attributes
112+
* @return array
113+
*/
114+
public static function cleanAttributes(?array $attributes = []): array
115+
{
116+
return array_filter($attributes, function ($k) {
117+
return in_array($k, self::$allowed_data_attribute);
118+
}, ARRAY_FILTER_USE_KEY);
119+
}
120+
}

src/helpers.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (c) 2017 - present
45
* LaravelGoogleRecaptcha - helpers.php
@@ -76,12 +77,13 @@ function htmlFormButton(?string $button_label = 'Submit', ?array $properties = [
7677
if (!function_exists('htmlFormSnippet')) {
7778

7879
/**
80+
* @param null|array $attributes
7981
* @return string
8082
*/
81-
function htmlFormSnippet(): string
83+
function htmlFormSnippet(?array $attributes = []): string
8284
{
8385

84-
return ReCaptcha::htmlFormSnippet();
86+
return ReCaptcha::htmlFormSnippet($attributes);
8587
}
8688
}
8789

@@ -133,4 +135,3 @@ function recaptchaFieldName(): string
133135
return \Biscolab\ReCaptcha\ReCaptchaBuilder::DEFAULT_RECAPTCHA_FIELD_NAME;
134136
}
135137
}
136-

tests/ReCaptchaHelpersV2ExplicitTest.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (c) 2017 - present
45
* LaravelGoogleRecaptcha - ReCaptchaHelpersV2ExplicitTest.php
@@ -25,8 +26,10 @@ public function testGetOnLoadCallbackFunction()
2526
/** @scrutinizer ignore-call */
2627
$callback = $recaptcha->getOnLoadCallback();
2728

28-
$this->assertEquals('<script>var biscolabOnloadCallback = function() {grecaptcha.render(\'recaptcha-element\', {"sitekey":"api_site_key","theme":"dark","size":"compact","tabindex":"2","callback":"callbackFunction","expired-callback":"expiredCallbackFunction","error-callback":"errorCallbackFunction"});};</script>',
29-
$callback);
29+
$this->assertEquals(
30+
'<script>var biscolabOnloadCallback = function() {grecaptcha.render(\'recaptcha-element\', {"sitekey":"api_site_key","theme":"dark","size":"compact","tabindex":"2","callback":"callbackFunction","expired-callback":"expiredCallbackFunction","error-callback":"errorCallbackFunction"});};</script>',
31+
$callback
32+
);
3033
}
3134

3235
/**
@@ -37,8 +40,10 @@ public function testHtmlScriptTagJsApiHasJavascriptRenderFunction()
3740

3841
$html = htmlScriptTagJsApi();
3942

40-
$this->assertEquals('<script>var biscolabOnloadCallback = function() {grecaptcha.render(\'recaptcha-element\', {"sitekey":"api_site_key","theme":"dark","size":"compact","tabindex":"2","callback":"callbackFunction","expired-callback":"expiredCallbackFunction","error-callback":"errorCallbackFunction"});};</script><script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=biscolabOnloadCallback" async defer></script>',
41-
$html);
43+
$this->assertEquals(
44+
'<script>var biscolabOnloadCallback = function() {grecaptcha.render(\'recaptcha-element\', {"sitekey":"api_site_key","theme":"dark","size":"compact","tabindex":"2","callback":"callbackFunction","expired-callback":"expiredCallbackFunction","error-callback":"errorCallbackFunction"});};</script><script src="https://www.google.com/recaptcha/api.js?render=explicit&onload=biscolabOnloadCallback" async defer></script>',
45+
$html
46+
);
4247
}
4348

4449
/**
@@ -85,9 +90,10 @@ public function testHtmlFormSnippet()
8590

8691
/** @scrutinizer ignore-call */
8792
$html_snippet = \recaptcha()->htmlFormSnippet();
88-
$this->assertEquals('<div class="g-recaptcha" data-sitekey="api_site_key" data-theme="dark" data-size="compact" data-tabindex="2" data-callback="callbackFunction" data-expired-callback="expiredCallbackFunction" data-error-callback="errorCallbackFunction" id="recaptcha-element"></div>',
89-
$html_snippet);
90-
93+
$this->assertEquals(
94+
'<div class="g-recaptcha" data-callback="callbackFunction" data-error-callback="errorCallbackFunction" data-expired-callback="expiredCallbackFunction" data-sitekey="api_site_key" data-size="compact" data-tabindex="2" data-theme="dark" id="recaptcha-element"></div>',
95+
$html_snippet
96+
);
9197
}
9298

9399
/**
@@ -112,4 +118,4 @@ protected function getEnvironmentSetUp($app)
112118
'error-callback' => 'errorCallbackFunction',
113119
]);
114120
}
115-
}
121+
}

tests/ReCaptchaHelpersV2Test.php

+65-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright (c) 2017 - present
45
* LaravelGoogleRecaptcha - ReCaptchaHelpersV2Test.php
@@ -27,7 +28,6 @@ public function testHtmlScriptTagJsApiCalledByFacade()
2728
->with(["key" => "val"]);
2829

2930
htmlScriptTagJsApi(["key" => "val"]);
30-
3131
}
3232

3333
/**
@@ -40,7 +40,6 @@ public function testHtmlFormSnippetCalledByFacade()
4040
->once();
4141

4242
htmlFormSnippet();
43-
4443
}
4544

4645
/**
@@ -95,9 +94,70 @@ public function testHtmlFormSnippet()
9594
{
9695

9796
$html_snippet = \recaptcha()->htmlFormSnippet();
98-
$this->assertEquals('<div class="g-recaptcha" data-sitekey="api_site_key" data-theme="dark" data-size="compact" data-tabindex="2" data-callback="callbackFunction" data-expired-callback="expiredCallbackFunction" data-error-callback="errorCallbackFunction" id="recaptcha-element"></div>',
99-
$html_snippet);
97+
$this->assertEquals(
98+
'<div class="g-recaptcha" data-callback="callbackFunction" data-error-callback="errorCallbackFunction" data-expired-callback="expiredCallbackFunction" data-sitekey="api_site_key" data-size="compact" data-tabindex="2" data-theme="dark" id="recaptcha-element"></div>',
99+
$html_snippet
100+
);
101+
}
102+
103+
/**
104+
* @test
105+
*/
106+
public function testHtmlFormSnippetOverridesDefaultAttributes()
107+
{
100108

109+
$html_snippet = \recaptcha()->htmlFormSnippet([
110+
"theme" => "light",
111+
"size" => "normal",
112+
"tabindex" => "3",
113+
"callback" => "callbackFunctionNew",
114+
"expired-callback" => "expiredCallbackFunctionNew",
115+
"error-callback" => "errorCallbackFunctionNew",
116+
"not-allowed-attribute" => "error",
117+
]);
118+
$this->assertEquals(
119+
'<div class="g-recaptcha" data-callback="callbackFunctionNew" data-error-callback="errorCallbackFunctionNew" data-expired-callback="expiredCallbackFunctionNew" data-sitekey="api_site_key" data-size="normal" data-tabindex="3" data-theme="light" id="recaptcha-element"></div>',
120+
$html_snippet
121+
);
122+
}
123+
124+
/**
125+
* @test
126+
*/
127+
public function testCleanAttributesReturnsOnlyAllowedAttributes()
128+
{
129+
$attributes = ReCaptchaBuilderV2::cleanAttributes([
130+
"theme" => "theme",
131+
"size" => "size",
132+
"tabindex" => "tabindex",
133+
"callback" => "callback",
134+
"expired-callback" => "expired-callback",
135+
"error-callback" => "error-callback",
136+
"not-allowed-attribute" => "error",
137+
]);
138+
$this->assertArrayHasKey("theme", $attributes);
139+
$this->assertArrayHasKey("size", $attributes);
140+
$this->assertArrayHasKey("tabindex", $attributes);
141+
$this->assertArrayHasKey("callback", $attributes);
142+
$this->assertArrayHasKey("expired-callback", $attributes);
143+
$this->assertArrayHasKey("error-callback", $attributes);
144+
$this->assertArrayNotHasKey("not-allowed-attribute", $attributes);
145+
}
146+
147+
/**
148+
* @test
149+
*/
150+
public function testHtmlFormSnippetKeepsDefaultConfigValuesUnlessOtherwiseStated()
151+
{
152+
$html_snippet = \recaptcha()->htmlFormSnippet([
153+
'callback' => 'callbackFunction',
154+
'expired-callback' => 'expiredCallbackFunction',
155+
'error-callback' => 'errorCallbackFunction',
156+
]);
157+
$this->assertEquals(
158+
'<div class="g-recaptcha" data-callback="callbackFunction" data-error-callback="errorCallbackFunction" data-expired-callback="expiredCallbackFunction" data-sitekey="api_site_key" data-size="compact" data-tabindex="2" data-theme="dark" id="recaptcha-element"></div>',
159+
$html_snippet
160+
);
101161
}
102162

103163
/**
@@ -121,4 +181,4 @@ protected function getEnvironmentSetUp($app)
121181
'error-callback' => 'errorCallbackFunction',
122182
]);
123183
}
124-
}
184+
}

tests/ReCaptchaTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testReCaptchaV2HtmlFormSnippet()
8585

8686
$recaptcha = $this->recaptcha_v2;
8787
$html_snippet = $recaptcha->htmlFormSnippet();
88-
$this->assertEquals('<div class="g-recaptcha" data-sitekey="api_site_key" data-theme="light" data-size="normal" id="recaptcha-element"></div>', $html_snippet);
88+
$this->assertEquals('<div class="g-recaptcha" data-sitekey="api_site_key" data-size="normal" data-theme="light" id="recaptcha-element"></div>', $html_snippet);
8989
}
9090

9191
/**

0 commit comments

Comments
 (0)