2016-02-22 02:13:35 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use SilverStripe\Forms\Schema\FormSchema;
|
|
|
|
|
|
|
|
class FormSchemaTest extends SapphireTest {
|
|
|
|
|
|
|
|
public function testGetSchema() {
|
|
|
|
$form = new Form(new Controller(), 'TestForm', new FieldList(), new FieldList());
|
|
|
|
$formSchema = new FormSchema();
|
2016-02-29 01:50:59 +01:00
|
|
|
$expected = [
|
2016-02-22 02:13:35 +01:00
|
|
|
'name' => 'TestForm',
|
|
|
|
'id' => null,
|
|
|
|
'action' => null,
|
|
|
|
'method' => '',
|
|
|
|
'schema_url' => '',
|
|
|
|
'attributes' => [
|
|
|
|
'id' => 'Form_TestForm',
|
|
|
|
'action' => 'Controller/TestForm',
|
|
|
|
'method' => 'POST',
|
|
|
|
'enctype' => 'application/x-www-form-urlencoded',
|
|
|
|
'target' => null,
|
|
|
|
'class' => ''
|
|
|
|
],
|
|
|
|
'data' => [],
|
|
|
|
'fields' => [
|
|
|
|
[
|
2016-03-21 10:54:08 +01:00
|
|
|
'id' => 'Form_TestForm_SecurityID',
|
|
|
|
'name' => 'SecurityID',
|
|
|
|
'type' => "Hidden",
|
2016-02-22 02:13:35 +01:00
|
|
|
'component' => null,
|
|
|
|
'holder_id' => null,
|
|
|
|
'title' => 'Security ID',
|
|
|
|
'source' => null,
|
|
|
|
'extraClass' => 'hidden',
|
|
|
|
'description' => null,
|
|
|
|
'rightTitle' => null,
|
|
|
|
'leftTitle' => null,
|
|
|
|
'readOnly' => false,
|
|
|
|
'disabled' => false,
|
|
|
|
'customValidationMessage' => '',
|
|
|
|
'attributes' => [],
|
|
|
|
'data' => []
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'actions' => []
|
2016-02-29 01:50:59 +01:00
|
|
|
];
|
2016-02-22 02:13:35 +01:00
|
|
|
|
|
|
|
$schema = $formSchema->getSchema($form);
|
2016-02-29 01:50:59 +01:00
|
|
|
$this->assertInternalType('array', $schema);
|
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($schema));
|
2016-02-22 02:13:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetState() {
|
|
|
|
$form = new Form(new Controller(), 'TestForm', new FieldList(), new FieldList());
|
|
|
|
$formSchema = new FormSchema();
|
2016-02-29 01:50:59 +01:00
|
|
|
$expected = [
|
|
|
|
'id' => 'TestForm',
|
2016-03-01 04:15:47 +01:00
|
|
|
'fields' => [
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_SecurityID',
|
|
|
|
'value' => $form->getSecurityToken()->getValue(),
|
|
|
|
'messages' => [],
|
|
|
|
'valid' => true,
|
|
|
|
'data' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'messages' => []
|
|
|
|
];
|
|
|
|
|
|
|
|
$state = $formSchema->getState($form);
|
|
|
|
$this->assertInternalType('array', $state);
|
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetStateWithFormMessages() {
|
|
|
|
$fields = new FieldList();
|
|
|
|
$actions = new FieldList();
|
|
|
|
$form = new Form(new Controller(), 'TestForm', $fields, $actions);
|
|
|
|
$form->sessionMessage('All saved', 'good');
|
|
|
|
$formSchema = new FormSchema();
|
|
|
|
$expected = [
|
|
|
|
'id' => 'TestForm',
|
|
|
|
'fields' => [
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_SecurityID',
|
|
|
|
'value' => $form->getSecurityToken()->getValue(),
|
|
|
|
'messages' => [],
|
|
|
|
'valid' => true,
|
|
|
|
'data' => []
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'messages' => [
|
|
|
|
[
|
|
|
|
'value' => 'All saved',
|
|
|
|
'type' => 'good'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$state = $formSchema->getState($form);
|
|
|
|
$this->assertInternalType('array', $state);
|
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetStateWithFieldValidationErrors() {
|
|
|
|
$fields = new FieldList(new TextField('Title'));
|
|
|
|
$actions = new FieldList();
|
|
|
|
$validator = new RequiredFields('Title');
|
|
|
|
$form = new Form(new Controller(), 'TestForm', $fields, $actions, $validator);
|
|
|
|
$form->loadDataFrom([
|
|
|
|
'Title' => 'My Title'
|
|
|
|
]);
|
|
|
|
$validator->validationError('Title', 'Title is invalid', 'error');
|
|
|
|
$formSchema = new FormSchema();
|
|
|
|
$expected = [
|
|
|
|
'id' => 'TestForm',
|
|
|
|
'fields' => [
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_Title',
|
|
|
|
'value' => 'My Title',
|
|
|
|
'messages' => [
|
|
|
|
['value' => 'Title is invalid', 'type' => 'error']
|
|
|
|
],
|
|
|
|
'valid' => false,
|
|
|
|
'data' => []
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_SecurityID',
|
|
|
|
'value' => $form->getSecurityToken()->getValue(),
|
|
|
|
'messages' => [],
|
|
|
|
'valid' => true,
|
|
|
|
'data' => []
|
|
|
|
]
|
|
|
|
],
|
2016-02-29 01:50:59 +01:00
|
|
|
'messages' => []
|
|
|
|
];
|
2016-02-22 02:13:35 +01:00
|
|
|
|
|
|
|
$state = $formSchema->getState($form);
|
2016-02-29 01:50:59 +01:00
|
|
|
$this->assertInternalType('array', $state);
|
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
|
2016-02-22 02:13:35 +01:00
|
|
|
}
|
|
|
|
}
|