1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SilverStripe\Forms\Schema\FormSchema; |
4
|
|
|
|
5
|
|
|
class FormSchemaTest extends SapphireTest { |
6
|
|
|
|
7
|
|
|
public function testGetSchema() { |
8
|
|
|
$form = new Form(new Controller(), 'TestForm', new FieldList(), new FieldList()); |
9
|
|
|
$formSchema = new FormSchema(); |
10
|
|
|
$expected = [ |
11
|
|
|
'name' => 'TestForm', |
12
|
|
|
'id' => null, |
13
|
|
|
'action' => null, |
14
|
|
|
'method' => '', |
15
|
|
|
'schema_url' => '', |
16
|
|
|
'attributes' => [ |
17
|
|
|
'id' => 'Form_TestForm', |
18
|
|
|
'action' => 'Controller/TestForm', |
19
|
|
|
'method' => 'POST', |
20
|
|
|
'enctype' => 'application/x-www-form-urlencoded', |
21
|
|
|
'target' => null, |
22
|
|
|
'class' => '' |
23
|
|
|
], |
24
|
|
|
'data' => [], |
25
|
|
|
'fields' => [ |
26
|
|
|
[ |
27
|
|
|
'type' => "HiddenField", |
28
|
|
|
'component' => null, |
29
|
|
|
'id' => null, |
30
|
|
|
'holder_id' => null, |
31
|
|
|
'name' => 'SecurityID', |
32
|
|
|
'title' => 'Security ID', |
33
|
|
|
'source' => null, |
34
|
|
|
'extraClass' => 'hidden', |
35
|
|
|
'description' => null, |
36
|
|
|
'rightTitle' => null, |
37
|
|
|
'leftTitle' => null, |
38
|
|
|
'readOnly' => false, |
39
|
|
|
'disabled' => false, |
40
|
|
|
'customValidationMessage' => '', |
41
|
|
|
'attributes' => [], |
42
|
|
|
'data' => [] |
43
|
|
|
], |
44
|
|
|
], |
45
|
|
|
'actions' => [] |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$schema = $formSchema->getSchema($form); |
49
|
|
|
$this->assertInternalType('array', $schema); |
50
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($schema)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testGetState() { |
54
|
|
|
$form = new Form(new Controller(), 'TestForm', new FieldList(), new FieldList()); |
55
|
|
|
$formSchema = new FormSchema(); |
56
|
|
|
$expected = [ |
57
|
|
|
'id' => 'TestForm', |
58
|
|
|
'fields' => [ |
59
|
|
|
[ |
60
|
|
|
'id' => 'Form_TestForm_SecurityID', |
61
|
|
|
'value' => $form->getSecurityToken()->getValue(), |
62
|
|
|
'messages' => [], |
63
|
|
|
'valid' => true, |
64
|
|
|
'data' => [] |
65
|
|
|
] |
66
|
|
|
], |
67
|
|
|
'messages' => [] |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$state = $formSchema->getState($form); |
71
|
|
|
$this->assertInternalType('array', $state); |
72
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testGetStateWithFormMessages() { |
76
|
|
|
$fields = new FieldList(); |
77
|
|
|
$actions = new FieldList(); |
78
|
|
|
$form = new Form(new Controller(), 'TestForm', $fields, $actions); |
79
|
|
|
$form->sessionMessage('All saved', 'good'); |
80
|
|
|
$formSchema = new FormSchema(); |
81
|
|
|
$expected = [ |
82
|
|
|
'id' => 'TestForm', |
83
|
|
|
'fields' => [ |
84
|
|
|
[ |
85
|
|
|
'id' => 'Form_TestForm_SecurityID', |
86
|
|
|
'value' => $form->getSecurityToken()->getValue(), |
87
|
|
|
'messages' => [], |
88
|
|
|
'valid' => true, |
89
|
|
|
'data' => [] |
90
|
|
|
] |
91
|
|
|
], |
92
|
|
|
'messages' => [ |
93
|
|
|
[ |
94
|
|
|
'value' => 'All saved', |
95
|
|
|
'type' => 'good' |
96
|
|
|
] |
97
|
|
|
] |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
$state = $formSchema->getState($form); |
101
|
|
|
$this->assertInternalType('array', $state); |
102
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testGetStateWithFieldValidationErrors() { |
106
|
|
|
$fields = new FieldList(new TextField('Title')); |
107
|
|
|
$actions = new FieldList(); |
108
|
|
|
$validator = new RequiredFields('Title'); |
109
|
|
|
$form = new Form(new Controller(), 'TestForm', $fields, $actions, $validator); |
110
|
|
|
$form->loadDataFrom([ |
111
|
|
|
'Title' => 'My Title' |
112
|
|
|
]); |
113
|
|
|
$validator->validationError('Title', 'Title is invalid', 'error'); |
114
|
|
|
$formSchema = new FormSchema(); |
115
|
|
|
$expected = [ |
116
|
|
|
'id' => 'TestForm', |
117
|
|
|
'fields' => [ |
118
|
|
|
[ |
119
|
|
|
'id' => 'Form_TestForm_Title', |
120
|
|
|
'value' => 'My Title', |
121
|
|
|
'messages' => [ |
122
|
|
|
['value' => 'Title is invalid', 'type' => 'error'] |
123
|
|
|
], |
124
|
|
|
'valid' => false, |
125
|
|
|
'data' => [] |
126
|
|
|
], |
127
|
|
|
[ |
128
|
|
|
'id' => 'Form_TestForm_SecurityID', |
129
|
|
|
'value' => $form->getSecurityToken()->getValue(), |
130
|
|
|
'messages' => [], |
131
|
|
|
'valid' => true, |
132
|
|
|
'data' => [] |
133
|
|
|
] |
134
|
|
|
], |
135
|
|
|
'messages' => [] |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
$state = $formSchema->getState($form); |
139
|
|
|
$this->assertInternalType('array', $state); |
140
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state)); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|