silverstripe-userforms/tests/EditableFormFieldTest.php

217 lines
7.4 KiB
PHP
Raw Normal View History

<?php
/**
* @package userforms
*/
2016-07-21 07:53:59 +02:00
class EditableFormFieldTest extends FunctionalTest
{
public static $fixture_file = 'userforms/tests/EditableFormFieldTest.yml';
public function testFormFieldPermissions()
{
$text = $this->objFromFixture('EditableTextField', 'basic-text');
$this->logInWithPermission('ADMIN');
$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());
$member = Member::currentUser();
$member->logout();
$this->logInWithPermission('SITETREE_VIEW_ALL');
$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');
$form = $this->objFromFixture('UserDefinedForm', 'custom-rules-form');
2016-07-21 07:53:59 +02:00
$checkbox = $form->Fields()->find('ClassName', 'EditableCheckbox');
$field = $form->Fields()->find('ClassName', 'EditableTextField');
2016-07-21 07:53:59 +02:00
$rules = $checkbox->DisplayRules();
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());
$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');
2016-07-21 07:53:59 +02:00
// If field is required then all custom rules are disabled
$checkbox->Required = true;
$this->assertEquals(0, $checkbox->EffectiveDisplayRules()->count());
}
2015-09-11 00:20:06 +02:00
2016-04-20 00:40:37 +02:00
/**
* @covers EditableOption::getValue
*/
2016-07-21 07:53:59 +02:00
public function testEditableOptionEmptyValue()
{
$option = $this->objFromFixture('EditableOption', 'option-1');
$option->Value = '';
2016-04-20 00:40:37 +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
// 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()
{
$dropdown = $this->objFromFixture('EditableDropdown', '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
2016-07-21 07:53:59 +02:00
$this->assertThat($field, $this->isInstanceOf('DropdownField'));
$values = $field->getSource();
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
$this->assertEquals(array('Option 1' => 'Option 1', 'Option 2' => 'Option 2'), $values);
}
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
public function testEditableRadioField()
{
$radio = $this->objFromFixture('EditableRadioField', '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
2016-07-21 07:53:59 +02:00
$this->assertThat($field, $this->isInstanceOf('OptionsetField'));
$values = $field->getSource();
2015-09-11 00:20:06 +02:00
2016-07-21 07:53:59 +02:00
$this->assertEquals(array('Option 5' => 'Option 5', 'Option 6' => 'Option 6'), $values);
}
2016-07-21 07:53:59 +02:00
public function testMultipleOptionDuplication()
{
$dropdown = $this->objFromFixture('EditableDropdown', '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
2016-07-21 07:53:59 +02:00
$this->assertEquals($clone->Options()->Count(), $dropdown->Options()->Count());
2016-07-21 07:53:59 +02:00
foreach ($clone->Options() as $option) {
$orginal = $dropdown->Options()->find('Title', $option->Title);
2016-07-21 07:53:59 +02:00
$this->assertEquals($orginal->Sort, $option->Sort);
}
}
2016-07-21 07:53:59 +02:00
public function testFileField()
{
$fileField = $this->objFromFixture('EditableFileField', 'file-field');
$formField = $fileField->getFormField();
2016-07-21 07:53:59 +02:00
$this->assertContains('jpg', $formField->getValidator()->getAllowedExtensions());
$this->assertNotContains('notallowedextension', $formField->getValidator()->getAllowedExtensions());
}
2016-07-21 07:53:59 +02:00
public function testFileFieldAllowedExtensionsBlacklist()
{
Config::inst()->update('EditableFileField', 'allowed_extensions_blacklist', array('jpg'));
$fileField = $this->objFromFixture('EditableFileField', 'file-field');
$formField = $fileField->getFormField();
2016-07-21 07:53:59 +02:00
$this->assertNotContains('jpg', $formField->getValidator()->getAllowedExtensions());
}
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);
}
2016-07-21 07:53:59 +02:00
public function testLengthRange()
{
/** @var EditableTextField $textField */
$textField = $this->objFromFixture('EditableTextField', 'basic-text');
// 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']);
}
public function testFormatDisplayRules()
{
/** @var EditableCheckbox $checkbox */
$checkbox = $this->objFromFixture('EditableFormField', 'irdNumberField');
$displayRules = $checkbox->formatDisplayRules();
$this->assertNotNull($displayRules);
$this->assertCount(1, $displayRules['operations']);
}
}