2013-07-15 22:44:44 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
2023-11-13 01:02:38 +01:00
|
|
|
use SilverStripe\Forms\Form;
|
2024-05-02 02:06:43 +02:00
|
|
|
use SilverStripe\Forms\SearchableDropdownField;
|
2023-11-13 01:02:38 +01:00
|
|
|
use SilverStripe\Forms\TreeDropdownField;
|
|
|
|
use SilverStripe\Security\Group;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class RequiredFieldsTest extends SapphireTest
|
|
|
|
{
|
|
|
|
public function testConstructingWithArray()
|
|
|
|
{
|
|
|
|
//can we construct with an array?
|
2020-04-20 19:58:09 +02:00
|
|
|
$fields = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'AnotherField'
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields = new RequiredFields($fields);
|
|
|
|
//check the fields and the array match
|
|
|
|
$this->assertEquals(
|
|
|
|
$fields,
|
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Failed to set the required fields using an array"
|
|
|
|
);
|
|
|
|
}
|
2013-07-15 22:44:44 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testConstructingWithArguments()
|
|
|
|
{
|
|
|
|
//can we construct with arguments?
|
|
|
|
$requiredFields = new RequiredFields(
|
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'AnotherField'
|
|
|
|
);
|
|
|
|
//check the fields match
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'AnotherField'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Failed to set the required fields using arguments"
|
|
|
|
);
|
|
|
|
}
|
2013-07-15 22:44:44 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testRemoveValidation()
|
|
|
|
{
|
|
|
|
//can we remove all fields at once?
|
|
|
|
$requiredFields = new RequiredFields(
|
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'AnotherField'
|
|
|
|
);
|
|
|
|
$requiredFields->removeValidation();
|
|
|
|
//check there are no required fields
|
|
|
|
$this->assertEmpty(
|
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Failed to remove all the required fields using 'removeValidation()'"
|
|
|
|
);
|
|
|
|
}
|
2013-07-15 22:44:44 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testRemoveRequiredField()
|
|
|
|
{
|
|
|
|
//set up the required fields
|
|
|
|
$requiredFields = new RequiredFields(
|
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'AnotherField'
|
|
|
|
);
|
|
|
|
//remove one
|
|
|
|
$requiredFields->removeRequiredField('Content');
|
|
|
|
//compare the arrays
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Title',
|
|
|
|
'Image',
|
|
|
|
'AnotherField'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Failed to remove the 'Content' field from required list"
|
|
|
|
);
|
|
|
|
//let's remove another
|
|
|
|
$requiredFields->removeRequiredField('Title');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Image',
|
|
|
|
'AnotherField'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Failed to remove 'Title' field from required list"
|
|
|
|
);
|
|
|
|
//lets try to remove one that doesn't exist
|
|
|
|
$requiredFields->removeRequiredField('DontExists');
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Image',
|
|
|
|
'AnotherField'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
2021-12-13 09:05:33 +01:00
|
|
|
"Removing a non-existent field from required list altered the list of required fields"
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
2013-07-15 22:44:44 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testAddRequiredField()
|
|
|
|
{
|
|
|
|
//set up the validator
|
|
|
|
$requiredFields = new RequiredFields(
|
|
|
|
'Title'
|
|
|
|
);
|
|
|
|
//add a field
|
|
|
|
$requiredFields->addRequiredField('Content');
|
|
|
|
//check it was added
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Title',
|
|
|
|
'Content'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Failed to add a new field to the required list"
|
|
|
|
);
|
|
|
|
//add another for good measure
|
|
|
|
$requiredFields->addRequiredField('Image');
|
|
|
|
//check it was added
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Failed to add a second new field to the required list"
|
|
|
|
);
|
|
|
|
//remove a field
|
|
|
|
$requiredFields->removeRequiredField('Title');
|
|
|
|
//check it was removed
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Content',
|
|
|
|
'Image'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Failed to remove 'Title' field from required list"
|
|
|
|
);
|
|
|
|
//add the same field back to check we can add and remove at will
|
|
|
|
$requiredFields->addRequiredField('Title');
|
|
|
|
//check it's in there
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'Title'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Failed to add 'Title' back to the required field list"
|
|
|
|
);
|
|
|
|
//add a field that already exists (we can't have the same field twice, can we?)
|
|
|
|
$requiredFields->addRequiredField('Content');
|
|
|
|
//check the field wasn't added
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'Title'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Adding a duplicate field to required field list had unexpected behaviour"
|
|
|
|
);
|
|
|
|
}
|
2013-07-15 22:44:44 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testAppendRequiredFields()
|
|
|
|
{
|
|
|
|
//get the validator
|
|
|
|
$requiredFields = new RequiredFields(
|
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'AnotherField'
|
|
|
|
);
|
|
|
|
//create another validator with other fields
|
|
|
|
$otherRequiredFields = new RequiredFields(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'ExtraField1',
|
|
|
|
'ExtraField2'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
//append the new fields
|
|
|
|
$requiredFields->appendRequiredFields($otherRequiredFields);
|
|
|
|
//check they were added correctly
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'AnotherField',
|
|
|
|
'ExtraField1',
|
|
|
|
'ExtraField2'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Merging of required fields failed to behave as expected"
|
|
|
|
);
|
|
|
|
// create the standard validator so we can check duplicates are ignored
|
|
|
|
$otherRequiredFields = new RequiredFields(
|
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'AnotherField'
|
|
|
|
);
|
|
|
|
//add the new validator
|
|
|
|
$requiredFields->appendRequiredFields($otherRequiredFields);
|
|
|
|
//check nothing was changed
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'AnotherField',
|
|
|
|
'ExtraField1',
|
|
|
|
'ExtraField2'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Merging of required fields with duplicates failed to behave as expected"
|
|
|
|
);
|
|
|
|
//add some new fields and some old ones in a strange order
|
|
|
|
$otherRequiredFields = new RequiredFields(
|
|
|
|
'ExtraField3',
|
|
|
|
'Title',
|
|
|
|
'ExtraField4',
|
|
|
|
'Image',
|
|
|
|
'Content'
|
|
|
|
);
|
|
|
|
//add the new validator
|
|
|
|
$requiredFields->appendRequiredFields($otherRequiredFields);
|
|
|
|
//check that only the new fields were added
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'AnotherField',
|
|
|
|
'ExtraField1',
|
|
|
|
'ExtraField2',
|
|
|
|
'ExtraField3',
|
|
|
|
'ExtraField4'
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$requiredFields->getRequired(),
|
|
|
|
"Merging of required fields with some duplicates in a muddled order failed to behave as expected"
|
|
|
|
);
|
|
|
|
}
|
2013-07-15 22:44:44 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFieldIsRequired()
|
|
|
|
{
|
|
|
|
//get the validator
|
|
|
|
$requiredFields = new RequiredFields(
|
2020-04-20 19:58:09 +02:00
|
|
|
$fieldNames = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'Title',
|
|
|
|
'Content',
|
|
|
|
'Image',
|
|
|
|
'AnotherField'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2013-07-15 22:44:44 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
foreach ($fieldNames as $field) {
|
|
|
|
$this->assertTrue(
|
|
|
|
$requiredFields->fieldIsRequired($field),
|
|
|
|
sprintf("Failed to find '%s' field in required list", $field)
|
|
|
|
);
|
|
|
|
}
|
2013-07-15 22:44:44 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
//add a new field
|
|
|
|
$requiredFields->addRequiredField('ExtraField1');
|
|
|
|
//check the new field is required
|
|
|
|
$this->assertTrue(
|
|
|
|
$requiredFields->fieldIsRequired('ExtraField1'),
|
|
|
|
"Failed to find 'ExtraField1' field in required list after adding it to the list"
|
|
|
|
);
|
2021-12-13 09:05:33 +01:00
|
|
|
//check a non-existent field returns false
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$requiredFields->fieldIsRequired('DoesntExist'),
|
2021-12-13 09:05:33 +01:00
|
|
|
"Unexpectedly returned true for a non-existent field"
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
2023-11-13 01:02:38 +01:00
|
|
|
|
2024-05-02 02:06:43 +02:00
|
|
|
public function provideHasOneRelationFieldInterfaceValidation(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'className' => TreeDropdownField::class,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'className' => SearchableDropdownField::class,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideHasOneRelationFieldInterfaceValidation
|
|
|
|
*/
|
|
|
|
public function testHasOneRelationFieldInterfaceValidation(string $className)
|
2023-11-13 01:02:38 +01:00
|
|
|
{
|
|
|
|
$form = new Form();
|
2024-05-02 02:06:43 +02:00
|
|
|
$param = $className === TreeDropdownField::class ? Group::class : Group::get();
|
|
|
|
$field = new $className('TestField', 'TestField', $param);
|
2023-11-13 01:02:38 +01:00
|
|
|
$form->Fields()->push($field);
|
|
|
|
$validator = new RequiredFields('TestField');
|
|
|
|
$validator->setForm($form);
|
2024-05-02 02:06:43 +02:00
|
|
|
// blank string and 0 and '0' and array with value of 0 fail required field validation
|
2023-11-13 01:02:38 +01:00
|
|
|
$this->assertFalse($validator->php(['TestField' => '']));
|
2024-05-02 02:06:43 +02:00
|
|
|
$this->assertFalse($validator->php(['TestField' => 0]));
|
2023-11-13 01:02:38 +01:00
|
|
|
$this->assertFalse($validator->php(['TestField' => '0']));
|
2024-05-02 02:06:43 +02:00
|
|
|
$this->assertFalse($validator->php(['TestField' => ['value' => 0]]));
|
|
|
|
$this->assertFalse($validator->php(['TestField' => ['value' => '0']]));
|
2023-11-13 01:02:38 +01:00
|
|
|
// '1' passes required field validation
|
|
|
|
$this->assertTrue($validator->php(['TestField' => '1']));
|
|
|
|
}
|
2013-07-15 22:44:44 +02:00
|
|
|
}
|