Completed
Push — master ( 4ee78f...ce1053 )
by Damian
14:52 queued 39s
created

FormSchemaTest::testMergeSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
use SilverStripe\Forms\CurrencyField;
4
use SilverStripe\Forms\DateField;
5
use SilverStripe\Forms\NumericField;
6
use SilverStripe\Forms\Schema\FormSchema;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\Form;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\Forms\RequiredFields;
13
use SilverStripe\Forms\FormAction;
14
use SilverStripe\Forms\PopoverField;
15
16
class FormSchemaTest extends SapphireTest {
17
18
	public function testGetSchema() {
19
		$form = new Form(new Controller(), 'TestForm', new FieldList(), new FieldList());
20
		$formSchema = new FormSchema();
21
		$expected = [
22
			'name' => 'TestForm',
23
			'id' => 'Form_TestForm',
24
			'action' => 'Controller/TestForm',
25
			'method' => 'POST',
26
			'attributes' => [
27
				'id' => 'Form_TestForm',
28
				'action' => 'Controller/TestForm',
29
				'method' => 'POST',
30
				'enctype' => 'application/x-www-form-urlencoded',
31
				'target' => null,
32
				'class' => ''
33
			],
34
			'data' => [],
35
			'fields' => [
36
				[
37
					'id' => 'Form_TestForm_SecurityID',
38
					'name' => 'SecurityID',
39
					'type' => "Hidden",
40
					'component' => null,
41
					'holderId' => 'Form_TestForm_SecurityID_Holder',
42
					'title' => 'Security ID',
43
					'source' => null,
44
					'extraClass' => 'hidden',
45
					'description' => null,
46
					'rightTitle' => null,
47
					'leftTitle' => null,
48
					'readOnly' => false,
49
					'disabled' => false,
50
					'customValidationMessage' => '',
51
					'attributes' => [],
52
					'data' => [],
53
					'validation' => [],
54
				],
55
			],
56
			'actions' => []
57
		];
58
59
		$schema = $formSchema->getSchema($form);
60
		$this->assertInternalType('array', $schema);
61
		$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($schema));
62
	}
63
64
	public function testGetState() {
65
		$form = new Form(new Controller(), 'TestForm', new FieldList(), new FieldList());
66
		$formSchema = new FormSchema();
67
		$expected = [
68
			'id' => 'Form_TestForm',
69
			'fields' => [
70
				[
71
					'id' => 'Form_TestForm_SecurityID',
72
					'value' => $form->getSecurityToken()->getValue(),
73
					'message' => null,
74
					'data' => [],
75
					'name' => 'SecurityID',
76
				]
77
			],
78
			'valid' => null,
79
			'messages' => [],
80
		];
81
82
		$state = $formSchema->getState($form);
83
		$this->assertInternalType('array', $state);
84
		$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
85
	}
86
87
	public function testGetStateWithFormMessages() {
88
		$fields = new FieldList();
89
		$actions = new FieldList();
90
		$form = new Form(new Controller(), 'TestForm', $fields, $actions);
91
		$form->sessionMessage('All saved', 'good');
92
		$formSchema = new FormSchema();
93
		$expected = [
94
			'id' => 'Form_TestForm',
95
			'fields' => [
96
				[
97
					'id' => 'Form_TestForm_SecurityID',
98
					'value' => $form->getSecurityToken()->getValue(),
99
					'data' => [],
100
					'message' => null,
101
					'name' => 'SecurityID',
102
				]
103
			],
104
			'messages' => [[
105
				'value' => ['html' => 'All saved'],
106
				'type' => 'good'
107
			]],
108
			'valid' => null,
109
		];
110
111
		$state = $formSchema->getState($form);
112
		$this->assertInternalType('array', $state);
113
		$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
114
	}
