2016-02-22 02:13:35 +01:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2016-10-28 05:22:58 +02:00
|
|
|
use SilverStripe\Forms\CurrencyField;
|
|
|
|
use SilverStripe\Forms\DateField;
|
|
|
|
use SilverStripe\Forms\NumericField;
|
2016-02-22 02:13:35 +01:00
|
|
|
use SilverStripe\Forms\Schema\FormSchema;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
use SilverStripe\Forms\PopoverField;
|
2023-11-22 02:17:47 +01:00
|
|
|
use SilverStripe\Forms\FormField;
|
|
|
|
use LogicException;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class FormSchemaTest extends SapphireTest
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2017-06-22 12:50:45 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// Clear old messages
|
|
|
|
$session = Controller::curr()->getRequest()->getSession();
|
|
|
|
$session
|
|
|
|
->clear("FormInfo.TestForm.result")
|
|
|
|
->clear("FormInfo.TestForm.data");
|
|
|
|
}
|
2016-02-22 02:13:35 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testGetSchema()
|
|
|
|
{
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'TestForm', new FieldList(), new FieldList());
|
2016-12-16 05:34:21 +01:00
|
|
|
$formSchema = new FormSchema();
|
2022-04-14 03:12:59 +02:00
|
|
|
$expected = json_decode(file_get_contents(__DIR__ . '/FormSchemaTest/testGetSchema.json') ?? '', true);
|
2016-02-22 02:13:35 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$schema = $formSchema->getSchema($form);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertIsArray($schema);
|
2017-05-04 02:52:40 +02:00
|
|
|
$this->assertEquals($expected, $schema);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2016-02-22 02:13:35 +01:00
|
|
|
|
2023-11-22 02:17:47 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideGetSchemaException
|
|
|
|
*/
|
|
|
|
public function testGetSchemaException(string $field, bool $expectException): void
|
|
|
|
{
|
|
|
|
$fields = [];
|
|
|
|
if ($field === '<HasComponent>') {
|
|
|
|
$fields[] = (new FormField('TestField'))->setSchemaComponent('MyPretendComponent');
|
|
|
|
} elseif ($field === '<HasDataType>') {
|
|
|
|
$fields[] = new class('TestField') extends FormField {
|
|
|
|
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_CUSTOM;
|
|
|
|
};
|
|
|
|
} elseif ($field === '<None>') {
|
|
|
|
$fields[] = new FormField('TestField');
|
|
|
|
}
|
|
|
|
$form = new Form(null, 'TestForm', new FieldList($fields));
|
|
|
|
$formSchema = new FormSchema($form);
|
|
|
|
if ($expectException) {
|
|
|
|
$this->expectException(LogicException::class);
|
|
|
|
} else {
|
|
|
|
$this->expectNotToPerformAssertions();
|
|
|
|
}
|
|
|
|
$formSchema->getSchema($form);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideGetSchemaException(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'field' => '<HasComponent>',
|
|
|
|
'expectException' => false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'field' => '<HasDataType>',
|
|
|
|
'expectException' => false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'field' => '<None>',
|
|
|
|
'expectException' => true,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testGetState()
|
|
|
|
{
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'TestForm', new FieldList(), new FieldList());
|
2016-12-16 05:34:21 +01:00
|
|
|
$formSchema = new FormSchema();
|
|
|
|
$expected = [
|
|
|
|
'id' => 'Form_TestForm',
|
|
|
|
'fields' => [
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_SecurityID',
|
|
|
|
'value' => $form->getSecurityToken()->getValue(),
|
|
|
|
'message' => null,
|
|
|
|
'data' => [],
|
|
|
|
'name' => 'SecurityID',
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'messages' => [],
|
2017-06-21 06:54:54 +02:00
|
|
|
'notifyUnsavedChanges' => false
|
2016-12-16 05:34:21 +01:00
|
|
|
];
|
2016-03-01 04:15:47 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$state = $formSchema->getState($form);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertIsArray($state);
|
2017-05-04 02:52:40 +02:00
|
|
|
$this->assertEquals($expected, $state);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2016-03-01 04:15:47 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testGetStateWithFormMessages()
|
|
|
|
{
|
|
|
|
$fields = new FieldList();
|
|
|
|
$actions = new FieldList();
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'TestForm', $fields, $actions);
|
2016-12-16 05:34:21 +01:00
|
|
|
$form->sessionMessage('All saved', 'good');
|
|
|
|
$formSchema = new FormSchema();
|
|
|
|
$expected = [
|
|
|
|
'id' => 'Form_TestForm',
|
|
|
|
'fields' => [
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_SecurityID',
|
|
|
|
'value' => $form->getSecurityToken()->getValue(),
|
|
|
|
'data' => [],
|
|
|
|
'message' => null,
|
|
|
|
'name' => 'SecurityID',
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'messages' => [[
|
|
|
|
'value' => 'All saved',
|
|
|
|
'type' => 'good'
|
|
|
|
]],
|
2017-06-21 06:54:54 +02:00
|
|
|
'notifyUnsavedChanges' => false
|
2016-12-16 05:34:21 +01:00
|
|
|
];
|
2016-03-01 04:15:47 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$state = $formSchema->getState($form);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertIsArray($state);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
|
|
|
|
}
|
2016-03-01 04:15:47 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testGetStateWithFieldValidationErrors()
|
|
|
|
{
|
|
|
|
$fields = new FieldList(new TextField('Title'));
|
|
|
|
$actions = new FieldList();
|
|
|
|
$validator = new RequiredFields('Title');
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'TestForm', $fields, $actions, $validator);
|
2017-06-22 12:50:45 +02:00
|
|
|
$form->clearMessage();
|
2016-12-16 05:34:21 +01:00
|
|
|
$form->loadDataFrom(
|
|
|
|
[
|
|
|
|
'Title' => null,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$this->assertFalse($form->validationResult()->isValid());
|
|
|
|
$formSchema = new FormSchema();
|
|
|
|
$expected = [
|
|
|
|
'id' => 'Form_TestForm',
|
|
|
|
'fields' => [
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_Title',
|
|
|
|
'value' => null,
|
|
|
|
'message' => [
|
|
|
|
'value' => '"Title" is required',
|
|
|
|
'type' => 'required'
|
|
|
|
],
|
|
|
|
'data' => [],
|
|
|
|
'name' => 'Title',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 'Form_TestForm_SecurityID',
|
|
|
|
'value' => $form->getSecurityToken()->getValue(),
|
|
|
|
'message' => null,
|
|
|
|
'data' => [],
|
|
|
|
'name' => 'SecurityID',
|
|
|
|
]
|
|
|
|
],
|
2017-06-21 06:54:54 +02:00
|
|
|
'messages' => [],
|
|
|
|
'notifyUnsavedChanges' => false
|
2016-12-16 05:34:21 +01:00
|
|
|
];
|
2016-02-22 02:13:35 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$state = $formSchema->getState($form);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertIsArray($state);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
|
|
|
|
}
|
2016-07-07 00:39:44 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testGetNestedSchema()
|
|
|
|
{
|
|
|
|
$form = new Form(
|
2017-03-02 03:24:38 +01:00
|
|
|
null,
|
2016-12-16 05:34:21 +01:00
|
|
|
'TestForm',
|
|
|
|
new FieldList(new TextField("Name")),
|
|
|
|
new FieldList(
|
|
|
|
(new FormAction("save", "Save"))
|
|
|
|
->setIcon('save'),
|
|
|
|
(new FormAction("cancel", "Cancel"))
|
|
|
|
->setUseButtonTag(true),
|
|
|
|
$pop = new PopoverField(
|
|
|
|
"More options",
|
|
|
|
[
|
|
|
|
new FormAction("publish", "Publish record"),
|
|
|
|
new FormAction("archive", "Archive"),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$formSchema = new FormSchema();
|
2022-04-14 03:12:59 +02:00
|
|
|
$expected = json_decode(file_get_contents(__DIR__ . '/FormSchemaTest/testGetNestedSchema.json') ?? '', true);
|
2016-12-16 05:34:21 +01:00
|
|
|
$schema = $formSchema->getSchema($form);
|
2016-07-07 00:39:44 +02:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertIsArray($schema);
|
2017-05-04 02:52:40 +02:00
|
|
|
$this->assertEquals($expected, $schema);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2016-09-30 03:11:31 +02:00
|
|
|
|
2016-12-16 05:34:21 +01: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-09-30 03:11:31 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testSchemaValidation()
|
|
|
|
{
|
|
|
|
$form = new Form(
|
2017-03-02 03:24:38 +01:00
|
|
|
null,
|
2016-12-16 05:34:21 +01:00
|
|
|
'TestForm',
|
|
|
|
new FieldList(
|
|
|
|
TextField::create("Name")
|
|
|
|
->setMaxLength(40),
|
|
|
|
new DateField("Date"),
|
|
|
|
new NumericField("Number"),
|
|
|
|
new CurrencyField("Money")
|
|
|
|
),
|
|
|
|
new FieldList(),
|
|
|
|
new RequiredFields('Name')
|
|
|
|
);
|
|
|
|
$formSchema = new FormSchema();
|
|
|
|
$schema = $formSchema->getSchema($form);
|
2022-04-14 03:12:59 +02:00
|
|
|
$expected = json_decode(file_get_contents(__DIR__ . '/FormSchemaTest/testSchemaValidation.json') ?? '', true);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertIsArray($schema);
|
2017-05-04 02:52:40 +02:00
|
|
|
$this->assertEquals($expected, $schema);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2016-02-22 02:13:35 +01:00
|
|
|
}
|