2008-12-04 23:38:32 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-12-04 23:38:32 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2015-01-14 14:07:41 +01:00
|
|
|
class FormFieldTest extends SapphireTest {
|
|
|
|
|
2015-02-08 07:14:31 +01:00
|
|
|
protected $requiredExtensions = array(
|
|
|
|
'FormField' => array('FormFieldTest_Extension')
|
|
|
|
);
|
|
|
|
|
2015-01-14 14:07:41 +01:00
|
|
|
public function testDefaultClasses() {
|
|
|
|
Config::nest();
|
|
|
|
|
|
|
|
Config::inst()->update('FormField', 'default_classes', array(
|
|
|
|
'class1',
|
|
|
|
));
|
|
|
|
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
|
|
|
|
$this->assertContains('class1', $field->extraClass(), 'Class list does not contain expected class');
|
|
|
|
|
|
|
|
Config::inst()->update('FormField', 'default_classes', array(
|
|
|
|
'class1',
|
|
|
|
'class2',
|
|
|
|
));
|
|
|
|
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
|
|
|
|
$this->assertContains('class1 class2', $field->extraClass(), 'Class list does not contain expected class');
|
|
|
|
|
|
|
|
Config::inst()->update('FormField', 'default_classes', array(
|
|
|
|
'class3',
|
|
|
|
));
|
|
|
|
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
|
|
|
|
$this->assertContains('class3', $field->extraClass(), 'Class list does not contain expected class');
|
|
|
|
|
|
|
|
$field->removeExtraClass('class3');
|
|
|
|
|
|
|
|
$this->assertNotContains('class3', $field->extraClass(), 'Class list contains unexpected class');
|
|
|
|
|
|
|
|
Config::inst()->update('TextField', 'default_classes', array(
|
|
|
|
'textfield-class',
|
|
|
|
));
|
|
|
|
|
|
|
|
$field = new TextField('MyField');
|
|
|
|
|
|
|
|
//check default classes inherit
|
|
|
|
$this->assertContains('class3', $field->extraClass(), 'Class list does not contain inherited class');
|
|
|
|
$this->assertContains('textfield-class', $field->extraClass(), 'Class list does not contain expected class');
|
|
|
|
|
|
|
|
Config::unnest();
|
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2012-10-31 15:44:57 +01:00
|
|
|
public function testAddExtraClass() {
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->addExtraClass('class1');
|
|
|
|
$field->addExtraClass('class2');
|
|
|
|
$this->assertStringEndsWith('class1 class2', $field->extraClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveExtraClass() {
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->addExtraClass('class1');
|
|
|
|
$field->addExtraClass('class2');
|
|
|
|
$this->assertStringEndsWith('class1 class2', $field->extraClass());
|
|
|
|
$field->removeExtraClass('class1');
|
|
|
|
$this->assertStringEndsWith('class2', $field->extraClass());
|
|
|
|
}
|
|
|
|
|
2013-06-29 13:07:59 +02:00
|
|
|
public function testAddManyExtraClasses() {
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
//test we can split by a range of spaces and tabs
|
|
|
|
$field->addExtraClass('class1 class2 class3 class4 class5');
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'class1 class2 class3 class4 class5',
|
|
|
|
$field->extraClass()
|
|
|
|
);
|
|
|
|
//test that duplicate classes don't get added
|
|
|
|
$field->addExtraClass('class1 class2');
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'class1 class2 class3 class4 class5',
|
|
|
|
$field->extraClass()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveManyExtraClasses() {
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->addExtraClass('class1 class2 class3 class4 class5');
|
|
|
|
//test we can remove a single class we just added
|
|
|
|
$field->removeExtraClass('class3');
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'class1 class2 class4 class5',
|
|
|
|
$field->extraClass()
|
|
|
|
);
|
|
|
|
//check we can remove many classes at once
|
|
|
|
$field->removeExtraClass('class1 class5');
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'class2 class4',
|
|
|
|
$field->extraClass()
|
|
|
|
);
|
|
|
|
//check that removing a dud class is fine
|
|
|
|
$field->removeExtraClass('dudClass');
|
|
|
|
$this->assertStringEndsWith(
|
|
|
|
'class2 class4',
|
|
|
|
$field->extraClass()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAttributes() {
|
2011-12-22 13:10:57 +01:00
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->setAttribute('foo', 'bar');
|
|
|
|
$this->assertEquals('bar', $field->getAttribute('foo'));
|
|
|
|
$attrs = $field->getAttributes();
|
|
|
|
$this->assertArrayHasKey('foo', $attrs);
|
|
|
|
$this->assertEquals('bar', $attrs['foo']);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAttributesHTML() {
|
2011-12-22 13:10:57 +01:00
|
|
|
$field = new FormField('MyField');
|
|
|
|
|
|
|
|
$field->setAttribute('foo', 'bar');
|
|
|
|
$this->assertContains('foo="bar"', $field->getAttributesHTML());
|
|
|
|
|
|
|
|
$field->setAttribute('foo', null);
|
|
|
|
$this->assertNotContains('foo=', $field->getAttributesHTML());
|
|
|
|
|
|
|
|
$field->setAttribute('foo', '');
|
|
|
|
$this->assertNotContains('foo=', $field->getAttributesHTML());
|
|
|
|
|
|
|
|
$field->setAttribute('foo', false);
|
|
|
|
$this->assertNotContains('foo=', $field->getAttributesHTML());
|
|
|
|
|
|
|
|
$field->setAttribute('foo', true);
|
|
|
|
$this->assertContains('foo="foo"', $field->getAttributesHTML());
|
|
|
|
|
|
|
|
$field->setAttribute('foo', 'false');
|
|
|
|
$this->assertContains('foo="false"', $field->getAttributesHTML());
|
|
|
|
|
|
|
|
$field->setAttribute('foo', 'true');
|
|
|
|
$this->assertContains('foo="true"', $field->getAttributesHTML());
|
|
|
|
|
|
|
|
$field->setAttribute('foo', 0);
|
|
|
|
$this->assertContains('foo="0"', $field->getAttributesHTML());
|
|
|
|
|
|
|
|
$field->setAttribute('one', 1);
|
|
|
|
$field->setAttribute('two', 2);
|
|
|
|
$field->setAttribute('three', 3);
|
|
|
|
$this->assertNotContains('one="1"', $field->getAttributesHTML('one', 'two'));
|
|
|
|
$this->assertNotContains('two="2"', $field->getAttributesHTML('one', 'two'));
|
|
|
|
$this->assertContains('three="3"', $field->getAttributesHTML('one', 'two'));
|
|
|
|
}
|
2012-10-31 15:57:48 +01:00
|
|
|
|
2014-03-18 05:00:53 +01:00
|
|
|
public function testReadonly() {
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->setReadonly(true);
|
|
|
|
$this->assertContains('readonly="readonly"', $field->getAttributesHTML());
|
|
|
|
$field->setReadonly(false);
|
|
|
|
$this->assertNotContains('readonly="readonly"', $field->getAttributesHTML());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisabled() {
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$field->setDisabled(true);
|
|
|
|
$this->assertContains('disabled="disabled"', $field->getAttributesHTML());
|
|
|
|
$field->setDisabled(false);
|
|
|
|
$this->assertNotContains('disabled="disabled"', $field->getAttributesHTML());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testEveryFieldTransformsReadonlyAsClone() {
|
2008-12-04 23:38:32 +01:00
|
|
|
$fieldClasses = ClassInfo::subclassesFor('FormField');
|
|
|
|
foreach($fieldClasses as $fieldClass) {
|
|
|
|
$reflectionClass = new ReflectionClass($fieldClass);
|
|
|
|
if(!$reflectionClass->isInstantiable()) continue;
|
|
|
|
$constructor = $reflectionClass->getMethod('__construct');
|
|
|
|
if($constructor->getNumberOfRequiredParameters() > 1) continue;
|
|
|
|
if($fieldClass == 'CompositeField' || is_subclass_of($fieldClass, 'CompositeField')) continue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
if ( $fieldClass = 'NullableField' ) {
|
|
|
|
$instance = new $fieldClass(new TextField("{$fieldClass}_instance"));
|
|
|
|
} else {
|
|
|
|
$instance = new $fieldClass("{$fieldClass}_instance");
|
|
|
|
}
|
2008-12-04 23:38:32 +01:00
|
|
|
$isReadonlyBefore = $instance->isReadonly();
|
|
|
|
$readonlyInstance = $instance->performReadonlyTransformation();
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$isReadonlyBefore,
|
2008-12-04 23:38:32 +01:00
|
|
|
$instance->isReadonly(),
|
2012-09-26 23:34:00 +02:00
|
|
|
"FormField class {$fieldClass} retains its readonly state after calling performReadonlyTransformation()"
|
2008-12-04 23:38:32 +01:00
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$readonlyInstance->isReadonly(),
|
2012-09-26 23:34:00 +02:00
|
|
|
"FormField class {$fieldClass} returns a valid readonly representation as of isReadonly()"
|
2008-12-04 23:38:32 +01:00
|
|
|
);
|
|
|
|
$this->assertNotSame(
|
|
|
|
$readonlyInstance,
|
|
|
|
$instance,
|
2012-09-26 23:34:00 +02:00
|
|
|
"FormField class {$fieldClass} returns a valid cloned readonly representation"
|
2008-12-04 23:38:32 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testEveryFieldTransformsDisabledAsClone() {
|
2008-12-04 23:38:32 +01:00
|
|
|
$fieldClasses = ClassInfo::subclassesFor('FormField');
|
|
|
|
foreach($fieldClasses as $fieldClass) {
|
|
|
|
$reflectionClass = new ReflectionClass($fieldClass);
|
|
|
|
if(!$reflectionClass->isInstantiable()) continue;
|
|
|
|
$constructor = $reflectionClass->getMethod('__construct');
|
|
|
|
if($constructor->getNumberOfRequiredParameters() > 1) continue;
|
|
|
|
if($fieldClass == 'CompositeField' || is_subclass_of($fieldClass, 'CompositeField')) continue;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
if ( $fieldClass = 'NullableField' ) {
|
|
|
|
$instance = new $fieldClass(new TextField("{$fieldClass}_instance"));
|
|
|
|
} else {
|
|
|
|
$instance = new $fieldClass("{$fieldClass}_instance");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
$isDisabledBefore = $instance->isDisabled();
|
|
|
|
$disabledInstance = $instance->performDisabledTransformation();
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
$isDisabledBefore,
|
2008-12-04 23:38:32 +01:00
|
|
|
$instance->isDisabled(),
|
2012-09-26 23:34:00 +02:00
|
|
|
"FormField class {$fieldClass} retains its disabled state after calling performDisabledTransformation()"
|
2008-12-04 23:38:32 +01:00
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$disabledInstance->isDisabled(),
|
2012-09-26 23:34:00 +02:00
|
|
|
"FormField class {$fieldClass} returns a valid disabled representation as of isDisabled()"
|
2008-12-04 23:38:32 +01:00
|
|
|
);
|
|
|
|
$this->assertNotSame(
|
|
|
|
$disabledInstance,
|
|
|
|
$instance,
|
2012-09-26 23:34:00 +02:00
|
|
|
"FormField class {$fieldClass} returns a valid cloned disabled representation"
|
2008-12-04 23:38:32 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-08 07:14:31 +01:00
|
|
|
public function testUpdateAttributes() {
|
|
|
|
$field = new FormField('MyField');
|
|
|
|
$this->assertArrayHasKey('extended', $field->getAttributes());
|
|
|
|
}
|
|
|
|
|
2015-01-14 14:07:41 +01:00
|
|
|
}
|
2015-02-08 07:14:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class FormFieldTest_Extension extends Extension implements TestOnly {
|
|
|
|
|
|
|
|
public function updateAttributes(&$attrs) {
|
|
|
|
$attrs['extended'] = true;
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:34:58 +01:00
|
|
|
}
|