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

Commit 7d706fa

Browse files
committed
Tests added for htmlFormSnippet method
1 parent 707e6be commit 7d706fa

File tree

3 files changed

+61
-14
lines changed

3 files changed

+61
-14
lines changed

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

+46-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,51 @@ 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" => "dark",
111+
"size" => "compact",
112+
"tabindex" => "2",
113+
"callback" => "callbackFunction",
114+
"expired-callback" => "expiredCallbackFunction",
115+
"error-callback" => "errorCallbackFunction",
116+
"not-allowed-attribute" => "error",
117+
]);
118+
$this->assertEquals(
119+
'<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>',
120+
$html_snippet
121+
);
122+
}
123+
124+
public function testCleanAttributesReturnsOnlyAllowedAttributes()
125+
{
126+
$attributes = ReCaptchaBuilderV2::cleanAttributes([
127+
"theme" => "theme",
128+
"size" => "size",
129+
"tabindex" => "tabindex",
130+
"callback" => "callback",
131+
"expired-callback" => "expired-callback",
132+
"error-callback" => "error-callback",
133+
"not-allowed-attribute" => "error",
134+
]);
135+
$this->assertArrayHasKey("theme", $attributes);
136+
$this->assertArrayHasKey("size", $attributes);
137+
$this->assertArrayHasKey("tabindex", $attributes);
138+
$this->assertArrayHasKey("callback", $attributes);
139+
$this->assertArrayHasKey("expired-callback", $attributes);
140+
$this->assertArrayHasKey("error-callback", $attributes);
141+
$this->assertArrayNotHasKey("not-allowed-attribute", $attributes);
101142
}
102143

103144
/**
@@ -121,4 +162,4 @@ protected function getEnvironmentSetUp($app)
121162
'error-callback' => 'errorCallbackFunction',
122163
]);
123164
}
124-
}
165+
}

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)