BUGFIX: Fix the broken perform readonly transformation which was always showing NO (#6453)

This commit is contained in:
Nicolaas 2012-03-24 13:17:48 +13:00 committed by Sam Minnee
parent fd649a418d
commit dee3939cf7
2 changed files with 21 additions and 4 deletions

View File

@ -50,7 +50,7 @@ class CheckboxField extends FormField {
* Returns a readonly version of this field
*/
function performReadonlyTransformation() {
$field = new CheckboxField_Readonly($this->name, $this->title, $this->value ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No'));
$field = new CheckboxField_Readonly($this->name, $this->title, $this->value);
$field->setForm($this->form);
return $field;
}
@ -73,9 +73,9 @@ class CheckboxField_Readonly extends ReadonlyField {
function performReadonlyTransformation() {
return clone $this;
}
function setValue($val) {
$this->value = (int)($val) ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No');
function Value() {
return Convert::raw2xml($this->value ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No'));
}
}

View File

@ -117,6 +117,23 @@ class CheckboxFieldTest extends SapphireTest {
/* Delete the record we tested */
$article->delete();
}
function testReadonlyCheckboxField() {
// Test 1: a checked checkbox goes to "Yes"
$field1 = new CheckboxField('IsChecked', 'Checked');
$field1->setValue('on');
$this->assertEquals(_t('CheckboxField.YES', 'Yes'), trim(strip_tags($field1->performReadonlyTransformation()->Field())));
// Test 2: an checkbox with the value set to false to "No"
$field2 = new CheckboxField('IsChecked', 'Checked');
$field2->setValue(false);
$this->assertEquals(_t('CheckboxField.NO', 'No'), trim(strip_tags($field2->performReadonlyTransformation()->Field())));
// Test 3: an checkbox with no value ever set goes to "No"
$field3 = new CheckboxField('IsChecked', 'Checked');
$this->assertEquals(_t('CheckboxField.NO', 'No'), trim(strip_tags($field3->performReadonlyTransformation()->Field())));
}
}
class CheckboxFieldTest_Article extends DataObject implements TestOnly {