1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class EditableSpamProtectionFieldTest extends SapphireTest |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
protected $usesDatabase = true; |
7
|
|
|
|
8
|
|
|
public function setUp() |
9
|
|
|
{ |
10
|
|
|
parent::setUp(); |
11
|
|
|
|
12
|
|
|
if (!class_exists('EditableSpamProtectionField')) { |
13
|
|
|
$this->markTestSkipped('"userforms" module not installed'); |
|
|
|
|
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
Config::inst()->update( |
17
|
|
|
'FormSpamProtectionExtension', |
18
|
|
|
'default_spam_protector', |
19
|
|
|
'EditableSpamProtectionFieldTest_Protector' |
20
|
|
|
); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testValidateFieldDoesntAddErrorOnSuccess() |
24
|
|
|
{ |
25
|
|
|
$formMock = $this->getFormMock(); |
26
|
|
|
$formFieldMock = $this->getEditableFormFieldMock(); |
27
|
|
|
|
28
|
|
|
$formFieldMock |
29
|
|
|
->getFormField() // mock |
30
|
|
|
->expects($this->once()) |
|
|
|
|
31
|
|
|
->method('validate') |
32
|
|
|
->will($this->returnValue(true)); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$formMock |
35
|
|
|
->expects($this->never()) |
|
|
|
|
36
|
|
|
->method('addErrorMessage'); |
37
|
|
|
|
38
|
|
|
$formFieldMock->validateField(array('MyField' => null), $formMock); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testValidateFieldAddsErrorFromField() |
42
|
|
|
{ |
43
|
|
|
$formMock = $this->getFormMock(); |
44
|
|
|
$formFieldMock = $this->getEditableFormFieldMock(); |
45
|
|
|
|
46
|
|
|
$formFieldMock |
47
|
|
|
->getFormField() // mock |
48
|
|
|
->expects($this->once()) |
|
|
|
|
49
|
|
|
->method('validate') |
50
|
|
|
->will($this->returnValue(false)); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$formMock->getValidator()->validationError('MyField', 'some field message', 'required'); |
53
|
|
|
|
54
|
|
|
$formMock |
55
|
|
|
->expects($this->once()) |
|
|
|
|
56
|
|
|
->method('addErrorMessage') |
57
|
|
|
->with($this->anything(), $this->stringContains('some field message'), $this->anything(), $this->anything()); |
|
|
|
|
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
$formFieldMock->validateField(array('MyField' => null), $formMock); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testValidateFieldAddsDefaultError() |
64
|
|
|
{ |
65
|
|
|
$formMock = $this->getFormMock(); |
66
|
|
|
$formFieldMock = $this->getEditableFormFieldMock(); |
67
|
|
|
|
68
|
|
|
$formFieldMock |
69
|
|
|
->getFormField() // mock |
70
|
|
|
->expects($this->once()) |
|
|
|
|
71
|
|
|
->method('validate') |
72
|
|
|
->will($this->returnValue(false)); |
|
|
|
|
73
|
|
|
|
74
|
|
|
// field doesn't set any validation errors here |
75
|
|
|
|
76
|
|
|
$formMock |
77
|
|
|
->expects($this->once()) |
|
|
|
|
78
|
|
|
->method('addErrorMessage') |
79
|
|
|
->with($this->anything(), $this->stringContains('default error message'), $this->anything(), $this->anything()); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$formFieldMock->validateField(array('MyField' => null), $formMock); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testSpamConfigurationShowsInCms() |
85
|
|
|
{ |
86
|
|
|
$field = $this->getEditableFormFieldMock(); |
87
|
|
|
$fields = $field->getCMSFields(); |
88
|
|
|
|
89
|
|
|
$this->assertInstanceOf('FieldGroup', $fields->fieldByName('Root.Main.SpamFieldMapping')); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testSpamMapSettingsAreSerialised() |
93
|
|
|
{ |
94
|
|
|
$field = $this->getEditableFormFieldMock(); |
95
|
|
|
$field->SpamFieldSettings = json_encode(array('foo' => 'bar', 'bar' => 'baz')); |
96
|
|
|
$field->write(); |
97
|
|
|
|
98
|
|
|
$this->assertJson($field->SpamFieldSettings); |
|
|
|
|
99
|
|
|
$this->assertSame('bar', $field->spamMapValue('foo')); |
|
|
|
|
100
|
|
|
$this->assertSame('baz', $field->spamMapValue('bar')); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function getFormMock() |
104
|
|
|
{ |
105
|
|
|
$formMock = $this->getMockBuilder('Form', array('addErrorMessage')) |
|
|
|
|
106
|
|
|
->disableOriginalConstructor() |
107
|
|
|
->getMock(); |
108
|
|
|
$formMock |
109
|
|
|
->expects($this->any()) |
|
|
|
|
110
|
|
|
->method('getValidator') |
111
|
|
|
->will($this->returnValue(new RequiredFields())); |
|
|
|
|
112
|
|
|
|
113
|
|
|
return $formMock; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function getEditableFormFieldMock() |
117
|
|
|
{ |
118
|
|
|
$page = new UserDefinedForm(); |
119
|
|
|
$page->write(); |
120
|
|
|
|
121
|
|
|
$formFieldMock = $this->getMockBuilder('TextField') |
|
|
|
|
122
|
|
|
->disableOriginalConstructor() |
123
|
|
|
->getMock(); |
124
|
|
|
|
125
|
|
|
$editableFormFieldMock = new EditableSpamProtectionField(array( |
126
|
|
|
'ParentID' => $page->ID, |
127
|
|
|
'Name' => 'MyField', |
128
|
|
|
'CustomErrorMessage' => 'default error message' |
129
|
|
|
)); |
130
|
|
|
$editableFormFieldMock->write(); |
131
|
|
|
$editableFormFieldMock->setFormField($formFieldMock); |
132
|
|
|
|
133
|
|
|
return $editableFormFieldMock; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.