silverstripe-framework/tests/php/Forms/NullableFieldTests.php

152 lines
4.7 KiB
PHP
Raw Permalink Normal View History

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