1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2017 - present |
5
|
|
|
* LaravelGoogleRecaptcha - ReCaptchaHelpersV2Test.php |
6
|
|
|
* author: Roberto Belotti - [email protected] |
7
|
|
|
* web : robertobelotti.com, github.com/biscolab |
8
|
|
|
* Initial version created on: 8/8/2019 |
9
|
|
|
* MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Biscolab\ReCaptcha\Tests; |
13
|
|
|
|
14
|
|
|
use Biscolab\ReCaptcha\Facades\ReCaptcha; |
15
|
|
|
use Biscolab\ReCaptcha\ReCaptchaBuilderV2; |
16
|
|
|
|
17
|
|
|
class ReCaptchaHelpersV2Test extends TestCase |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @test |
22
|
|
|
*/ |
23
|
|
|
public function testHtmlScriptTagJsApiCalledByFacade() |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
ReCaptcha::shouldReceive('htmlScriptTagJsApi') |
27
|
|
|
->once() |
28
|
|
|
->with(["key" => "val"]); |
29
|
|
|
|
30
|
|
|
htmlScriptTagJsApi(["key" => "val"]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @test |
35
|
|
|
*/ |
36
|
|
|
public function testHtmlFormSnippetCalledByFacade() |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
ReCaptcha::shouldReceive('htmlFormSnippet') |
40
|
|
|
->once(); |
41
|
|
|
|
42
|
|
|
htmlFormSnippet(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @test |
47
|
|
|
*/ |
48
|
|
|
public function testTagAttributes() |
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
$recaptcha = \recaptcha(); |
52
|
|
|
|
53
|
|
|
$tag_attributes = $recaptcha->getTagAttributes(); |
|
|
|
|
54
|
|
|
|
55
|
|
|
$this->assertArrayHasKey('sitekey', $recaptcha->getTagAttributes()); |
56
|
|
|
$this->assertArrayHasKey('theme', $recaptcha->getTagAttributes()); |
57
|
|
|
$this->assertArrayHasKey('size', $tag_attributes); |
58
|
|
|
$this->assertArrayHasKey('tabindex', $tag_attributes); |
59
|
|
|
$this->assertArrayHasKey('callback', $tag_attributes); |
60
|
|
|
$this->assertArrayHasKey('expired-callback', $tag_attributes); |
61
|
|
|
$this->assertArrayHasKey('error-callback', $tag_attributes); |
62
|
|
|
|
63
|
|
|
$this->assertEquals($tag_attributes['sitekey'], 'api_site_key'); |
64
|
|
|
$this->assertEquals($tag_attributes['theme'], 'dark'); |
65
|
|
|
$this->assertEquals($tag_attributes['size'], 'compact'); |
66
|
|
|
$this->assertEquals($tag_attributes['tabindex'], '2'); |
67
|
|
|
$this->assertEquals($tag_attributes['callback'], 'callbackFunction'); |
68
|
|
|
$this->assertEquals($tag_attributes['expired-callback'], 'expiredCallbackFunction'); |
69
|
|
|
$this->assertEquals($tag_attributes['error-callback'], 'errorCallbackFunction'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @test |
74
|
|
|
*/ |
75
|
|
|
public function testExpectReCaptchaInstanceOfReCaptchaBuilderV2() |
76
|
|
|
{ |
77
|
|
|
|
78
|
|
|
$this->assertInstanceOf(ReCaptchaBuilderV2::class, \recaptcha()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @expectedException \Error |
83
|
|
|
*/ |
84
|
|
|
public function testExpectExceptionWhenGetFormIdFunctionIsCalled() |
85
|
|
|
{ |
86
|
|
|
$this->expectException('\Error'); |
87
|
|
|
getFormId(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
*/ |
93
|
|
|
public function testHtmlFormSnippet() |
94
|
|
|
{ |
95
|
|
|
|
96
|
|
|
$html_snippet = \recaptcha()->htmlFormSnippet(); |
|
|
|
|
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
|
|
|
{ |
108
|
|
|
|
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
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Define environment setup. |
165
|
|
|
* |
166
|
|
|
* @param \Illuminate\Foundation\Application $app |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
protected function getEnvironmentSetUp($app) |
171
|
|
|
{ |
172
|
|
|
|
173
|
|
|
$app['config']->set('recaptcha.api_site_key', 'api_site_key'); |
174
|
|
|
$app['config']->set('recaptcha.version', 'v2'); |
175
|
|
|
$app['config']->set('recaptcha.tag_attributes', [ |
176
|
|
|
'theme' => 'dark', |
177
|
|
|
'size' => 'compact', |
178
|
|
|
'tabindex' => '2', |
179
|
|
|
'callback' => 'callbackFunction', |
180
|
|
|
'expired-callback' => 'expiredCallbackFunction', |
181
|
|
|
'error-callback' => 'errorCallbackFunction', |
182
|
|
|
]); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|