2010-09-03 07:06:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package userforms
|
|
|
|
*/
|
|
|
|
|
|
|
|
class EditableFormFieldTest extends FunctionalTest {
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2013-12-20 02:23:13 +01:00
|
|
|
static $fixture_file = 'userforms/tests/EditableFormFieldTest.yml';
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
function testFormFieldPermissions() {
|
|
|
|
$text = $this->objFromFixture('EditableTextField', 'basic-text');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2016-03-14 01:53:25 +01:00
|
|
|
$this->assertTrue($text->canCreate());
|
|
|
|
$this->assertTrue($text->canView());
|
2010-09-08 05:20:28 +02:00
|
|
|
$this->assertTrue($text->canEdit());
|
|
|
|
$this->assertTrue($text->canDelete());
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
$text->setReadonly(true);
|
2016-03-14 01:53:25 +01:00
|
|
|
$this->assertTrue($text->canView());
|
2010-09-08 05:20:28 +02:00
|
|
|
$this->assertFalse($text->canEdit());
|
|
|
|
$this->assertFalse($text->canDelete());
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
$text->setReadonly(false);
|
2016-03-14 01:53:25 +01:00
|
|
|
$this->assertTrue($text->canView());
|
2010-09-08 05:20:28 +02:00
|
|
|
$this->assertTrue($text->canEdit());
|
|
|
|
$this->assertTrue($text->canDelete());
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
$member = Member::currentUser();
|
|
|
|
$member->logout();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
$this->logInWithPermission('SITETREE_VIEW_ALL');
|
2016-03-14 01:53:25 +01:00
|
|
|
$this->assertFalse($text->canCreate());
|
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
$text->setReadonly(false);
|
2016-03-14 01:53:25 +01:00
|
|
|
$this->assertTrue($text->canView());
|
2010-09-08 05:20:28 +02:00
|
|
|
$this->assertFalse($text->canEdit());
|
|
|
|
$this->assertFalse($text->canDelete());
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
$text->setReadonly(true);
|
2016-03-14 01:53:25 +01:00
|
|
|
$this->assertTrue($text->canView());
|
2010-09-08 05:20:28 +02:00
|
|
|
$this->assertFalse($text->canEdit());
|
|
|
|
$this->assertFalse($text->canDelete());
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
function testCustomRules() {
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$form = $this->objFromFixture('UserDefinedForm', 'custom-rules-form');
|
|
|
|
|
|
|
|
$checkbox = $form->Fields()->find('ClassName', 'EditableCheckbox');
|
|
|
|
$field = $form->Fields()->find('ClassName', 'EditableTextField');
|
|
|
|
|
2015-07-24 04:37:48 +02:00
|
|
|
$rules = $checkbox->DisplayRules();
|
2010-09-08 05:20:28 +02:00
|
|
|
|
|
|
|
// 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
|
2015-12-11 05:38:31 +01:00
|
|
|
$this->assertEquals(1, $rules->Count());
|
|
|
|
$this->assertEquals($rules, $checkbox->EffectiveDisplayRules());
|
2010-09-08 05:20:28 +02:00
|
|
|
|
|
|
|
$checkboxRule = $rules->First();
|
2015-07-24 04:37:48 +02:00
|
|
|
$checkboxRule->ConditionFieldID = $field->ID;
|
|
|
|
|
2010-09-08 05:20:28 +02:00
|
|
|
$this->assertEquals($checkboxRule->Display, 'Hide');
|
|
|
|
$this->assertEquals($checkboxRule->ConditionOption, 'HasValue');
|
2015-07-24 04:37:48 +02:00
|
|
|
$this->assertEquals($checkboxRule->FieldValue, '6');
|
2015-12-11 05:38:31 +01:00
|
|
|
|
|
|
|
// If field is required then all custom rules are disabled
|
|
|
|
$checkbox->Required = true;
|
|
|
|
$this->assertEquals(0, $checkbox->EffectiveDisplayRules()->count());
|
2010-09-08 05:20:28 +02:00
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
function testEditableDropdownField() {
|
|
|
|
$dropdown = $this->objFromFixture('EditableDropdown', 'basic-dropdown');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
$field = $dropdown->getFormField();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
$this->assertThat($field, $this->isInstanceOf('DropdownField'));
|
|
|
|
$values = $field->getSource();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
$this->assertEquals(array('Option 1' => 'Option 1', 'Option 2' => 'Option 2'), $values);
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
function testEditableRadioField() {
|
|
|
|
$radio = $this->objFromFixture('EditableRadioField', 'radio-field');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
$field = $radio->getFormField();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
$this->assertThat($field, $this->isInstanceOf('OptionsetField'));
|
|
|
|
$values = $field->getSource();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
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
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 12:35:43 +02:00
|
|
|
function testMultipleOptionDuplication() {
|
|
|
|
$dropdown = $this->objFromFixture('EditableDropdown','basic-dropdown');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 12:35:43 +02:00
|
|
|
$clone = $dropdown->duplicate();
|
|
|
|
|
|
|
|
$this->assertEquals($clone->Options()->Count(), $dropdown->Options()->Count());
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 12:35:43 +02:00
|
|
|
foreach($clone->Options() as $option) {
|
|
|
|
$orginal = $dropdown->Options()->find('Title', $option->Title);
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2010-09-08 12:35:43 +02:00
|
|
|
$this->assertEquals($orginal->Sort, $option->Sort);
|
|
|
|
}
|
|
|
|
}
|
2013-12-04 19:06:34 +01:00
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2015-08-28 00:42:32 +02:00
|
|
|
public function testFileFieldAllowedExtensionsBlacklist() {
|
|
|
|
Config::inst()->update('EditableFileField', 'allowed_extensions_blacklist', array('jpg'));
|
|
|
|
$fileField = $this->objFromFixture('EditableFileField', 'file-field');
|
|
|
|
$formField = $fileField->getFormField();
|
|
|
|
|
|
|
|
$this->assertNotContains('jpg', $formField->getValidator()->getAllowedExtensions());
|
|
|
|
}
|
|
|
|
|
2015-10-28 02:33:44 +01:00
|
|
|
/**
|
|
|
|
* Verify that unique names are automatically generated for each formfield
|
|
|
|
*/
|
|
|
|
public function testUniqueName() {
|
|
|
|
$textfield1 = new EditableTextField();
|
|
|
|
$this->assertEmpty($textfield1->Name);
|
|
|
|
|
|
|
|
// Write values
|
|
|
|
$textfield1->write();
|
|
|
|
$textfield2 = new EditableTextField();
|
|
|
|
$textfield2->write();
|
|
|
|
$checkboxField = new EditableCheckbox();
|
|
|
|
$checkboxField->write();
|
|
|
|
|
|
|
|
// Test values are in the expected format
|
|
|
|
$this->assertRegExp('/^EditableTextField_.+/', $textfield1->Name);
|
|
|
|
$this->assertRegExp('/^EditableTextField_.+/', $textfield2->Name);
|
|
|
|
$this->assertRegExp('/^EditableCheckbox_.+/', $checkboxField->Name);
|
|
|
|
$this->assertNotEquals($textfield1->Name, $textfield2->Name);
|
|
|
|
}
|
|
|
|
|
2013-12-04 19:06:34 +01:00
|
|
|
}
|