1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace X3dgoo\HCaptcha\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_Error; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Dev\FunctionalTest; |
8
|
|
|
use SilverStripe\Forms\RequiredFields; |
9
|
|
|
use X3dgoo\HCaptcha\Forms\HCaptchaField; |
10
|
|
|
|
11
|
|
|
class HCaptchaFieldTest extends FunctionalTest |
12
|
|
|
{ |
13
|
|
|
protected $usesDatabase = true; |
14
|
|
|
|
15
|
|
|
public function testCreateHCaptchaField() |
16
|
|
|
{ |
17
|
|
|
$hCaptchaField = new HCaptchaField('HCaptchaField'); |
18
|
|
|
|
19
|
|
|
$this->assertNotNull($hCaptchaField); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testFieldFailure() |
23
|
|
|
{ |
24
|
|
|
$hCaptchaField = new HCaptchaField('HCaptchaField'); |
25
|
|
|
|
26
|
|
|
$this->expectException(PHPUnit_Framework_Error::class); |
27
|
|
|
$this->expectExceptionMessage( |
28
|
|
|
'You must configure HCaptchaField.site_key and HCaptchaField.secret_key. ' . |
29
|
|
|
'You can retrieve these at https://hcaptcha.com' |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
$hCaptchaField->Field(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testField() |
36
|
|
|
{ |
37
|
|
|
Config::modify()->set(HCaptchaField::class, 'site_key', '{site-key}'); |
38
|
|
|
Config::modify()->set(HCaptchaField::class, 'secret_key', '{secret-key}'); |
39
|
|
|
|
40
|
|
|
$hCaptchaField = new HCaptchaField('HCaptchaField'); |
41
|
|
|
|
42
|
|
|
$field = $hCaptchaField->Field(); |
43
|
|
|
|
44
|
|
|
$this->assertNotNull($field); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testValidateFailure() |
48
|
|
|
{ |
49
|
|
|
Config::modify()->set(HCaptchaField::class, 'site_key', '{site-key}'); |
50
|
|
|
Config::modify()->set(HCaptchaField::class, 'secret_key', '{secret-key}'); |
51
|
|
|
|
52
|
|
|
$hCaptchaField = new HCaptchaField('HCaptchaField'); |
53
|
|
|
|
54
|
|
|
$result = $hCaptchaField->validate(new RequiredFields()); |
55
|
|
|
|
56
|
|
|
$this->assertFalse($result); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testSetSiteKey() |
60
|
|
|
{ |
61
|
|
|
$hCaptchaField = new HCaptchaField('HCaptchaField'); |
62
|
|
|
|
63
|
|
|
$hCaptchaField->setSiteKey('{new-site-key}'); |
64
|
|
|
|
65
|
|
|
$this->assertEquals('{new-site-key}', $hCaptchaField->getSiteKey()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testSetSecretKey() |
69
|
|
|
{ |
70
|
|
|
$hCaptchaField = new HCaptchaField('HCaptchaField'); |
71
|
|
|
|
72
|
|
|
$hCaptchaField->setSecretKey('{new-secret-key}'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|