silverstripe-framework/tests/forms/FormSchemaTest.php

143 lines
3.6 KiB
PHP
Raw Normal View History

<?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 = [
'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' => [
[
'id' => 'Form_TestForm_SecurityID',
'name' => 'SecurityID',
'type' => "Hidden",
'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
];
$schema = $formSchema->getSchema($form);
2016-02-29 01:50:59 +01:00
$this->assertInternalType('array', $schema);
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($schema));
}
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',
'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' => []
];
$state = $formSchema->getState($form);
2016-02-29 01:50:59 +01:00
$this->assertInternalType('array', $state);
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
}
}