Completed
Push — master ( d19955...af891e )
by Sam
22s
created

testGetStateWithFieldValidationErrors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
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' => 'Form_TestForm',
13
			'action' => 'Controller/TestForm',
14
			'method' => 'POST',
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
					'id' => 'Form_TestForm_SecurityID',
28
					'name' => 'SecurityID',
29
					'type' => "Hidden",
30
					'component' => null,
31
					'holder_id' => 'Form_TestForm_SecurityID_Holder',
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' => 'Form_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' => 'Form_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' => 'Form_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
	public function testGetNestedSchema() {
144
		$form = new Form(
145
			new Controller(),
146
			'TestForm',
147
			new FieldList(new TextField("Name")),
148
			new FieldList(
149
				(new FormAction("save", "Save"))
150
					->setIcon('save'),
151
				(new FormAction("cancel", "Cancel"))
152
					->setUseButtonTag(true),
153
				new PopoverField("More options", [
154
					new FormAction("publish", "Publish record"),
155
					new FormAction("archive", "Archive"),
156
				])
157
			)
158
		);
159
		$formSchema = new FormSchema();
160
		$expected = [
161
			'name' => 'TestForm',
162
			'id' => 'Form_TestForm',
163
			'action' => 'Controller/TestForm',
164
			'method' => 'POST',
165
			'schema_url' => '',
166
			'attributes' => [
167
				'id' => 'Form_TestForm',
168
				'action' => 'Controller/TestForm',
169
				'method' => 'POST',
170
				'enctype' => 'application/x-www-form-urlencoded',
171
				'target' => null,
172
				'class' => ''
173
			],
174
			'data' => [],
175
			'fields' => [
176
				[
177
					'id' => 'Form_TestForm_Name',
178
					'name' => 'Name',
179
					'type' => 'Text',
180
				 	'component' => null,
181
				 	'holder_id' => 'Form_TestForm_Name_Holder',
182
					'title' => 'Name',
183
					'source' => null,
184
					'extraClass' => 'text',
185
					'description' => null,
186
					'rightTitle' => null,
187
					'leftTitle' => null,
188
					'readOnly' => false,
189
					'disabled' => false,
190
					'customValidationMessage' => '',
191
					'attributes' => [],
192
					'data' => [],
193
				],
194
				[
195
					'id' => 'Form_TestForm_SecurityID',
196
					'name' => 'SecurityID',
197
					'type' => "Hidden",
198
					'component' => null,
199
					'holder_id' => 'Form_TestForm_SecurityID_Holder',
200
					'title' => 'Security ID',
201
					'source' => null,
202
					'extraClass' => 'hidden',
203
					'description' => null,
204
					'rightTitle' => null,
205
					'leftTitle' => null,
206
					'readOnly' => false,
207
					'disabled' => false,
208
					'customValidationMessage' => '',
209
					'attributes' => [],
210
					'data' => []
211
				],
212
			],
213
			'actions' => [
214
				[
215
					'id' => 'Form_TestForm_action_save',
216
					'title' => 'Save',
217
					'name' => 'action_save',
218
					'type' => null,
219
					'component' => 'FormAction',
220
					'holder_id' => 'Form_TestForm_action_save_Holder',
221
					'source' => null,
222
					'extraClass' => 'action',
223
					'description' => null,
224
					'rightTitle' => null,
225
					'leftTitle' => null,
226
					'readOnly' => false,
227
					'disabled' => false,
228
					'customValidationMessage' => '',
229
					'attributes' => [
230
						'type' => 'submit',
231
					],
232
					'data' => [
233
						'icon' => 'save',
234
					],
235
				],
236
				[
237
					'id' => 'Form_TestForm_action_cancel',
238
					'title' => 'Cancel',
239
					'name' => 'action_cancel',
240
					'type' => null,
241
					'component' => 'FormAction',
242
					'holder_id' => 'Form_TestForm_action_cancel_Holder',
243
					'source' => null,
244
					'extraClass' => 'action',
245
					'description' => null,
246
					'rightTitle' => null,
247
					'leftTitle' => null,
248
					'readOnly' => false,
249
					'disabled' => false,
250
					'customValidationMessage' => '',
251
					'attributes' => [
252
						'type' => 'button'
253
					],
254
					'data' => [
255
						'icon' => null
256
					],
257
				],
258
				[
259
					'id' => 'Form_TestForm_Moreoptions',
260
					'title' => 'More options',
261
					'name' => 'Moreoptions',
262
					'type' => 'Structural',
263
					'component' => 'PopoverField',
264
					'holder_id' => 'Form_TestForm_Moreoptions_Holder',
265
					'source' => null,
266
					'extraClass' => 'field CompositeField popover',
267
					'description' => null,
268
					'rightTitle' => null,
269
					'leftTitle' => null,
270
					'readOnly' => null,
271
					'disabled' => false,
272
					'customValidationMessage' => '',
273
					'attributes' => [],
274
					'data' => [],
275
					'children' => [
276
						[
277
							'id' => 'Form_TestForm_action_publish',
278
							'title' => 'Publish record',
279
							'name' => 'action_publish',
280
							'type' => null,
281
							'component' => 'FormAction',
282
							'holder_id' => 'Form_TestForm_action_publish_Holder',
283
							'source' => null,
284
							'extraClass' => 'action',
285
							'description' => null,
286
							'rightTitle' => null,
287
							'leftTitle' => null,
288
							'readOnly' => false,
289
							'disabled' => false,
290
							'customValidationMessage' => '',
291
							'attributes' => [
292
								'type' => 'submit',
293
							],
294
							'data' => [
295
								'icon' => null,
296
							],
297
						],
298
						[
299
							'id' => 'Form_TestForm_action_archive',
300
							'title' => 'Archive',
301
							'name' => 'action_archive',
302
							'type' => null,
303
							'component' => 'FormAction',
304
							'holder_id' => 'Form_TestForm_action_archive_Holder',
305
							'source' => null,
306
							'extraClass' => 'action',
307
							'description' => null,
308
							'rightTitle' => null,
309
							'leftTitle' => null,
310
							'readOnly' => false,
311
							'disabled' => false,
312
							'customValidationMessage' => '',
313
							'attributes' => [
314
								'type' => 'submit',
315
							],
316
							'data' => [
317
								'icon' => null,
318
							],
319
						],
320
					]
321
				]
322
			]
323
		];
324
325
		$schema = $formSchema->getSchema($form);
326
327
		$this->assertInternalType('array', $schema);
328
		$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($schema));
329
	}
330
}
331