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