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

Commit

Permalink
Merge branch 'release/v4'
Browse files Browse the repository at this point in the history
  • Loading branch information
biscolab committed Sep 30, 2020
2 parents 47c941c + b24b048 commit a9f5645
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
32 changes: 28 additions & 4 deletions src/ReCaptchaBuilderV2.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright (c) 2017 - present
* LaravelGoogleRecaptcha - ReCaptchaBuilderV2.php
Expand All @@ -20,6 +21,15 @@
class ReCaptchaBuilderV2 extends ReCaptchaBuilder
{

protected static $allowed_data_attribute = [
"theme",
"size",
"tabindex",
"callback",
"expired-callback",
"error-callback",
];

/**
* ReCaptchaBuilderV2 constructor.
*
Expand All @@ -35,14 +45,16 @@ public function __construct(string $api_site_key, string $api_secret_key)
/**
* Write ReCAPTCHA HTML tag in your FORM
* Insert before </form> 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 . '"';
Expand Down Expand Up @@ -93,4 +105,16 @@ public function getOnLoadCallback(): string
return "<script>var biscolabOnloadCallback = function() {grecaptcha.render('recaptcha-element', " . json_encode($attributes) . ");};</script>";
}

}
/**
* 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);
}
}
7 changes: 4 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright (c) 2017 - present
* LaravelGoogleRecaptcha - helpers.php
Expand Down Expand Up @@ -76,12 +77,13 @@ function htmlFormButton(?string $button_label = 'Submit', ?array $properties = [
if (!function_exists('htmlFormSnippet')) {

/**
* @param null|array $attributes
* @return string
*/
function htmlFormSnippet(): string
function htmlFormSnippet(?array $attributes = []): string
{

return ReCaptcha::htmlFormSnippet();
return ReCaptcha::htmlFormSnippet($attributes);
}
}

Expand Down Expand Up @@ -133,4 +135,3 @@ function recaptchaFieldName(): string
return \Biscolab\ReCaptcha\ReCaptchaBuilder::DEFAULT_RECAPTCHA_FIELD_NAME;
}
}

22 changes: 14 additions & 8 deletions tests/ReCaptchaHelpersV2ExplicitTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright (c) 2017 - present
* LaravelGoogleRecaptcha - ReCaptchaHelpersV2ExplicitTest.php
Expand All @@ -25,8 +26,10 @@ public function testGetOnLoadCallbackFunction()
/** @scrutinizer ignore-call */
$callback = $recaptcha->getOnLoadCallback();

$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>',
$callback);
$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>',
$callback
);
}

/**
Expand All @@ -37,8 +40,10 @@ public function testHtmlScriptTagJsApiHasJavascriptRenderFunction()

$html = htmlScriptTagJsApi();

$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>',
$html);
$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>',
$html
);
}

/**
Expand Down Expand Up @@ -85,9 +90,10 @@ public function testHtmlFormSnippet()

/** @scrutinizer ignore-call */
$html_snippet = \recaptcha()->htmlFormSnippet();
$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>',
$html_snippet);

$this->assertEquals(
'<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>',
$html_snippet
);
}

/**
Expand All @@ -112,4 +118,4 @@ protected function getEnvironmentSetUp($app)
'error-callback' => 'errorCallbackFunction',
]);
}
}
}
70 changes: 65 additions & 5 deletions tests/ReCaptchaHelpersV2Test.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright (c) 2017 - present
* LaravelGoogleRecaptcha - ReCaptchaHelpersV2Test.php
Expand Down Expand Up @@ -27,7 +28,6 @@ public function testHtmlScriptTagJsApiCalledByFacade()
->with(["key" => "val"]);

htmlScriptTagJsApi(["key" => "val"]);

}

/**
Expand All @@ -40,7 +40,6 @@ public function testHtmlFormSnippetCalledByFacade()
->once();

htmlFormSnippet();

}

/**
Expand Down Expand Up @@ -95,9 +94,70 @@ public function testHtmlFormSnippet()
{

$html_snippet = \recaptcha()->htmlFormSnippet();
$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>',
$html_snippet);
$this->assertEquals(
'<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>',
$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(
'<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>',
$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(
'<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>',
$html_snippet
);
}

/**
Expand All @@ -121,4 +181,4 @@ protected function getEnvironmentSetUp($app)
'error-callback' => 'errorCallbackFunction',
]);
}
}
}
2 changes: 1 addition & 1 deletion tests/ReCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testReCaptchaV2HtmlFormSnippet()

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

/**
Expand Down

0 comments on commit a9f5645

Please sign in to comment.