115
116
	public function testGetStateWithFieldValidationErrors() {
117
		$fields = new FieldList(new TextField('Title'));
118
		$actions = new FieldList();
119
		$validator = new RequiredFields('Title');
120
		$form = new Form(new Controller(), 'TestForm', $fields, $actions, $validator);
121
		$form->loadDataFrom([
122
			'Title' => null,
123
		]);
124
		$this->assertFalse($form->validate());
125
		$formSchema = new FormSchema();
126
		$expected = [
127
			'id' => 'Form_TestForm',
128
			'fields' => [
129
				[
130
					'id' => 'Form_TestForm_Title',
131
					'value' => null,
132
					'message' =>  [
133
						'value' => ['html' => '&quot;Title&quot; is required'],
134
						'type' => 'required'
135
					],
136
					'data' => [],
137
					'name' => 'Title',
138
				],
139
				[
140
					'id' => 'Form_TestForm_SecurityID',
141
					'value' => $form->getSecurityToken()->getValue(),
142
					'message' => null,
143
					'data' => [],
144
					'name' => 'SecurityID',
145
				]
146
			],
147
			'valid' => false,
148
			'messages' => []
149
		];
150
151
		$state = $formSchema->getState($form);
152
		$this->assertInternalType('array', $state);
153
		$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
154
	}
155
156
	public function testGetNestedSchema() {
157
		$form = new Form(
158
			new Controller(),
159
			'TestForm',
160
			new FieldList(new TextField("Name")),
161
			new FieldList(
162
				(new FormAction("save", "Save"))
163
					->setIcon('save'),
164
				(new FormAction("cancel", "Cancel"))
165
					->setUseButtonTag(true),
166
				new PopoverField("More options", [
167
					new FormAction("publish", "Publish record"),
168
					new FormAction("archive", "Archive"),
169
				])
170
			)
171
		);
172
		$formSchema = new FormSchema();
173
		/** @skipUpgrade */
174
		$expected = [
175
			'name' => 'TestForm',
176
			'id' => 'Form_TestForm',
177
			'action' => 'Controller/TestForm',
178
			'method' => 'POST',
179
			'attributes' => [
180
				'id' => 'Form_TestForm',
181
				'action' => 'Controller/TestForm',
182
				'method' => 'POST',
183
				'enctype' => 'application/x-www-form-urlencoded',
184
				'target' => null,
185
				'class' => ''
186
			],
187
			'data' => [],
188
			'fields' => [
189
				[
190
					'id' => 'Form_TestForm_Name',
191
					'name' => 'Name',
192
					'type' => 'Text',
193
				 	'component' => null,
194
				 	'holderId' => 'Form_TestForm_Name_Holder',
195
					'title' => 'Name',
196
					'source' => null,
197
					'extraClass' => 'text',
198
					'description' => null,
199
					'rightTitle' => null,
200
					'leftTitle' => null,
201
					'readOnly' => false,
202
					'disabled' => false,
203
					'customValidationMessage' => '',
204
					'attributes' => [],
205
					'data' => [],
206
					'validation' => [],
207
				],
208
				[
209
					'id' => 'Form_TestForm_SecurityID',
210
					'name' => 'SecurityID',
211
					'type' => "Hidden",
212
					'component' => null,
213
					'holderId' => 'Form_TestForm_SecurityID_Holder',
214
					'title' => 'Security ID',
215
					'source' => null,
216
					'extraClass' => 'hidden',
217
					'description' => null,
218
					'rightTitle' => null,
219
					'leftTitle' => null,
220
					'readOnly' => false,
221
					'disabled' => false,
222
					'customValidationMessage' => '',
223
					'attributes' => [],
224
					'data' => [],
225
					'validation' => [],
226
				],
227
			],
228
			'actions' => [
229
				[
230
					'id' => 'Form_TestForm_action_save',
231
					'title' => 'Save',
232
					'name' => 'action_save',
233
					'type' => null,
234
					'component' => 'FormAction',
235
					'holderId' => 'Form_TestForm_action_save_Holder',
236
					'source' => null,
237
					'extraClass' => 'action',
238
					'description' => null,
239
					'rightTitle' => null,
240
					'leftTitle' => null,
241
					'readOnly' => false,
242
					'disabled' => false,
243
					'customValidationMessage' => '',
244
					'attributes' => [
245
						'type' => 'submit',
246
					],
247
					'data' => [
248
						'icon' => 'save',
249
					],
250
					'validation' => [],
251
				],
252
				[
253
					'id' => 'Form_TestForm_action_cancel',
254
					'title' => 'Cancel',
255
					'name' => 'action_cancel',
256
					'type' => null,
257
					'component' => 'FormAction',
258
					'holderId' => 'Form_TestForm_action_cancel_Holder',
259
					'source' => null,
260
					'extraClass' => 'action',
261
					'description' => null,
262
					'rightTitle' => null,
263
					'leftTitle' => null,
264
					'readOnly' => false,
265
					'disabled' => false,
266
					'customValidationMessage' => '',
267
					'attributes' => [
268
						'type' => 'button'
269
					],
270
					'data' => [
271
						'icon' => null
272
					],
273
					'validation' => [],
274
				],
275
				[
276
					'id' => 'Form_TestForm_Moreoptions',
277
					'title' => 'More options',
278
					'name' => 'Moreoptions',
279
					'type' => 'Structural',
280
					'component' => 'PopoverField',
281
					'holderId' => 'Form_TestForm_Moreoptions_Holder',
282
					'source' => null,
283
					'extraClass' => 'field CompositeField popover',
284
					'description' => null,
285
					'rightTitle' => null,
286
					'leftTitle' => null,
287
					'readOnly' => null,
288
					'disabled' => false,
289
					'customValidationMessage' => '',
290
					'attributes' => [],
291
					'data' => [
292
						'popoverTitle' => null,
293
						'placement' => 'bottom',
294
						'tag' => 'div',
295
						'legend' => null,
296
					],
297
					'validation' => [],
298
					'children' => [
299
						[
300
							'id' => 'Form_TestForm_action_publish',
301
							'title' => 'Publish record',
302
							'name' => 'action_publish',
303
							'type' => null,
304
							'component' => 'FormAction',
305
							'holderId' => 'Form_TestForm_action_publish_Holder',
306
							'source' => null,
307
							'extraClass' => 'action',
308
							'description' => null,
309
							'rightTitle' => null,
310
							'leftTitle' => null,
311
							'readOnly' => false,
312
							'disabled' => false,
313
							'customValidationMessage' => '',
314
							'attributes' => [
315
								'type' => 'submit',
316
							],
317
							'data' => [
318
								'icon' => null,
319
							],
320
							'validation' => [],
321
						],
322
						[
323
							'id' => 'Form_TestForm_action_archive',
324
							'title' => 'Archive',
325
							'name' => 'action_archive',
326
							'type' => null,
327
							'component' => 'FormAction',
328
							'holderId' => 'Form_TestForm_action_archive_Holder',
329
							'source' => null,
330
							'extraClass' => 'action',
331
							'description' => null,
332
							'rightTitle' => null,
333
							'leftTitle' => null,
334
							'readOnly' => false,
335
							'disabled' => false,
336
							'customValidationMessage' => '',
337
							'attributes' => [
338
								'type' => 'submit',
339
							],
340
							'data' => [
341
								'icon' => null,
342
							],
343
							'validation' => [],
344
						],
345
					]
346
				]
347
			]
348
		];
349
350
		$schema = $formSchema->getSchema($form);
351
352
		$this->assertInternalType('array', $schema);
353
		$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($schema));
