Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

BUGFIX: Fix the broken perform readonly transformation which was alwa…

…ys showing NO (#6453)
  • Loading branch information...
commit dee3939cf7285f4d54d94123d52b9bf583b73d40 1 parent fd649a4
@sunnysideup sunnysideup authored sminnee committed
Showing with 21 additions and 4 deletions.
  1. +4 −4 forms/CheckboxField.php
  2. +17 −0 tests/forms/CheckboxFieldTest.php
View
8 forms/CheckboxField.php
@@ -50,7 +50,7 @@ function getAttributes() {
* 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
17 tests/forms/CheckboxFieldTest.php
@@ -117,6 +117,23 @@ function testSavingUnchecked() {
/* 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 {
Please sign in to comment.
Something went wrong with that request. Please try again.