2015-05-22 21:55:54 +02:00
|
|
|
<?php
|
|
|
|
|
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\GroupedDropdownField;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class GroupedDropdownFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
public function testValidation()
|
|
|
|
{
|
|
|
|
$field = GroupedDropdownField::create(
|
|
|
|
'Test',
|
|
|
|
'Testing',
|
2019-05-27 03:58:51 +02:00
|
|
|
[
|
|
|
|
"1" => "One",
|
|
|
|
"Group One" => [
|
|
|
|
"2" => "Two",
|
|
|
|
"3" => "Three"
|
|
|
|
],
|
|
|
|
"Group Two" => [
|
|
|
|
"4" => "Four"
|
|
|
|
],
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(["1", "2", "3", "4"], $field->getValidValues());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
|
|
|
|
$field->setValue("1");
|
|
|
|
$this->assertTrue($field->validate($validator));
|
|
|
|
|
|
|
|
//test grouped values
|
|
|
|
$field->setValue("3");
|
|
|
|
$this->assertTrue($field->validate($validator));
|
|
|
|
|
|
|
|
//non-existent value should make the field invalid
|
|
|
|
$field->setValue("Over 9000");
|
|
|
|
$this->assertFalse($field->validate($validator));
|
|
|
|
|
|
|
|
//empty string shouldn't validate
|
|
|
|
$field->setValue('');
|
|
|
|
$this->assertFalse($field->validate($validator));
|
|
|
|
|
|
|
|
//empty field should validate after being set
|
|
|
|
$field->setEmptyString('Empty String');
|
|
|
|
$field->setValue('');
|
|
|
|
$this->assertTrue($field->validate($validator));
|
|
|
|
|
|
|
|
//disabled items shouldn't validate
|
2020-04-20 19:58:09 +02:00
|
|
|
$field->setDisabledItems(['1']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$field->setValue('1');
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(["2", "3", "4"], $field->getValidValues());
|
|
|
|
$this->assertEquals(["1"], $field->getDisabledItems());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertFalse($field->validate($validator));
|
|
|
|
}
|
2019-05-27 03:58:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that empty-string values are supported by GroupDropdownTest
|
|
|
|
*/
|
|
|
|
public function testEmptyString()
|
|
|
|
{
|
|
|
|
// Case A: empty value in the top level of the source
|
|
|
|
$field = GroupedDropdownField::create(
|
|
|
|
'Test',
|
|
|
|
'Testing',
|
|
|
|
[
|
|
|
|
"" => "(Choose A)",
|
|
|
|
"1" => "One",
|
|
|
|
"Group One" => [
|
|
|
|
"2" => "Two",
|
|
|
|
"3" => "Three"
|
|
|
|
],
|
|
|
|
"Group Two" => [
|
|
|
|
"4" => "Four"
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/<option value="" selected="selected" >\(Choose A\)<\/option>/',
|
|
|
|
preg_replace('/\s+/', ' ', (string)$field->Field())
|
|
|
|
);
|
|
|
|
|
|
|
|
// Case B: empty value in the nested level of the source
|
|
|
|
$field = GroupedDropdownField::create(
|
|
|
|
'Test',
|
|
|
|
'Testing',
|
|
|
|
[
|
|
|
|
"1" => "One",
|
|
|
|
"Group One" => [
|
|
|
|
"" => "(Choose B)",
|
|
|
|
"2" => "Two",
|
|
|
|
"3" => "Three"
|
|
|
|
],
|
|
|
|
"Group Two" => [
|
|
|
|
"4" => "Four"
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/<option value="" selected="selected" >\(Choose B\)<\/option>/',
|
|
|
|
preg_replace('/\s+/', ' ', (string)$field->Field())
|
|
|
|
);
|
|
|
|
|
|
|
|
// Case C: setEmptyString
|
|
|
|
$field = GroupedDropdownField::create(
|
|
|
|
'Test',
|
|
|
|
'Testing',
|
|
|
|
[
|
|
|
|
"1" => "One",
|
|
|
|
"Group One" => [
|
|
|
|
"2" => "Two",
|
|
|
|
"3" => "Three"
|
|
|
|
],
|
|
|
|
"Group Two" => [
|
|
|
|
"4" => "Four"
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$field->setEmptyString('(Choose C)');
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/<option value="" selected="selected" >\(Choose C\)<\/option>/',
|
|
|
|
preg_replace('/\s+/', ' ', (string)$field->Field())
|
|
|
|
);
|
|
|
|
}
|
2020-07-16 14:40:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that readonly version of GroupedDropdownField displays all values
|
|
|
|
*/
|
|
|
|
public function testReadonlyValue()
|
|
|
|
{
|
|
|
|
$field = GroupedDropdownField::create(
|
|
|
|
'Test',
|
|
|
|
'Testing',
|
|
|
|
[
|
|
|
|
"1" => "One",
|
|
|
|
"Group One" => [
|
|
|
|
"2" => "Two",
|
|
|
|
"3" => "Three"
|
|
|
|
],
|
|
|
|
"Group Two" => [
|
|
|
|
"4" => "Four"
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
// value on first level
|
|
|
|
$field->setValue("1");
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/<span class="readonly" id="Test">One<\/span><input type="hidden" name="Test" value="1" \/>/',
|
|
|
|
(string)$field->performReadonlyTransformation()->Field()
|
|
|
|
);
|
|
|
|
|
|
|
|
// value on first level
|
|
|
|
$field->setValue("2");
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/<span class="readonly" id="Test">Two<\/span><input type="hidden" name="Test" value="2" \/>/',
|
|
|
|
(string)$field->performReadonlyTransformation()->Field()
|
|
|
|
);
|
|
|
|
}
|
2015-05-22 21:55:54 +02:00
|
|
|
}
|