354
	}
355
356
	/**
357
	 * Test that schema is merged correctly
358
	 */
359
	public function testMergeSchema() {
360
		$publishAction = FormAction::create('publish', 'Publish');
361
		$publishAction->setIcon('save');
362
		$publishAction->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
363
		$schema = $publishAction->getSchemaData();
364
		$this->assertEquals(
365
			[
366
				'icon' => 'save',
367
				'buttonStyle' => 'primary',
368
			],
369
			$schema['data']
370
		);
371
372
	}
373
374
	public function testSchemaValidation() {
375
		$form = new Form(
376
			new Controller(),
377
			'TestForm',
378
			new FieldList(
379
				TextField::create("Name")
380
					->setMaxLength(40),
381
				new DateField("Date"),
382
				new NumericField("Number"),
383
				new CurrencyField("Money")
384
			),
385
			new FieldList(),
386
			new RequiredFields('Name')
387
		);
388
		$formSchema = new FormSchema();
389
		$schema = $formSchema->getSchema($form);
390
		$expected = [
391
			'name' => 'TestForm',
392
			'id' => 'Form_TestForm',
393
			'action' => 'Controller/TestForm',
394
			'method' => 'POST',
395
			'attributes' =>
396
				[
397
					'id' => 'Form_TestForm',
398
					'action' => 'Controller/TestForm',
399
					'method' => 'POST',
400
					'enctype' => 'application/x-www-form-urlencoded',
401
					'target' => null,
402
					'class' => '',
403
				],
404
			'data' =>
405
				[],
406
			'fields' =>
407
				[
408
					[
409
						'name' => 'Name',
410
						'id' => 'Form_TestForm_Name',
411
						'type' => 'Text',
412
						'component' => null,
413
						'holderId' => 'Form_TestForm_Name_Holder',
414
						'title' => 'Name',
415
						'source' => null,
416
						'extraClass' => 'text',
417
						'description' => null,
418
						'rightTitle' => null,
419
						'leftTitle' => null,
420
						'readOnly' => false,
421
						'disabled' => false,
422
						'customValidationMessage' => '',
423
						'validation' =>
424
							[
425
								'required' => true,
426
								'max' => 40,
427
							],
428
						'attributes' =>
429
							[],
430
						'data' =>
431
							[],
432
					],
433
					[
434
						'name' => 'Date',
435
						'id' => 'Form_TestForm_Date',
436
						'type' => 'Date',
437
						'component' => null,
438
						'holderId' => 'Form_TestForm_Date_Holder',
439
						'title' => 'Date',
440
						'source' => null,
441
						'extraClass' => 'date text',
442
						'description' => null,
443
						'rightTitle' => null,
444
						'leftTitle' => null,
445
						'readOnly' => false,
446
						'disabled' => false,
447
						'customValidationMessage' => '',
448
						'validation' =>
449
							[
450
								'date' => true,
451
							],
452
						'attributes' =>
453
							[],
454
						'data' =>
455
							[],
456
					],
457
					[
458
						'name' => 'Number',
459
						'id' => 'Form_TestForm_Number',
460
						'type' => 'Decimal',
461
						'component' => null,
462
						'holderId' => 'Form_TestForm_Number_Holder',
463
						'title' => 'Number',
464
						'source' => null,
465
						'extraClass' => 'numeric text',
466
						'description' => null,
467
						'rightTitle' => null,
468
						'leftTitle' => null,
469
						'readOnly' => false,
470
						'disabled' => false,
471
						'customValidationMessage' => '',
472
						'validation' =>
473
							[
474
								'numeric' => true,
475
							],
476
						'attributes' =>
477
							[],
478
						'data' =>
479
							[],
480
					],
481
					[
482
						'name' => 'Money',
483
						'id' => 'Form_TestForm_Money',
484
						'type' => 'Text',
485
						'component' => null,
486
						'holderId' => 'Form_TestForm_Money_Holder',
487
						'title' => 'Money',
488
						'source' => null,
489
						'extraClass' => 'currency text',
490
						'description' => null,
491
						'rightTitle' => null,
492
						'leftTitle' => null,
493
						'readOnly' => false,
494
						'disabled' => false,
495
						'customValidationMessage' => '',
496
						'validation' =>
497
							[
498
								'currency' => true,
499
							],
500
						'attributes' =>
501
							[],
502
						'data' =>
503
							[],
504
					],
505
					[
506
						'name' => 'SecurityID',
507
						'id' => 'Form_TestForm_SecurityID',
508
						'type' => 'Hidden',
509
						'component' => null,
510
						'holderId' => 'Form_TestForm_SecurityID_Holder',
511
						'title' => 'Security ID',
512
						'source' => null,
513
						'extraClass' => 'hidden',
514
						'description' => null,
515
						'rightTitle' => null,
516
						'leftTitle' => null,
517
						'readOnly' => false,
518
						'disabled' => false,
519
						'customValidationMessage' => '',
520
						'validation' =>
521
							[],
522
						'attributes' =>
523
							[],
524
						'data' =>
525
							[],
526
					],
527
				],
528
			'actions' =>
529
				[],
530
		];
531
532
		$this->assertInternalType('array', $schema);
533
		$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($schema));
534
	}
535
}
536