2010-02-22 07:11:58 +01:00
|
|
|
<?php
|
2016-08-22 17:39:11 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
2016-08-22 17:39:11 +02:00
|
|
|
|
2017-05-19 05:47:51 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\OptionsetField;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class OptionsetFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
public function testSetDisabledItems()
|
|
|
|
{
|
|
|
|
$f = new OptionsetField(
|
|
|
|
'Test',
|
|
|
|
false,
|
2020-04-20 19:58:09 +02:00
|
|
|
[0 => 'Zero', 1 => 'One']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$f->setDisabledItems([0]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$p = new CSSContentParser($f->Field());
|
|
|
|
$item0 = $p->getBySelector('#Test_0');
|
|
|
|
$item1 = $p->getBySelector('#Test_1');
|
|
|
|
$this->assertEquals(
|
|
|
|
(string)$item0[0]['disabled'],
|
|
|
|
'disabled'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
(string)$item1[0]['disabled'],
|
|
|
|
''
|
|
|
|
);
|
|
|
|
}
|
2013-12-11 22:23:29 +01:00
|
|
|
|
2017-03-02 03:24:38 +01:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testValidation()
|
|
|
|
{
|
|
|
|
$field = OptionsetField::create(
|
|
|
|
'Test',
|
|
|
|
'Testing',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
"One" => "One",
|
|
|
|
"Two" => "Two",
|
|
|
|
"Five" => "Five"
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$validator = new RequiredFields('Test');
|
2017-03-02 03:24:38 +01:00
|
|
|
$form = new Form(null, 'Form', new FieldList($field), new FieldList(), $validator);
|
2015-12-14 17:41:18 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$field->setValue("One");
|
|
|
|
$this->assertTrue($field->validate($validator));
|
2015-12-14 17:41:18 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
//non-existent value should make the field invalid
|
|
|
|
$field->setValue("Three");
|
|
|
|
$this->assertFalse($field->validate($validator));
|
2015-12-14 17:41:18 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
//empty string should pass field-level validation...
|
|
|
|
$field->setValue('');
|
|
|
|
$this->assertTrue($field->validate($validator));
|
2015-12-14 17:41:18 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// ... but should not pass "RequiredFields" validation
|
|
|
|
$this->assertFalse($form->validationResult()->isValid());
|
2015-12-14 17:41:18 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
//disabled items shouldn't validate
|
2020-04-20 19:58:09 +02:00
|
|
|
$field->setDisabledItems(['Five']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$field->setValue('Five');
|
|
|
|
$this->assertFalse($field->validate($validator));
|
|
|
|
}
|
2015-12-14 17:41:18 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testReadonlyField()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$sourceArray = [0 => 'No', 1 => 'Yes'];
|
2016-12-16 05:34:21 +01:00
|
|
|
$field = new OptionsetField('FeelingOk', 'are you feeling ok?', $sourceArray, 1);
|
|
|
|
$field->setEmptyString('(Select one)');
|
|
|
|
$field->setValue(1);
|
|
|
|
$readonlyField = $field->performReadonlyTransformation();
|
2022-04-14 03:12:59 +02:00
|
|
|
preg_match('/Yes/', $readonlyField->Field() ?? '', $matches);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($matches[0], 'Yes');
|
|
|
|
}
|
2016-08-03 01:23:17 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testSafelyCast()
|
|
|
|
{
|
|
|
|
$field1 = new OptionsetField(
|
|
|
|
'Options',
|
|
|
|
'Options',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
1 => 'One',
|
|
|
|
2 => 'Two & Three',
|
|
|
|
3 => DBField::create_field('HTMLText', 'Four & Five & Six')
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$fieldHTML = (string)$field1->Field();
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('One', $fieldHTML);
|
|
|
|
$this->assertStringContainsString('Two & Three', $fieldHTML);
|
|
|
|
$this->assertStringNotContainsString('Two & Three', $fieldHTML);
|
|
|
|
$this->assertStringContainsString('Four & Five & Six', $fieldHTML);
|
|
|
|
$this->assertStringNotContainsString('Four & Five & Six', $fieldHTML);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2017-05-19 05:47:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* #2939 OptionSetField creates invalid HTML when required
|
|
|
|
*/
|
|
|
|
public function testNoAriaRequired()
|
|
|
|
{
|
|
|
|
$field = new OptionsetField('RequiredField', 'myRequiredField');
|
|
|
|
|
|
|
|
$form = new Form(
|
2017-05-22 23:13:50 +02:00
|
|
|
Controller::curr(),
|
|
|
|
"form",
|
|
|
|
new FieldList($field),
|
|
|
|
new FieldList(),
|
2017-05-19 05:47:51 +02:00
|
|
|
new RequiredFields(["RequiredField"])
|
|
|
|
);
|
|
|
|
$this->assertTrue($field->Required());
|
|
|
|
|
|
|
|
$attributes = $field->getAttributes();
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertFalse(array_key_exists("name", $attributes ?? []));
|
|
|
|
$this->assertFalse(array_key_exists("required", $attributes ?? []));
|
|
|
|
$this->assertTrue(array_key_exists("role", $attributes ?? []));
|
2017-05-19 05:47:51 +02:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|