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

Commit b5f559a

Browse files
committed
New tests
1 parent 059a232 commit b5f559a

7 files changed

+485
-260
lines changed

tests/ReCaptchaConfigurationTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public function testSkipIpWhiteListIsArray()
4848
$ip_whitelist = $this->recaptcha->getIpWhitelist();
4949
$this->assertTrue(is_array($ip_whitelist));
5050
$this->assertCount(2, $ip_whitelist);
51+
52+
$this->assertEquals('10.0.0.1', $ip_whitelist[0]);
53+
$this->assertEquals('10.0.0.2', $ip_whitelist[1]);
5154
}
5255

5356
/**
@@ -69,6 +72,8 @@ public function testCurlTimeoutIsSet()
6972
protected function getEnvironmentSetUp($app)
7073
{
7174

75+
$app['config']->set('recaptcha.api_site_key', 'api_site_key');
76+
$app['config']->set('recaptcha.api_secret_key', 'api_secret_key');
7277
$app['config']->set('recaptcha.skip_ip', '10.0.0.1,10.0.0.2');
7378
$app['config']->set('recaptcha.curl_timeout', 3);
7479
}
@@ -81,6 +86,6 @@ protected function setUp(): void
8186

8287
parent::setUp(); // TODO: Change the autogenerated stub
8388

84-
$this->recaptcha = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key');
89+
$this->recaptcha = recaptcha();
8590
}
8691
}

tests/ReCaptchaHelpersInvisibleTest.php

Lines changed: 80 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,80 +8,91 @@
88
* MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
99
*/
1010

11-
1211
namespace Biscolab\ReCaptcha\Tests;
1312

1413
use Biscolab\ReCaptcha\Facades\ReCaptcha;
15-
use Biscolab\ReCaptcha\ReCaptchaServiceProvider;
1614

