2009-04-29 03:20:24 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2017-05-19 05:47:51 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\CheckboxFieldtest\Article;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class CheckboxFieldTest extends SapphireTest
|
|
|
|
{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_dataobjects = array(
|
2016-12-16 05:34:21 +01:00
|
|
|
Article::class,
|
|
|
|
);
|
2009-04-29 03:20:24 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFieldValueTrue()
|
|
|
|
{
|
|
|
|
/* Create the field, and set the value as boolean true */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
$field->setValue(true);
|
2014-11-12 03:19:12 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/* dataValue() for the field is 1 */
|
|
|
|
$this->assertEquals($field->dataValue(), 1, 'dataValue() returns a 1');
|
|
|
|
|
|
|
|
/* Value() returns 1 as well */
|
|
|
|
$this->assertEquals($field->Value(), 1, 'Value() returns a 1');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFieldValueString()
|
|
|
|
{
|
|
|
|
/* Create the field, and set the value as "on" (raw request field value from DOM) */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
$field->setValue('on');
|
|
|
|
|
|
|
|
/* dataValue() for the field is 1 */
|
|
|
|
$this->assertEquals($field->dataValue(), 1, 'dataValue() returns a 1');
|
|
|
|
|
|
|
|
/* Value() returns 1 as well */
|
|
|
|
$this->assertEquals($field->Value(), 1, 'Value() returns a 1');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFieldValueSettingNull()
|
|
|
|
{
|
|
|
|
/* Create the field, and set the value as NULL */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
$field->setValue(null);
|
|
|
|
|
|
|
|
/* dataValue() for the field is null */
|
|
|
|
$this->assertEquals($field->dataValue(), null, 'dataValue() returns a 0');
|
|
|
|
|
|
|
|
/* Value() returns 0 as well */
|
|
|
|
$this->assertEquals($field->Value(), 0, 'Value() returns a 0');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFieldValueSettingFalse()
|
|
|
|
{
|
|
|
|
/* Create the field, and set the value as NULL */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
$field->setValue(false);
|
|
|
|
|
|
|
|
/* dataValue() for the field is null */
|
|
|
|
$this->assertEquals($field->dataValue(), null, 'dataValue() returns a 0');
|
|
|
|
|
|
|
|
/* Value() returns 0 as well */
|
|
|
|
$this->assertEquals($field->Value(), 0, 'Value() returns a 0');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFieldValueWithoutSettingValue()
|
|
|
|
{
|
|
|
|
/* Create the field, but don't set any value on it */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
|
|
|
|
/* dataValue() for the field is null */
|
|
|
|
$this->assertEquals($field->dataValue(), null, 'dataValue() returns a 0');
|
|
|
|
|
|
|
|
/* Value() returns 0 as well */
|
|
|
|
$this->assertEquals($field->Value(), 0, 'Value() returns a 0');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavingChecked()
|
|
|
|
{
|
|
|
|
/* Create a new test data record */
|
|
|
|
$article = new Article();
|
|
|
|
|
|
|
|
/* Create a field, with a value of 1 */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked', 1);
|
|
|
|
|
|
|
|
/* Save the field into our Article object */
|
|
|
|
$field->saveInto($article);
|
|
|
|
|
|
|
|
/* Write the record to the test database */
|
|
|
|
$article->write();
|
|
|
|
|
|
|
|
/* Check that IsChecked column contains a 1 */
|
|
|
|
$this->assertEquals(
|
|
|
|
DB::query("SELECT \"IsChecked\" FROM \"CheckboxFieldTest_Article\"")->value(),
|
|
|
|
1,
|
|
|
|
'We have a 1 set in the database, because the field saved into as a 1'
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Delete the record we tested */
|
|
|
|
$article->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSavingUnchecked()
|
|
|
|
{
|
|
|
|
/* Create a new test data record */
|
|
|
|
$article = new Article();
|
|
|
|
|
|
|
|
/* Create a field, with no value */
|
|
|
|
$field = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
|
|
|
|
/* Save the field into our Article object */
|
|
|
|
$field->saveInto($article);
|
|
|
|
|
|
|
|
/* Write the record to the test database */
|
|
|
|
$article->write();
|
|
|
|
|
|
|
|
/* Check that IsChecked column contains a 0 */
|
|
|
|
$this->assertEquals(
|
|
|
|
DB::query("SELECT \"IsChecked\" FROM \"CheckboxFieldTest_Article\"")->value(),
|
|
|
|
0,
|
|
|
|
'We have a 0 set in the database, because the field saved into as a 0'
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Delete the record we tested */
|
|
|
|
$article->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadonlyCheckboxField()
|
|
|
|
{
|
|
|
|
// Test 1: a checked checkbox goes to "Yes"
|
|
|
|
$field1 = new CheckboxField('IsChecked', 'Checked');
|
|
|
|
$field1->setValue('on');
|
|
|
|
$this->assertEquals(
|
2017-04-20 03:15:24 +02:00
|
|
|
_t('SilverStripe\\Forms\\CheckboxField.YESANSWER', 'Yes'),
|
2016-12-16 05:34:21 +01:00
|
|
|
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(
|
2017-04-20 03:15:24 +02:00
|
|
|
_t('SilverStripe\\Forms\\CheckboxField.NOANSWER', 'No'),
|
2016-12-16 05:34:21 +01:00
|
|
|
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(
|
2017-04-20 03:15:24 +02:00
|
|
|
_t('SilverStripe\\Forms\\CheckboxField.NOANSWER', 'No'),
|
2016-12-16 05:34:21 +01:00
|
|
|
trim(strip_tags($field3->performReadonlyTransformation()->Field()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidation()
|
|
|
|
{
|
|
|
|
$field = CheckboxField::create('Test', 'Testing');
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
$field->setValue(1);
|
|
|
|
$this->assertTrue(
|
|
|
|
$field->validate($validator),
|
|
|
|
'Field correctly validates integers as allowed'
|
|
|
|
);
|
|
|
|
//string value should validate
|
|
|
|
$field->setValue("test");
|
|
|
|
$this->assertTrue(
|
|
|
|
$field->validate($validator),
|
|
|
|
'Field correctly validates words as allowed'
|
|
|
|
);
|
|
|
|
//empty string should validate
|
|
|
|
$field->setValue('');
|
|
|
|
$this->assertTrue(
|
|
|
|
$field->validate($validator),
|
|
|
|
'Field correctly validates empty strings as allowed'
|
|
|
|
);
|
|
|
|
//null should validate
|
|
|
|
$field->setValue(null);
|
|
|
|
$this->assertTrue(
|
|
|
|
$field->validate($validator),
|
|
|
|
'Field correct validates null as allowed'
|
|
|
|
);
|
|
|
|
}
|
2017-05-19 05:47:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* #2939 CheckboxField creates invalid HTML when required
|
|
|
|
*/
|
|
|
|
public function testNoAriaRequired()
|
|
|
|
{
|
|
|
|
$field = new CheckboxField('RequiredField', 'myRequiredField');
|
|
|
|
|
|
|
|
$form = new Form(
|
2017-05-22 23:13:50 +02:00
|
|
|
Controller::curr(),
|
|
|
|
"form",
|
|
|
|
new FieldList($field),
|
|
|
|
new FieldList(),
|
2017-05-19 05:47:51 +02:00
|
|
|
new RequiredFields(["RequiredField"])
|
|
|
|
);
|
|
|
|
$this->assertTrue($field->Required());
|
|
|
|
|
|
|
|
$attributes = $field->getAttributes();
|
|
|
|
$this->assertFalse(array_key_exists("aria-required", $attributes));
|
|
|
|
}
|
2009-04-29 03:20:24 +02:00
|
|
|
}
|