1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\SpamProtection\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Forms\Form; |
10
|
|
|
use SilverStripe\Forms\TextField; |
11
|
|
|
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension; |
12
|
|
|
use SilverStripe\SpamProtection\Tests\Stub\FooProtector; |
13
|
|
|
use SilverStripe\SpamProtection\Tests\Stub\BarProtector; |
14
|
|
|
use SilverStripe\SpamProtection\Tests\Stub\BazProtector; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @package spamprotection |
18
|
|
|
*/ |
19
|
|
|
class FormSpamProtectionExtensionTest extends SapphireTest |
20
|
|
|
{ |
21
|
|
|
protected $usesDatabase = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Form |
25
|
|
|
*/ |
26
|
|
|
protected $form = null; |
27
|
|
|
|
28
|
|
|
protected function setUp() |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
$this->form = new Form(new Controller, 'Form', new FieldList( |
33
|
|
|
new TextField('Title'), |
34
|
|
|
new TextField('Comment'), |
35
|
|
|
new TextField('URL') |
36
|
|
|
), new FieldList()); |
37
|
|
|
|
38
|
|
|
$this->form->disableSecurityToken(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testEnableSpamProtection() |
42
|
|
|
{ |
43
|
|
|
Config::modify()->set( |
44
|
|
|
FormSpamProtectionExtension::class, |
45
|
|
|
'default_spam_protector', |
46
|
|
|
FooProtector::class |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$form = $this->form->enableSpamProtection(); |
50
|
|
|
|
51
|
|
|
$this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testEnableSpamProtectionCustomProtector() |
55
|
|
|
{ |
56
|
|
|
$form = $this->form->enableSpamProtection(array( |
57
|
|
|
'protector' => BarProtector::class |
58
|
|
|
)); |
59
|
|
|
|
60
|
|
|
$this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testEnableSpamProtectionCustomTitle() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$form = $this->form->enableSpamProtection(array( |
66
|
|
|
'protector' => BarProtector::class, |
67
|
|
|
'title' => 'Baz', |
68
|
|
|
)); |
69
|
|
|
|
70
|
|
|
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function testCustomOptions() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$form = $this->form->enableSpamProtection(array( |
76
|
|
|
'protector' => BazProtector::class, |
77
|
|
|
'title' => 'Qux', |
78
|
|
|
'name' => 'Borris' |
79
|
|
|
)); |
80
|
|
|
|
81
|
|
|
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testConfigurableName() |
85
|
|
|
{ |
86
|
|
|
$field_name = "test_configurable_name"; |
87
|
|
|
Config::modify()->set( |
88
|
|
|
FormSpamProtectionExtension::class, |
89
|
|
|
'default_spam_protector', |
90
|
|
|
FooProtector::class |
91
|
|
|
); |
92
|
|
|
Config::modify()->set( |
93
|
|
|
FormSpamProtectionExtension::class, |
94
|
|
|
'field_name', |
95
|
|
|
$field_name |
96
|
|
|
); |
97
|
|
|
$form = $this->form->enableSpamProtection(); |
98
|
|
|
// remove for subsequent tests |
99
|
|
|
Config::modify()->remove(FormSpamProtectionExtension::class, 'field_name'); |
100
|
|
|
// field should take up configured name |
101
|
|
|
$this->assertEquals('Foo', $form->Fields()->fieldByName($field_name)->Title()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function testInsertBefore() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$form = $this->form->enableSpamProtection(array( |
107
|
|
|
'protector' => FooProtector::class, |
108
|
|
|
'insertBefore' => 'URL' |
109
|
|
|
)); |
110
|
|
|
|
111
|
|
|
$fields = $form->Fields(); |
112
|
|
|
$this->assertEquals('Title', $fields[0]->Title()); |
113
|
|
|
$this->assertEquals('Comment', $fields[1]->Title()); |
114
|
|
|
$this->assertEquals('Foo', $fields[2]->Title()); |
115
|
|
|
$this->assertEquals('URL', $fields[3]->Title()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
View Code Duplication |
public function testInsertBeforeMissing() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$form = $this->form->enableSpamProtection(array( |
121
|
|
|
'protector' => FooProtector::class, |
122
|
|
|
'insertBefore' => 'NotAField' |
123
|
|
|
)); |
124
|
|
|
|
125
|
|
|
// field should default to the end instead |
126
|
|
|
$fields = $form->Fields(); |
127
|
|
|
$this->assertEquals('Title', $fields[0]->Title()); |
128
|
|
|
$this->assertEquals('Comment', $fields[1]->Title()); |
129
|
|
|
$this->assertEquals('URL', $fields[2]->Title()); |
130
|
|
|
$this->assertEquals('Foo', $fields[3]->Title()); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.