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