mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Removed error when saving checkboxsetfield with no values.
Added tests for CheckboxSetField git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60664 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
cb2919f56d
commit
e020ff3f25
@ -120,7 +120,7 @@ class CheckboxSetField extends OptionsetField {
|
||||
|
||||
if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
|
||||
$idList = array();
|
||||
foreach($this->value as $id => $bool) {
|
||||
if($this->value) foreach($this->value as $id => $bool) {
|
||||
if($bool) $idList[] = $id;
|
||||
}
|
||||
$record->$fieldname()->setByIDList($idList);
|
||||
|
57
tests/forms/CheckboxSetFieldTest.php
Normal file
57
tests/forms/CheckboxSetFieldTest.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
class CheckboxSetFieldTest extends SapphireTest {
|
||||
|
||||
function testSaveWithNothingSelected() {
|
||||
/* Set up a test data object */
|
||||
$one = new CheckboxSetFieldTest_Obj();
|
||||
$one->A = "A1";
|
||||
$one->write();
|
||||
|
||||
/* Create a CheckboxSetField with nothing selected */
|
||||
$field = new CheckboxSetField("Relation", "Test field", DataObject::get("CheckboxSetFieldTest_Obj")->map());
|
||||
|
||||
/* Saving should work */
|
||||
$field->saveInto($one);
|
||||
|
||||
/* Nothing should go into CheckboxSetFieldTest_Obj_Relation */
|
||||
$this->assertNull(DB::query("SELECT * FROM CheckboxSetFieldTest_Obj_Relation")->value());
|
||||
}
|
||||
|
||||
function testSaveWithArrayValueSet() {
|
||||
/* Set up two test data object */
|
||||
$one = new CheckboxSetFieldTest_Obj();
|
||||
$one->A = "A1";
|
||||
$one->write();
|
||||
|
||||
$two = new CheckboxSetFieldTest_Obj();
|
||||
$two->A = "A2";
|
||||
$two->write();
|
||||
|
||||
/* Create a CheckboxSetField with 2 items selected. Note that the array is in the format (key) => (selected) */
|
||||
$field = new CheckboxSetField("Relation", "Test field", DataObject::get("CheckboxSetFieldTest_Obj")->map());
|
||||
$field->setValue(array(
|
||||
1 => true,
|
||||
2 => true));
|
||||
|
||||
/* Saving should work */
|
||||
$field->saveInto($one);
|
||||
|
||||
/* Data shold be saved into CheckboxSetField */
|
||||
$this->assertEquals(array($one->ID,$one->ID), DB::query("SELECT CheckboxSetFieldTest_ObjID FROM CheckboxSetFieldTest_Obj_Relation")->column());
|
||||
$this->assertEquals(array(1,2), DB::query("SELECT ChildID FROM CheckboxSetFieldTest_Obj_Relation")->column());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class CheckboxSetFieldTest_Obj extends DataObject implements TestOnly {
|
||||
static $db = array(
|
||||
"A" => "Varchar",
|
||||
"B" => "Varchar",
|
||||
);
|
||||
|
||||
static $many_many = array(
|
||||
"Relation" => "CheckboxSetFieldTest_Obj",
|
||||
);
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user