These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | View Code Duplication | 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 | View Code Duplication | public function testCustomOptions() |
|
60 | { |
||
61 | $form = $this->form->enableSpamProtection(array( |
||
62 | 'protector' => 'FormSpamProtectionExtensionTest_BazProtector', |
||
63 | 'title' => 'Qux', |
||
64 | 'name' => 'Borris' |
||
65 | )); |
||
66 | |||
67 | $this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title()); |
||
68 | } |
||
69 | |||
70 | public function testConfigurableName() |
||
71 | { |
||
72 | $field_name = "test_configurable_name"; |
||
73 | Config::inst()->update( |
||
74 | 'FormSpamProtectionExtension', 'default_spam_protector', |
||
75 | 'FormSpamProtectionExtensionTest_FooProtector' |
||
76 | ); |
||
77 | Config::inst()->update( |
||
78 | 'FormSpamProtectionExtension', 'field_name', |
||
79 | $field_name |
||
80 | ); |
||
81 | $form = $this->form->enableSpamProtection(); |
||
82 | // remove for subsequent tests |
||
83 | Config::inst()->remove('FormSpamProtectionExtension', 'field_name'); |
||
84 | // field should take up configured name |
||
85 | $this->assertEquals('Foo', $form->Fields()->fieldByName($field_name)->Title()); |
||
86 | } |
||
87 | |||
88 | View Code Duplication | public function testInsertBefore() |
|
89 | { |
||
90 | $form = $this->form->enableSpamProtection(array( |
||
91 | 'protector' => 'FormSpamProtectionExtensionTest_FooProtector', |
||
92 | 'insertBefore' => 'URL' |
||
93 | )); |
||
94 | |||
95 | $fields = $form->Fields(); |
||
96 | $this->assertEquals('Title', $fields[0]->Title()); |
||
97 | $this->assertEquals('Comment', $fields[1]->Title()); |
||
98 | $this->assertEquals('Foo', $fields[2]->Title()); |
||
99 | $this->assertEquals('URL', $fields[3]->Title()); |
||
100 | } |
||
101 | |||
102 | View Code Duplication | public function testInsertBeforeMissing() |
|
103 | { |
||
104 | $form = $this->form->enableSpamProtection(array( |
||
105 | 'protector' => 'FormSpamProtectionExtensionTest_FooProtector', |
||
106 | 'insertBefore' => 'NotAField' |
||
107 | )); |
||
108 | |||
109 | // field should default to the end instead |
||
110 | $fields = $form->Fields(); |
||
111 | $this->assertEquals('Title', $fields[0]->Title()); |
||
112 | $this->assertEquals('Comment', $fields[1]->Title()); |
||
113 | $this->assertEquals('URL', $fields[2]->Title()); |
||
114 | $this->assertEquals('Foo', $fields[3]->Title()); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @package spamprotection |
||
120 | */ |
||
121 | View Code Duplication | class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly |
|
0 ignored issues
–
show
|
|||
122 | { |
||
123 | public function getFormField($name = null, $title = null, $value = null) |
||
124 | { |
||
125 | return new TextField($name, $title, $value); |
||
126 | } |
||
127 | |||
128 | public function setFieldMapping($fieldMapping) |
||
129 | { |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @package spamprotection |
||
135 | */ |
||
136 | View Code Duplication | class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly |
|
0 ignored issues
–
show
|
|||
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 | View Code Duplication | class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly |
|
0 ignored issues
–
show
|
|||
153 | { |
||
154 | public function getFormField($name = null, $title = null, $value = null) |
||
155 | { |
||
156 | return new TextField($name, 'Foo', $value); |
||
157 | } |
||
158 | |||
159 | public function setFieldMapping($fieldMapping) |
||
160 | { |
||
161 | } |
||
162 | } |
||
163 |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.