2016-02-22 02:13:35 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use SilverStripe\Forms\Schema\FormSchema;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
use SilverStripe\Forms\PopoverField;
|
|
|
|
|
2016-02-22 02:13:35 +01:00
|
|
|
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',
|
2016-04-12 06:47:24 +02:00
|
|
|
'id' => 'Form_TestForm',
|
|
|
|
'action' => 'Controller/TestForm',
|
|
|
|
'method' => 'POST',
|
2016-02-22 02:13:35 +01:00
|
|
|
'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,
|
2016-09-29 22:10:02 +02:00
|
|
|
'holderId' => 'Form_TestForm_SecurityID_Holder',
|
2016-02-22 02:13:35 +01:00
|
|
|
'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
|
|
|
|
2016-09-30 03:11:31 +02:00
|
|
|
$schema = $formSchema->getSchema($form, 'admin/mysection/schema');
|
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 = [
|
2016-04-12 06:47:24 +02:00
|
|
|
'id' => 'Form_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 = [
|
2016-04-12 06:47:24 +02:00
|
|
|
'id' => 'Form_TestForm',
|
2016-03-01 04:15:47 +01:00
|
|
|
'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 = [
|
2016-04-12 06:47:24 +02:00
|
|
|
'id' => 'Form_TestForm',
|
2016-03-01 04:15:47 +01:00
|
|
|
'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
|
|
|
}
|
2016-07-07 00:39:44 +02:00
|
|
|
|
|
|
|
public function testGetNestedSchema() {
|
|
|
|
$form = new Form(
|
|
|
|
new Controller(),
|
|
|
|
'TestForm',
|
|
|
|
new FieldList(new TextField("Name")),
|
|
|
|
new FieldList(
|
|
|
|
(new FormAction("save", "Save"))
|
|
|
|
->setIcon('save'),
|
|
|
|
(new FormAction("cancel", "Cancel"))
|
|
|
|
->setUseButtonTag(true),
|
|
|
|
new PopoverField("More options", [
|
|
|
|
new FormAction("publish", "Publish record"),
|
|
|
|
new FormAction("archive", "Archive"),
|
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$formSchema = new FormSchema();
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2016-07-07 00:39:44 +02:00
|
|
|
$expected = [
|
|
|
|
'name' => 'TestForm',
|
|
|
|
'id' => 'Form_TestForm',
|
|
|
|
'action' => 'Controller/TestForm',
|
|
|
|
'method' => 'POST',
|
|
|
|
'attributes' => [
|
|
|
|
'id' => 'Form_TestForm',
|
|
|
|
'action' => 'Controller/TestForm',
|
|
|
|
'method' => 'POST',
|
|
|
|
'enctype' => 'application/x-www-form-urlencoded',
|
|
|
|
'target' => null,
|
|
|
|
'class' => ''
|
|
|
|
],
|
|
|
|
'data' => [],
|
|
|
|
'fields' => [
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_Name',
|
|
|
|
'name' => 'Name',
|
|
|
|
'type' => 'Text',
|
|
|
|
'component' => null,
|
2016-09-29 22:10:02 +02:00
|
|
|
'holderId' => 'Form_TestForm_Name_Holder',
|
2016-07-07 00:39:44 +02:00
|
|
|
'title' => 'Name',
|
|
|
|
'source' => null,
|
|
|
|
'extraClass' => 'text',
|
|
|
|
'description' => null,
|
|
|
|
'rightTitle' => null,
|
|
|
|
'leftTitle' => null,
|
|
|
|
'readOnly' => false,
|
|
|
|
'disabled' => false,
|
|
|
|
'customValidationMessage' => '',
|
|
|
|
'attributes' => [],
|
|
|
|
'data' => [],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_SecurityID',
|
|
|
|
'name' => 'SecurityID',
|
|
|
|
'type' => "Hidden",
|
|
|
|
'component' => null,
|
2016-09-29 22:10:02 +02:00
|
|
|
'holderId' => 'Form_TestForm_SecurityID_Holder',
|
2016-07-07 00:39:44 +02:00
|
|
|
'title' => 'Security ID',
|
|
|
|
'source' => null,
|
|
|
|
'extraClass' => 'hidden',
|
|
|
|
'description' => null,
|
|
|
|
'rightTitle' => null,
|
|
|
|
'leftTitle' => null,
|
|
|
|
'readOnly' => false,
|
|
|
|
'disabled' => false,
|
|
|
|
'customValidationMessage' => '',
|
|
|
|
'attributes' => [],
|
|
|
|
'data' => []
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'actions' => [
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_action_save',
|
|
|
|
'title' => 'Save',
|
|
|
|
'name' => 'action_save',
|
|
|
|
'type' => null,
|
|
|
|
'component' => 'FormAction',
|
2016-09-29 22:10:02 +02:00
|
|
|
'holderId' => 'Form_TestForm_action_save_Holder',
|
2016-07-07 00:39:44 +02:00
|
|
|
'source' => null,
|
|
|
|
'extraClass' => 'action',
|
|
|
|
'description' => null,
|
|
|
|
'rightTitle' => null,
|
|
|
|
'leftTitle' => null,
|
|
|
|
'readOnly' => false,
|
|
|
|
'disabled' => false,
|
|
|
|
'customValidationMessage' => '',
|
|
|
|
'attributes' => [
|
|
|
|
'type' => 'submit',
|
|
|
|
],
|
|
|
|
'data' => [
|
|
|
|
'icon' => 'save',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_action_cancel',
|
|
|
|
'title' => 'Cancel',
|
|
|
|
'name' => 'action_cancel',
|
|
|
|
'type' => null,
|
|
|
|
'component' => 'FormAction',
|
2016-09-29 22:10:02 +02:00
|
|
|
'holderId' => 'Form_TestForm_action_cancel_Holder',
|
2016-07-07 00:39:44 +02:00
|
|
|
'source' => null,
|
|
|
|
'extraClass' => 'action',
|
|
|
|
'description' => null,
|
|
|
|
'rightTitle' => null,
|
|
|
|
'leftTitle' => null,
|
|
|
|
'readOnly' => false,
|
|
|
|
'disabled' => false,
|
|
|
|
'customValidationMessage' => '',
|
|
|
|
'attributes' => [
|
|
|
|
'type' => 'button'
|
|
|
|
],
|
|
|
|
'data' => [
|
|
|
|
'icon' => null
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_Moreoptions',
|
|
|
|
'title' => 'More options',
|
|
|
|
'name' => 'Moreoptions',
|
|
|
|
'type' => 'Structural',
|
|
|
|
'component' => 'PopoverField',
|
2016-09-29 22:10:02 +02:00
|
|
|
'holderId' => 'Form_TestForm_Moreoptions_Holder',
|
2016-07-07 00:39:44 +02:00
|
|
|
'source' => null,
|
|
|
|
'extraClass' => 'field CompositeField popover',
|
|
|
|
'description' => null,
|
|
|
|
'rightTitle' => null,
|
|
|
|
'leftTitle' => null,
|
|
|
|
'readOnly' => null,
|
|
|
|
'disabled' => false,
|
|
|
|
'customValidationMessage' => '',
|
|
|
|
'attributes' => [],
|
2016-08-12 06:34:51 +02:00
|
|
|
'data' => [
|
|
|
|
'popoverTitle' => null,
|
|
|
|
'placement' => 'bottom',
|
2016-09-07 05:35:47 +02:00
|
|
|
'tag' => 'div',
|
|
|
|
'legend' => null,
|
2016-08-12 06:34:51 +02:00
|
|
|
],
|
2016-07-07 00:39:44 +02:00
|
|
|
'children' => [
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_action_publish',
|
|
|
|
'title' => 'Publish record',
|
|
|
|
'name' => 'action_publish',
|
|
|
|
'type' => null,
|
|
|
|
'component' => 'FormAction',
|
2016-09-29 22:10:02 +02:00
|
|
|
'holderId' => 'Form_TestForm_action_publish_Holder',
|
2016-07-07 00:39:44 +02:00
|
|
|
'source' => null,
|
|
|
|
'extraClass' => 'action',
|
|
|
|
'description' => null,
|
|
|
|
'rightTitle' => null,
|
|
|
|
'leftTitle' => null,
|
|
|
|
'readOnly' => false,
|
|
|
|
'disabled' => false,
|
|
|
|
'customValidationMessage' => '',
|
|
|
|
'attributes' => [
|
|
|
|
'type' => 'submit',
|
|
|
|
],
|
|
|
|
'data' => [
|
|
|
|
'icon' => null,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_action_archive',
|
|
|
|
'title' => 'Archive',
|
|
|
|
'name' => 'action_archive',
|
|
|
|
'type' => null,
|
|
|
|
'component' => 'FormAction',
|
2016-09-29 22:10:02 +02:00
|
|
|
'holderId' => 'Form_TestForm_action_archive_Holder',
|
2016-07-07 00:39:44 +02:00
|
|
|
'source' => null,
|
|
|
|
'extraClass' => 'action',
|
|
|
|
'description' => null,
|
|
|
|
'rightTitle' => null,
|
|
|
|
'leftTitle' => null,
|
|
|
|
'readOnly' => false,
|
|
|
|
'disabled' => false,
|
|
|
|
'customValidationMessage' => '',
|
|
|
|
'attributes' => [
|
|
|
|
'type' => 'submit',
|
|
|
|
],
|
|
|
|
'data' => [
|
|
|
|
'icon' => null,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2016-09-30 03:11:31 +02:00
|
|
|
$schema = $formSchema->getSchema($form, 'admin/mysection/schema');
|
2016-07-07 00:39:44 +02:00
|
|
|
|
|
|
|
$this->assertInternalType('array', $schema);
|
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($schema));
|
|
|
|
}
|
2016-09-30 03:11:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that schema is merged correctly
|
|
|
|
*/
|
|
|
|
public function testMergeSchema() {
|
|
|
|
$publishAction = FormAction::create('publish', 'Publish');
|
|
|
|
$publishAction->setIcon('save');
|
|
|
|
$publishAction->setSchemaData(['data' => ['buttonStyle' => 'primary']]);
|
|
|
|
$schema = $publishAction->getSchemaData();
|
|
|
|
$this->assertEquals(
|
|
|
|
[
|
|
|
|
'icon' => 'save',
|
|
|
|
'buttonStyle' => 'primary',
|
|
|
|
],
|
|
|
|
$schema['data']
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2016-02-22 02:13:35 +01:00
|
|
|
}
|