2010-09-03 07:06:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package userforms
|
|
|
|
*/
|
|
|
|
|
|
|
|
class EditableFormFieldTest extends FunctionalTest {
|
|
|
|
|
2013-12-20 02:23:13 +01:00
|
|
|
static $fixture_file = 'userforms/tests/EditableFormFieldTest.yml';
|
2013-12-04 19:06:34 +01:00
|
|
|
|
2013-12-20 02:23:13 +01:00
|
|
|
protected $extraDataObjects = array(
|
2015-08-03 07:03:36 +02:00
|
|
|
'ExtendedEditableFormFieldTestOnly',
|
|
|
|
'EditableFormFieldExtensionTestOnly'
|
2013-12-20 02:23:13 +01:00
|
|
|
);
|
2010-09-08 05:20:28 +02:00
|
|
|
|
|
|
|
function testFormFieldPermissions() {
|
|
|
|
$text = $this->objFromFixture('EditableTextField', 'basic-text');
|
|
|
|
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$this->assertTrue($text->canEdit());
|
|
|
|
$this->assertTrue($text->canDelete());
|
|
|
|
|
|
|
|
$text->setReadonly(true);
|
|
|
|
$this->assertFalse($text->canEdit());
|
|
|
|
$this->assertFalse($text->canDelete());
|
|
|
|
|
|
|
|
$text->setReadonly(false);
|
|
|
|
$this->assertTrue($text->canEdit());
|
|
|
|
$this->assertTrue($text->canDelete());
|
|
|
|
|
|
|
|
$member = Member::currentUser();
|
|
|
|
$member->logout();
|
|
|
|
|
|
|
|
$this->logInWithPermission('SITETREE_VIEW_ALL');
|
|
|
|
$text->setReadonly(false);
|
|
|
|
$this->assertFalse($text->canEdit());
|
|
|
|
$this->assertFalse($text->canDelete());
|
|
|
|
|
|
|
|
$text->setReadonly(true);
|
|
|
|
$this->assertFalse($text->canEdit());
|
|
|
|
$this->assertFalse($text->canDelete());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGettingAndSettingSettings() {
|
|
|
|
$text = $this->objFromFixture('EditableTextField', 'basic-text');
|
|
|
|
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$this->assertEquals($text->getSettings(), array());
|
|
|
|
$text->setSetting('Test', 'Value');
|
|
|
|
$text->write();
|
|
|
|
|
|
|
|
$this->assertEquals($text->getSetting('Test'), 'Value');
|
|
|
|
$this->assertEquals($text->getSettings(), array('Test' => 'Value'));
|
|
|
|
|
|
|
|
$text->setSetting('Foo', 'Bar');
|
|
|
|
$text->write();
|
|
|
|
|
|
|
|
$this->assertEquals($text->getSetting('Foo'), 'Bar');
|
|
|
|
$this->assertEquals($text->getSettings(), array('Test' => 'Value', 'Foo' => 'Bar'));
|
|
|
|
|
|
|
|
// test overridding an existing setting
|
|
|
|
$text->setSetting('Foo', 'Baz');
|
|
|
|
$text->write();
|
|
|
|
|
|
|
|
$this->assertEquals($text->getSetting('Foo'), 'Baz');
|
|
|
|
$this->assertEquals($text->getSettings(), array('Test' => 'Value', 'Foo' => 'Baz'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function testShowOnLoad() {
|
|
|
|
$text = $this->objFromFixture('EditableTextField', 'basic-text');
|
|
|
|
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$this->assertTrue($text->getShowOnLoad());
|
|
|
|
|
|
|
|
$text->setSetting('ShowOnLoad', 'Show');
|
|
|
|
$this->assertTrue($text->getShowOnLoad());
|
|
|
|
|
|
|
|
$text->setSetting('ShowOnLoad', 'Hide');
|
|
|
|
$this->assertFalse($text->getShowOnLoad());
|
|
|
|
|
|
|
|
$text->setSetting('ShowOnLoad', '');
|
|
|
|
$this->assertTrue($text->getShowOnLoad());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testPopulateFromPostData() {
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2012-04-22 21:25:26 +02:00
|
|
|
$set = new ArrayList();
|
2010-09-08 05:20:28 +02:00
|
|
|
|
|
|
|
$field = new EditableFormField();
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Title' => 'Field Title',
|
|
|
|
'Default' => 'Default Value',
|
|
|
|
'Sort' => '2',
|
|
|
|
'Required' => 0,
|
|
|
|
'CustomErrorMessage' => 'Custom'
|
|
|
|
);
|
|
|
|
|
|
|
|
$field->populateFromPostData($data);
|
|
|
|
$set->push($field);
|
|
|
|
$this->assertDOSEquals(array($data), $set);
|
|
|
|
|
|
|
|
// test the custom settings
|
|
|
|
$data['CustomSettings'] = array(
|
|
|
|
'Foo' => 'Bar'
|
|
|
|
);
|
|
|
|
|
|
|
|
$checkbox = new EditableCheckbox();
|
|
|
|
$checkbox->write();
|
|
|
|
|
|
|
|
$checkbox->populateFromPostData(array('Title' => 'Checkbox'));
|
|
|
|
|
|
|
|
$field->populateFromPostData($data);
|
|
|
|
|
|
|
|
$this->assertEquals($field->getSettings(), array('Foo' => 'Bar'));
|
|
|
|
|
|
|
|
$rule = array(
|
|
|
|
'Display' => 'Hide',
|
|
|
|
'ConditionField' => $checkbox->Name,
|
|
|
|
'ConditionOption' => 'HasValue',
|
|
|
|
'Value' => 6
|
|
|
|
);
|
|
|
|
|
|
|
|
// test the custom rules
|
|
|
|
$data['CustomRules'] = array(
|
|
|
|
'Rule1' => $rule
|
|
|
|
);
|
|
|
|
|
|
|
|
$field->populateFromPostData($data);
|
|
|
|
|
|
|
|
$rules = unserialize($field->CustomRules);
|
|
|
|
|
|
|
|
$this->assertEquals($rules[0], $rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testCustomRules() {
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$form = $this->objFromFixture('UserDefinedForm', 'custom-rules-form');
|
|
|
|
|
|
|
|
$checkbox = $form->Fields()->find('ClassName', 'EditableCheckbox');
|
|
|
|
$field = $form->Fields()->find('ClassName', 'EditableTextField');
|
|
|
|
|
|
|
|
$rule = array(
|
|
|
|
'Display' => 'Hide',
|
|
|
|
'ConditionField' => $checkbox->Name,
|
|
|
|
'ConditionOption' => 'HasValue',
|
|
|
|
'Value' => 6
|
|
|
|
);
|
|
|
|
|
|
|
|
$data['CustomRules'] = array(
|
|
|
|
'Rule1' => $rule
|
|
|
|
);
|
|
|
|
|
|
|
|
$field->populateFromPostData($data);
|
|
|
|
|
|
|
|
$rules = $field->CustomRules();
|
|
|
|
|
|
|
|
// form has 2 fields - a checkbox and a text field
|
2012-07-17 06:09:31 +02:00
|
|
|
// it has 1 rule - when ticked the checkbox hides the text field
|
2010-09-08 05:20:28 +02:00
|
|
|
$this->assertEquals($rules->Count(), 1);
|
|
|
|
|
|
|
|
// rules are ArrayDatas not dataobjects
|
|
|
|
// $this->assertDOSEquals(array($rule), $rules);
|
|
|
|
|
|
|
|
$checkboxRule = $rules->First();
|
|
|
|
$this->assertEquals($checkboxRule->Display, 'Hide');
|
|
|
|
$this->assertEquals($checkboxRule->ConditionField, $checkbox->Name);
|
|
|
|
$this->assertEquals($checkboxRule->ConditionOption, 'HasValue');
|
|
|
|
$this->assertEquals($checkboxRule->Value, '6');
|
|
|
|
|
|
|
|
foreach($checkboxRule->Fields as $condition) {
|
|
|
|
if($checkbox->Name == $condition->Name) {
|
|
|
|
$this->assertTrue($condition->isSelected);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->assertFalse($condition->isSelected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['CustomRules'] = array(
|
|
|
|
'Rule2' => array(
|
|
|
|
'Display' => 'Hide',
|
|
|
|
'ConditionField' => $checkbox->Name,
|
|
|
|
'ConditionOption' => 'Blank'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$field->populateFromPostData($data);
|
|
|
|
|
|
|
|
$rules = $field->CustomRules();
|
|
|
|
|
|
|
|
// test that saving additional rules deletes the old one
|
|
|
|
$this->assertEquals($rules->Count(), 1);
|
|
|
|
|
|
|
|
}
|
2010-09-03 07:06:13 +02:00
|
|
|
|
|
|
|
function testEditableDropdownField() {
|
|
|
|
$dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown');
|
|
|
|
|
|
|
|
$field = $dropdown->getFormField();
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertThat($field, $this->isInstanceOf('DropdownField'));
|
|
|
|
$values = $field->getSource();
|
|
|
|
|
|
|
|
$this->assertEquals(array('Option 1' => 'Option 1', 'Option 2' => 'Option 2'), $values);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testEditableRadioField() {
|
|
|
|
$radio = $this->objFromFixture('EditableRadioField', 'radio-field');
|
2010-09-08 12:35:43 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
$field = $radio->getFormField();
|
|
|
|
|
|
|
|
$this->assertThat($field, $this->isInstanceOf('OptionsetField'));
|
|
|
|
$values = $field->getSource();
|
|
|
|
|
2010-09-08 12:35:43 +02:00
|
|
|
$this->assertEquals(array('Option 5' => 'Option 5', 'Option 6' => 'Option 6'), $values);
|
2010-09-03 07:06:13 +02:00
|
|
|
}
|
2010-09-08 05:20:28 +02:00
|
|
|
|
|
|
|
function testTitleField() {
|
|
|
|
$text = $this->objFromFixture('EditableTextField', 'basic-text');
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$title = $text->TitleField();
|
|
|
|
|
|
|
|
$this->assertThat($title, $this->isInstanceOf('TextField'));
|
|
|
|
$this->assertEquals($title->Title(), "Enter Question");
|
|
|
|
$this->assertEquals($title->Value(), "Basic Text Field");
|
|
|
|
|
|
|
|
$member = Member::currentUser();
|
|
|
|
$member->logOut();
|
|
|
|
|
|
|
|
// read only version
|
|
|
|
$title = $text->TitleField();
|
|
|
|
|
|
|
|
$this->assertThat($title, $this->isInstanceOf('ReadonlyField'));
|
|
|
|
$this->assertEquals($title->Title(), "Enter Question");
|
|
|
|
$this->assertEquals($title->Value(), "Basic Text Field");
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGettingFieldAndSettingNames() {
|
|
|
|
$text = $this->objFromFixture('EditableTextField', 'basic-text');
|
|
|
|
|
|
|
|
$this->assertEquals($text->getFieldName(), "Fields[". $text->ID ."]");
|
|
|
|
$this->assertEquals($text->getFieldName('Setting'), "Fields[". $text->ID ."][Setting]");
|
|
|
|
|
2012-04-14 08:36:50 +02:00
|
|
|
$this->assertEquals($text->getSettingName('Foo'), "Fields[". $text->ID ."][CustomSettings][Foo]");
|
2010-09-08 05:20:28 +02:00
|
|
|
}
|
2010-09-08 12:35:43 +02:00
|
|
|
|
|
|
|
function testMultipleOptionDuplication() {
|
|
|
|
$dropdown = $this->objFromFixture('EditableDropdown','basic-dropdown');
|
|
|
|
|
|
|
|
$clone = $dropdown->duplicate();
|
|
|
|
|
|
|
|
$this->assertEquals($clone->Options()->Count(), $dropdown->Options()->Count());
|
|
|
|
|
|
|
|
foreach($clone->Options() as $option) {
|
|
|
|
$orginal = $dropdown->Options()->find('Title', $option->Title);
|
|
|
|
|
|
|
|
$this->assertEquals($orginal->Sort, $option->Sort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMultipleOptionPopulateFromPostData() {
|
|
|
|
$dropdown = $this->objFromFixture('EditableDropdown','basic-dropdown');
|
|
|
|
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
foreach($dropdown->Options() as $option) {
|
|
|
|
$orginal[$option->ID] = array(
|
|
|
|
'Title' => $option->Title,
|
|
|
|
'Sort' => $option->Sort
|
|
|
|
);
|
|
|
|
|
|
|
|
$data[$option->ID] = array(
|
|
|
|
'Title' => 'New - '. $option->Title,
|
|
|
|
'Sort' => $option->Sort + 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dropdown->populateFromPostData($data);
|
|
|
|
|
|
|
|
$count = $dropdown->Options()->Count();
|
|
|
|
|
|
|
|
foreach($dropdown->Options() as $option) {
|
|
|
|
$this->assertEquals($option->Title, 'New - '. $orginal[$option->ID]['Title']);
|
|
|
|
$this->assertEquals($option->Sort, $orginal[$option->ID]['Sort'] + 1);
|
|
|
|
}
|
2010-09-20 06:18:25 +02:00
|
|
|
|
|
|
|
// remove the first one. can't assume by ID
|
|
|
|
foreach($data as $key => $value) {
|
|
|
|
unset($data[$key]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-09-08 12:35:43 +02:00
|
|
|
$dropdown->populateFromPostData($data);
|
|
|
|
|
|
|
|
$this->assertEquals($dropdown->Options()->Count(), $count-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testEditableTextFieldConfiguration() {
|
2010-09-08 12:52:19 +02:00
|
|
|
// $text = $this->objFromFixture('EditableTextField', 'basic-text');
|
2010-09-08 12:35:43 +02:00
|
|
|
|
2010-09-08 12:52:19 +02:00
|
|
|
// $configuration = $text->getFieldConfiguration();
|
|
|
|
|
2010-09-08 12:35:43 +02:00
|
|
|
}
|
2013-12-04 19:06:34 +01:00
|
|
|
|
|
|
|
function testExtendedEditableFormField() {
|
|
|
|
/** @var ExtendedEditableFormField $field */
|
2015-08-03 07:03:36 +02:00
|
|
|
$field = $this->objFromFixture('ExtendedEditableFormFieldTestOnly', 'extended-field');
|
2013-12-04 19:06:34 +01:00
|
|
|
|
|
|
|
// Check db fields
|
|
|
|
$dbFields = $field->stat('db');
|
|
|
|
$this->assertTrue(array_key_exists('TestExtraField', $dbFields));
|
|
|
|
$this->assertTrue(array_key_exists('TestValidationField', $dbFields));
|
|
|
|
|
|
|
|
// Check Field Configuration
|
|
|
|
$fieldConfiguration = $field->getFieldConfiguration();
|
|
|
|
$extraField = $fieldConfiguration->dataFieldByName($field->getSettingName('TestExtraField'));
|
|
|
|
$this->assertNotNull($extraField);
|
|
|
|
|
|
|
|
// Check Validation Fields
|
|
|
|
$fieldValidation = $field->getFieldValidationOptions();
|
|
|
|
$validationField = $fieldValidation->dataFieldByName($field->getSettingName('TestValidationField'));
|
|
|
|
$this->assertNotNull($validationField);
|
|
|
|
}
|
|
|
|
|
2015-07-15 23:23:43 +02:00
|
|
|
public function testFileField() {
|
|
|
|
$fileField = $this->objFromFixture('EditableFileField', 'file-field');
|
|
|
|
$formField = $fileField->getFormField();
|
|
|
|
|
|
|
|
$this->assertContains('jpg', $formField->getValidator()->getAllowedExtensions());
|
|
|
|
$this->assertNotContains('notallowedextension', $formField->getValidator()->getAllowedExtensions());
|
|
|
|
}
|
|
|
|
|
2013-12-04 19:06:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ExtendedEditableFormField
|
|
|
|
* A base EditableFormFieldClass that will be extended with {@link EditableFormFieldExtension}
|
|
|
|
* @mixin EditableFormFieldExtension
|
|
|
|
*/
|
2015-08-03 07:03:36 +02:00
|
|
|
class ExtendedEditableFormFieldTestOnly extends EditableFormField implements TestOnly
|
2013-12-04 19:06:34 +01:00
|
|
|
{
|
|
|
|
private static $extensions = array(
|
2015-08-03 07:03:36 +02:00
|
|
|
'EditableFormFieldExtensionTestOnly'
|
2013-12-04 19:06:34 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class EditableFormFieldExtension
|
|
|
|
* Used for testing extensions to EditableFormField and the extended Fields methods
|
|
|
|
* @property EditableFormField owner
|
|
|
|
*/
|
2015-08-03 07:03:36 +02:00
|
|
|
class EditableFormFieldExtensionTestOnly extends DataExtension implements TestOnly
|
2013-12-04 19:06:34 +01:00
|
|
|
{
|
|
|
|
private static $db = array(
|
|
|
|
'TestExtraField' => 'Varchar',
|
|
|
|
'TestValidationField' => 'Boolean'
|
|
|
|
);
|
|
|
|
|
|
|
|
public function updateFieldConfiguration(FieldList $fields)
|
|
|
|
{
|
|
|
|
$extraField = 'TestExtraField';
|
|
|
|
$fields->push(TextField::create(
|
|
|
|
$this->owner->getSettingName($extraField),
|
|
|
|
'Test extra field',
|
|
|
|
$this->owner->getSetting($extraField)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function updateFieldValidationOptions(FieldList $fields)
|
|
|
|
{
|
|
|
|
$extraField = 'TestValidationField';
|
|
|
|
$fields->push(CheckboxField::create(
|
|
|
|
$this->owner->getSettingName($extraField),
|
|
|
|
'Test validation field',
|
|
|
|
$this->owner->getSetting($extraField)
|
|
|
|
));
|
|
|
|
}
|
2012-07-17 06:09:31 +02:00
|
|
|
}
|