API Upgrade test classes, remove tests for obsolete classes

This commit is contained in:
Robbie Averill 2017-08-11 12:20:12 +12:00
parent b27fdcb7f0
commit b8d6ac336f
20 changed files with 338 additions and 801 deletions

View File

@ -2,20 +2,13 @@
namespace SilverStripe\UserForms\Test\Form;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\CMS\Controllers\ModelAsController;
use SilverStripe\UserForms\Form\UserForm;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\UserForms\Form\UserForm;
class UserFormTest extends SapphireTest
{
protected static $fixture_file = 'UserDefinedFormTest.yml';
/**

View File

@ -2,19 +2,14 @@
namespace SilverStripe\UserForms\Test\FormField;
use SilverStripe\UserForms\FormField\UserFormsCheckboxSetField;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\UserForms\FormField\UserFormsCheckboxSetField;
class UserFormsCheckboxSetFieldTest extends SapphireTest
{
public function testValidate() {
$field = new UserFormsCheckboxSetField('Field', 'My field', array('One' => 'One', 'Two' => 'Two'));
$field = new UserFormsCheckboxSetField('Field', 'My field', ['One' => 'One', 'Two' => 'Two']);
$validator = new RequiredFields();
// String values
@ -37,4 +32,4 @@ class UserFormsCheckboxSetFieldTest extends SapphireTest
$field->setValue(array('Three', 'Four'));
$this->assertFalse($field->validate($validator));
}
}
}

View File

@ -2,18 +2,15 @@
namespace SilverStripe\UserForms\Test\Model;
use SilverStripe\UserForms\Model\EditableCustomRule;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\UserForms\Model\EditableCustomRule;
/**
* Class EditableCustomRulesTest
*/
class EditableCustomRuleTest extends SapphireTest
{
protected static $fixture_file = 'userforms/tests/EditableCustomRuleTest.yml';
protected static $fixture_file = 'EditableCustomRuleTest.yml';
public function testBuildExpression()
{
@ -24,15 +21,18 @@ class EditableCustomRuleTest extends SapphireTest
//Dropdowns expect change event
$this->assertEquals('change', $result1['event']);
$this->assertNotEmpty($result1['operation']);
//Check for equals sign
$this->assertContains('==', $result1['operation']);
/** @var EditableCustomRule $rule2 */
$rule2 = $this->objFromFixture(EditableCustomRule::class, 'rule2');
$result2 = $rule2->buildExpression();
//TextField expect change event
$this->assertEquals('keyup', $result2['event']);
$this->assertNotEmpty($result2['operation']);
//Check for greater than sign
$this->assertContains('>', $result2['operation']);
}

View File

@ -1,30 +1,30 @@
EditableFormField:
SilverStripe\UserForms\Model\EditableFormField:
countryDropdown:
ClassName: EditableCountryDropdownField
ClassName: SilverStripe\UserForms\Model\EditableFormField\EditableCountryDropdownField
Name: CountrySelection
Title: "Choose your country"
DisplayRulesConjunction: And
ShowOnLoad: false
irdNumberField:
ClassName: EditableTextField
ClassName: SilverStripe\UserForms\Model\EditableFormField\EditableTextField
Name: IRDNumber
Title: "Enter your IRD Number"
countryTextField:
ClassName: EditableTextField
ClassName: SilverStripe\UserForms\Model\EditableFormField\EditableTextField
Name: CountryTextSelection
Title: "Enter your country (2 digit prefix)"
DisplayRulesConjunction: And
ShowOnLoad: false
EditableCustomRule:
SilverStripe\UserForms\Model\EditableCustomRule:
rule1:
Display: Show
ConditionOption: HasValue
FieldValue: NZ
ConditionField: =>EditableFormField.countryDropdown
Parent: =>EditableFormField.irdNumberField
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField.countryDropdown
Parent: =>SilverStripe\UserForms\Model\EditableFormField.irdNumberField
rule2:
Display: Show
ConditionOption: ValueGreaterThan
FieldValue: 1
ConditionField: =>EditableFormField.countryTextField
Parent: =>EditableFormField.irdNumberField
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField.countryTextField
Parent: =>SilverStripe\UserForms\Model\EditableFormField.irdNumberField

View File

@ -2,25 +2,15 @@
namespace SilverStripe\UserForms\Test\Model\EditableFormField;
use SilverStripe\UserForms\Model\EditableFormField\EditableDropdown;
use SilverStripe\Dev\SapphireTest;
/**
* Tests the {@see EditableDropdown} class
*/
class EditableDropdownTest extends SapphireTest
{
public static $fixture_file = 'userforms/tests/EditableFormFieldTest.yml';
public function setUp()
{
parent::setUp();
}
protected static $fixture_file = '../EditableFormFieldTest.yml';
/**
* Tests that the field sets the empty string if set
@ -45,7 +35,5 @@ class EditableDropdownTest extends SapphireTest
$alternateDropdown->UseEmptyString = true;
$alternateDropdown->write();
$this->assertEquals($formField->getEmptyString(), '');
}
}

View File

@ -2,23 +2,16 @@
namespace SilverStripe\UserForms\Test\Model\EditableFormField;
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\ValidationException;
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
/**
* @package userforms
*/
class EditableFileFieldTest extends SapphireTest
{
/**
* @var string
*/
public static $fixture_file = 'userforms/tests/EditableFormFieldTest.yml';
protected static $fixture_file = '../EditableFormFieldTest.yml';
/**
* @var
@ -28,13 +21,12 @@ class EditableFileFieldTest extends SapphireTest
/**
* Hold the server default max file size upload limit for later
*/
public function setUp()
protected function setUp()
{
parent::setUp();
$editableFileField = singleton(EditableFileField::class);
$this->php_max_file_size = $editableFileField::get_php_max_file_size();
}
/**
@ -73,4 +65,4 @@ class EditableFileFieldTest extends SapphireTest
$this->assertEquals($formField->getValidator()->getAllowedMaxFileSize(), 262144);
}
}
}

View File

@ -2,28 +2,22 @@
namespace SilverStripe\UserForms\Test\Model\EditableFormField;
use HtmlEditorConfig;
use SilverStripe\UserForms\Model\EditableFormField\EditableLiteralField;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
use SilverStripe\Forms\LiteralField;
use SilverStripe\UserForms\Model\EditableFormField\EditableLiteralField;
/**
* Tests the {@see EditableLiteralField} class
*/
class EditableLiteralFieldTest extends SapphireTest
{
public function setUp()
protected function setUp()
{
parent::setUp();
HtmlEditorConfig::set_active('cms');
HTMLEditorConfig::set_active('cms');
}
/**
@ -36,21 +30,21 @@ class EditableLiteralFieldTest extends SapphireTest
$field = new EditableLiteralField();
// Test with sanitisation enabled
Config::inst()->update('HtmlEditorField', 'sanitise_server_side', true);
Config::modify()->update('HtmlEditorField', 'sanitise_server_side', true);
$field->setContent($rawContent);
$this->assertEquals($safeContent, $field->getContent());
// Test with sanitisation disabled
Config::inst()->remove('HtmlEditorField', 'sanitise_server_side');
Config::modify()->remove('HtmlEditorField', 'sanitise_server_side');
$field->setContent($rawContent);
$this->assertEquals($rawContent, $field->getContent());
}
public function testHideLabel()
{
$field = new EditableLiteralField(array(
$field = new EditableLiteralField([
'Title' => 'Test label'
));
]);
$this->assertContains('Test label', $field->getFormField()->FieldHolder());
$this->assertEquals('Test label', $field->getFormField()->Title());
@ -80,7 +74,11 @@ class EditableLiteralFieldTest extends SapphireTest
$field = new EditableLiteralField;
$formField = $field->getFormField();
$this->assertInstanceOf(CompositeField::class, $formField, 'Literal field is contained within a composite field');
$this->assertInstanceOf(
CompositeField::class,
$formField,
'Literal field is contained within a composite field'
);
$this->assertInstanceOf(
LiteralField::class,
$formField->FieldList()->first(),

View File

@ -2,30 +2,23 @@
namespace SilverStripe\UserForms\Test\Model\EditableFormField;
use SilverStripe\Core\Config\Config;
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
use SilverStripe\Assets\Filesystem;
use SilverStripe\Assets\Folder;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
/**
* Tests integration of EditableFileField with the securefiles module
*
* @todo
* @author dmooyman
*/
class SecureEditableFileFieldTest extends SapphireTest
{
protected $usesDatabase = true;
public function setUp()
protected function setUp()
{
parent::setUp();
@ -33,11 +26,11 @@ class SecureEditableFileFieldTest extends SapphireTest
$this->skipTest = true;
$this->markTestSkipped(get_class() . ' skipped unless running with securefiles');
}
Config::inst()->update(EditableFileField::class, 'secure_folder_name', 'SecureEditableFileFieldTest/SecureUploads');
Config::modify()->set(EditableFileField::class, 'secure_folder_name', 'SecureEditableFileFieldTest/SecureUploads');
$this->clearPath();
}
public function tearDown()
protected function tearDown()
{
$this->clearPath();
parent::tearDown();
@ -69,7 +62,7 @@ class SecureEditableFileFieldTest extends SapphireTest
*/
public function testCreateInsecure()
{
Config::inst()->update(EditableFileField::class, 'disable_security', true);
Config::modify()->set(EditableFileField::class, 'disable_security', true);
// Esure folder is created without a folder
$field = new EditableFileField();
@ -88,7 +81,7 @@ class SecureEditableFileFieldTest extends SapphireTest
$this->assertEquals('Inherit', $field->Folder()->CanViewType);
// Enabling security and re-saving will force this field to be made secure (but not changed)
Config::inst()->update(EditableFileField::class, 'disable_security', false);
Config::modify()->set(EditableFileField::class, 'disable_security', false);
singleton(EditableFileField::class)->requireDefaultRecords();
// Reload record from DB

View File

@ -2,36 +2,26 @@
namespace SilverStripe\UserForms\Test\Model\EditableFormField;
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
use SilverStripe\Security\Member;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\UserForms\Model\EditableFormField\EditableCheckbox;
use SilverStripe\UserForms\Model\EditableFormField\EditableOption;
use SilverStripe\UserForms\Model\EditableFormField\EditableDropdown;
use SilverStripe\Forms\DropdownField;
use SilverStripe\UserForms\Model\EditableFormField\EditableRadioField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
use SilverStripe\Core\Config\Config;
use SilverStripe\UserForms\Model\EditableFormField\EditableFormField;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Security\Member;
use SilverStripe\UserForms\Model\EditableFormField\EditableCheckbox;
use SilverStripe\UserForms\Model\EditableFormField\EditableDropdown;
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
use SilverStripe\UserForms\Model\EditableFormField\EditableFormField;
use SilverStripe\UserForms\Model\EditableFormField\EditableOption;
use SilverStripe\UserForms\Model\EditableFormField\EditableRadioField;
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
use SilverStripe\UserForms\Model\UserDefinedForm;
/**
* @package userforms
*/
class EditableFormFieldTest extends FunctionalTest
{
public static $fixture_file = 'userforms/tests/EditableFormFieldTest.yml';
protected static $fixture_file = 'EditableFormFieldTest.yml';
public function testFormFieldPermissions()
{
@ -54,7 +44,7 @@ class EditableFormFieldTest extends FunctionalTest
$this->assertTrue($text->canDelete());
$member = Member::currentUser();
$member->logout();
$member->logOut();
$this->logInWithPermission('SITETREE_VIEW_ALL');
$this->assertFalse($text->canCreate());
@ -105,15 +95,15 @@ class EditableFormFieldTest extends FunctionalTest
$option = $this->objFromFixture(EditableOption::class, 'option-1');
$option->Value = '';
// Disallow empty values
EditableOption::set_allow_empty_values(false);
// Disallow empty values
EditableOption::set_allow_empty_values(false);
$this->assertEquals($option->Title, $option->Value);
$option->Value = 'test';
$this->assertEquals('test', $option->Value);
// Allow empty values
EditableOption::set_allow_empty_values(true);
// Allow empty values
EditableOption::set_allow_empty_values(true);
$option->Value = '';
$this->assertEquals('', $option->Value);
}
@ -124,11 +114,10 @@ class EditableFormFieldTest extends FunctionalTest
$field = $dropdown->getFormField();
$this->assertThat($field, $this->isInstanceOf(DropdownField::class));
$values = $field->getSource();
$this->assertEquals(array('Option 1' => 'Option 1', 'Option 2' => 'Option 2'), $values);
$this->assertEquals(['Option 1' => 'Option 1', 'Option 2' => 'Option 2'], $values);
}
public function testEditableRadioField()
@ -140,7 +129,7 @@ class EditableFormFieldTest extends FunctionalTest
$this->assertThat($field, $this->isInstanceOf(OptionsetField::class));
$values = $field->getSource();
$this->assertEquals(array('Option 5' => 'Option 5', 'Option 6' => 'Option 6'), $values);
$this->assertEquals(['Option 5' => 'Option 5', 'Option 6' => 'Option 6'], $values);
}
public function testMultipleOptionDuplication()
@ -169,7 +158,7 @@ class EditableFormFieldTest extends FunctionalTest
public function testFileFieldAllowedExtensionsBlacklist()
{
Config::inst()->update(EditableFileField::class, 'allowed_extensions_blacklist', array('jpg'));
Config::modify()->merge(EditableFileField::class, 'allowed_extensions_blacklist', ['jpg']);
$fileField = $this->objFromFixture(EditableFileField::class, 'file-field');
$formField = $fileField->getFormField();

View File

@ -1,15 +1,15 @@
EditableFormField:
SilverStripe\UserForms\Model\EditableFormField:
irdNumberField:
ClassName: EditableTextField
ClassName: SilverStripe\UserForms\Model\EditableFormField\EditableTextField
Name: IRDNumber
Title: "Enter your IRD Number"
countryTextField:
ClassName: EditableTextField
ClassName: SilverStripe\UserForms\Model\EditableFormField\EditableTextField
Name: CountryTextSelection
Title: "Enter your country (2 digit prefix)"
DisplayRulesConjunction: And
ShowOnLoad: false
EditableCustomRule:
SilverStripe\UserForms\Model\EditableCustomRule:
rule1:
Display: Show
ConditionOption: HasValue
@ -20,7 +20,7 @@ EditableCustomRule:
Display: Hide
ConditionOption: HasValue
FieldValue: 6
EditableOption:
SilverStripe\UserForms\Model\EditableFormField\EditableOption:
option-1:
Name: Option1
Title: Option 1
@ -61,7 +61,7 @@ EditableOption:
Title: Option 6
Value: Option 6
UserDefinedForm_EmailRecipient:
SilverStripe\UserForms\Model\Recipient\EmailRecipient:
recipient-1:
EmailAddress: test@example.com
EmailSubject: Email Subject
@ -79,7 +79,7 @@ UserDefinedForm_EmailRecipient:
EmailFrom: no-reply@example.com
HideFormData: true
EditableTextField:
SilverStripe\UserForms\Model\EditableFormField\EditableTextField:
basic-text:
Name: basic-text-name
Title: Basic Text Field
@ -94,18 +94,18 @@ EditableTextField:
CustomErrorMessage: Custom Error Message
Required: true
EditableDropdown:
SilverStripe\UserForms\Model\EditableFormField\EditableDropdown:
basic-dropdown:
Name: basic-dropdown
Title: Basic Dropdown Field
Options: =>EditableOption.option-1, =>EditableOption.option-2
Options: =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-1, =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-2
department-dropdown:
Name: department
Title: Department
Options: =>EditableOption.department-1, =>EditableOption.department-2
Options: =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.department-1, =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.department-2
EditableCheckbox:
SilverStripe\UserForms\Model\EditableFormField\EditableCheckbox:
checkbox-1:
Name: checkbox-1
Title: Checkbox 1
@ -117,35 +117,35 @@ EditableCheckbox:
checkbox-with-rule:
Name: checkbox-with-rule
Title: Checkbox with rule
DisplayRules: =>EditableCustomRule.rule-1
DisplayRules: =>SilverStripe\UserForms\Model\EditableCustomRule.rule-1
EditableCheckboxGroupField:
SilverStripe\UserForms\Model\EditableFormField\EditableCheckboxGroupField:
checkbox-group:
Name: check-box-group
Title: Check box group
Options: =>EditableOption.option-3, =>EditableOption.option-4
Options: =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-3, =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-4
EditableEmailField:
SilverStripe\UserForms\Model\EditableFormField\EditableEmailField:
email-field:
Name: email-field
Title: Email
EditableRadioField:
SilverStripe\UserForms\Model\EditableFormField\EditableRadioField:
radio-field:
Name: radio-option
Title: Radio Option
Options: =>EditableOption.option-5, =>EditableOption.option-6
Options: =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-5, =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-6
EditableFileField:
SilverStripe\UserForms\Model\EditableFormField\EditableFileField:
file-field:
Name: file-uploader
Title: Set file
UserDefinedForm:
SilverStripe\UserForms\Model\UserDefinedForm:
basic-form-page:
Title: User Defined Form
Fields: =>EditableTextField.basic-text
EmailRecipients: =>UserDefinedForm_EmailRecipient.recipient-1, =>UserDefinedForm_EmailRecipient.no-html, =>UserDefinedForm_EmailRecipient.no-data
Fields: =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.basic-text
EmailRecipients: =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.recipient-1, =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.no-html, =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.no-data
form-with-reset-and-custom-action:
Title: Form with Reset Action
@ -154,10 +154,10 @@ UserDefinedForm:
validation-form:
Title: Validation Form
Fields: =>EditableTextField.required-text
Fields: =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.required-text
custom-rules-form:
Title: Custom Rules Form
Fields: =>EditableCheckbox.checkbox-with-rule, =>EditableTextField.basic-text-2
Fields: =>SilverStripe\UserForms\Model\EditableFormField\EditableCheckbox.checkbox-with-rule, =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.basic-text-2
empty-form:
Title: Empty Form
Title: Empty Form

View File

@ -0,0 +1,62 @@
<?php
namespace SilverStripe\UserForms\Test\Model\Recipient;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\UserForms\Model\Recipient\EmailRecipientCondition;
/**
* Class EditableCustomRulesTest
*/
class EmailRecipientConditionTest extends SapphireTest
{
protected static $fixture_file = 'EmailRecipientConditionTest.yml';
/**
* Various matching tests
*/
public function testMatches()
{
$fixtureClass = EmailRecipientCondition::class;
//Test Blank
$blankObj = $this->objFromFixture($fixtureClass, 'blankTest');
$this->assertTrue($blankObj->matches(['Name' => null]));
$this->assertFalse($blankObj->matches(['Name' => 'Jane']));
//Test IsNotBlank
$blankObj = $this->objFromFixture($fixtureClass, 'isNotBlankTest');
$this->assertTrue($blankObj->matches(['Name' => 'Jane']));
$this->assertFalse($blankObj->matches(['Name' => null]));
//Test ValueLessthan
$blankObj = $this->objFromFixture($fixtureClass, 'valueLessThanTest');
$this->assertTrue($blankObj->matches(['Age' => 17]));
$this->assertFalse($blankObj->matches(['Age' => 19]));
//Test ValueLessThanEquals
$blankObj = $this->objFromFixture($fixtureClass, 'valueLessThanEqualTest');
$this->assertTrue($blankObj->matches(['Age' => 18]));
$this->assertFalse($blankObj->matches(['Age' => 19]));
//Test ValueGreaterThan
$blankObj = $this->objFromFixture($fixtureClass, 'valueGreaterThanTest');
$this->assertTrue($blankObj->matches(['Age' => 19]));
$this->assertFalse($blankObj->matches(['Age' => 17]));
//Test ValueGreaterThanEquals
$blankObj = $this->objFromFixture($fixtureClass, 'valueGreaterThanEqualTest');
$this->assertTrue($blankObj->matches(['Age' => 18]));
$this->assertFalse($blankObj->matches(['Age' => 17]));
//Test Equals
$blankObj = $this->objFromFixture($fixtureClass, 'equalsTest');
$this->assertTrue($blankObj->matches(['Age' => 18]));
$this->assertFalse($blankObj->matches(['Age' => 17]));
//Test NotEquals
$blankObj = $this->objFromFixture($fixtureClass, 'notEqualsTest');
$this->assertTrue($blankObj->matches(['Age' => 17]));
$this->assertFalse($blankObj->matches(['Age' => 18]));
}
}

View File

@ -0,0 +1,39 @@
SilverStripe\UserForms\Model\EditableFormField\EditableTextField:
nameField:
Name: Name
SilverStripe\UserForms\Model\EditableFormField\EditableNumericField:
ageField:
Name: Age
SilverStripe\UserForms\Model\EditableFormField\EmailRecipientCondition:
blankTest:
ConditionOption: IsBlank
ConditionValue: null
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.nameField
isNotBlankTest:
ConditionOption: IsNotBlank
ConditionValue: null
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.nameField
equalsTest:
ConditionOption: Equals
ConditionValue: 18
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableNumericField.ageField
notEqualsTest:
ConditionOption: NotEquals
ConditionValue: 18
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableNumericField.ageField
valueLessThanTest:
ConditionOption: ValueLessThan
ConditionValue: 18
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableNumericField.ageField
valueLessThanEqualTest:
ConditionOption: ValueLessThanEqual
ConditionValue: 18
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableNumericField.ageField
valueGreaterThanTest:
ConditionOption: ValueGreaterThan
ConditionValue: 18
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableNumericField.ageField
valueGreaterThanEqualTest:
ConditionOption: ValueGreaterThanEqual
ConditionValue: 18
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableNumericField.ageField

View File

@ -1,72 +0,0 @@
<?php
namespace SilverStripe\UserForms\Test\Model\Recipient;
use SilverStripe\UserForms\Model\Recipient\UserDefinedForm_EmailRecipientCondition;
use SilverStripe\Dev\SapphireTest;
/**
* Class EditableCustomRulesTest
*/
class UserDefinedForm_EmailRecipientConditionTest extends SapphireTest
{
protected static $fixture_file = 'userforms/tests/UserDefinedForm_EmailRecipientConditionTest.yml';
/**
* Various matching tests
*/
public function testMatches()
{
$fixtureClass = UserDefinedForm_EmailRecipientCondition::class;
//Test Blank
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
$blankObj = $this->objFromFixture($fixtureClass, 'blankTest');
$this->assertTrue($blankObj->matches(array('Name' => null)));
$this->assertFalse($blankObj->matches(array('Name' => 'Jane')));
//Test IsNotBlank
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
$blankObj = $this->objFromFixture($fixtureClass, 'isNotBlankTest');
$this->assertTrue($blankObj->matches(array('Name' => 'Jane')));
$this->assertFalse($blankObj->matches(array('Name' => null)));
//Test ValueLessthan
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
$blankObj = $this->objFromFixture($fixtureClass, 'valueLessThanTest');
$this->assertTrue($blankObj->matches(array('Age' => 17)));
$this->assertFalse($blankObj->matches(array('Age' => 19)));
//Test ValueLessThanEquals
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
$blankObj = $this->objFromFixture($fixtureClass, 'valueLessThanEqualTest');
$this->assertTrue($blankObj->matches(array('Age' => 18)));
$this->assertFalse($blankObj->matches(array('Age' => 19)));
//Test ValueGreaterThan
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
$blankObj = $this->objFromFixture($fixtureClass, 'valueGreaterThanTest');
$this->assertTrue($blankObj->matches(array('Age' => 19)));
$this->assertFalse($blankObj->matches(array('Age' => 17)));
//Test ValueGreaterThanEquals
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
$blankObj = $this->objFromFixture($fixtureClass, 'valueGreaterThanEqualTest');
$this->assertTrue($blankObj->matches(array('Age' => 18)));
$this->assertFalse($blankObj->matches(array('Age' => 17)));
//Test Equals
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
$blankObj = $this->objFromFixture($fixtureClass, 'equalsTest');
$this->assertTrue($blankObj->matches(array('Age' => 18)));
$this->assertFalse($blankObj->matches(array('Age' => 17)));
//Test NotEquals
/** @var UserDefinedForm_EmailRecipientCondition $blankObj */
$blankObj = $this->objFromFixture($fixtureClass, 'notEqualsTest');
$this->assertTrue($blankObj->matches(array('Age' => 17)));
$this->assertFalse($blankObj->matches(array('Age' => 18)));
}
}

View File

@ -1,39 +0,0 @@
EditableTextField:
nameField:
Name: Name
EditableNumericField:
ageField:
Name: Age
UserDefinedForm_EmailRecipientCondition:
blankTest:
ConditionOption: IsBlank
ConditionValue: null
ConditionField: =>EditableTextField.nameField
isNotBlankTest:
ConditionOption: IsNotBlank
ConditionValue: null
ConditionField: =>EditableTextField.nameField
equalsTest:
ConditionOption: Equals
ConditionValue: 18
ConditionField: =>EditableNumericField.ageField
notEqualsTest:
ConditionOption: NotEquals
ConditionValue: 18
ConditionField: =>EditableNumericField.ageField
valueLessThanTest:
ConditionOption: ValueLessThan
ConditionValue: 18
ConditionField: =>EditableNumericField.ageField
valueLessThanEqualTest:
ConditionOption: ValueLessThanEqual
ConditionValue: 18
ConditionField: =>EditableNumericField.ageField
valueGreaterThanTest:
ConditionOption: ValueGreaterThan
ConditionValue: 18
ConditionField: =>EditableNumericField.ageField
valueGreaterThanEqualTest:
ConditionOption: ValueGreaterThanEqual
ConditionValue: 18
ConditionField: =>EditableNumericField.ageField

View File

@ -2,31 +2,18 @@
namespace SilverStripe\UserForms\Test\Model;
use ResetFormAction;
use UserDefinedForm_Controller;
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
use SilverStripe\UserForms\Model\Submission\SubmittedFormField;
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\CSSContentParser;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\FieldList;
use SilverStripe\View\ArrayData;
use SilverStripe\UserForms\Test\Model\UserDefinedFormControllerTest;
use SilverStripe\Security\Member;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member;
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
use SilverStripe\UserForms\Model\Submission\SubmittedFormField;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\UserForms\Model\UserDefinedFormController;
use SilverStripe\View\ArrayData;
/**
* @package userforms
@ -34,14 +21,13 @@ use SilverStripe\Dev\TestOnly;
class UserDefinedFormControllerTest extends FunctionalTest
{
public static $fixture_file = 'UserDefinedFormTest.yml';
protected static $fixture_file = 'UserDefinedFormTest.yml';
public function testProcess()
{
$form = $this->setupFormFrontend();
$controller = new UserDefinedFormControllerTest_Controller($form);
$controller = new UserDefinedFormController($form);
$this->autoFollowRedirection = false;
$this->clearEmails();
@ -51,11 +37,18 @@ class UserDefinedFormControllerTest extends FunctionalTest
$field = $this->objFromFixture(EditableTextField::class, 'basic-text');
$response = $this->submitForm('UserForm_Form_' . $form->ID, null, array($field->Name => 'Basic Value'));
$response = $this->submitForm('UserForm_Form_' . $form->ID, null, [$field->Name => 'Basic Value']);
// should have a submitted form field now
$submitted = DataObject::get(SubmittedFormField::class, "\"Name\" = 'basic-text-name'");
$this->assertDOSAllMatch(array('Name' => 'basic-text-name', 'Value' => 'Basic Value', 'Title' => 'Basic Text Field'), $submitted);
$this->assertDOSAllMatch(
[
'Name' => 'basic-text-name',
'Value' => 'Basic Value',
'Title' => 'Basic Text Field'
],
$submitted
);
// check emails
$this->assertEmailSent('test@example.com', 'no-reply@example.com', 'Email Subject');
@ -96,31 +89,31 @@ class UserDefinedFormControllerTest extends FunctionalTest
// Post with no fields
$this->get($form->URLSegment);
$response = $this->submitForm('UserForm_Form_' . $form->ID, null, array());
$response = $this->submitForm('UserForm_Form_' . $form->ID, null, []);
$this->assertPartialMatchBySelector(
'.field .message',
array('This field is required')
['This field is required']
);
// Post with all fields, but invalid email
$this->get($form->URLSegment);
$this->submitForm('UserForm_Form_' . $form->ID, null, array(
$this->submitForm('UserForm_Form_' . $form->ID, null, [
'required-email' => 'invalid',
'required-text' => 'bob'
));
]);
$this->assertPartialMatchBySelector(
'.field .message',
array('Please enter an email address')
['Please enter an email address']
);
// Post with only required
$this->get($form->URLSegment);
$this->submitForm('UserForm_Form_' . $form->ID, null, array(
$this->submitForm('UserForm_Form_' . $form->ID, null, [
'required-text' => 'bob'
));
]);
$this->assertPartialMatchBySelector(
'p',
array("Thanks, we've received your submission.")
["Thanks, we've received your submission."]
);
}
@ -129,8 +122,8 @@ class UserDefinedFormControllerTest extends FunctionalTest
$form = $this->setupFormFrontend();
// set formProcessed and SecurityID to replicate the form being filled out
$this->session()->inst_set('SecurityID', 1);
$this->session()->inst_set('FormProcessed', 1);
$this->session()->set('SecurityID', 1);
$this->session()->set('FormProcessed', 1);
$response = $this->get($form->URLSegment.'/finished');
@ -142,8 +135,8 @@ class UserDefinedFormControllerTest extends FunctionalTest
$form = $this->setupFormFrontend();
// replicate finished being added to the end of the form URL without the form being filled out
$this->session()->inst_set('SecurityID', 1);
$this->session()->inst_set('FormProcessed', null);
$this->session()->set('SecurityID', 1);
$this->session()->set('FormProcessed', null);
$response = $this->get($form->URLSegment.'/finished');
@ -154,7 +147,7 @@ class UserDefinedFormControllerTest extends FunctionalTest
{
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
$controller = new UserDefinedFormControllerTest_Controller($form);
$controller = new UserDefinedFormController($form);
// test form
$this->assertEquals($controller->Form()->getName(), 'Form_' . $form->ID, 'The form is referenced as Form');
@ -163,7 +156,7 @@ class UserDefinedFormControllerTest extends FunctionalTest
$this->assertEquals(count($controller->Form()->getValidator()->getRequired()), 0);
$requiredForm = $this->objFromFixture(UserDefinedForm::class, 'validation-form');
$controller = new UserDefinedFormControllerTest_Controller($requiredForm);
$controller = new UserDefinedFormController($requiredForm);
$this->assertEquals($controller->Form()->Fields()->Count(), 1); // disabled SecurityID token fields
$this->assertEquals($controller->Form()->Actions()->Count(), 1);
@ -175,7 +168,7 @@ class UserDefinedFormControllerTest extends FunctionalTest
// generating the fieldset of fields
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
$controller = new UserDefinedFormControllerTest_Controller($form);
$controller = new UserDefinedFormController($form);
$formSteps = $controller->Form()->getFormFields();
$firstStep = $formSteps->first();
@ -185,9 +178,9 @@ class UserDefinedFormControllerTest extends FunctionalTest
// custom error message on a form field
$requiredForm = $this->objFromFixture(UserDefinedForm::class, 'validation-form');
$controller = new UserDefinedFormControllerTest_Controller($requiredForm);
$controller = new UserDefinedFormController($requiredForm);
UserDefinedForm::config()->required_identifier = "*";
Config::modify()->set(UserDefinedForm::class, 'required_identifier', '*');
$formSteps = $controller->Form()->getFormFields();
$firstStep = $formSteps->first();
@ -201,7 +194,7 @@ class UserDefinedFormControllerTest extends FunctionalTest
$field->RightTitle = 'Right Title';
$field->write();
$controller = new UserDefinedFormControllerTest_Controller($form);
$controller = new UserDefinedFormController($form);
$formSteps = $controller->Form()->getFormFields();
$firstStep = $formSteps->first();
@ -209,7 +202,7 @@ class UserDefinedFormControllerTest extends FunctionalTest
// test empty form
$emptyForm = $this->objFromFixture(UserDefinedForm::class, 'empty-form');
$controller = new UserDefinedFormControllerTest_Controller($emptyForm);
$controller = new UserDefinedFormController($emptyForm);
$this->assertFalse($controller->Form()->getFormFields()->exists());
}
@ -219,7 +212,7 @@ class UserDefinedFormControllerTest extends FunctionalTest
// generating the fieldset of actions
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
$controller = new UserDefinedFormControllerTest_Controller($form);
$controller = new UserDefinedFormController($form);
$actions = $controller->Form()->getFormActions();
// by default will have 1 submit button which links to process
@ -230,7 +223,7 @@ class UserDefinedFormControllerTest extends FunctionalTest
// the custom popup should have a reset button and a custom text
$custom = $this->objFromFixture(UserDefinedForm::class, 'form-with-reset-and-custom-action');
$controller = new UserDefinedFormControllerTest_Controller($custom);
$controller = new UserDefinedFormController($custom);
$actions = $controller->Form()->getFormActions();
$expected = new FieldList(new FormAction('process', 'Custom Button'));
@ -247,11 +240,11 @@ class UserDefinedFormControllerTest extends FunctionalTest
$form->Content = 'This is some content without a form nested between it';
$form->doPublish();
$controller = new UserDefinedFormControllerTest_Controller($form);
$controller = new UserDefinedFormController($form);
// check to see if $Form is replaced to inside the content
$index = new ArrayData($controller->index());
$parser = new CSSContentParser($index->renderWith(array(UserDefinedFormControllerTest::class)));
$parser = new CSSContentParser($index->renderWith(__CLASS__));
$this->checkTemplateIsCorrect($parser, $form);
}
@ -260,11 +253,11 @@ class UserDefinedFormControllerTest extends FunctionalTest
{
$form = $this->setupFormFrontend();
$controller = new UserDefinedFormControllerTest_Controller($form);
$controller = new UserDefinedFormController($form);
// check to see if $Form is replaced to inside the content
$index = new ArrayData($controller->index());
$parser = new CSSContentParser($index->renderWith(array(UserDefinedFormControllerTest::class)));
$parser = new CSSContentParser($index->renderWith(__CLASS__));
$this->checkTemplateIsCorrect($parser, $form);
}
@ -307,21 +300,3 @@ class UserDefinedFormControllerTest extends FunctionalTest
$this->assertEquals((string) $action[0]['value'], "Submit", "Submit button has default text");
}
}
class UserDefinedFormControllerTest_Controller extends UserDefinedForm_Controller implements TestOnly
{
/**
* Overloaded to avoid inconsistencies between 2.4.2 and 2.4.3 (disables all security tokens in unit tests by default)
*/
public function Form()
{
$form = parent::Form();
if ($form) {
$form->disableSecurityToken();
}
return $form;
}
}

View File

@ -2,47 +2,33 @@
namespace SilverStripe\UserForms\Test\Model;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\Versioned\Versioned;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\UserForms\Model\Recipient\UserDefinedForm_EmailRecipient;
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\UserForms\Model\EditableFormField\EditableDropdown;
use SilverStripe\Security\Member;
use SilverStripe\UserForms\Model\EditableFormField\EditableFormField;
use SilverStripe\UserForms\Model\EditableCustomRule;
use SilverStripe\ORM\DB;
use SilverStripe\UserForms\Extension\UserFormValidator;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FieldList;
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup;
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd;
use SilverStripe\Core\Convert;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\ORM\DB;
use SilverStripe\Security\Member;
use SilverStripe\UserForms\Extension\UserFormValidator;
use SilverStripe\UserForms\Model\EditableCustomRule;
use SilverStripe\UserForms\Model\EditableFormField\EditableEmailField;
use SilverStripe\UserForms\Model\EditableFormField\EditableDropdown;
use SilverStripe\UserForms\Model\EditableFormField\EditableFormField;
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup;
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd;
use SilverStripe\UserForms\Model\Recipient\EmailRecipient;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\Versioned\Versioned;
/**
* @package userforms
*/
class UserDefinedFormTest extends FunctionalTest
{
public static $fixture_file = 'UserDefinedFormTest.yml';
protected static $fixture_file = 'UserDefinedFormTest.yml';
public function testRollbackToVersion()
{
@ -64,12 +50,12 @@ class UserDefinedFormTest extends FunctionalTest
$form->doPublish();
// check published site
$updated = Versioned::get_one_by_stage(UserDefinedForm::class, "Stage", "\"UserDefinedForm\".\"ID\" = $form->ID");
$updated = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', \"UserDefinedForm\".\"ID\" = $form->ID");
$this->assertEquals($updated->SubmitButtonText, 'Updated Button Text');
$form->doRollbackTo($origVersion);
$orignal = Versioned::get_one_by_stage(UserDefinedForm::class, "Stage", "\"UserDefinedForm\".\"ID\" = $form->ID");
$orignal = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID");
$this->assertEquals($orignal->SubmitButtonText, 'Button Text');
}
@ -112,7 +98,7 @@ class UserDefinedFormTest extends FunctionalTest
$form = $this->objFromFixture(UserDefinedForm::class, 'basic-form-page');
$popup = new UserDefinedForm_EmailRecipient();
$popup = new EmailRecipient();
$popup->FormID = $form->ID;
$fields = $popup->getCMSFields();
@ -145,7 +131,7 @@ class UserDefinedFormTest extends FunctionalTest
public function testGetEmailBodyContent()
{
$recipient = new UserDefinedForm_EmailRecipient();
$recipient = new EmailRecipient();
$emailBody = 'not html';
$emailBodyHtml = '<p>html</p>';
@ -167,16 +153,16 @@ class UserDefinedFormTest extends FunctionalTest
public function testGetEmailTemplateDropdownValues()
{
$recipient = new UserDefinedForm_EmailRecipient();
$recipient = new EmailRecipient();
$defaultValues = array('SubmittedFormEmail' => 'SubmittedFormEmail');
$defaultValues = ['SubmittedFormEmail' => 'SubmittedFormEmail'];
$this->assertEquals($recipient->getEmailTemplateDropdownValues(), $defaultValues);
}
public function testEmailTemplateExists()
{
$recipient = new UserDefinedForm_EmailRecipient();
$recipient = new EmailRecipient();
// Set the default template
$recipient->EmailTemplate = current(array_keys($recipient->getEmailTemplateDropdownValues()));
@ -220,7 +206,7 @@ class UserDefinedFormTest extends FunctionalTest
$form->doPublish();
$live = Versioned::get_one_by_stage(UserDefinedForm::class, "Live", "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
$live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
$this->assertNotNull($live);
$this->assertEquals(2, $live->Fields()->Count()); // one page and one field
@ -228,17 +214,17 @@ class UserDefinedFormTest extends FunctionalTest
$dropdown = $this->objFromFixture(EditableDropdown::class, 'basic-dropdown');
$form->Fields()->add($dropdown);
$stage = Versioned::get_one_by_stage(UserDefinedForm::class, "Stage", "\"UserDefinedForm\".\"ID\" = $form->ID");
$stage = Versioned::get_one_by_stage(UserDefinedForm::class, 'Stage', "\"UserDefinedForm\".\"ID\" = $form->ID");
$this->assertEquals(3, $stage->Fields()->Count());
// should not have published the dropdown
$liveDropdown = Versioned::get_one_by_stage(EditableFormField::class, "Live", "\"EditableFormField_Live\".\"ID\" = $dropdown->ID");
$liveDropdown = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $dropdown->ID");
$this->assertNull($liveDropdown);
// when publishing it should have added it
$form->doPublish();
$live = Versioned::get_one_by_stage(UserDefinedForm::class, "Live", "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
$live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
$this->assertEquals(3, $live->Fields()->Count());
// edit the title
@ -246,12 +232,12 @@ class UserDefinedFormTest extends FunctionalTest
$text->Title = 'Edited title';
$text->write();
$liveText = Versioned::get_one_by_stage(EditableFormField::class, "Live", "\"EditableFormField_Live\".\"ID\" = $text->ID");
$liveText = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $text->ID");
$this->assertFalse($liveText->Title == $text->Title);
$form->doPublish();
$liveText = Versioned::get_one_by_stage(EditableFormField::class, "Live", "\"EditableFormField_Live\".\"ID\" = $text->ID");
$liveText = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $text->ID");
$this->assertTrue($liveText->Title == $text->Title);
// Add a display rule to the dropdown
@ -262,24 +248,24 @@ class UserDefinedFormTest extends FunctionalTest
$ruleID = $displayRule->ID;
// Not live
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, "Live", "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
$this->assertEmpty($liveRule);
// Publish form, it's now live
$form->doPublish();
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, "Live", "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
$this->assertNotEmpty($liveRule);
// Remove rule
$displayRule->delete();
// Live rule still exists
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, "Live", "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
$this->assertNotEmpty($liveRule);
// Publish form, it should remove this rule
$form->doPublish();
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, "Live", "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
$liveRule = Versioned::get_one_by_stage(EditableCustomRule::class, 'Live', "\"EditableCustomRule_Live\".\"ID\" = $ruleID");
$this->assertEmpty($liveRule);
}
@ -292,7 +278,7 @@ class UserDefinedFormTest extends FunctionalTest
$form->doPublish();
// assert that it exists and has a field
$live = Versioned::get_one_by_stage(UserDefinedForm::class, "Live", "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
$live = Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID");
$this->assertTrue(isset($live));
$this->assertEquals(2, DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value());
@ -300,7 +286,7 @@ class UserDefinedFormTest extends FunctionalTest
// unpublish
$form->doUnpublish();
$this->assertNull(Versioned::get_one_by_stage(UserDefinedForm::class, "Live", "\"UserDefinedForm_Live\".\"ID\" = $form->ID"));
$this->assertNull(Versioned::get_one_by_stage(UserDefinedForm::class, 'Live', "\"UserDefinedForm_Live\".\"ID\" = $form->ID"));
$this->assertEquals(0, DB::query("SELECT COUNT(*) FROM \"EditableFormField_Live\"")->value());
}
@ -319,14 +305,14 @@ class UserDefinedFormTest extends FunctionalTest
$field->write();
// check that the published version is not updated
$live = Versioned::get_one_by_stage(EditableFormField::class, "Live", "\"EditableFormField_Live\".\"ID\" = $field->ID");
$live = Versioned::get_one_by_stage(EditableFormField::class, 'Live', "\"EditableFormField_Live\".\"ID\" = $field->ID");
$this->assertEquals('Title', $live->Title);
// revert back to the live data
$form->doRevertToLive();
$form->flushCache();
$check = Versioned::get_one_by_stage(EditableFormField::class, "Stage", "\"EditableFormField\".\"ID\" = $field->ID");
$check = Versioned::get_one_by_stage(EditableFormField::class, 'Stage', "\"EditableFormField\".\"ID\" = $field->ID");
$this->assertEquals('Title', $check->Title);
}
@ -392,85 +378,83 @@ class UserDefinedFormTest extends FunctionalTest
->sort('EmailAddress')
->column('EmailAddress');
$this->assertEquals(
array(
[
'filtered1@example.com',
'filtered2@example.com',
'unfiltered@example.com'
),
],
$result0
);
// check filters based on given data
$result1 = $form->FilteredEmailRecipients(
array(
[
'your-name' => 'Value',
'address' => '',
'street' => 'Anything',
'city' => 'Matches Not Equals',
'colours' => array('Red') // matches 2
), null
'colours' => ['Red'] // matches 2
], null
)
->sort('EmailAddress')
->column('EmailAddress');
$this->assertEquals(
array(
[
'filtered2@example.com',
'unfiltered@example.com'
),
],
$result1
);
// Check all positive matches
$result2 = $form->FilteredEmailRecipients(
array(
[
'your-name' => '',
'address' => 'Anything',
'street' => 'Matches Equals',
'city' => 'Anything',
'colours' => array('Red', 'Blue') // matches 2
), null
'colours' => ['Red', 'Blue'] // matches 2
], null
)
->sort('EmailAddress')
->column('EmailAddress');
$this->assertEquals(
array(
[
'filtered1@example.com',
'filtered2@example.com',
'unfiltered@example.com'
),
],
$result2
);
$result3 = $form->FilteredEmailRecipients(
array(
[
'your-name' => 'Should be blank but is not',
'address' => 'Anything',
'street' => 'Matches Equals',
'city' => 'Anything',
'colours' => array('Blue')
), null
'colours' => ['Blue']
], null
)->column('EmailAddress');
$this->assertEquals(
array(
[
'unfiltered@example.com'
),
],
$result3
);
$result4 = $form->FilteredEmailRecipients(
array(
[
'your-name' => '',
'address' => 'Anything',
'street' => 'Wrong value for this field',
'city' => '',
'colours' => array('Blue', 'Green')
), null
'colours' => ['Blue', 'Green']
], null
)->column('EmailAddress');
$this->assertEquals(
array(
'unfiltered@example.com'
),
['unfiltered@example.com'],
$result4
);
}
@ -497,7 +481,7 @@ class UserDefinedFormTest extends FunctionalTest
$this->logInWithPermission('ADMIN');
// test invalid email addresses fail validation
$recipient = $this->objFromFixture(UserDefinedForm_EmailRecipient::class,
$recipient = $this->objFromFixture(EmailRecipient::class,
'invalid-recipient-list');
$result = $recipient->validate();
$this->assertFalse($result->valid());
@ -505,7 +489,7 @@ class UserDefinedFormTest extends FunctionalTest
$this->assertNotContains('filtered2@example.com', $result->message());
// test valid email addresses pass validation
$recipient = $this->objFromFixture(UserDefinedForm_EmailRecipient::class,
$recipient = $this->objFromFixture(EmailRecipient::class,
'valid-recipient-list');
$result = $recipient->validate();
$this->assertTrue($result->valid());

View File

@ -1,4 +1,4 @@
EditableFormStep:
SilverStripe\UserForms\Model\EditableFormField\EditableFormStep:
form1step1:
Title: 'Step 1'
form3step1:
@ -16,7 +16,7 @@ EditableFormStep:
form7step1:
Title: 'Step 1'
EditableOption:
SilverStripe\UserForms\Model\EditableFormField\EditableOption:
option-1:
Name: Option1
Title: Option 1
@ -61,7 +61,7 @@ EditableOption:
Name: Option9
Title: Green
EditableTextField:
SilverStripe\UserForms\Model\EditableFormField\EditableTextField:
basic-text:
Name: basic-text-name
Title: Basic Text Field
@ -117,18 +117,18 @@ EditableTextField:
Title: Summary Text Field
ShowInSummary: false
EditableDropdown:
SilverStripe\UserForms\Model\EditableFormField\EditableDropdown:
basic-dropdown:
Name: basic-dropdown
Title: Basic Dropdown Field
Options: =>EditableOption.option-1, =>EditableOption.option-2
Options: =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-1, =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-2
department-dropdown:
Name: department
Title: Department
Options: =>EditableOption.department-1, =>EditableOption.department-2
Options: =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.department-1, =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.department-2
EditableCheckbox:
SilverStripe\UserForms\Model\EditableFormField\EditableCheckbox:
checkbox-1:
Name: checkbox-1
Title: Checkbox 1
@ -137,18 +137,18 @@ EditableCheckbox:
Name: checkbox-1
Title: Checkbox 1
EditableCheckboxGroupField:
SilverStripe\UserForms\Model\EditableFormField\EditableCheckboxGroupField:
checkbox-group:
Name: check-box-group
Title: Check box group
Options: =>EditableOption.option-3, =>EditableOption.option-4
Options: =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-3, =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-4
colour-checkbox-group:
Name: colours
Title: 'Select Colours'
Options: =>EditableOption.option-7, =>EditableOption.option-8, =>EditableOption.option-9
Options: =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-7, =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-8, =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-9
EditableEmailField:
SilverStripe\UserForms\Model\EditableFormField\EditableEmailField:
email-field:
Name: email-field
Title: Email
@ -158,49 +158,49 @@ EditableEmailField:
Title: Enter your email
CustomErrorMessage: 'That email is not valid'
EditableRadioField:
SilverStripe\UserForms\Model\EditableFormField\EditableRadioField:
radio-field:
Name: radio-option
Title: Radio Option
Options: =>EditableOption.option-5, =>EditableOption.option-6
Options: =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-5, =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-6
EditableFieldGroupEnd:
SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd:
group1end:
Name: group1end
EditableFieldGroup:
SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup:
group1start:
Name: group1start
End: =>EditableFieldGroupEnd.group1end
End: =>SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd.group1end
UserDefinedForm_EmailRecipientCondition:
SilverStripe\UserForms\Model\Recipient\EmailRecipientCondition:
# filtered recipient 1
blank-rule:
ConditionOption: IsBlank
ConditionField: =>EditableTextField.your-name-field
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.your-name-field
not-blank-rule:
ConditionOption: IsNotBlank
ConditionField: =>EditableTextField.address-field
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.address-field
equals-rule:
ConditionOption: Equals
ConditionField: =>EditableTextField.street-field
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.street-field
ConditionValue: 'Matches Equals'
not-equals-rule:
ConditionOption: NotEquals
ConditionField: =>EditableTextField.city-field
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.city-field
ConditionValue: 'Matches Not Equals'
# filtered recipient 2
group-equals-rule:
ConditionOption: Equals
ConditionField: =>EditableCheckboxGroupField.colour-checkbox-group
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableCheckboxGroupField.colour-checkbox-group
ConditionValue: Red
group-not-equals-rule:
ConditionOption: NotEquals
ConditionField: =>EditableCheckboxGroupField.colour-checkbox-group
ConditionField: =>SilverStripe\UserForms\Model\EditableFormField\EditableCheckboxGroupField.colour-checkbox-group
ConditionValue: Blue
UserDefinedForm_EmailRecipient:
SilverStripe\UserForms\Model\Recipient\EmailRecipient:
recipient-1:
EmailAddress: test@example.com
EmailSubject: Email Subject
@ -227,14 +227,14 @@ UserDefinedForm_EmailRecipient:
EmailAddress: filtered1@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
CustomRules: =>UserDefinedForm_EmailRecipientCondition.blank-rule, =>UserDefinedForm_EmailRecipientCondition.not-blank-rule, =>UserDefinedForm_EmailRecipientCondition.equals-rule, =>UserDefinedForm_EmailRecipientCondition.not-equals-rule
CustomRules: =>SilverStripe\UserForms\Model\Recipient\EmailRecipientCondition.blank-rule, =>SilverStripe\UserForms\Model\Recipient\EmailRecipientCondition.not-blank-rule, =>SilverStripe\UserForms\Model\Recipient\EmailRecipientCondition.equals-rule, =>SilverStripe\UserForms\Model\Recipient\EmailRecipientCondition.not-equals-rule
CustomRulesCondition: 'And'
filtered-recipient-2:
EmailAddress: filtered2@example.com
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
CustomRules: =>UserDefinedForm_EmailRecipientCondition.group-equals-rule, =>UserDefinedForm_EmailRecipientCondition.group-not-equals-rule
CustomRules: =>SilverStripe\UserForms\Model\Recipient\EmailRecipientCondition.group-equals-rule, =>SilverStripe\UserForms\Model\Recipient\EmailRecipientCondition.group-not-equals-rule
CustomRulesCondition: 'Or'
valid-recipient-list:
@ -247,17 +247,17 @@ UserDefinedForm_EmailRecipient:
EmailSubject: Email Subject
EmailFrom: no-reply@example.com
UserDefinedForm:
SilverStripe\UserForms\Model\UserDefinedForm:
basic-form-page:
Content: '<p>Here is my form</p><p>$UserDefinedForm</p><p>Thank you for filling it out</p>'
Title: User Defined Form
Fields: =>EditableFormStep.form1step1,=>EditableTextField.basic-text
EmailRecipients: =>UserDefinedForm_EmailRecipient.recipient-1, =>UserDefinedForm_EmailRecipient.no-html, =>UserDefinedForm_EmailRecipient.no-data
Fields: =>SilverStripe\UserForms\Model\EditableFormField\EditableFormStep.form1step1,=>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.basic-text
EmailRecipients: =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.recipient-1, =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.no-html, =>SilverStripe\UserForms\Model\Recipient\EmailRecipient.no-data
page-with-group:
Content: 'Page with group'
Title: 'page with group'
Fields: =>EditableFormStep.form7step1, =>EditableFieldGroup.group1start, =>EditableTextField.some-field, =>EditableFieldGroupEnd.group1end
Fields: =>SilverStripe\UserForms\Model\EditableFormField\EditableFormStep.form7step1, =>SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup.group1start, =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.some-field, =>SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd.group1end
form-with-reset-and-custom-action:
Title: Form with Reset Action
@ -266,28 +266,28 @@ UserDefinedForm:
validation-form:
Title: Validation Form
Fields: =>EditableFormStep.form3step1,=>EditableTextField.required-text
Fields: =>SilverStripe\UserForms\Model\EditableFormField\EditableFormStep.form3step1,=>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.required-text
custom-rules-form:
Title: Custom Rules Form
Fields: =>EditableCheckbox.checkbox-2, =>EditableTextField.basic-text-2
Fields: =>SilverStripe\UserForms\Model\EditableFormField\EditableCheckbox.checkbox-2, =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.basic-text-2
summary-rules-form:
Title: Summary Fields Form
Fields: =>EditableTextField.summary-show, =>EditableTextField.summary-hide
Fields: =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.summary-show, =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.summary-hide
empty-form:
Title: Empty Form
filtered-form-page:
Title: 'Page with filtered recipients'
Fields: =>EditableFormStep.form5step1,=>EditableCheckboxGroupField.checkbox-group, =>EditableTextField.your-name-field, =>EditableTextField.street-field, =>EditableTextField.city-field
EmailRecipients: =>UserDefinedForm_EmailRecipient.unfiltered-recipient-1, =>UserDefinedForm_EmailRecipient.filtered-recipient-1, =>UserDefinedForm_EmailRecipient.filtered-recipient-2
Fields: =>SilverStripe\UserForms\Model\EditableFormField\EditableFormStep.form5step1,=>SilverStripe\UserForms\Model\EditableFormField\EditableCheckboxGroupField.checkbox-group, =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.your-name-field, =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.street-field, =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.city-field
EmailRecipients: =>EmailRecipient.unfiltered-recipient-1, =>EmailRecipient.filtered-recipient-1, =>EmailRecipient.filtered-recipient-2
empty-page:
Title: 'Page with empty step'
Fields: =>EditableFormStep.form6step1, =>EditableTextField.field-1, =>EditableFormStep.form6step2, =>EditableTextField.field-2, =>EditableFormStep.form6step3
Fields: =>SilverStripe\UserForms\Model\EditableFormField\EditableFormStep.form6step1, =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.field-1, =>SilverStripe\UserForms\Model\EditableFormField\EditableFormStep.form6step2, =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.field-2, =>SilverStripe\UserForms\Model\EditableFormField\EditableFormStep.form6step3
email-form:
Title: 'Page with email field'
Fields: =>EditableEmailField.another-email-field, =>EditableTextField.another-required
Fields: =>SilverStripe\UserForms\Model\EditableFormField\EditableEmailField.another-email-field, =>SilverStripe\UserForms\Model\EditableFormField\EditableTextField.another-required

View File

@ -1,241 +0,0 @@
<?php
namespace SilverStripe\UserForms\Test\Task;
use SilverStripe\Core\Config\Config;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
use SilverStripe\UserForms\Model\EditableFormField\EditableNumericField;
use SilverStripe\Security\Group;
use SilverStripe\UserForms\Model\EditableFormField\EditableMemberListField;
use SilverStripe\UserForms\Model\EditableFormField\EditableLiteralField;
use SilverStripe\UserForms\Model\EditableFormField\EditableFormHeading;
use SilverStripe\Assets\Folder;
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
use SilverStripe\UserForms\Model\EditableFormField\EditableDateField;
use SilverStripe\UserForms\Model\EditableFormField\EditableCheckbox;
use SilverStripe\UserForms\Task\UserFormsUpgradeService;
use SilverStripe\Dev\SapphireTest;
class UserFormsUpgradeServiceTest extends SapphireTest
{
public static $fixture_file = 'UserFormsUpgradeServiceTest.yml';
public function setUp()
{
Config::inst()->update(UserDefinedForm::class, 'upgrade_on_build', false);
parent::setUp();
// Assign rules programatically
$field1 = $this->objFromFixture(EditableTextField::class, 'text1');
$field2 = $this->objFromFixture(EditableTextField::class, 'text2');
$field3 = $this->objFromFixture(EditableTextField::class, 'text3');
$field3->CustomRules = serialize(array(
array(
'Display' => 'Show',
'ConditionField' => $field1->Name,
'ConditionOption' => 'IsBlank',
'Value' => ''
),
array(
'Display' => 'Hide',
'ConditionField' => $field2->Name,
'ConditionOption' => 'HasValue',
'Value' => 'bob'
)
));
$field3->write();
// Assign settings programatically
$field4 = $this->objFromFixture(EditableTextField::class, 'text4');
$field4->CustomSettings = serialize(array(
'MinLength' => 20,
'MaxLength' => 100,
'Rows' => 4,
'ExtraClass' => 'special class',
'RightTitle' => 'My Field',
'ShowOnLoad' => '',
'Default' => 'Enter your text here'
));
$field4->write();
$numeric1 = $this->objFromFixture(EditableNumericField::class, 'numeric1');
$numeric1->CustomSettings = serialize(array(
'RightTitle' => 'Number of %',
'Default' => 1,
'MinValue' => 1,
'MaxValue' => 100,
'ShowOnLoad' => 'Show'
));
$numeric1->write();
$group1 = $this->objFromFixture(Group::class, 'group1');
$members1 = $this->objFromFixture(EditableMemberListField::class, 'members1');
$members1->CustomSettings = serialize(array(
'RightTitle' => 'Select group',
'GroupID' => $group1->ID,
'ShowOnLoad' => 'Hide'
));
$members1->write();
$literal1 = $this->objFromFixture(EditableLiteralField::class, 'literal1');
$literal1->CustomSettings = serialize(array(
'HideFromReports' => 1,
'RightTitle' => 'Literal',
'Content' => '<p>Content</p>',
'ShowOnLoad' => true
));
$literal1->write();
$heading1 = $this->objFromFixture(EditableFormHeading::class, 'heading1');
$heading1->CustomSettings = serialize(array(
'RightTitle' => 'Right',
'Level' => 3,
'HideFromReports' => true,
'ShowOnLoad' => false
));
$heading1->write();
$folder = $this->objFromFixture(Folder::class, 'folder1');
$file1 = $this->objFromFixture(EditableFileField::class, 'file1');
$file1->CustomSettings = serialize(array(
'RightTitle' => 'File field',
'Folder' => $folder->ID
));
$file1->write();
$date1 = $this->objFromFixture(EditableDateField::class, 'date1');
$date1->CustomSettings = serialize(array(
'RightTitle' => 'Date field',
'DefaultToToday' => '1'
));
$date1->write();
$checkbox1 = $this->objFromFixture(EditableCheckbox::class, 'checkbox1');
$checkbox1->CustomSettings = serialize(array(
'Default' => true,
'RightTitle' => 'Check this'
));
$checkbox1->write();
}
/**
* @return UserFormsUpgradeService;
*/
protected function getService()
{
return singleton(UserFormsUpgradeService::class);
}
/**
* Tests migration of custom rules
*/
public function testCustomRulesMigration()
{
$service = $this->getService();
$service->setQuiet(true);
$service->run();
$field1 = $this->objFromFixture(EditableTextField::class, 'text1');
$field2 = $this->objFromFixture(EditableTextField::class, 'text2');
$field3 = $this->objFromFixture(EditableTextField::class, 'text3');
$this->assertDOSEquals(array(
array(
'Display' => 'Show',
'ConditionFieldID' => $field1->ID,
'ConditionOption' => 'IsBlank'
),
array(
'Display' => 'Hide',
'ConditionFieldID' => $field2->ID,
'ConditionOption' => 'HasValue',
'FieldValue' => 'bob'
)
), $field3->DisplayRules());
}
/**
* Tests migration of all custom settings
*/
public function testCustomSettingsMigration()
{
$service = $this->getService();
$service->setQuiet(true);
$service->run();
$group1 = $this->objFromFixture(Group::class, 'group1');
$form = $this->objFromFixture(UserDefinedForm::class, 'form-with-settings');
$folder = $this->objFromFixture(Folder::class, 'folder1');
$this->assertDOSEquals(array(
array(
'ClassName' => EditableTextField::class,
'Title' => 'Text with rule',
'MinLength' => 20,
'MaxLength' => 100,
'Rows' => 4,
'ExtraClass' => 'special class',
'RightTitle' => 'My Field',
'ShowOnLoad' => true,
'Default' => 'Enter your text here',
),
array(
'ClassName' => EditableNumericField::class,
'Title' => 'Numeric 1',
'RightTitle' => 'Number of %',
'Default' => 1,
'MinValue' => 1,
'MaxValue' => 100,
'ShowOnLoad' => true,
),
array(
'ClassName' => EditableMemberListField::class,
'Title' => 'Members 1',
'RightTitle' => 'Select group',
'GroupID' => $group1->ID,
'ShowOnLoad' => false,
),
array(
'ClassName' => EditableLiteralField::class,
'Title' => 'Literal 1',
'HideFromReports' => true,
'RightTitle' => 'Literal',
'Content' => '<p>Content</p>',
'ShowOnLoad' => true,
),
array(
'ClassName' => EditableFormHeading::class,
'Title' => 'Heading 1',
'RightTitle' => 'Right',
'Level' => 3,
'HideFromReports' => true,
'ShowOnLoad' => false,
),
array(
'ClassName' => EditableFileField::class,
'Title' => 'File 1',
'RightTitle' => 'File field',
'FolderID' => $folder->ID,
),
array(
'ClassName' => EditableDateField::class,
'Title' => 'Date 1',
'RightTitle' => 'Date field',
'DefaultToToday' => true,
),
array(
'ClassName' => EditableCheckbox::class,
'Title' => 'Checkbox 1',
'CheckedDefault' => true,
'RightTitle' => 'Check this',
),
), $form->Fields());
}
}

View File

@ -1,57 +0,0 @@
Group:
group1:
Title: 'Awesome Group'
Folder:
folder1:
Title: 'Folder 1'
EditableTextField:
text1:
Name: text1
Title: 'First field'
text2:
Name: text2
Title: 'Second field'
text3:
Name: text3
Title: 'Third field'
text4:
Name: textwithrule
Title: 'Text with rule'
EditableNumericField:
numeric1:
Name: numeric1
Title: 'Numeric 1'
EditableMemberListField:
members1:
Name: members1
Title: 'Members 1'
EditableLiteralField:
literal1:
Name: literal1
Title: 'Literal 1'
EditableFormHeading:
heading1:
Name: heading1
Title: 'Heading 1'
EditableFileField:
file1:
Name: file1
Title: 'File 1'
EditableDateField:
date1:
Name: date1
Title: 'Date 1'
EditableCheckbox:
checkbox1:
Name: checkbox1
Title: 'Checkbox 1'
UserDefinedForm:
form-with-rules:
Title: 'User Defined Form'
Fields: =>EditableTextField.text1,=>EditableTextField.text2,=>EditableTextField.text3
form-with-settings:
Title: 'Form with custom settings'
Fields: =>EditableTextField.text4,=>EditableNumericField.numeric1,=>EditableMemberListField.members1,=>EditableLiteralField.literal1,=>EditableFormHeading.heading1,=>EditableFileField.file1,=>EditableDateField.date1,=>EditableCheckbox.checkbox1

View File

@ -1,62 +0,0 @@
<?php
namespace SilverStripe\UserForms\Test\Task;
use SilverStripe\Versioned\Versioned;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\UserForms\Model\EditableFormField\EditableOption;
use SilverStripe\UserForms\Model\EditableFormField\EditableCheckboxGroupField;
use SilverStripe\Dev\SapphireTest;
class UserFormsVersionedTaskTest extends SapphireTest
{
protected static $fixture_file = 'UserDefinedFormTest.yml';
public function setUp()
{
parent::setUp();
Versioned::reading_stage('Stage');
}
public function testPublishing()
{
/** @var UserDefinedForm $form */
$form = $this->objFromFixture(UserDefinedForm::class, 'filtered-form-page');
// Get id of options
$optionID = $this->idFromFixture(EditableOption::class, 'option-3');
$this->assertEmpty(Versioned::get_one_by_stage(EditableOption::class, 'Live', array('"ID" = ?' => $optionID)));
// Publishing writes this to live
$form->doPublish();
$liveVersion = Versioned::get_versionnumber_by_stage(EditableOption::class, 'Live', $optionID, false);
$this->assertNotEmpty($liveVersion);
// Add new option, and repeat publish process
/** @var EditableCheckboxGroupField $list */
$list = $this->objFromFixture(EditableCheckboxGroupField::class, 'checkbox-group');
$newOption = new EditableOption();
$newOption->Title = 'New option';
$newOption->Value = 'ok';
$newOption->write();
$newOptionID = $newOption->ID;
$list->Options()->add($newOption);
$form->doPublish();
// Un-modified option should not create a new version
$newLiveVersion = Versioned::get_versionnumber_by_stage(EditableOption::class, 'Live', $optionID, false);
$this->assertNotEmpty($newLiveVersion);
$this->assertEquals($liveVersion, $newLiveVersion);
// New option is successfully published
$newOptionLiveVersion = Versioned::get_versionnumber_by_stage(EditableOption::class, 'Live', $newOptionID, false);
$this->assertNotEmpty($newOptionLiveVersion);
}
}