silverstripe-framework/tests/forms/CompositeFieldTest.php

81 lines
2.2 KiB
PHP
Raw Normal View History

<?php
/**
* @package framework
* @subpackage tests
*/
class CompositeFieldTest extends SapphireTest {
2014-08-15 08:53:05 +02:00
public function testFieldPosition() {
$compositeOuter = new CompositeField(
new TextField('A'),
new TextField('B'),
$compositeInner = new CompositeField(
new TextField('C1'),
new TextField('C2')
),
new TextField('D')
);
$this->assertEquals(0, $compositeOuter->fieldPosition('A'));
$this->assertEquals(1, $compositeOuter->fieldPosition('B'));
$this->assertEquals(3, $compositeOuter->fieldPosition('D'));
2014-08-15 08:53:05 +02:00
$this->assertEquals(0, $compositeInner->fieldPosition('C1'));
$this->assertEquals(1, $compositeInner->fieldPosition('C2'));
2014-08-15 08:53:05 +02:00
$compositeOuter->insertBefore('B', new TextField('AB'));
$this->assertEquals(0, $compositeOuter->fieldPosition('A'));
$this->assertEquals(1, $compositeOuter->fieldPosition('AB'));
$this->assertEquals(2, $compositeOuter->fieldPosition('B'));
}
2014-08-15 08:53:05 +02:00
public function testTag() {
$div = new CompositeField(
new TextField('A'),
new TextField('B')
);
$this->assertStringStartsWith('<div', trim($div->FieldHolder()));
$this->assertStringEndsWith('/div>', trim($div->FieldHolder()));
2014-08-15 08:53:05 +02:00
$fieldset = new CompositeField();
$fieldset->setTag('fieldset');
$this->assertStringStartsWith('<fieldset', trim($fieldset->FieldHolder()));
2014-08-15 08:53:05 +02:00
$this->assertStringEndsWith('/fieldset>', trim($fieldset->FieldHolder()));
}
public function testLegend() {
$composite = new CompositeField(
new TextField('A'),
new TextField('B')
);
2014-08-15 08:53:05 +02:00
$composite->setTag('fieldset');
$composite->setLegend('My legend');
2014-08-15 08:53:05 +02:00
$parser = new CSSContentParser($composite->Field());
$root = $parser->getBySelector('fieldset.composite');
$legend = $parser->getBySelector('fieldset.composite legend');
2014-08-15 08:53:05 +02:00
$this->assertNotNull($legend);
$this->assertEquals('My legend', (string)$legend[0]);
}
public function testValidation() {
$field = CompositeField::create(
$fieldOne = DropdownField::create('A'),
$fieldTwo = TextField::create('B')
);
$validator = new RequiredFields();
$this->assertFalse(
$field->validate($validator),
"Validation fails when child is invalid"
);
$fieldOne->setEmptyString('empty');
$this->assertTrue(
$field->validate($validator),
"Validates when children are valid"
);
}
}