2009-10-23 03:29:55 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
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\TextField;
|
|
|
|
use SilverStripe\Forms\NullableField;
|
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
* Tests the NullableField form field class.
|
|
|
|
*
|
2016-10-14 03:30:05 +02:00
|
|
|
* @author Pete Bacon Darwin
|
2009-10-23 03:29:55 +02:00
|
|
|
*/
|
|
|
|
class NullableFieldTests extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
* Test that the NullableField works when it wraps a TextField containing actual content
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWithContent() {
|
2009-10-23 03:29:55 +02:00
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1", "abc"));
|
2011-10-29 13:07:40 +02:00
|
|
|
$this->assertEquals("Field1", $a->getName());
|
2009-10-23 03:29:55 +02:00
|
|
|
$this->assertEquals("Field 1", $a->Title());
|
|
|
|
$this->assertSame("abc", $a->Value());
|
|
|
|
$this->assertSame("abc", $a->dataValue());
|
|
|
|
$field = $a->Field();
|
|
|
|
$this->assertTag(array(
|
|
|
|
'tag'=>'input',
|
|
|
|
'id'=>'Field1',
|
|
|
|
'attributes'=>array('type'=>'text', 'name'=>'Field1', 'value'=>'abc'),
|
|
|
|
), $field);
|
|
|
|
$this->assertTag(array(
|
|
|
|
'tag'=>'input',
|
|
|
|
'id'=>'Field1_IsNull',
|
|
|
|
'attributes'=>array('type'=>'checkbox', 'name'=>'Field1_IsNull', 'value'=>'1'),
|
|
|
|
), $field);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Test that the NullableField works when it wraps a TextField containing an empty string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWithEmpty() {
|
2009-10-23 03:29:55 +02:00
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1", ""));
|
2011-10-29 13:07:40 +02:00
|
|
|
$this->assertEquals("Field1", $a->getName());
|
2009-10-23 03:29:55 +02:00
|
|
|
$this->assertEquals("Field 1", $a->Title());
|
|
|
|
$this->assertSame("", $a->Value());
|
|
|
|
$this->assertSame("", $a->dataValue());
|
|
|
|
$field = $a->Field();
|
|
|
|
$this->assertTag(array(
|
|
|
|
'tag'=>'input',
|
|
|
|
'id'=>'Field1',
|
|
|
|
'attributes'=>array('type'=>'text', 'name'=>'Field1', 'value'=>''),
|
|
|
|
), $field);
|
|
|
|
$this->assertTag(array(
|
|
|
|
'tag'=>'input',
|
|
|
|
'id'=>'Field1_IsNull',
|
|
|
|
'attributes'=>array('type'=>'checkbox', 'name'=>'Field1_IsNull', 'value'=>'1'),
|
|
|
|
), $field);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Test that the NullableField works when it wraps a TextField containing a null string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testWithNull() {
|
2009-10-23 03:29:55 +02:00
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1", null));
|
2011-10-29 13:07:40 +02:00
|
|
|
$this->assertEquals("Field1", $a->getName());
|
2009-10-23 03:29:55 +02:00
|
|
|
$this->assertEquals("Field 1", $a->Title());
|
|
|
|
$this->assertSame(null, $a->Value());
|
|
|
|
$this->assertSame(null, $a->dataValue());
|
|
|
|
$field = $a->Field();
|
|
|
|
$this->assertTag(array(
|
|
|
|
'tag'=>'input',
|
|
|
|
'id'=>'Field1',
|
|
|
|
'attributes'=>array('type'=>'text', 'name'=>'Field1', 'value'=>''),
|
|
|
|
), $field);
|
|
|
|
$this->assertTag(array(
|
|
|
|
'tag'=>'input',
|
|
|
|
'id'=>'Field1_IsNull',
|
|
|
|
'attributes'=>array('type'=>'checkbox', 'name'=>'Field1_IsNull', 'value'=>'1', 'checked'=>'checked'),
|
|
|
|
), $field);
|
|
|
|
unset($a);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
* Test NullableField::setValue works when passed simple values
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetValueSimple() {
|
2009-10-23 03:29:55 +02:00
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1"));
|
|
|
|
$a->setValue("abc");
|
|
|
|
$this->assertSame("abc", $a->dataValue());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1"));
|
|
|
|
$a->setValue(null);
|
|
|
|
$this->assertSame(null, $a->dataValue());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1", "abc"));
|
|
|
|
$a->setValue(null);
|
|
|
|
$this->assertSame(null, $a->dataValue());
|
|
|
|
|
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1", "abc"));
|
|
|
|
$a->setValue("xyz");
|
|
|
|
$this->assertSame("xyz", $a->dataValue());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1"));
|
|
|
|
$a->setValue("");
|
|
|
|
$this->assertSame("", $a->dataValue());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1", "abc"));
|
|
|
|
$a->setValue("");
|
|
|
|
$this->assertSame("", $a->dataValue());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-23 03:29:55 +02:00
|
|
|
/**
|
|
|
|
* Test NullableField::setValue works when passed an array values,
|
|
|
|
* which happens when the form submits.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetValueArray() {
|
2009-10-23 03:29:55 +02:00
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1"));
|
|
|
|
$a->setValue("abc", array("Field1_IsNull"=>false));
|
|
|
|
$this->assertSame("abc", $a->dataValue());
|
|
|
|
|
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1"));
|
|
|
|
$a->setValue("", array("Field1_IsNull"=>false));
|
|
|
|
$this->assertSame("", $a->dataValue());
|
|
|
|
|
|
|
|
$a = new NullableField(new TextField("Field1", "Field 1"));
|
|
|
|
$a->setValue("", array("Field1_IsNull"=>true));
|
|
|
|
$this->assertSame(null, $a->dataValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|