1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package spamprotection |
5
|
|
|
*/ |
6
|
|
|
class FormSpamProtectionExtensionTest extends SapphireTest |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
protected $usesDatabase = false; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var Form |
12
|
|
|
*/ |
13
|
|
|
protected $form = null; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
parent::setUp(); |
18
|
|
|
|
19
|
|
|
$this->form = new Form($this, 'Form', new FieldList( |
|
|
|
|
20
|
|
|
new TextField('Title'), |
21
|
|
|
new TextField('Comment'), |
22
|
|
|
new TextField('URL') |
23
|
|
|
), new FieldList() |
24
|
|
|
); |
25
|
|
|
$this->form->disableSecurityToken(); |
26
|
|
|
|
27
|
|
|
//for the tests, ignore any field_name value set in config |
28
|
|
|
Config::inst()->remove('FormSpamProtectionExtension', 'field_name'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testEnableSpamProtection() |
32
|
|
|
{ |
33
|
|
|
Config::inst()->update( |
34
|
|
|
'FormSpamProtectionExtension', 'default_spam_protector', |
35
|
|
|
'FormSpamProtectionExtensionTest_FooProtector' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
$form = $this->form->enableSpamProtection(); |
39
|
|
|
|
40
|
|
|
$this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title()); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testEnableSpamProtectionCustomProtector() |
44
|
|
|
{ |
45
|
|
|
$form = $this->form->enableSpamProtection(array( |
46
|
|
|
'protector' => 'FormSpamProtectionExtensionTest_BarProtector' |
47
|
|
|
)); |
48
|
|
|
|
49
|
|
|
$this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title()); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function testEnableSpamProtectionCustomTitle() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$form = $this->form->enableSpamProtection(array( |
55
|
|
|
'protector' => 'FormSpamProtectionExtensionTest_BarProtector', |
56
|
|
|
'title' => 'Baz', |
57
|
|
|
)); |
58
|
|
|
|
59
|
|
|
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title()); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
public function testCustomOptions() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$form = $this->form->enableSpamProtection(array( |
65
|
|
|
'protector' => 'FormSpamProtectionExtensionTest_BazProtector', |
66
|
|
|
'title' => 'Qux', |
67
|
|
|
'name' => 'Borris' |
68
|
|
|
)); |
69
|
|
|
|
70
|
|
|
$this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title()); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testConfigurableName() |
74
|
|
|
{ |
75
|
|
|
$field_name = "test_configurable_name"; |
76
|
|
|
Config::inst()->update( |
77
|
|
|
'FormSpamProtectionExtension', 'default_spam_protector', |
78
|
|
|
'FormSpamProtectionExtensionTest_FooProtector' |
79
|
|
|
); |
80
|
|
|
Config::inst()->update( |
81
|
|
|
'FormSpamProtectionExtension', 'field_name', |
82
|
|
|
$field_name |
83
|
|
|
); |
84
|
|
|
$form = $this->form->enableSpamProtection(); |
85
|
|
|
// remove for subsequent tests |
86
|
|
|
Config::inst()->remove('FormSpamProtectionExtension', 'field_name'); |
87
|
|
|
// field should take up configured name |
88
|
|
|
$this->assertEquals('Foo', $form->Fields()->fieldByName($field_name)->Title()); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
View Code Duplication |
public function testInsertBefore() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$form = $this->form->enableSpamProtection(array( |
94
|
|
|
'protector' => 'FormSpamProtectionExtensionTest_FooProtector', |
95
|
|
|
'insertBefore' => 'URL' |
96
|
|
|
)); |
97
|
|
|
|
98
|
|
|
$fields = $form->Fields(); |
99
|
|
|
$this->assertEquals('Title', $fields[0]->Title()); |
|
|
|
|
100
|
|
|
$this->assertEquals('Comment', $fields[1]->Title()); |
|
|
|
|
101
|
|
|
$this->assertEquals('Foo', $fields[2]->Title()); |
|
|
|
|
102
|
|
|
$this->assertEquals('URL', $fields[3]->Title()); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
public function testInsertBeforeMissing() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$form = $this->form->enableSpamProtection(array( |
108
|
|
|
'protector' => 'FormSpamProtectionExtensionTest_FooProtector', |
109
|
|
|
'insertBefore' => 'NotAField' |
110
|
|
|
)); |
111
|
|
|
|
112
|
|
|
// field should default to the end instead |
113
|
|
|
$fields = $form->Fields(); |
114
|
|
|
$this->assertEquals('Title', $fields[0]->Title()); |
|
|
|
|
115
|
|
|
$this->assertEquals('Comment', $fields[1]->Title()); |
|
|
|
|
116
|
|
|
$this->assertEquals('URL', $fields[2]->Title()); |
|
|
|
|
117
|
|
|
$this->assertEquals('Foo', $fields[3]->Title()); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @package spamprotection |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
public function getFormField($name = null, $title = null, $value = null) |
127
|
|
|
{ |
128
|
|
|
return new TextField($name, $title, $value); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function setFieldMapping($fieldMapping) |
132
|
|
|
{ |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @package spamprotection |
138
|
|
|
*/ |
139
|
|
View Code Duplication |
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
public function getFormField($name = null, $title = null, $value = null) |
142
|
|
|
{ |
143
|
|
|
$title = $title ?: 'Bar'; |
144
|
|
|
return new TextField($name, $title, $value); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function setFieldMapping($fieldMapping) |
148
|
|
|
{ |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @package spamprotection |
154
|
|
|
*/ |
155
|
|
View Code Duplication |
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
public function getFormField($name = null, $title = null, $value = null) |
158
|
|
|
{ |
159
|
|
|
return new TextField($name, 'Foo', $value); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function setFieldMapping($fieldMapping) |
163
|
|
|
{ |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.