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
|
|
|
* @var ReCaptchaBuilderInvisible |
27
|
|
|
*/ |
28
|
|
|
protected $recaptcha_invisible = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ReCaptchaBuilderV2 |
32
|
|
|
*/ |
33
|
|
|
protected $recaptcha_v2 = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ReCaptchaBuilderV3 |
37
|
|
|
*/ |
38
|
|
|
protected $recaptcha_v3 = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @tests |
42
|
|
|
*/ |
43
|
|
|
public function testHtmlScriptTagJsApiGetHtmlScriptTag() { |
44
|
|
|
|
45
|
|
|
$r = ReCaptcha::htmlScriptTagJsApi(); |
|
|
|
|
46
|
|
|
$this->assertEquals('<script src="https://www.google.com/recaptcha/api.js" async defer></script>', $r); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @test |
51
|
|
|
*/ |
52
|
|
|
public function testReCaptchaInvisibleHtmlFormButtonDefault() { |
53
|
|
|
|
54
|
|
|
$recaptcha = $this->recaptcha_invisible; |
55
|
|
|
$html_button = $recaptcha->htmlFormButton(); |
56
|
|
|
$this->assertEquals('<button class="g-recaptcha" data-sitekey="api_site_key" data-callback="biscolabLaravelReCaptcha">Submit</button>', |
57
|
|
|
$html_button); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @test |
62
|
|
|
*/ |
63
|
|
|
public function testReCaptchaInvisibleHtmlFormButtonCustom() { |
64
|
|
|
|
65
|
|
|
$recaptcha = $this->recaptcha_invisible; |
66
|
|
|
$html_button = $recaptcha->htmlFormButton('Custom Text'); |
67
|
|
|
$this->assertEquals('<button class="g-recaptcha" data-sitekey="api_site_key" data-callback="biscolabLaravelReCaptcha">Custom Text</button>', |
68
|
|
|
$html_button); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
*/ |
74
|
|
|
public function testReCaptchaV2HtmlFormSnippet() { |
75
|
|
|
|
76
|
|
|
$recaptcha = $this->recaptcha_v2; |
77
|
|
|
$html_snippet = $recaptcha->htmlFormSnippet(); |
78
|
|
|
$this->assertEquals('<div class="g-recaptcha" data-sitekey="api_site_key"></div>', $html_snippet); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @test |
83
|
|
|
* @expectedException \Error |
84
|
|
|
*/ |
85
|
|
|
public function testReCaptchaInvisibleHtmlFormSnippetShouldThrowError() { |
86
|
|
|
|
87
|
|
|
$this->recaptcha_invisible->htmlFormSnippet(); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
*/ |
93
|
|
|
public function testDefaultCurlTimeout() { |
94
|
|
|
|
95
|
|
|
$this->assertEquals($this->recaptcha_invisible->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT); |
96
|
|
|
$this->assertEquals($this->recaptcha_v2->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT); |
97
|
|
|
$this->assertEquals($this->recaptcha_v3->getCurlTimeout(), ReCaptchaBuilder::DEFAULT_CURL_TIMEOUT); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @test |
102
|
|
|
* @expectedException \Error |
103
|
|
|
*/ |
104
|
|
|
public function testReCaptchaV2htmlFormButtonShouldThrowError() { |
105
|
|
|
|
106
|
|
|
$this->recaptcha_v2->htmlFormButton(); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function setUp(): void { |
110
|
|
|
|
111
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub |
112
|
|
|
|
113
|
|
|
$this->recaptcha_invisible = new ReCaptchaBuilderInvisible('api_site_key', 'api_secret_key'); |
114
|
|
|
$this->recaptcha_v2 = new ReCaptchaBuilderV2('api_site_key', 'api_secret_key'); |
115
|
|
|
$this->recaptcha_v3 = new ReCaptchaBuilderV3('api_site_key', 'api_secret_key'); |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |