From dee3939cf7285f4d54d94123d52b9bf583b73d40 Mon Sep 17 00:00:00 2001 From: Nicolaas Date: Sat, 24 Mar 2012 13:17:48 +1300 Subject: [PATCH] BUGFIX: Fix the broken perform readonly transformation which was always showing NO (#6453) --- forms/CheckboxField.php | 8 ++++---- tests/forms/CheckboxFieldTest.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/forms/CheckboxField.php b/forms/CheckboxField.php index 48466f955..22af0ebb8 100644 --- a/forms/CheckboxField.php +++ b/forms/CheckboxField.php @@ -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')); } } \ No newline at end of file diff --git a/tests/forms/CheckboxFieldTest.php b/tests/forms/CheckboxFieldTest.php index 985dffb28..a2f01e0c3 100644 --- a/tests/forms/CheckboxFieldTest.php +++ b/tests/forms/CheckboxFieldTest.php @@ -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 {