BUGFIX Ensure that CheckboxField always returns 1 from the form request data instead of "on" - this was because the value attribute didn't exist on the <input> tag

MINOR Added tests for checking saveInto() behaviour on CheckboxFieldTest


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@75049 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2009-04-23 21:38:15 +00:00 committed by Sam Minnee
parent c272082448
commit bc443b109a
2 changed files with 56 additions and 0 deletions

View File

@ -26,6 +26,7 @@ class CheckboxField extends FormField {
'class' => ($this->extraClass() ? $this->extraClass() : ''), 'class' => ($this->extraClass() ? $this->extraClass() : ''),
'id' => $this->id(), 'id' => $this->id(),
'name' => $this->Name(), 'name' => $this->Name(),
'value' => 1,
'checked' => $this->value ? 'checked' : '', 'checked' => $this->value ? 'checked' : '',
'tabindex' => $this->getTabIndex() 'tabindex' => $this->getTabIndex()
); );

View File

@ -64,5 +64,60 @@ class CheckboxFieldTest extends SapphireTest {
$this->assertEquals($field->Value(), 0, 'Value() returns a 0'); $this->assertEquals($field->Value(), 0, 'Value() returns a 0');
} }
function testSavingChecked() {
/* Create a new test data record */
$article = new CheckboxFieldTest_Article();
/* Create a field, with a value of 1 */
$field = new CheckboxField('IsChecked', 'Checked', 1);
/* Save the field into our Article object */
$field->saveInto($article);
/* Write the record to the test database */
$article->write();
/* Check that IsChecked column contains a 1 */
$this->assertEquals(
DB::query("SELECT IsChecked FROM CheckboxFieldTest_Article")->value(),
1,
'We have a 1 set in the database, because the field saved into as a 1'
);
/* Delete the record we tested */
$article->delete();
}
function testSavingUnchecked() {
/* Create a new test data record */
$article = new CheckboxFieldTest_Article();
/* Create a field, with no value */
$field = new CheckboxField('IsChecked', 'Checked');
/* Save the field into our Article object */
$field->saveInto($article);
/* Write the record to the test database */
$article->write();
/* Check that IsChecked column contains a 0 */
$this->assertEquals(
DB::query("SELECT IsChecked FROM CheckboxFieldTest_Article")->value(),
0,
'We have a 0 set in the database, because the field saved into as a 0'
);
/* Delete the record we tested */
$article->delete();
}
}
class CheckboxFieldTest_Article extends DataObject implements TestOnly {
public static $db = array(
'IsChecked' => 'Boolean'
);
} }
?> ?>