1715
class ReCaptchaHelpersInvisibleTest extends TestCase
1816
{
1917

20-
/**
21-
* @test
22-
*/
23-
public function testHtmlScriptTagJsApiCalledByFacade()
24-
{
25-
ReCaptcha::shouldReceive('htmlScriptTagJsApi')
26-
->once()
27-
->with("key");
28-
29-
htmlScriptTagJsApi("key");
30-
31-
}
32-
33-
/**
34-
* @test
35-
*/
36-
public function testHtmlScriptTagJsApiV3CalledByFacade()
37-
{
38-
ReCaptcha::shouldReceive('htmlScriptTagJsApiV3')
39-
->once()
40-
->with([]);
41-
42-
htmlScriptTagJsApiV3([]);
43-
44-
}
45-
46-
/**
47-
* @test
48-
*/
49-
public function testHtmlFormButtonCalledByFacade()
50-
{
51-
ReCaptcha::shouldReceive('htmlFormButton')
52-
->once()
53-
->with("key");
54-
55-
htmlFormButton("key");
56-
57-
}
58-
59-
/**
60-
* @test
61-
* @expectedException \TypeError
62-
*/
63-
public function testHtmlFormSnippetCalledByFacade()
64-
{
65-
ReCaptcha::shouldReceive('htmlFormSnippet')
66-
->once();
67-
68-
htmlFormSnippet();
69-
70-
}
71-
72-
73-
/**
74-
* Define environment setup.
75-
*
76-
* @param \Illuminate\Foundation\Application $app
77-
*
78-
* @return void
79-
*/
80-
protected function getEnvironmentSetUp($app)
81-
{
82-
83-
$app['config']->set('recaptcha.api_site_key', 'api_site_key');
84-
$app['config']->set('recaptcha.api_site_key', 'api_site_key');
85-
$app['config']->set('recaptcha.version', 'invisible');
86-
}
18+
/**
19+
* @test
20+
*/
21+
public function testHtmlScriptTagJsApiCalledByFacade()
22+
{
23+
24+
ReCaptcha::shouldReceive('htmlScriptTagJsApi')
25+
->once()
26+
->with(["form_id" => "test-form"]);
27+
28+
htmlScriptTagJsApi(["form_id" => "test-form"]);
29+
30+
}
31+
32+
/**
33+
* @test
34+
*/
35+
public function testHtmlFormButtonCalledByFacade()
36+
{
37+
38+
ReCaptcha::shouldReceive('htmlFormButton')
39+
->once()
40+
->with("Inner text", ['id' => 'button_id']);
41+
42+
htmlFormButton("Inner text", ['id' => 'button_id']);
43+
44+
}
45+
46+
/**
47+
* @test
48+
*/
49+
public function testGetFormIdCalledByFacade()
50+
{
51+
52+
ReCaptcha::shouldReceive('getFormId')
53+
->once();
54+
55+
getFormId();
56+
57+
}
58+
59+
public function testHtmlFormButtonConfiguration() {
60+
$button_html = htmlFormButton("Inner text", ['id' => 'button_id', 'class' => 'button_class', 'data-sitekey' => 'custom-data-sitekey', 'data-callback' => 'myCallback']);
61+
62+
$this->assertEquals('<button class="button_class g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key" id="button_id">Inner text</button>', $button_html);
63+
}
64+
65+
/**
66+
* @test
67+
* @expectedException \TypeError
68+
*/
69+
public function testHtmlFormSnippetCalledByFacade()
70+
{
71+
72+
ReCaptcha::shouldReceive('htmlFormSnippet')
73+
->once();
74+
75+
htmlFormSnippet();
76+
77+
}
78+
79+
public function testGetFormIdReturnDefaultFormIdValue()
80+
{
81+
$this->assertEquals('biscolab-recaptcha-invisible-form', getFormId());
82+
}
83+
84+
/**
85+
* Define environment setup.
86+
*
87+
* @param \Illuminate\Foundation\Application $app
88+
*
89+
* @return void
90+
*/
91+
protected function getEnvironmentSetUp($app)
92+
{
93+
94+
$app['config']->set('recaptcha.api_site_key', 'api_site_key');
95+
$app['config']->set('recaptcha.api_site_key', 'api_site_key');
96+
$app['config']->set('recaptcha.version', 'invisible');
97+
}
8798
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2017 - present
4+
* LaravelGoogleRecaptcha - ReCaptchaHelpersV2ExplicitTest.php
5+
* author: Roberto Belotti - [email protected]
6+
* web : robertobelotti.com, github.com/biscolab
7+
* Initial version created on: 2/9/2019
8+
* MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE
9+
*/
10+
11+
namespace Biscolab\ReCaptcha\Tests;
12+
13+
use Biscolab\ReCaptcha\ReCaptchaBuilderV2;
14+
15+
class ReCaptchaHelpersV2ExplicitTest extends TestCase
16+
{
17+
18+
/**
19+
* @test
20+
*/
21+
public function testGetOnLoadCallbackFunction()
22+
{
23+
24+
$recaptcha = \recaptcha();
25+
26+
$callback = $recaptcha->getOnLoadCallback();
27+
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>', $callback);
29+
}
30+
31+
/**
32+
* @test
33+
*/
34+
public function testHtmlScriptTagJsApiHasJavascriptRenderFunction()
35+
{
36+
37+
$html = htmlScriptTagJsApi();
38+
39+
$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>',
40+
$html);
41+
}
42+
43+
/**
44+
* @test
45+
*/
46+
public function testTagAttributes()
47+
{
48+
49+
$recaptcha = \recaptcha();
50+
51+
$tag_attributes = $recaptcha->getTagAttributes();
52+
53+
$this->assertArrayHasKey('sitekey', $tag_attributes);
54+
$this->assertArrayHasKey('theme', $tag_attributes);
55+
$this->assertArrayHasKey('size', $tag_attributes);
56+
$this->assertArrayHasKey('tabindex', $tag_attributes);
57+
$this->assertArrayHasKey('callback', $tag_attributes);
58+
$this->assertArrayHasKey('expired-callback', $tag_attributes);
59+
$this->assertArrayHasKey('error-callback', $tag_attributes);
60+
61+
$this->assertEquals($tag_attributes['sitekey'], 'api_site_key');
62+
$this->assertEquals($tag_attributes['theme'], 'dark');
63+
$this->assertEquals($tag_attributes['size'], 'compact');
64+
$this->assertEquals($tag_attributes['tabindex'], '2');
65+
$this->assertEquals($tag_attributes['callback'], 'callbackFunction');
66+
$this->assertEquals($tag_attributes['expired-callback'], 'expiredCallbackFunction');
67+
$this->assertEquals($tag_attributes['error-callback'], 'errorCallbackFunction');
68+
}
69+
70+
/**
71+
* @test
72+
*/
73+
public function testExpectReCaptchaInstanceOfReCaptchaBuilderV2()
74+
{
75+
76+
$this->assertInstanceOf(ReCaptchaBuilderV2::class, \recaptcha());
77+
}
78+
79+
/**
80+
* @test
81+
*/
82+
public function testHtmlFormSnippet()
83+
{
84+
85+
$html_snippet = \recaptcha()->htmlFormSnippet();
86+
$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>',
87+
$html_snippet);
88+
89+
}
90+
91+
/**
92+
* Define environment setup.
93+
*
94+
* @param \Illuminate\Foundation\Application $app
95+
*
96+
* @return void
97+
*/
98+
protected function getEnvironmentSetUp($app)
99+
{
100+
101+
$app['config']->set('recaptcha.api_site_key', 'api_site_key');
102+
$app['config']->set('recaptcha.version', 'v2');
103+
$app['config']->set('recaptcha.explicit', true);
104+
$app['config']->set('recaptcha.tag_attributes', [
105+
'theme' => 'dark',
106+
'size' => 'compact',
107+
'tabindex' => '2',
108+
'callback' => 'callbackFunction',
109+
'expired-callback' => 'expiredCallbackFunction',
110+
'error-callback' => 'errorCallbackFunction',
111+
]);
112+
}
113+
}

0 commit comments

Comments
 (0)