Skip to content
This repository
Browse code

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

…ys showing NO (#6453)
  • Loading branch information...
commit dee3939cf7285f4d54d94123d52b9bf583b73d40 1 parent fd649a4
Nicolaas authored March 24, 2012 sminnee committed March 24, 2012
8  forms/CheckboxField.php
@@ -50,7 +50,7 @@ function getAttributes() {
50 50
 	 * Returns a readonly version of this field
51 51
 	 */
52 52
 	function performReadonlyTransformation() {
53  
-		$field = new CheckboxField_Readonly($this->name, $this->title, $this->value ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No'));
  53
+		$field = new CheckboxField_Readonly($this->name, $this->title, $this->value);
54 54
 		$field->setForm($this->form);
55 55
 		return $field;	
56 56
 	}
@@ -73,9 +73,9 @@ class CheckboxField_Readonly extends ReadonlyField {
73 73
 	function performReadonlyTransformation() {
74 74
 		return clone $this;
75 75
 	}
76  
-	
77  
-	function setValue($val) {
78  
-		$this->value = (int)($val) ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No');
  76
+
  77
+	function Value() {
  78
+		return Convert::raw2xml($this->value ? _t('CheckboxField.YES', 'Yes') : _t('CheckboxField.NO', 'No'));
79 79
 	}
80 80
 
81 81
 }
17  tests/forms/CheckboxFieldTest.php
@@ -117,6 +117,23 @@ function testSavingUnchecked() {
117 117
 		/* Delete the record we tested */
118 118
 		$article->delete();
119 119
 	}
  120
+
  121
+	function testReadonlyCheckboxField() {
  122
+		// Test 1: a checked checkbox goes to "Yes"
  123
+		$field1 = new CheckboxField('IsChecked', 'Checked');
  124
+		$field1->setValue('on');
  125
+		$this->assertEquals(_t('CheckboxField.YES', 'Yes'), trim(strip_tags($field1->performReadonlyTransformation()->Field())));
  126
+
  127
+		// Test 2: an checkbox with the value set to false to "No"
  128
+		$field2 = new CheckboxField('IsChecked', 'Checked');
  129
+		$field2->setValue(false);
  130
+		$this->assertEquals(_t('CheckboxField.NO', 'No'), trim(strip_tags($field2->performReadonlyTransformation()->Field())));
  131
+
  132
+		// Test 3: an checkbox with no value ever set goes to "No"
  133
+		$field3 = new CheckboxField('IsChecked', 'Checked');
  134
+		$this->assertEquals(_t('CheckboxField.NO', 'No'), trim(strip_tags($field3->performReadonlyTransformation()->Field())));
  135
+
  136
+	}
120 137
 	
121 138
 }
122 139
 class CheckboxFieldTest_Article extends DataObject implements TestOnly {

0 notes on commit dee3939

Please sign in to comment.
Something went wrong with that request. Please try again.