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
|
|
|
|
28
|
|
|
public function testEnableSpamProtection() |
29
|
|
|
{ |
30
|
|
|
Config::inst()->update( |
31
|
|
|
'FormSpamProtectionExtension', 'default_spam_protector', |
32
|
|
|
'FormSpamProtectionExtensionTest_FooProtector' |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$form = $this->form->enableSpamProtection(); |
36
|
|
|
|
37
|
|
|
$this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title()); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testEnableSpamProtectionCustomProtector() |
41
|
|
|
{ |
42
|
|
|
$form = $this->form->enableSpamProtection(array( |
43
|
|
|
'protector' => 'FormSpamProtectionExtensionTest_BarProtector' |
44
|
|
|
)); |
45
|
|
|
|
46
|
|
|
$this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title()); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testEnableSpamProtectionCustomTitle() |
50
|
|
|
{ |
51
|
|
|
$form = $this->form->enableSpamProtection(array( |
52
|
|
|
'protector' => 'FormSpamProtectionExtensionTest_BarProtector', |
53
|
|
|
'title' => 'Baz', |
54
|
|
|
)); |
55
|
|
|
|
56
|
|
|
$this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testCustomOptions() |
60
|
|
|
{ |
61
|
|
|
$form = $this->form->enableSpamProtection(array( |
62
|
|
|
'protector' => 'FormSpamProtectionExtensionTest_BazProtector', |
63
|
|
|
'title' => 'Qux', |
64
|
|
|
'name' => 'Borris', |
65
|
|
|
'righttitle' => 'Lipsum' |
66
|
|
|
)); |
67
|
|
|
|
68
|
|
|
$formfield = $form->Fields()->fieldByName('Borris'); |
69
|
|
|
$this->assertEquals('Qux', $formfield->Title()); |
|
|
|
|
70
|
|
|
$this->assertEquals('Lipsum', $formfield->RightTitle()); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function testInsertBefore() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$form = $this->form->enableSpamProtection(array( |
76
|
|
|
'protector' => 'FormSpamProtectionExtensionTest_FooProtector', |
77
|
|
|
'insertBefore' => 'URL' |
78
|
|
|
)); |
79
|
|
|
|
80
|
|
|
$fields = $form->Fields(); |
81
|
|
|
$this->assertEquals('Title', $fields[0]->Title()); |
|
|
|
|
82
|
|
|
$this->assertEquals('Comment', $fields[1]->Title()); |
|
|
|
|
83
|
|
|
$this->assertEquals('Foo', $fields[2]->Title()); |
|
|
|
|
84
|
|
|
$this->assertEquals('URL', $fields[3]->Title()); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
public function testInsertBeforeMissing() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$form = $this->form->enableSpamProtection(array( |
90
|
|
|
'protector' => 'FormSpamProtectionExtensionTest_FooProtector', |
91
|
|
|
'insertBefore' => 'NotAField' |
92
|
|
|
)); |
93
|
|
|
|
94
|
|
|
// field should default to the end instead |
95
|
|
|
$fields = $form->Fields(); |
96
|
|
|
$this->assertEquals('Title', $fields[0]->Title()); |
|
|
|
|
97
|
|
|
$this->assertEquals('Comment', $fields[1]->Title()); |
|
|
|
|
98
|
|
|
$this->assertEquals('URL', $fields[2]->Title()); |
|
|
|
|
99
|
|
|
$this->assertEquals('Foo', $fields[3]->Title()); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @package spamprotection |
105
|
|
|
*/ |
106
|
|
|
class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
protected $options = array(); |
109
|
|
|
|
110
|
|
|
public function __construct($options = array()) |
111
|
|
|
{ |
112
|
|
|
$this->options = $options; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getFormField($name = null, $title = null, $value = null) |
116
|
|
|
{ |
117
|
|
|
$field = new TextField($name, $title, $value); |
118
|
|
|
if(isset($this->options['righttitle'])) { |
119
|
|
|
$field->setRightTitle($this->options['righttitle']); |
120
|
|
|
} |
121
|
|
|
return $field; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setFieldMapping($fieldMapping) |
125
|
|
|
{ |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @package spamprotection |
131
|
|
|
*/ |
132
|
|
|
class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
public function __construct($options = array()) |
135
|
|
|
{ |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getFormField($name = null, $title = null, $value = null) |
139
|
|
|
{ |
140
|
|
|
$title = $title ?: 'Bar'; |
141
|
|
|
return new TextField($name, $title, $value); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function setFieldMapping($fieldMapping) |
145
|
|
|
{ |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @package spamprotection |
151
|
|
|
*/ |
152
|
|
|
class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
public function __construct($options = array()) |
155
|
|
|
{ |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getFormField($name = null, $title = null, $value = null) |
159
|
|
|
{ |
160
|
|
|
return new TextField($name, 'Foo', $value); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function setFieldMapping($fieldMapping) |
164
|
|
|
{ |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
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.