mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Add more tests for CheckboxField_Readonly and CompositeField, improve PHPDocs
This commit is contained in:
parent
3cdb73bd44
commit
60b375d995
@ -21,11 +21,13 @@ class CompositeField extends FormField
|
||||
|
||||
/**
|
||||
* Set to true when this field is a readonly field
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $readonly;
|
||||
|
||||
/**
|
||||
* @var $columnCount int Toggle different css-rendering for multiple columns
|
||||
* @var int Toggle different css-rendering for multiple columns
|
||||
* ("onecolumn", "twocolumns", "threecolumns"). The content is determined
|
||||
* by the $children-array, so wrap all items you want to have grouped in a
|
||||
* column inside a CompositeField.
|
||||
@ -35,12 +37,12 @@ class CompositeField extends FormField
|
||||
protected $columnCount = null;
|
||||
|
||||
/**
|
||||
* @var String custom HTML tag to render with, e.g. to produce a <fieldset>.
|
||||
* @var string custom HTML tag to render with, e.g. to produce a <fieldset>.
|
||||
*/
|
||||
protected $tag = 'div';
|
||||
|
||||
/**
|
||||
* @var String Optional description for this set of fields.
|
||||
* @var string Optional description for this set of fields.
|
||||
* If the {@link $tag} property is set to use a 'fieldset', this will be
|
||||
* rendered as a <legend> tag, otherwise its a 'title' attribute.
|
||||
*/
|
||||
@ -214,7 +216,7 @@ class CompositeField extends FormField
|
||||
'tabindex' => null,
|
||||
'type' => null,
|
||||
'value' => null,
|
||||
'title' => ($this->tag == 'fieldset') ? null : $this->legend
|
||||
'title' => ($this->tag === 'fieldset') ? null : $this->legend
|
||||
)
|
||||
);
|
||||
}
|
||||
|
17
tests/php/Forms/CheckboxField_ReadonlyTest.php
Normal file
17
tests/php/Forms/CheckboxField_ReadonlyTest.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\Forms\Tests;
|
||||
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\Forms\CheckboxField_Readonly;
|
||||
|
||||
class CheckboxField_ReadonlyTest extends SapphireTest
|
||||
{
|
||||
public function testPerformReadonlyTransformation()
|
||||
{
|
||||
$field = new CheckboxField_Readonly('Test');
|
||||
$result = $field->performReadonlyTransformation();
|
||||
$this->assertInstanceOf(CheckboxField_Readonly::class, $result);
|
||||
$this->assertNotSame($result, $field);
|
||||
}
|
||||
}
|
@ -2,13 +2,14 @@
|
||||
|
||||
namespace SilverStripe\Forms\Tests;
|
||||
|
||||
use PHPUnit_Framework_Error;
|
||||
use SilverStripe\Dev\CSSContentParser;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\Forms\FieldList;
|
||||
use SilverStripe\Forms\TextField;
|
||||
use SilverStripe\Forms\CompositeField;
|
||||
use SilverStripe\Forms\DropdownField;
|
||||
use SilverStripe\Forms\FieldList;
|
||||
use SilverStripe\Forms\RequiredFields;
|
||||
use SilverStripe\Forms\TextField;
|
||||
|
||||
class CompositeFieldTest extends SapphireTest
|
||||
{
|
||||
@ -36,6 +37,9 @@ class CompositeFieldTest extends SapphireTest
|
||||
$this->assertEquals(0, $compositeOuter->fieldPosition('A'));
|
||||
$this->assertEquals(1, $compositeOuter->fieldPosition('AB'));
|
||||
$this->assertEquals(2, $compositeOuter->fieldPosition('B'));
|
||||
|
||||
$this->assertFalse($compositeOuter->fieldPosition(null), 'Falsy input should return false');
|
||||
$this->assertFalse($compositeOuter->fieldPosition('FOO'), 'Non-exitent child should return false');
|
||||
}
|
||||
|
||||
public function testTag()
|
||||
@ -124,4 +128,146 @@ class CompositeFieldTest extends SapphireTest
|
||||
$this->assertEquals($expectedChildren, $field->getChildren());
|
||||
$this->assertEquals($field, $expectedChildren->getContainerField());
|
||||
}
|
||||
|
||||
public function testExtraClass()
|
||||
{
|
||||
$field = CompositeField::create();
|
||||
$field->setColumnCount(3);
|
||||
$result = $field->extraClass();
|
||||
|
||||
$this->assertContains('field', $result, 'Default class was not added');
|
||||
$this->assertContains('CompositeField', $result, 'Default class was not added');
|
||||
$this->assertContains('multicolumn', $result, 'Multi column field did not have extra class added');
|
||||
}
|
||||
|
||||
public function testGetAttributes()
|
||||
{
|
||||
$field = CompositeField::create();
|
||||
$field->setLegend('test');
|
||||
$result = $field->getAttributes();
|
||||
|
||||
$this->assertNull($result['tabindex']);
|
||||
$this->assertNull($result['type']);
|
||||
$this->assertNull($result['value']);
|
||||
$this->assertSame('test', $result['title']);
|
||||
}
|
||||
|
||||
public function testGetAttributesReturnsEmptyTitleForFieldSets()
|
||||
{
|
||||
$field = CompositeField::create();
|
||||
$field->setLegend('not used');
|
||||
$field->setTag('fieldset');
|
||||
$result = $field->getAttributes();
|
||||
$this->assertNull($result['title']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException PHPUnit_Framework_Error
|
||||
* @expectedExceptionMessageRegExp /a field called 'Test' appears twice in your form.*TextField.*TextField/
|
||||
*/
|
||||
public function testCollateDataFieldsThrowsErrorOnDuplicateChildren()
|
||||
{
|
||||
$field = CompositeField::create(
|
||||
TextField::create('Test'),
|
||||
TextField::create('Test')
|
||||
);
|
||||
|
||||
$list = [];
|
||||
$field->collateDataFields($list);
|
||||
}
|
||||
|
||||
public function testCollateDataFieldsWithSaveableOnly()
|
||||
{
|
||||
$field = CompositeField::create(
|
||||
TextField::create('Test')
|
||||
->setReadonly(false)
|
||||
->setDisabled(true)
|
||||
);
|
||||
|
||||
$list = [];
|
||||
$field->collateDataFields($list, true);
|
||||
$this->assertEmpty($list, 'Unsaveable fields should not be collated when $saveableOnly = true');
|
||||
|
||||
$field->collateDataFields($list, false);
|
||||
$this->assertNotEmpty($list, 'Unsavable fields should be collated when $saveableOnly = false');
|
||||
}
|
||||
|
||||
public function testSetDisabledPropagatesToChildren()
|
||||
{
|
||||
$field = CompositeField::create(
|
||||
$testField = TextField::create('Test')
|
||||
->setDisabled(false)
|
||||
)->setDisabled(true);
|
||||
$this->assertTrue($field->fieldByName('Test')->isDisabled(), 'Children should also be set to disabled');
|
||||
}
|
||||
|
||||
public function testIsComposite()
|
||||
{
|
||||
$this->assertTrue(CompositeField::create()->isComposite());
|
||||
}
|
||||
|
||||
public function testMakeFieldReadonlyPassedFieldName()
|
||||
{
|
||||
$field = CompositeField::create(
|
||||
TextField::create('Test')->setDisabled(false)
|
||||
);
|
||||
|
||||
$this->assertFalse($field->fieldByName('Test')->isReadonly());
|
||||
$this->assertTrue($field->makeFieldReadonly('Test'), 'makeFieldReadonly should return true');
|
||||
$this->assertTrue($field->fieldByName('Test')->isReadonly(), 'Named child field should be made readonly');
|
||||
}
|
||||
|
||||
public function testMakeFieldReadonlyPassedFormField()
|
||||
{
|
||||
$field = CompositeField::create(
|
||||
$testField = TextField::create('Test')->setDisabled(false)
|
||||
);
|
||||
|
||||
$this->assertFalse($field->fieldByName('Test')->isReadonly());
|
||||
$this->assertTrue($field->makeFieldReadonly($testField), 'makeFieldReadonly should return true');
|
||||
$this->assertTrue($field->fieldByName('Test')->isReadonly(), 'Named child field should be made readonly');
|
||||
}
|
||||
|
||||
public function testMakeFieldReadonlyWithNestedCompositeFields()
|
||||
{
|
||||
$field = CompositeField::create(
|
||||
CompositeField::create(
|
||||
TextField::create('Test')->setDisabled(false)
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertFalse($field->getChildren()->first()->fieldByName('Test')->isReadonly());
|
||||
$this->assertTrue($field->makeFieldReadonly('Test'), 'makeFieldReadonly should return true');
|
||||
$this->assertTrue(
|
||||
$field->getChildren()->first()->fieldByName('Test')->isReadonly(),
|
||||
'Named child field should be made readonly'
|
||||
);
|
||||
}
|
||||
|
||||
public function testMakeFieldReadonlyReturnsFalseWhenFieldNotFound()
|
||||
{
|
||||
$field = CompositeField::create(
|
||||
CompositeField::create(
|
||||
TextField::create('Test')
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
$field->makeFieldReadonly('NonExistent'),
|
||||
'makeFieldReadonly should return false when field is not found'
|
||||
);
|
||||
}
|
||||
|
||||
public function testDebug()
|
||||
{
|
||||
$field = new CompositeField(
|
||||
new TextField('TestTextField')
|
||||
);
|
||||
$field->setName('TestComposite');
|
||||
|
||||
$result = $field->debug();
|
||||
$this->assertContains(CompositeField::class . ' (TestComposite)', $result);
|
||||
$this->assertContains('TestTextField', $result);
|
||||
$this->assertContains('<ul', $result, 'Result should be formatted as a <ul>');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user