2008-12-04 23:38:32 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2022-11-04 12:59:52 +01:00
|
|
|
use Exception;
|
2021-10-27 04:39:47 +02:00
|
|
|
use LogicException;
|
2017-06-22 12:50:45 +02:00
|
|
|
use ReflectionClass;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2022-11-04 12:59:52 +01:00
|
|
|
use SilverStripe\Dev\TestOnly;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Forms\CompositeField;
|
2022-11-04 12:59:52 +01:00
|
|
|
use SilverStripe\Forms\FieldGroup;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\FormField;
|
2022-11-04 12:59:52 +01:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_FormAction;
|
|
|
|
use SilverStripe\Forms\GridField\GridState;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Forms\NullableField;
|
2022-11-04 12:59:52 +01:00
|
|
|
use SilverStripe\Forms\PopoverField;
|
|
|
|
use SilverStripe\Forms\PrintableTransformation_TabSet;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2022-11-04 12:59:52 +01:00
|
|
|
use SilverStripe\Forms\SelectionGroup;
|
|
|
|
use SilverStripe\Forms\SelectionGroup_Item;
|
|
|
|
use SilverStripe\Forms\Tab;
|
|
|
|
use SilverStripe\Forms\Tests\FormFieldTest\FieldValidationExtension;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\FormFieldTest\TestExtension;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\TextField;
|
2021-07-31 04:45:24 +02:00
|
|
|
use SilverStripe\Forms\Tip;
|
2022-11-04 12:59:52 +01:00
|
|
|
use SilverStripe\Forms\ToggleCompositeField;
|
|
|
|
use SilverStripe\Forms\TreeDropdownField;
|
|
|
|
use SilverStripe\Forms\TreeDropdownField_Readonly;
|
2020-02-03 03:56:10 +01:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2022-11-04 12:59:52 +01:00
|
|
|
use SilverStripe\Security\Group;
|
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
use SilverStripe\Security\PermissionCheckboxSetField;
|
|
|
|
use SilverStripe\Security\PermissionCheckboxSetField_Readonly;
|
2023-12-14 03:28:19 +01:00
|
|
|
use SilverStripe\Forms\SearchableMultiDropdownField;
|
|
|
|
use SilverStripe\Forms\SearchableDropdownField;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class FormFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
2017-05-12 13:13:51 +02:00
|
|
|
protected static $required_extensions = [
|
|
|
|
FormField::class => [
|
|
|
|
TestExtension::class,
|
2022-11-04 12:59:52 +01:00
|
|
|
FieldValidationExtension::class,
|
2017-05-12 13:13:51 +02:00
|
|
|
],
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
public function testDefaultClasses()
|
|
|
|
{
|
|
|
|
Config::nest();
|
|
|
|
|
2022-11-15 06:20:54 +01:00
|
|
|
FormField::config()->merge(
|
2016-12-16 05:34:21 +01:00
|
|
|
'default_classes',
|
2017-05-12 13:13:51 +02:00
|
|
|
[
|
|
|
|
'class1',
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('class1', $field->extraClass(), 'Class list does not contain expected class');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-11-15 06:20:54 +01:00
|
|
|
FormField::config()->merge(
|
2016-12-16 05:34:21 +01:00
|
|
|
'default_classes',
|
2017-05-12 13:13:51 +02:00
|
|
|
[
|
|
|
|
'class1',
|
|
|
|
'class2',
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('class1 class2', $field->extraClass(), 'Class list does not contain expected class');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-11-15 06:20:54 +01:00
|
|
|
FormField::config()->merge(
|
2016-12-16 05:34:21 +01:00
|
|
|
'default_classes',
|
2017-05-12 13:13:51 +02:00
|
|
|
[
|
|
|
|
'class3',
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('class3', $field->extraClass(), 'Class list does not contain expected class');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$field->removeExtraClass('class3');
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('class3', $field->extraClass(), 'Class list contains unexpected class');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2022-11-15 06:20:54 +01:00
|
|
|
TextField::config()->merge(
|
2016-12-16 05:34:21 +01:00
|
|
|
'default_classes',
|
2017-05-12 13:13:51 +02:00
|
|
|
[
|
|
|
|
'textfield-class',
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$field = new TextField('MyField');
|
|
|
|
|
|
|
|
//check default classes inherit
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('class3', $field->extraClass(), 'Class list does not contain inherited class');
|
|
|
|
$this->assertStringContainsString('textfield-class', $field->extraClass(), 'Class list does not contain expected class');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
Config::unnest();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddExtraClass()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->addExtraClass('class1');
|
|
|
|
$field->addExtraClass('class2');
|
|
|
|
$this->assertStringEndsWith('class1 class2', $field->extraClass());
|
|
|
|
}
|
|
|
|
|
2021-11-01 05:01:17 +01:00
|
|
|
public function testHasExtraClass()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->addExtraClass('class1');
|
|
|
|
$field->addExtraClass('class2');
|
|
|
|
$this->assertTrue($field->hasExtraClass('class1'));
|
|
|
|
$this->assertTrue($field->hasExtraClass('class2'));
|
|
|
|
$this->assertTrue($field->hasExtraClass('class1 class2'));
|
|
|
|
$this->assertTrue($field->hasExtraClass('class2 class1'));
|
|
|
|
$this->assertFalse($field->hasExtraClass('class3'));
|
|
|
|
$this->assertFalse($field->hasExtraClass('class2 class3'));
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testRemoveExtraClass()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->addExtraClass('class1');
|
|
|
|
$field->addExtraClass('class2');
|
|
|
|
$this->assertStringEndsWith('class1 class2', $field->extraClass());
|
|
|
|
$field->removeExtraClass('class1');
|
|
|
|
$this->assertStringEndsWith('class2', $field->extraClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddManyExtraClasses()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
//test we can split by a range of spaces and tabs
|
|
|
|
$field->addExtraClass('class1 class2 class3 class4 class5');
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'class1 class2 class3 class4 class5',
|
|
|
|
$field->extraClass()
|
|
|
|
);
|
|
|
|
//test that duplicate classes don't get added
|
|
|
|
$field->addExtraClass('class1 class2');
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'class1 class2 class3 class4 class5',
|
|
|
|
$field->extraClass()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveManyExtraClasses()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->addExtraClass('class1 class2 class3 class4 class5');
|
|
|
|
//test we can remove a single class we just added
|
|
|
|
$field->removeExtraClass('class3');
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'class1 class2 class4 class5',
|
|
|
|
$field->extraClass()
|
|
|
|
);
|
|
|
|
//check we can remove many classes at once
|
|
|
|
$field->removeExtraClass('class1 class5');
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'class2 class4',
|
|
|
|
$field->extraClass()
|
|
|
|
);
|
|
|
|
//check that removing a dud class is fine
|
|
|
|
$field->removeExtraClass('dudClass');
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'class2 class4',
|
|
|
|
$field->extraClass()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAttributes()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->setAttribute('foo', 'bar');
|
|
|
|
$this->assertEquals('bar', $field->getAttribute('foo'));
|
|
|
|
$attrs = $field->getAttributes();
|
|
|
|
$this->assertArrayHasKey('foo', $attrs);
|
|
|
|
$this->assertEquals('bar', $attrs['foo']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAttributesHTML()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
|
|
|
|
$field->setAttribute('foo', 'bar');
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('foo="bar"', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$field->setAttribute('foo', null);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('foo=', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$field->setAttribute('foo', '');
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('foo=', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$field->setAttribute('foo', false);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('foo=', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$field->setAttribute('foo', true);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('foo="foo"', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$field->setAttribute('foo', 'false');
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('foo="false"', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$field->setAttribute('foo', 'true');
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('foo="true"', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$field->setAttribute('foo', 0);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('foo="0"', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$field->setAttribute('one', 1);
|
|
|
|
$field->setAttribute('two', 2);
|
|
|
|
$field->setAttribute('three', 3);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('one="1"', $field->getAttributesHTML('one', 'two'));
|
|
|
|
$this->assertStringNotContainsString('two="2"', $field->getAttributesHTML('one', 'two'));
|
|
|
|
$this->assertStringContainsString('three="3"', $field->getAttributesHTML('one', 'two'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 03:56:10 +01:00
|
|
|
/**
|
|
|
|
* Covering all potential inputs for Convert::raw2xml
|
|
|
|
*/
|
|
|
|
public function escapeHtmlDataProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['<html>'],
|
|
|
|
[['<html>']],
|
|
|
|
[['<html>' => '<html>']]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider escapeHtmlDataProvider
|
|
|
|
**/
|
|
|
|
public function testGetAttributesEscapeHtml($value)
|
|
|
|
{
|
|
|
|
$key = bin2hex(random_bytes(4));
|
|
|
|
|
|
|
|
if (is_scalar($value)) {
|
|
|
|
$field = new FormField('<html>', '<html>', '<html>');
|
|
|
|
$field->setAttribute($value, $key);
|
|
|
|
$html = $field->getAttributesHTML();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertFalse(strpos($html ?? '', '<html>'));
|
2020-02-03 03:56:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$field = new FormField('<html>', '<html>', '<html>');
|
|
|
|
$field->setAttribute($key, $value);
|
|
|
|
$html = $field->getAttributesHTML();
|
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertFalse(strpos($html ?? '', '<html>'));
|
2020-02-03 03:56:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider escapeHtmlDataProvider
|
|
|
|
*/
|
|
|
|
public function testDebugEscapeHtml($value)
|
|
|
|
{
|
|
|
|
$field = new FormField('<html>', '<html>', '<html>');
|
|
|
|
$field->setAttribute('<html>', $value);
|
|
|
|
$field->setMessage('<html>', null, ValidationResult::CAST_HTML);
|
|
|
|
|
|
|
|
$html = $field->debug();
|
|
|
|
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertFalse(strpos($html ?? '', '<html>'));
|
2020-02-03 03:56:10 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testReadonly()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->setReadonly(true);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('readonly="readonly"', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
$field->setReadonly(false);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('readonly="readonly"', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 12:52:11 +01:00
|
|
|
public function testReadonlyPreservesExtraClass()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->addExtraClass('myextraclass1')->addExtraClass('myextraclass2');
|
|
|
|
$field->setReadonly(true);
|
|
|
|
$this->assertStringContainsString('myextraclass1 myextraclass2', $field->getAttributesHTML());
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testDisabled()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->setDisabled(true);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('disabled="disabled"', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
$field->setDisabled(false);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('disabled="disabled"', $field->getAttributesHTML());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEveryFieldTransformsReadonlyAsClone()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$fieldClasses = ClassInfo::subclassesFor(FormField::class);
|
2016-12-16 05:34:21 +01:00
|
|
|
foreach ($fieldClasses as $fieldClass) {
|
|
|
|
$reflectionClass = new ReflectionClass($fieldClass);
|
|
|
|
if (!$reflectionClass->isInstantiable()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$constructor = $reflectionClass->getMethod('__construct');
|
|
|
|
if ($constructor->getNumberOfRequiredParameters() > 1) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
if (is_a($fieldClass, CompositeField::class, true)) {
|
2016-12-16 05:34:21 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fieldName = $reflectionClass->getShortName() . '_instance';
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var FormField $instance */
|
|
|
|
if ($fieldClass = NullableField::class) {
|
2016-12-16 05:34:21 +01:00
|
|
|
$instance = new $fieldClass(new TextField($fieldName));
|
|
|
|
} else {
|
|
|
|
$instance = new $fieldClass($fieldName);
|
|
|
|
}
|
|
|
|
$isReadonlyBefore = $instance->isReadonly();
|
|
|
|
$readonlyInstance = $instance->performReadonlyTransformation();
|
|
|
|
$this->assertEquals(
|
|
|
|
$isReadonlyBefore,
|
|
|
|
$instance->isReadonly(),
|
|
|
|
"FormField class {$fieldClass} retains its readonly state after calling performReadonlyTransformation()"
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$readonlyInstance->isReadonly(),
|
|
|
|
"FormField class {$fieldClass} returns a valid readonly representation as of isReadonly()"
|
|
|
|
);
|
|
|
|
$this->assertNotSame(
|
|
|
|
$readonlyInstance,
|
|
|
|
$instance,
|
|
|
|
"FormField class {$fieldClass} returns a valid cloned readonly representation"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEveryFieldTransformsDisabledAsClone()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$fieldClasses = ClassInfo::subclassesFor(FormField::class);
|
2016-12-16 05:34:21 +01:00
|
|
|
foreach ($fieldClasses as $fieldClass) {
|
|
|
|
$reflectionClass = new ReflectionClass($fieldClass);
|
|
|
|
if (!$reflectionClass->isInstantiable()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$constructor = $reflectionClass->getMethod('__construct');
|
|
|
|
if ($constructor->getNumberOfRequiredParameters() > 1) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
if (is_a($fieldClass, CompositeField::class, true)) {
|
2016-12-16 05:34:21 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fieldName = $reflectionClass->getShortName() . '_instance';
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var FormField $instance */
|
|
|
|
if ($fieldClass = NullableField::class) {
|
2016-12-16 05:34:21 +01:00
|
|
|
$instance = new $fieldClass(new TextField($fieldName));
|
|
|
|
} else {
|
|
|
|
$instance = new $fieldClass($fieldName);
|
|
|
|
}
|
|
|
|
|
|
|
|
$isDisabledBefore = $instance->isDisabled();
|
|
|
|
$disabledInstance = $instance->performDisabledTransformation();
|
|
|
|
$this->assertEquals(
|
|
|
|
$isDisabledBefore,
|
|
|
|
$instance->isDisabled(),
|
|
|
|
"FormField class {$fieldClass} retains its disabled state after calling performDisabledTransformation()"
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$disabledInstance->isDisabled(),
|
|
|
|
"FormField class {$fieldClass} returns a valid disabled representation as of isDisabled()"
|
|
|
|
);
|
|
|
|
$this->assertNotSame(
|
|
|
|
$disabledInstance,
|
|
|
|
$instance,
|
|
|
|
"FormField class {$fieldClass} returns a valid cloned disabled representation"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateAttributes()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$this->assertArrayHasKey('extended', $field->getAttributes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetSchemaComponent()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field = $field->setSchemaComponent('MyComponent');
|
|
|
|
$component = $field->getSchemaComponent();
|
|
|
|
$this->assertEquals('MyComponent', $component);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSchemaDataDefaults()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$schema = $field->getSchemaDataDefaults();
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertIsArray($schema);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2021-07-31 04:45:24 +02:00
|
|
|
public function testGetSchemaDataDefaultsTitleTip()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$schema = $field->getSchemaDataDefaults();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertFalse(array_key_exists('titleTip', $schema ?? []));
|
2021-07-31 04:45:24 +02:00
|
|
|
$field->setTitleTip(new Tip('Test tip'));
|
|
|
|
$schema = $field->getSchemaDataDefaults();
|
|
|
|
$this->assertSame('Test tip', $schema['titleTip']['content']);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testGetSchemaData()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$schema = $field->getSchemaData();
|
|
|
|
$this->assertEquals('MyField', $schema['name']);
|
|
|
|
|
|
|
|
// Make sure the schema data is up-to-date with object properties.
|
|
|
|
$field->setName('UpdatedField');
|
|
|
|
$schema = $field->getSchemaData();
|
|
|
|
$this->assertEquals($field->getName(), $schema['name']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetSchemaData()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
|
|
|
|
// Make sure the user can update values.
|
2017-06-22 12:50:45 +02:00
|
|
|
$field->setSchemaData(['name' => 'MyUpdatedField']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$schema = $field->getSchemaData();
|
|
|
|
$this->assertEquals($schema['name'], 'MyUpdatedField');
|
|
|
|
|
|
|
|
// Make user the user can't define custom keys on the schema.
|
|
|
|
$field = $field->setSchemaData(['myCustomKey' => 'yolo']);
|
|
|
|
$schema = $field->getSchemaData();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(array_key_exists('myCustomKey', $schema ?? []), false);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSchemaState()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->setValue('My value');
|
|
|
|
$schema = $field->getSchemaState();
|
|
|
|
$this->assertEquals('My value', $schema['value']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetSchemaState()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
|
|
|
|
// Make sure the user can update values.
|
2017-06-22 12:50:45 +02:00
|
|
|
$field->setSchemaState(['value' => 'My custom value']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$schema = $field->getSchemaState();
|
|
|
|
$this->assertEquals($schema['value'], 'My custom value');
|
|
|
|
|
|
|
|
// Make user the user can't define custom keys on the schema.
|
2017-06-22 12:50:45 +02:00
|
|
|
$field->setSchemaState(['myCustomKey' => 'yolo']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$schema = $field->getSchemaState();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(array_key_exists('myCustomKey', $schema ?? []), false);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSchemaStateWithFormValidation()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField', 'My Field');
|
|
|
|
$validator = new RequiredFields('MyField');
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'TestForm', new FieldList($field), new FieldList(), $validator);
|
2016-12-16 05:34:21 +01:00
|
|
|
$form->validationResult();
|
|
|
|
$schema = $field->getSchemaState();
|
|
|
|
$this->assertEquals(
|
|
|
|
'"My Field" is required',
|
|
|
|
$schema['message']['value']
|
|
|
|
);
|
|
|
|
}
|
2017-08-23 04:16:54 +02:00
|
|
|
|
2022-11-04 12:59:52 +01:00
|
|
|
public function testValidationExtensionHooks()
|
|
|
|
{
|
|
|
|
/** @var TextField|FieldValidationExtension $field */
|
|
|
|
$field = new TextField('Test');
|
|
|
|
$field->setMaxLength(5);
|
|
|
|
$field->setValue('IAmLongerThan5Characters');
|
|
|
|
$result = $field->validate(new RequiredFields('Test'));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
// Call extension method in FieldValidationExtension
|
|
|
|
$field->setExcludeFromValidation(true);
|
|
|
|
$result = $field->validate(new RequiredFields('Test'));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
// Call extension methods in FieldValidationExtension
|
|
|
|
$field->setValue('1234');
|
|
|
|
$field->setExcludeFromValidation(false);
|
|
|
|
$field->setTriggerTestValidationError(true);
|
|
|
|
|
|
|
|
// Ensure messages set via updateValidationResult() propagate through to form fields after validation
|
|
|
|
$form = new Form(null, 'TestForm', new FieldList($field), new FieldList(), new RequiredFields());
|
|
|
|
$form->validationResult();
|
|
|
|
$schema = $field->getSchemaState();
|
|
|
|
$this->assertEquals(
|
|
|
|
'A test error message',
|
|
|
|
$schema['message']['value']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidationExtensionHooksAreCalledOnFormFieldSubclasses()
|
|
|
|
{
|
|
|
|
// Can't use a dataProvider for this as dataProviders are fetched very early by phpunit,
|
|
|
|
// and the ClassManifest isn't ready then
|
|
|
|
$formFieldClasses = ClassInfo::subclassesFor(FormField::class, false);
|
|
|
|
foreach ($formFieldClasses as $formFieldClass) {
|
|
|
|
$reflection = new ReflectionClass($formFieldClass);
|
|
|
|
// Skip abstract classes, like MultiSelectField, and fields that only exist for unit tests
|
|
|
|
if ($reflection->isAbstract() || is_a($formFieldClass, TestOnly::class, true)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-22 17:09:35 +01:00
|
|
|
// Create appropriate constructor arguments for the form field class. These don't have to be offer realistic
|
|
|
|
// data, they just need to ensure we can construct the field and call ->validate() on it
|
2022-11-04 12:59:52 +01:00
|
|
|
switch ($formFieldClass) {
|
2023-02-22 17:09:35 +01:00
|
|
|
//
|
|
|
|
// Fields in framework with specific argument requirements
|
|
|
|
//
|
2022-11-04 12:59:52 +01:00
|
|
|
case NullableField::class:
|
|
|
|
case CompositeField::class:
|
|
|
|
case FieldGroup::class:
|
|
|
|
case PopoverField::class:
|
|
|
|
$args = [TextField::create('Test2')];
|
|
|
|
break;
|
|
|
|
case SelectionGroup_Item::class:
|
|
|
|
$args = ['Test', [TextField::create('Test2')]];
|
|
|
|
break;
|
|
|
|
case ToggleCompositeField::class:
|
|
|
|
$args = ['Test', 'Test', TextField::create('Test2')];
|
|
|
|
break;
|
|
|
|
case PrintableTransformation_TabSet::class:
|
|
|
|
$args = [Tab::create('TestTab', 'Testtab', TextField::create('Test2'))];
|
|
|
|
break;
|
|
|
|
case TreeDropdownField::class:
|
|
|
|
case TreeDropdownField_Readonly::class:
|
|
|
|
$args = ['Test', 'Test', Group::class];
|
|
|
|
break;
|
|
|
|
case PermissionCheckboxSetField::class:
|
|
|
|
case PermissionCheckboxSetField_Readonly::class:
|
|
|
|
$args = ['Test', 'Test', Permission::class, 'Test'];
|
|
|
|
break;
|
|
|
|
case SelectionGroup::class:
|
|
|
|
$args = ['Test', []];
|
|
|
|
break;
|
|
|
|
case GridField_FormAction::class:
|
|
|
|
$args = [GridField::create('GF'), 'Test', 'Test label', 'Test action name', []];
|
|
|
|
break;
|
|
|
|
case GridState::class:
|
|
|
|
$args = [GridField::create('GF')];
|
|
|
|
break;
|
2023-12-14 03:28:19 +01:00
|
|
|
case SearchableDropdownField::class:
|
|
|
|
case SearchableMultiDropdownField::class:
|
|
|
|
$args = ['Test', 'Test', Group::get()];
|
|
|
|
break;
|
2023-02-22 17:09:35 +01:00
|
|
|
//
|
|
|
|
// Fields from other modules included in the kitchensink recipe
|
|
|
|
//
|
|
|
|
case \SilverStripe\Blog\Admin\GridFieldFormAction::class:
|
|
|
|
$args = [GridField::create('GF'), 'Test', 'Test label', 'Test action name', []];
|
|
|
|
break;
|
|
|
|
case \SilverStripe\Blog\Forms\BlogAdminSidebar::class:
|
|
|
|
$args = [TextField::create('Test2')];
|
|
|
|
break;
|
|
|
|
case \SilverStripe\CKANRegistry\Forms\PresentedOptionsField::class:
|
|
|
|
$args = ['Test', \SilverStripe\CKANRegistry\Model\Resource::create()];
|
|
|
|
break;
|
|
|
|
case \SilverStripe\DocumentConverter\SettingsField::class:
|
|
|
|
$args = [];
|
|
|
|
break;
|
|
|
|
case \DNADesign\Elemental\Forms\ElementalAreaField::class:
|
|
|
|
$args = ['Test', \DNADesign\Elemental\Models\ElementalArea::create(), []];
|
|
|
|
break;
|
|
|
|
case \SilverStripe\MFA\FormField\RegisteredMFAMethodListField::class:
|
|
|
|
$args = ['Test', 'Test label', 1];
|
|
|
|
break;
|
|
|
|
case \SilverStripe\Subsites\Forms\SubsitesTreeDropdownField::class:
|
|
|
|
$args = ['Test', 'Test', Group::class];
|
|
|
|
break;
|
|
|
|
case \SilverStripe\UserForms\FormField\UserFormsCompositeField::class:
|
|
|
|
case \SilverStripe\UserForms\FormField\UserFormsGroupField::class:
|
|
|
|
case \SilverStripe\UserForms\FormField\UserFormsStepField::class:
|
|
|
|
$args = [TextField::create('Test2')];
|
|
|
|
break;
|
|
|
|
case \Symbiote\AdvancedWorkflow\FormFields\WorkflowField::class:
|
|
|
|
$args = ['Test', 'Test label', \Symbiote\AdvancedWorkflow\DataObjects\WorkflowDefinition::create()];
|
|
|
|
break;
|
|
|
|
//
|
|
|
|
// Default arguments, this covers most simple form fields
|
|
|
|
//
|
2022-11-04 12:59:52 +01:00
|
|
|
default:
|
|
|
|
$args = ['Test', 'Test label'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert that extendValidationResult is called once each time ->validate() is called
|
|
|
|
$mock = $this->getMockBuilder($formFieldClass)
|
|
|
|
->setConstructorArgs($args)
|
|
|
|
->onlyMethods(['extendValidationResult'])
|
|
|
|
->getMock();
|
|
|
|
$mock->expects($invocationRule = $this->once())
|
|
|
|
->method('extendValidationResult')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$isValid = $mock->validate(new RequiredFields());
|
|
|
|
$this->assertTrue($isValid, "$formFieldClass should be valid");
|
|
|
|
|
|
|
|
// This block is not essential and only exists to make test debugging easier - without this,
|
|
|
|
// the error message on failure is generic and doesn't include the class name that failed
|
|
|
|
try {
|
|
|
|
$invocationRule->verify();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->fail("Expectation failed for '$formFieldClass' class: {$e->getMessage()}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 04:16:54 +02:00
|
|
|
public function testHasClass()
|
|
|
|
{
|
|
|
|
$field = new FormField('Test');
|
|
|
|
$field->addExtraClass('foo BAr cool-banana');
|
|
|
|
|
|
|
|
$this->assertTrue($field->hasClass('foo'));
|
|
|
|
$this->assertTrue($field->hasClass('bAr'));
|
|
|
|
$this->assertFalse($field->hasClass('banana'));
|
|
|
|
$this->assertTrue($field->hasClass('cool-BAnana'));
|
|
|
|
}
|
2018-06-25 01:06:43 +02:00
|
|
|
|
|
|
|
public function testLinkWithForm()
|
|
|
|
{
|
|
|
|
$field = new FormField('Test');
|
|
|
|
$form = new Form(null, 'Test', new FieldList, new FieldList);
|
|
|
|
$form->setFormAction('foo');
|
|
|
|
$field->setForm($form);
|
|
|
|
$this->assertSame('foo/field/Test/bar', $field->Link('bar'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLinkWithoutForm()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectException(LogicException::class);
|
2018-06-25 01:06:43 +02:00
|
|
|
$field = new FormField('Test');
|
|
|
|
$field->Link('bar');
|
|
|
|
}
|
2019-01-07 17:26:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param string $expected
|
|
|
|
* @dataProvider nameToLabelProvider
|
|
|
|
*/
|
|
|
|
public function testNameToLabel($name, $expected)
|
|
|
|
{
|
|
|
|
$this->assertSame($expected, FormField::name_to_label($name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function nameToLabelProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['TotalAmount', 'Total amount'],
|
|
|
|
['Organisation.ZipCode', 'Organisation zip code'],
|
|
|
|
['Organisation.zipCode', 'Organisation zip code'],
|
|
|
|
['FooBarBaz', 'Foo bar baz'],
|
|
|
|
['URLSegment', 'URL segment'],
|
|
|
|
['ONLYCAPS', 'ONLYCAPS'],
|
|
|
|
['onlylower', 'Onlylower'],
|
|
|
|
['SpecialURL', 'Special URL'],
|
|
|
|
];
|
|
|
|
}
|
2021-07-31 04:45:24 +02:00
|
|
|
|
|
|
|
public function testGetSetTitleTip()
|
|
|
|
{
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$this->assertNull($field->getTitleTip());
|
|
|
|
$field->setTitleTip(new Tip('Test tip'));
|
|
|
|
$this->assertInstanceOf(Tip::class, $field->getTitleTip());
|
|
|
|
$this->assertSame('Test tip', $field->getTitleTip()->getMessage());
|
|
|
|
}
|
2015-01-14 14:07:41 +01:00
|
|
|
}
|