Skip to content

Commit 58354fa

Browse files
committed
updated heap sort
1 parent e3212a6 commit 58354fa

File tree

4 files changed

+154
-15
lines changed

4 files changed

+154
-15
lines changed

src/Facades/GoogleReCaptchaV3.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
/**
99
* @method static GoogleReCaptchaV3Response verifyResponse($value, $ip = null)
10-
* @method static \TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3 setAction(string $value)
10+
* @method static \TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3 setAction($value)
11+
* @method static \TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3 setScore($value)
1112
* @method static render($mappers)
1213
* @see \TimeHunter\LaravelGoogleReCaptchaV3\GoogleReCaptchaV3
1314
*/

src/GoogleReCaptchaV3.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,16 @@ public function setAction($value = null)
9393

9494
return $this;
9595
}
96+
97+
98+
/**
99+
* @param string|null $value
100+
* @return $this
101+
*/
102+
public function setScore($value = null)
103+
{
104+
$this->service->setScore($value);
105+
106+
return $this;
107+
}
96108
}

src/Services/GoogleReCaptchaV3Service.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class GoogleReCaptchaV3Service
1717
private $config;
1818
private $requestClient;
1919
private $action;
20+
private $score;
2021

2122
public function __construct(ReCaptchaConfigV3Interface $config, RequestClientInterface $requestClient)
2223
{
@@ -31,7 +32,7 @@ public function __construct(ReCaptchaConfigV3Interface $config, RequestClientInt
3132
*/
3233
public function verifyResponse($response, $ip = null)
3334
{
34-
if (! $this->config->isServiceEnabled()) {
35+
if (!$this->config->isServiceEnabled()) {
3536
$res = new GoogleReCaptchaV3Response([], $ip);
3637
$res->setSuccess(true);
3738

@@ -66,7 +67,7 @@ public function verifyResponse($response, $ip = null)
6667
return $rawResponse;
6768
}
6869

69-
if (! empty($this->config->getHostName()) && strcasecmp($this->config->getHostName(), $rawResponse->getHostname()) !== 0) {
70+
if (!empty($this->config->getHostName()) && strcasecmp($this->config->getHostName(), $rawResponse->getHostname()) !== 0) {
7071
$rawResponse->setMessage(GoogleReCaptchaV3Response::ERROR_HOSTNAME);
7172
$rawResponse->setSuccess(false);
7273

@@ -80,19 +81,28 @@ public function verifyResponse($response, $ip = null)
8081
return $rawResponse;
8182
}
8283

83-
if ($this->getConfig()->isScoreEnabled()) {
84-
$count = collect($this->getConfig()->getSetting())
85-
->where('action', '=', $rawResponse->getAction())
86-
->where('score_comparision', '=', true)
87-
->where('threshold', '>', $rawResponse->getScore())
88-
->count();
89-
if ($count > 0) {
90-
$rawResponse->setSuccess(false);
91-
$rawResponse->setMessage(GoogleReCaptchaV3Response::ERROR_SCORE_THRESHOLD);
92-
93-
return $rawResponse;
84+
if (isset($this->score) && $this->score > $rawResponse->getScore()) {
85+
$rawResponse->setSuccess(false);
86+
$rawResponse->setMessage(GoogleReCaptchaV3Response::ERROR_SCORE_THRESHOLD);
87+
return $rawResponse;
88+
} else {
89+
if ($this->getConfig()->isScoreEnabled()) {
90+
$count = collect($this->getConfig()->getSetting())
91+
->where('action', '=', $rawResponse->getAction())
92+
->where('score_comparision', '=', true)
93+
->where('threshold', '>', $rawResponse->getScore())
94+
->count();
95+
96+
if ($count > 0) {
97+
$rawResponse->setSuccess(false);
98+
$rawResponse->setMessage(GoogleReCaptchaV3Response::ERROR_SCORE_THRESHOLD);
99+
100+
return $rawResponse;
101+
}
94102
}
95103
}
104+
105+
96106
$rawResponse->setSuccess(true);
97107
$rawResponse->setMessage('Successfully passed.');
98108

@@ -111,10 +121,21 @@ public function getConfig()
111121
* @param string|null $value
112122
* @return $this
113123
*/
114-
public function setAction(string $value = null)
124+
public function setAction($value = null)
115125
{
116126
$this->action = $value;
117127

118128
return $this;
119129
}
130+
131+
/**
132+
* @param string|null $value
133+
* @return $this
134+
*/
135+
public function setScore($value = null)
136+
{
137+
$this->score = $value;
138+
139+
return $this;
140+
}
120141
}

tests/ScoreTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,109 @@ public function testScore6()
216216
$response = $service->verifyResponse('test response');
217217
$this->assertEquals(true, $response->isSuccess());
218218
}
219+
220+
221+
public function testScore7()
222+
{
223+
// Create a stub for the SomeClass class.
224+
$configStub = $this->createMock(ReCaptchaConfigV3::class);
225+
226+
// Configure the stub.
227+
$configStub->method('isServiceEnabled')
228+
->willReturn(true);
229+
230+
$configStub->method('isScoreEnabled')
231+
->willReturn(false);
232+
233+
$configStub->method('getSetting')
234+
->willReturn([
235+
[
236+
'action' => 'contact_us_test',
237+
'threshold' => 0.91,
238+
'score_comparision' => true,
239+
],
240+
]);
241+
242+
$testJson = '{ "success": true, "challenge_ts": "2018-12-25T03:35:32Z", "hostname": "ryandeng.test", "score": 0.9, "action": "contact_us" }';
243+
244+
$clientStub = $this->createMock(GuzzleRequestClient::class);
245+
$clientStub->method('post')
246+
->willReturn($testJson);
247+
248+
$_service = new GoogleReCaptchaV3Service($configStub, $clientStub);
249+
$service = new GoogleReCaptchaV3($_service);
250+
251+
$response = $service->setScore(0.8)->verifyResponse('test response');
252+
$this->assertEquals(true, $response->isSuccess());
253+
}
254+
255+
256+
public function testScore11()
257+
{
258+
// Create a stub for the SomeClass class.
259+
$configStub = $this->createMock(ReCaptchaConfigV3::class);
260+
261+
// Configure the stub.
262+
$configStub->method('isServiceEnabled')
263+
->willReturn(true);
264+
265+
$configStub->method('isScoreEnabled')
266+
->willReturn(true);
267+
268+
$configStub->method('getSetting')
269+
->willReturn([
270+
[
271+
'action' => 'contact_us',
272+
'threshold' => 0.91,
273+
'score_comparision' => true,
274+
],
275+
]);
276+
277+
$testJson = '{ "success": true, "challenge_ts": "2018-12-25T03:35:32Z", "hostname": "ryandeng.test", "score": 0.9, "action": "contact_us" }';
278+
279+
$clientStub = $this->createMock(GuzzleRequestClient::class);
280+
$clientStub->method('post')
281+
->willReturn($testJson);
282+
283+
$_service = new GoogleReCaptchaV3Service($configStub, $clientStub);
284+
$service = new GoogleReCaptchaV3($_service);
285+
286+
$response = $service->verifyResponse('test response');
287+
$this->assertEquals(false, $response->isSuccess());
288+
}
289+
290+
291+
public function testScore8()
292+
{
293+
// Create a stub for the SomeClass class.
294+
$configStub = $this->createMock(ReCaptchaConfigV3::class);
295+
296+
// Configure the stub.
297+
$configStub->method('isServiceEnabled')
298+
->willReturn(true);
299+
300+
$configStub->method('isScoreEnabled')
301+
->willReturn(true);
302+
303+
$configStub->method('getSetting')
304+
->willReturn([
305+
[
306+
'action' => 'contact_us',
307+
'threshold' => 0.91,
308+
'score_comparision' => true,
309+
],
310+
]);
311+
312+
$testJson = '{ "success": true, "challenge_ts": "2018-12-25T03:35:32Z", "hostname": "ryandeng.test", "score": 0.9, "action": "contact_us" }';
313+
314+
$clientStub = $this->createMock(GuzzleRequestClient::class);
315+
$clientStub->method('post')
316+
->willReturn($testJson);
317+
318+
$_service = new GoogleReCaptchaV3Service($configStub, $clientStub);
319+
$service = new GoogleReCaptchaV3($_service);
320+
321+
$response = $service->setScore(0.8)->verifyResponse('test response');
322+
$this->assertEquals(false, $response->isSuccess());
323+
}
219324
}

0 commit comments

Comments
 (0)