1 | <?php |
||
2 | /** |
||
3 | * Copyright (c) 2017 - present |
||
4 | * LaravelGoogleRecaptcha - ReCaptchaTest.php |
||
5 | * author: Roberto Belotti - [email protected] |
||
6 | * web : robertobelotti.com, github.com/biscolab |
||
7 | * Initial version created on: 12/9/2018 |
||
8 | * MIT license: https://github.com/biscolab/laravel-recaptcha/blob/master/LICENSE |
||
9 | */ |
||
10 | |||
11 | namespace Biscolab\ReCaptcha\Tests; |
||
12 | |||
13 | use Biscolab\ReCaptcha\Facades\ReCaptcha; |
||
14 | use Biscolab\ReCaptcha\ReCaptchaBuilder; |
||
15 | use Biscolab\ReCaptcha\ReCaptchaBuilderInvisible; |
||
16 | use Biscolab\ReCaptcha\ReCaptchaBuilderV2; |
||
17 | use Biscolab\ReCaptcha\ReCaptchaBuilderV3; |
||
18 | |||
19 | /** |
||
20 | * Class ReCaptchaTest |
||
21 | * @package Biscolab\ReCaptcha\Tests |
||
22 | */ |
||
23 | class ReCaptchaTest extends TestCase |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var ReCaptchaBuilderInvisible |
||
28 | */ |
||
29 | protected $recaptcha_invisible = null; |
||
30 | |||
31 | /** |
||
32 | * @var ReCaptchaBuilderV2 |
||
33 | */ |
||
34 | protected $recaptcha_v2 = null; |
||
35 | |||
36 | /** |
||
37 | * @var ReCaptchaBuilderV3 |
||
38 | */ |
||
39 | protected $recaptcha_v3 = null; |
||
40 | |||
41 | /** |
||
42 | * @tests |
||
43 | */ |
||
44 | public function testHtmlScriptTagJsApiGetHtmlScriptTag() |
||
45 | { |
||
46 | |||
47 | $r = ReCaptcha::htmlScriptTagJsApi(); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
48 | $this->assertEquals('<script src="https://www.google.com/recaptcha/api.js" async defer></script>', $r); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @test |
||
53 | */ |
||
54 | public function testReCaptchaInvisibleHtmlFormButtonDefault() |
||
55 | { |
||
56 | |||
57 | $recaptcha = $this->recaptcha_invisible; |
||
58 | $html_button = $recaptcha->htmlFormButton(); |
||
59 | $this->assertEquals('<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Submit</button>', |
||
60 | $html_button); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @test |
||
65 | */ |
||
66 | public function testReCaptchaInvisibleHtmlFormButtonCustom() |
||
67 | { |
||
68 | |||
69 | $recaptcha = $this->recaptcha_invisible; |
||
70 | $html_button = $recaptcha->htmlFormButton('Custom Text'); |
||
71 | $this->assertEquals('<button class="g-recaptcha" data-callback="biscolabLaravelReCaptcha" data-sitekey="api_site_key">Custom Text</button>', |
||
72 | $html_button); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @test |
||
77 | */ |
||
78 | public function testReCaptchaV2HtmlFormSnippet() |
||
79 | { |
||
80 | |||
81 | $recaptcha = $this->recaptcha_v2; |
||
82 | $html_snippet = $recaptcha->htmlFormSnippet(); |
||
83 | $this->assertEquals('<div class="g-recaptcha" data-sitekey="api_site_key" data-theme="light" data-size="normal" id="recaptcha-element"></div>', $html_snippet); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @test |
||
88 | * @expectedException \Error |
||
89 | */ |
||
90 | public function testReCaptchaInvisibleHtmlFormSnippetShouldThrowError() |
||
91 | { |
||
92 | |||
93 | $this->recaptcha_invisible->htmlFormSnippet(); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @test |
||
98 | */ |
||
99 | public function testSkipByIpAndReturnArrayReturnsDefaultArray() |
||
100 | { |
||
101 | |||
102 | $mock = $this->getMockBuilder(ReCaptchaBuilder::class) |
||
103 | ->setConstructorArgs([ |
||
104 | "api_site_key", |
||
105 | "api_secret_key" |
||
106 | ]) |
||
107 | ->setMethods([ |
||
108 | 'returnArray' |
||
109 | ]) |
||
110 | ->getMock(); |
||
111 | |||
112 | $mock->method('returnArray') |
||
113 | ->willReturn(true); |
||
114 | |||
115 | $this->setSkipByIp($this->recaptcha_v3, true); |
||
116 | |||
117 | $validate = $this->recaptcha_v3->validate(""); |
||
118 | |||
119 | $this->assertEquals([ |
||
120 | "skip_by_ip" => true, |
||
121 | "score" => 0.9, |
||
122 | "success" => true |
||
123 | ], $validate); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @test |
||
128 | */ |
||
129 | public function testSlipByIpReturnsValidResponse() |
||
130 | { |
||
131 | |||
132 | $this->setSkipByIp($this->recaptcha_invisible, true); |
||
133 | $validate = $this->recaptcha_invisible->validate(""); |
||
134 | |||
135 | $this->assertTrue($validate); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @test |
||
140 | */ |
||
141 | public function testDefaultCurlTimeout() |
||
142 | { |
||
143 | |||
144 | $this->assertEquals($this->recaptcha_invisible->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT); |
||
145 | $this->assertEquals($this->recaptcha_v2->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT); |
||
146 | $this->assertEquals($this->recaptcha_v3->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @test |
||
151 | * @expectedException \Error |
||
152 | */ |
||
153 | public function testReCaptchaV2htmlFormButtonShouldThrowError() |
||
154 | { |
||
155 | |||
156 | $this->recaptcha_v2->htmlFormButton(); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @test |
||
161 | */ |
||
162 | public function testRecaptchaFieldNameHelperReturnsReCaptchaBuilderDefaultFieldName() { |
||
163 | $this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_FIELD_NAME, recaptchaFieldName()); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @test |
||
168 | */ |
||
169 | public function testRecaptchaRuleNameHelperReturnsReCaptchaBuilderDefaultRuleName() { |
||
170 | $this->assertEquals(ReCaptchaBuilder::DEFAULT_RECAPTCHA_RULE_NAME, recaptchaRuleName()); |
||
171 | } |
||
172 | |||
173 | protected function setSkipByIp(ReCaptchaBuilder $builder, bool $value) |
||
174 | { |
||
175 | |||
176 | $reflection = new \ReflectionClass($builder); |
||
177 | $reflection_property = $reflection->getProperty('skip_by_ip'); |
||
178 | $reflection_property->setAccessible(true); |
||
179 | $reflection_property->setValue($builder, $value); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @inheritdoc |
||
184 | */ |
||
185 | protected function setUp(): void |
||
186 | { |
||
187 | |||
188 | parent::setUp(); // TODO: Change the autogenerated stub |
||
189 | |||
190 | $this->recaptcha_invisible = new ReCaptchaBuilderInvisible('api_site_key', 'api_secret_key'); |
||
191 | $this->recaptcha_v2 = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key'); |
||
192 | $this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key'); |
||
193 | |||
194 | } |
||
195 | } |