1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
6
|
|
|
use SilverStripe\Forms\CurrencyField; |
7
|
|
|
use SilverStripe\Forms\DateField; |
8
|
|
|
use SilverStripe\Forms\NumericField; |
9
|
|
|
use SilverStripe\Forms\Schema\FormSchema; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Forms\Form; |
13
|
|
|
use SilverStripe\Forms\TextField; |
14
|
|
|
use SilverStripe\Forms\RequiredFields; |
15
|
|
|
use SilverStripe\Forms\FormAction; |
16
|
|
|
use SilverStripe\Forms\PopoverField; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @skipUpgrade |
20
|
|
|
*/ |
21
|
|
|
class FormSchemaTest extends SapphireTest |
22
|
|
|
{ |
23
|
|
|
protected function setUp(): void |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
|
27
|
|
|
// Clear old messages |
28
|
|
|
$session = Controller::curr()->getRequest()->getSession(); |
29
|
|
|
$session |
30
|
|
|
->clear("FormInfo.TestForm.result") |
31
|
|
|
->clear("FormInfo.TestForm.data"); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetSchema() |
35
|
|
|
{ |
36
|
|
|
$form = new Form(null, 'TestForm', new FieldList(), new FieldList()); |
37
|
|
|
$formSchema = new FormSchema(); |
38
|
|
|
$expected = json_decode(file_get_contents(__DIR__ . '/FormSchemaTest/testGetSchema.json'), true); |
39
|
|
|
|
40
|
|
|
$schema = $formSchema->getSchema($form); |
41
|
|
|
$this->assertInternalType('array', ($schema); |
|
|
|
|
42
|
|
|
$this->assertEquals($expected, $schema); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testGetState() |
46
|
|
|
{ |
47
|
|
|
$form = new Form(null, 'TestForm', new FieldList(), new FieldList()); |
48
|
|
|
$formSchema = new FormSchema(); |
49
|
|
|
$expected = [ |
50
|
|
|
'id' => 'Form_TestForm', |
51
|
|
|
'fields' => [ |
52
|
|
|
[ |
53
|
|
|
'id' => 'Form_TestForm_SecurityID', |
54
|
|
|
'value' => $form->getSecurityToken()->getValue(), |
55
|
|
|
'message' => null, |
56
|
|
|
'data' => [], |
57
|
|
|
'name' => 'SecurityID', |
58
|
|
|
] |
59
|
|
|
], |
60
|
|
|
'messages' => [], |
61
|
|
|
'notifyUnsavedChanges' => false |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
$state = $formSchema->getState($form); |
65
|
|
|
$this->assertInternalType('array', ($state); |
66
|
|
|
$this->assertEquals($expected, $state); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testGetStateWithFormMessages() |
70
|
|
|
{ |
71
|
|
|
$fields = new FieldList(); |
72
|
|
|
$actions = new FieldList(); |
73
|
|
|
$form = new Form(null, 'TestForm', $fields, $actions); |
74
|
|
|
$form->sessionMessage('All saved', 'good'); |
75
|
|
|
$formSchema = new FormSchema(); |
76
|
|
|
$expected = [ |
77
|
|
|
'id' => 'Form_TestForm', |
78
|
|
|
'fields' => [ |
79
|
|
|
[ |
80
|
|
|
'id' => 'Form_TestForm_SecurityID', |
81
|
|
|
'value' => $form->getSecurityToken()->getValue(), |
82
|
|
|
'data' => [], |
83
|
|
|
'message' => null, |
84
|
|
|
'name' => 'SecurityID', |
85
|
|
|
] |
86
|
|
|
], |
87
|
|
|
'messages' => [[ |
88
|
|
|
'value' => 'All saved', |
89
|
|
|
'type' => 'good' |
90
|
|
|
]], |
91
|
|
|
'notifyUnsavedChanges' => false |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
$state = $formSchema->getState($form); |
95
|
|
|
$this->assertInternalType('array', ($state); |
96
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testGetStateWithFieldValidationErrors() |
100
|
|
|
{ |
101
|
|
|
$fields = new FieldList(new TextField('Title')); |
102
|
|
|
$actions = new FieldList(); |
103
|
|
|
$validator = new RequiredFields('Title'); |
104
|
|
|
$form = new Form(null, 'TestForm', $fields, $actions, $validator); |
105
|
|
|
$form->clearMessage(); |
106
|
|
|
$form->loadDataFrom( |
107
|
|
|
[ |
108
|
|
|
'Title' => null, |
109
|
|
|
] |
110
|
|
|
); |
111
|
|
|
$this->assertFalse($form->validationResult()->isValid()); |
112
|
|
|
$formSchema = new FormSchema(); |
113
|
|
|
$expected = [ |
114
|
|
|
'id' => 'Form_TestForm', |
115
|
|
|
'fields' => [ |
116
|
|
|
[ |
117
|
|
|
'id' => 'Form_TestForm_Title', |
118
|
|
|
'value' => null, |
119
|
|
|
'message' => [ |
120
|
|
|
'value' => '"Title" is required', |
121
|
|
|
'type' => 'required' |
122
|
|
|
], |
123
|
|
|
'data' => [], |
124
|
|
|
'name' => 'Title', |
125
|
|
|
], |
126
|
|
|
[ |
127
|
|
|
'id' => 'Form_TestForm_SecurityID', |
128
|
|
|
'value' => $form->getSecurityToken()->getValue(), |
129
|
|
|
'message' => null, |
130
|
|
|
'data' => [], |
131
|
|
|
'name' => 'SecurityID', |
132
|
|
|
] |
133
|
|
|
], |
134
|
|
|
'messages' => [], |
135
|
|
|
'notifyUnsavedChanges' => false |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
$state = $formSchema->getState($form); |
139
|
|
|
$this->assertInternalType('array', ($state); |
140
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @skipUpgrade |
145
|
|
|
*/ |
146
|
|
|
public function testGetNestedSchema() |
147
|
|
|
{ |
148
|
|
|
$form = new Form( |
149
|
|
|
null, |
150
|
|
|
'TestForm', |
151
|
|
|
new FieldList(new TextField("Name")), |
152
|
|
|
new FieldList( |
153
|
|
|
(new FormAction("save", "Save")) |
154
|
|
|
->setIcon('save'), |
155
|
|
|
(new FormAction("cancel", "Cancel")) |
156
|
|
|
->setUseButtonTag(true), |
157
|
|
|
$pop = new PopoverField( |
158
|
|
|
"More options", |
159
|
|
|
[ |
160
|
|
|
new FormAction("publish", "Publish record"), |
161
|
|
|
new FormAction("archive", "Archive"), |
162
|
|
|
] |
163
|
|
|
) |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
$formSchema = new FormSchema(); |
167
|
|
|
$expected = json_decode(file_get_contents(__DIR__ . '/FormSchemaTest/testGetNestedSchema.json'), true); |
168
|
|
|
$schema = $formSchema->getSchema($form); |
169
|
|
|
|
170
|
|
|
$this->assertInternalType('array', ($schema); |
171
|
|
|
$this->assertEquals($expected, $schema); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Test that schema is merged correctly |
176
|
|
|
*/ |
177
|
|
|
public function testMergeSchema() |
178
|
|
|
{ |
179
|
|
|
$publishAction = FormAction::create('publish', 'Publish'); |
180
|
|
|
$publishAction->setIcon('save'); |
181
|
|
|
$publishAction->setSchemaData(['data' => ['buttonStyle' => 'primary']]); |
182
|
|
|
$schema = $publishAction->getSchemaData(); |
183
|
|
|
$this->assertEquals( |
184
|
|
|
[ |
185
|
|
|
'icon' => 'save', |
186
|
|
|
'buttonStyle' => 'primary', |
187
|
|
|
], |
188
|
|
|
$schema['data'] |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testSchemaValidation() |
193
|
|
|
{ |
194
|
|
|
$form = new Form( |
195
|
|
|
null, |
196
|
|
|
'TestForm', |
197
|
|
|
new FieldList( |
198
|
|
|
TextField::create("Name") |
199
|
|
|
->setMaxLength(40), |
200
|
|
|
new DateField("Date"), |
201
|
|
|
new NumericField("Number"), |
202
|
|
|
new CurrencyField("Money") |
203
|
|
|
), |
204
|
|
|
new FieldList(), |
205
|
|
|
new RequiredFields('Name') |
206
|
|
|
); |
207
|
|
|
$formSchema = new FormSchema(); |
208
|
|
|
$schema = $formSchema->getSchema($form); |
209
|
|
|
$expected = json_decode(file_get_contents(__DIR__ . '/FormSchemaTest/testSchemaValidation.json'), true); |
210
|
|
|
$this->assertInternalType('array', ($schema); |
211
|
|
|
$this->assertEquals($expected, $schema); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|