2009-04-29 03:20:24 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-04-29 03:20:24 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class CheckboxFieldTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-30 02:48:26 +01:00
|
|
|
protected $usesDatabase = true;
|
2009-04-29 03:20:24 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'CheckboxFieldTest_Article',
|
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldValueTrue() {
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Create the field, and set the value as boolean true */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
$field->setValue(true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* dataValue() for the field is 1 */
|
|
|
|
$this->assertEquals($field->dataValue(), 1, 'dataValue() returns a 1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Value() returns 1 as well */
|
|
|
|
$this->assertEquals($field->Value(), 1, 'Value() returns a 1');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldValueString() {
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Create the field, and set the value as "on" (raw request field value from DOM) */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
$field->setValue('on');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* dataValue() for the field is 1 */
|
|
|
|
$this->assertEquals($field->dataValue(), 1, 'dataValue() returns a 1');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Value() returns 1 as well */
|
|
|
|
$this->assertEquals($field->Value(), 1, 'Value() returns a 1');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldValueSettingNull() {
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Create the field, and set the value as NULL */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
$field->setValue(null);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-23 22:58:36 +02:00
|
|
|
/* dataValue() for the field is null */
|
|
|
|
$this->assertEquals($field->dataValue(), null, 'dataValue() returns a 0');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Value() returns 0 as well */
|
|
|
|
$this->assertEquals($field->Value(), 0, 'Value() returns a 0');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldValueSettingFalse() {
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Create the field, and set the value as NULL */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
$field->setValue(false);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-23 22:58:36 +02:00
|
|
|
/* dataValue() for the field is null */
|
|
|
|
$this->assertEquals($field->dataValue(), null, 'dataValue() returns a 0');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Value() returns 0 as well */
|
|
|
|
$this->assertEquals($field->Value(), 0, 'Value() returns a 0');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFieldValueWithoutSettingValue() {
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Create the field, but don't set any value on it */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-23 22:58:36 +02:00
|
|
|
/* dataValue() for the field is null */
|
|
|
|
$this->assertEquals($field->dataValue(), null, 'dataValue() returns a 0');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Value() returns 0 as well */
|
|
|
|
$this->assertEquals($field->Value(), 0, 'Value() returns a 0');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSavingChecked() {
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Create a new test data record */
|
|
|
|
$article = new CheckboxFieldTest_Article();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Create a field, with a value of 1 */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked', 1);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Save the field into our Article object */
|
|
|
|
$field->saveInto($article);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Write the record to the test database */
|
|
|
|
$article->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Check that IsChecked column contains a 1 */
|
|
|
|
$this->assertEquals(
|
2009-09-17 02:02:53 +02:00
|
|
|
DB::query("SELECT \"IsChecked\" FROM \"CheckboxFieldTest_Article\"")->value(),
|
2009-04-29 03:20:24 +02:00
|
|
|
1,
|
|
|
|
'We have a 1 set in the database, because the field saved into as a 1'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Delete the record we tested */
|
|
|
|
$article->delete();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSavingUnchecked() {
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Create a new test data record */
|
|
|
|
$article = new CheckboxFieldTest_Article();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Create a field, with no value */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Save the field into our Article object */
|
|
|
|
$field->saveInto($article);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Write the record to the test database */
|
|
|
|
$article->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Check that IsChecked column contains a 0 */
|
|
|
|
$this->assertEquals(
|
2009-09-17 02:02:53 +02:00
|
|
|
DB::query("SELECT \"IsChecked\" FROM \"CheckboxFieldTest_Article\"")->value(),
|
2009-04-29 03:20:24 +02:00
|
|
|
0,
|
|
|
|
'We have a 0 set in the database, because the field saved into as a 0'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
/* Delete the record we tested */
|
|
|
|
$article->delete();
|
|
|
|
}
|
2012-03-24 01:17:48 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testReadonlyCheckboxField() {
|
2012-03-24 01:17:48 +01:00
|
|
|
// Test 1: a checked checkbox goes to "Yes"
|
|
|
|
$field1 = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
$field1->setValue('on');
|
2012-12-13 13:51:28 +01:00
|
|
|
$copy = $field1->performReadonlyTransformation();
|
2013-04-12 01:09:53 +02:00
|
|
|
$this->assertEquals(_t('CheckboxField.YESANSWER', 'Yes'),
|
2012-09-26 23:34:00 +02:00
|
|
|
trim(strip_tags($field1->performReadonlyTransformation()->Field())));
|
2012-03-24 01:17:48 +01:00
|
|
|
|
|
|
|
// Test 2: an checkbox with the value set to false to "No"
|
|
|
|
$field2 = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
$field2->setValue(false);
|
2013-04-12 01:09:53 +02:00
|
|
|
$this->assertEquals(_t('CheckboxField.NOANSWER', 'No'),
|
2012-09-26 23:34:00 +02:00
|
|
|
trim(strip_tags($field2->performReadonlyTransformation()->Field())));
|
2012-03-24 01:17:48 +01:00
|
|
|
|
|
|
|
// Test 3: an checkbox with no value ever set goes to "No"
|
|
|
|
$field3 = new CheckboxField('IsChecked', 'Checked');
|
2013-04-12 01:09:53 +02:00
|
|
|
$this->assertEquals(_t('CheckboxField.NOANSWER', 'No'),
|
2012-09-26 23:34:00 +02:00
|
|
|
trim(strip_tags($field3->performReadonlyTransformation()->Field())));
|
2012-03-24 01:17:48 +01:00
|
|
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
}
|
|
|
|
class CheckboxFieldTest_Article extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-04-29 03:20:24 +02:00
|
|
|
'IsChecked' => 'Boolean'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 03:20:24 +02:00
|
|
|
}
|