2010-09-03 07:06:13 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-15 23:31:47 +02:00
|
|
|
namespace SilverStripe\UserForms\Tests\Model;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\OptionsetField;
|
2017-08-15 05:30:59 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableCheckbox;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableDropdown;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
|
2017-08-11 02:20:12 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableOption;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableRadioField;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
|
|
|
|
use SilverStripe\UserForms\Model\UserDefinedForm;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
/**
|
|
|
|
* @package userforms
|
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableFormFieldTest extends FunctionalTest
|
|
|
|
{
|
2017-08-11 02:20:12 +02:00
|
|
|
protected static $fixture_file = 'EditableFormFieldTest.yml';
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
public function testFormFieldPermissions()
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$text = $this->objFromFixture(EditableTextField::class, 'basic-text');
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2017-08-15 05:30:59 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertTrue($text->canCreate());
|
|
|
|
$this->assertTrue($text->canView());
|
|
|
|
$this->assertTrue($text->canEdit());
|
|
|
|
$this->assertTrue($text->canDelete());
|
|
|
|
|
|
|
|
$text->setReadonly(true);
|
|
|
|
$this->assertTrue($text->canView());
|
|
|
|
$this->assertFalse($text->canEdit());
|
|
|
|
$this->assertFalse($text->canDelete());
|
|
|
|
|
|
|
|
$text->setReadonly(false);
|
|
|
|
$this->assertTrue($text->canView());
|
|
|
|
$this->assertTrue($text->canEdit());
|
|
|
|
$this->assertTrue($text->canDelete());
|
|
|
|
|
2017-08-15 05:30:59 +02:00
|
|
|
$this->logOut();
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->logInWithPermission('SITETREE_VIEW_ALL');
|
2017-08-15 05:30:59 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertFalse($text->canCreate());
|
|
|
|
|
|
|
|
$text->setReadonly(false);
|
|
|
|
$this->assertTrue($text->canView());
|
|
|
|
$this->assertFalse($text->canEdit());
|
|
|
|
$this->assertFalse($text->canDelete());
|
|
|
|
|
|
|
|
$text->setReadonly(true);
|
|
|
|
$this->assertTrue($text->canView());
|
|
|
|
$this->assertFalse($text->canEdit());
|
|
|
|
$this->assertFalse($text->canDelete());
|
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testCustomRules()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2017-08-09 01:55:09 +02:00
|
|
|
$form = $this->objFromFixture(UserDefinedForm::class, 'custom-rules-form');
|
2010-09-08 05:20:28 +02:00
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
$checkbox = $form->Fields()->find('ClassName', EditableCheckbox::class);
|
|
|
|
$field = $form->Fields()->find('ClassName', EditableTextField::class);
|
2010-09-08 05:20:28 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$rules = $checkbox->DisplayRules();
|
2010-09-08 05:20:28 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// form has 2 fields - a checkbox and a text field
|
|
|
|
// it has 1 rule - when ticked the checkbox hides the text field
|
|
|
|
$this->assertEquals(1, $rules->Count());
|
2010-09-08 05:20:28 +02:00
|
|
|
|
2020-01-13 00:12:14 +01:00
|
|
|
// EffectiveDisplayRules rule has been deprecated
|
|
|
|
$this->assertEquals($rules, $checkbox->EffectiveDisplayRules());
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$checkboxRule = $rules->First();
|
|
|
|
$checkboxRule->ConditionFieldID = $field->ID;
|
2015-07-24 04:37:48 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($checkboxRule->Display, 'Hide');
|
|
|
|
$this->assertEquals($checkboxRule->ConditionOption, 'HasValue');
|
|
|
|
$this->assertEquals($checkboxRule->FieldValue, '6');
|
2020-01-13 00:12:14 +01:00
|
|
|
|
|
|
|
// If field is required then all custom rules are disabled
|
|
|
|
$checkbox->Required = true;
|
|
|
|
$this->assertEquals(0, $checkbox->EffectiveDisplayRules()->count());
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testEditableOptionEmptyValue()
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$option = $this->objFromFixture(EditableOption::class, 'option-1');
|
2016-07-21 07:53:59 +02:00
|
|
|
$option->Value = '';
|
2016-04-20 00:40:37 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
// Disallow empty values
|
|
|
|
EditableOption::set_allow_empty_values(false);
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertEquals($option->Title, $option->Value);
|
2016-04-20 00:40:37 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$option->Value = 'test';
|
|
|
|
$this->assertEquals('test', $option->Value);
|
2016-04-20 00:40:37 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
// Allow empty values
|
|
|
|
EditableOption::set_allow_empty_values(true);
|
2016-07-21 07:53:59 +02:00
|
|
|
$option->Value = '';
|
|
|
|
$this->assertEquals('', $option->Value);
|
2016-04-20 00:40:37 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testEditableDropdownField()
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$dropdown = $this->objFromFixture(EditableDropdown::class, 'basic-dropdown');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$field = $dropdown->getFormField();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
$this->assertThat($field, $this->isInstanceOf(DropdownField::class));
|
2016-07-21 07:53:59 +02:00
|
|
|
$values = $field->getSource();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
$this->assertEquals(['Option 1' => 'Option 1', 'Option 2' => 'Option 2'], $values);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testEditableRadioField()
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$radio = $this->objFromFixture(EditableRadioField::class, 'radio-field');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$field = $radio->getFormField();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
$this->assertThat($field, $this->isInstanceOf(OptionsetField::class));
|
2016-07-21 07:53:59 +02:00
|
|
|
$values = $field->getSource();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2017-08-11 02:20:12 +02:00
|
|
|
$this->assertEquals(['Option 5' => 'Option 5', 'Option 6' => 'Option 6'], $values);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2010-09-08 12:35:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testMultipleOptionDuplication()
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$dropdown = $this->objFromFixture(EditableDropdown::class, 'basic-dropdown');
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$clone = $dropdown->duplicate();
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2018-03-05 02:31:33 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
$dropdown->Options()->Count(),
|
|
|
|
$clone->Options()->Count(),
|
|
|
|
"The duplicate should have contain same number of options"
|
|
|
|
);
|
2013-12-04 19:06:34 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
foreach ($clone->Options() as $option) {
|
2017-08-15 05:30:59 +02:00
|
|
|
$original = $dropdown->Options()->find('Title', $option->Title);
|
2015-07-15 23:23:43 +02:00
|
|
|
|
2017-08-15 05:30:59 +02:00
|
|
|
$this->assertEquals($original->Sort, $option->Sort);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-15 23:23:43 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testFileField()
|
|
|
|
{
|
2017-08-09 01:55:09 +02:00
|
|
|
$fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
|
2016-07-21 07:53:59 +02:00
|
|
|
$formField = $fileField->getFormField();
|
2015-08-28 00:42:32 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertContains('jpg', $formField->getValidator()->getAllowedExtensions());
|
|
|
|
$this->assertNotContains('notallowedextension', $formField->getValidator()->getAllowedExtensions());
|
|
|
|
}
|
2015-08-28 00:42:32 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testFileFieldAllowedExtensionsBlacklist()
|
|
|
|
{
|
2017-08-11 02:20:12 +02:00
|
|
|
Config::modify()->merge(EditableFileField::class, 'allowed_extensions_blacklist', ['jpg']);
|
2017-08-09 01:55:09 +02:00
|
|
|
$fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
|
2016-07-21 07:53:59 +02:00
|
|
|
$formField = $fileField->getFormField();
|
2015-10-28 02:33:44 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$this->assertNotContains('jpg', $formField->getValidator()->getAllowedExtensions());
|
|
|
|
}
|
2015-10-28 02:33:44 +01:00
|
|
|
|
2016-07-21 07:53:59 +02: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);
|
|
|
|
}
|
2015-10-28 02:33:44 +01:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
public function testLengthRange()
|
|
|
|
{
|
2016-05-16 08:05:05 +02:00
|
|
|
/** @var EditableTextField $textField */
|
2017-08-09 01:55:09 +02:00
|
|
|
$textField = $this->objFromFixture(EditableTextField::class, 'basic-text');
|
2016-05-16 08:05:05 +02:00
|
|
|
|
|
|
|
// Empty range
|
|
|
|
/** @var TextField $formField */
|
|
|
|
$textField->MinLength = 0;
|
|
|
|
$textField->MaxLength = 0;
|
|
|
|
$attributes = $textField->getFormField()->getAttributes();
|
|
|
|
$this->assertFalse(isset($attributes['maxLength']));
|
|
|
|
$this->assertFalse(isset($attributes['data-rule-minlength']));
|
|
|
|
$this->assertFalse(isset($attributes['data-rule-maxlength']));
|
|
|
|
|
|
|
|
// Test valid range
|
|
|
|
$textField->MinLength = 10;
|
|
|
|
$textField->MaxLength = 20;
|
|
|
|
$attributes = $textField->getFormField()->getAttributes();
|
|
|
|
$this->assertEquals(20, $attributes['maxLength']);
|
|
|
|
$this->assertEquals(20, $attributes['size']);
|
|
|
|
$this->assertEquals(10, $attributes['data-rule-minlength']);
|
|
|
|
$this->assertEquals(20, $attributes['data-rule-maxlength']);
|
|
|
|
|
|
|
|
// textarea
|
|
|
|
$textField->Rows = 3;
|
|
|
|
$attributes = $textField->getFormField()->getAttributes();
|
|
|
|
$this->assertFalse(isset($attributes['maxLength']));
|
|
|
|
$this->assertEquals(10, $attributes['data-rule-minlength']);
|
|
|
|
$this->assertEquals(20, $attributes['data-rule-maxlength']);
|
|
|
|
}
|
2017-04-28 00:22:15 +02:00
|
|
|
|
2017-05-22 05:54:28 +02:00
|
|
|
public function testFormatDisplayRules()
|
|
|
|
{
|
2017-08-15 05:30:59 +02:00
|
|
|
$field = $this->objFromFixture(EditableTextField::class, 'irdNumberField');
|
2017-05-22 05:54:28 +02:00
|
|
|
$displayRules = $field->formatDisplayRules();
|
|
|
|
$this->assertNotNull($displayRules);
|
|
|
|
$this->assertCount(1, $displayRules['operations']);
|
|
|
|
|
|
|
|
// Field is initially visible, so the "view" method should be to hide it
|
|
|
|
$this->assertSame('addClass("hide")', $displayRules['view']);
|
|
|
|
// The opposite method should be to return it to its original state, i.e. show it again
|
|
|
|
$this->assertSame('removeClass("hide")', $displayRules['opposite']);
|
|
|
|
}
|
2017-11-02 22:44:45 +01:00
|
|
|
|
|
|
|
public function testGetIcon()
|
|
|
|
{
|
|
|
|
$field = new EditableTextField;
|
|
|
|
|
|
|
|
$this->assertContains('/images/editabletextfield.png', $field->getIcon());
|
|
|
|
}
|
2020-01-10 05:36:04 +01:00
|
|
|
|
|
|
|
public function displayedProvider()
|
|
|
|
{
|
|
|
|
$one = ['basic_text_name' => 'foobar'];
|
|
|
|
$two = array_merge($one, ['basic_text_name_2' => 'foobar']);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'no display rule AND' => ['alwaysVisible', [], true],
|
|
|
|
'no display rule OR' => ['alwaysVisibleOr', [], true],
|
|
|
|
|
|
|
|
'no display rule hidden AND' => ['neverVisible', [], false],
|
|
|
|
'no display rule hidden OR' => ['neverVisibleOr', [], false],
|
|
|
|
|
|
|
|
'1 unmet display rule AND' => ['singleDisplayRule', [], false],
|
|
|
|
'1 met display rule AND' => ['singleDisplayRule', $one, true],
|
|
|
|
'1 unmet display rule OR' => ['singleDisplayRuleOr', [], false],
|
|
|
|
'1 met display rule OR' => ['singleDisplayRuleOr', $one, true],
|
|
|
|
|
|
|
|
'1 unmet hide rule AND' => ['singleHiddingRule', [], true],
|
|
|
|
'1 met hide rule AND' => ['singleHiddingRule', $one, false],
|
|
|
|
'1 unmet hide rule OR' => ['singleHiddingRuleOr', [], true],
|
|
|
|
'1 met hide rule OR' => ['singleHiddingRuleOr', $one, false],
|
|
|
|
|
|
|
|
'multi display rule AND none met' => ['multiDisplayRule', [], false],
|
|
|
|
'multi display rule AND partially met' => ['multiDisplayRule', $one, false],
|
|
|
|
'multi display rule AND all met' => ['multiDisplayRule', $two, true],
|
|
|
|
|
|
|
|
'multi display rule OR none met' => ['multiDisplayRuleOr', [], false],
|
|
|
|
'multi display rule OR partially met' => ['multiDisplayRuleOr', $one, true],
|
|
|
|
'multi display rule OR all met' => ['multiDisplayRuleOr', $two, true],
|
|
|
|
|
|
|
|
'multi hide rule AND none met' => ['multiHiddingRule', [], true],
|
|
|
|
'multi hide rule AND partially met' => ['multiHiddingRule', $one, true],
|
|
|
|
'multi hide rule AND all met' => ['multiHiddingRule', $two, false],
|
|
|
|
|
|
|
|
'multi hide rule OR none met' => ['multiHiddingRuleOr', [], true],
|
|
|
|
'multi hide rule OR partially met' => ['multiHiddingRuleOr', $one, false],
|
|
|
|
'multi hide rule OR all met' => ['multiHiddingRuleOr', $two, false],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $fieldName
|
|
|
|
* @param $data
|
|
|
|
* @param $expected
|
|
|
|
* @dataProvider displayedProvider
|
|
|
|
*/
|
2020-01-13 00:25:47 +01:00
|
|
|
public function testIsDisplayed($fieldName, $data, $expected)
|
2020-01-10 05:36:04 +01:00
|
|
|
{
|
|
|
|
/** @var EditableFormField $field */
|
|
|
|
$field = $this->objFromFixture(EditableTextField::class, $fieldName);
|
|
|
|
$this->assertEquals($expected, $field->isDisplayed($data));
|
|
|
|
}
|
2013-12-04 19:06:34 +01:00
|
|
|
}
|