Patch and reorganise some test

This commit is contained in:
Maxime Rainville 2020-01-13 12:15:34 +13:00
parent 8d0a5dd093
commit d280c54860
4 changed files with 170 additions and 108 deletions

View File

@ -2,6 +2,7 @@
namespace SilverStripe\UserForms\Model;
use InvalidArgumentException;
use LogicException;
use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\Control\Controller;
@ -237,6 +238,7 @@ class EditableCustomRule extends DataObject
*
* @param array $data Submitted form data
* @return boolean
* @throws LogicException Invalid ConditionOption is set for this rule.
*/
public function validateAgainstFormData($data)
{

View File

@ -29,7 +29,42 @@ class UserFormsRequiredFieldsTest extends SapphireTest
$this->assertInstanceOf(UserFormsRequiredFields::class, $validator, 'Uses UserFormsRequiredFields validator');
}
public function testValidationOfConditionalRequiredFields()
public function dataProviderValidationOfConditionalRequiredFields()
{
return [
'Passes when non-conditional required field has a value' => [
[
'required-text-field-2' => 'some text',
'radio-option-2' => 'N',
'conditional-required-text' => ''
],
true
],
'Fails when conditional required is displayed but not completed' => [
[
'required-text-field-2' => 'some text',
'radio-option-2' => 'Y',
'conditional-required-text' => ''
],
false
],
'Passes when conditional required field has a value' => [
[
'required-text-field-2' => 'some text',
'radio-option-2' => 'Y',
'conditional-required-text' => 'some more text'
],
true
]
];
}
/**
* @param $data
* @param $expected
* @dataProvider dataProviderValidationOfConditionalRequiredFields
*/
public function testValidationOfConditionalRequiredFields($data, $expected)
{
$page = $this->objFromFixture(UserDefinedForm::class, 'required-custom-rules-form');
$validator = $this->getValidatorFromPage($page);
@ -40,110 +75,73 @@ class UserFormsRequiredFieldsTest extends SapphireTest
'Fails when non-conditional required field is empty'
);
$this->assertTrue(
$validator->php(
[
'required-text-field-2' => 'some text',
'radio-option-2' => 'N',
'conditional-required-text' => ''
]
),
'Passes when non-conditional required field has a value'
);
$this->assertFalse(
$validator->php(
[
'required-text-field-2' => 'some text',
'radio-option-2' => 'Y',
'conditional-required-text' => ''
]
),
'Fails when conditional required is displayed but not completed'
);
$this->assertTrue(
$validator->php(
[
'required-text-field-2' => 'some text',
'radio-option-2' => 'Y',
'conditional-required-text' => 'some more text'
]
),
'Passes when conditional required field has a value'
);
$this->assertEquals($expected, $validator->php($data));
}
public function testValidationOfNestedConditionalRequiredFields()
public function dataProviderValidationOfNestedConditionalRequiredFields()
{
return [
'Fails when non-conditional required field is empty' => [[], false],
'Passes when non-conditional required field has a value' => [
[
'required-text-field-3' => 'some text',
'radio-option-3' => 'N',
'conditional-required-text-2' => '',
'conditional-required-text-3' => ''
],
true
],
'Fails when conditional required is displayed but not completed' => [
[
'required-text-field-3' => 'some text',
'radio-option-3' => 'Y',
'conditional-required-text-2' => '',
'conditional-required-text-3' => ''
],
false
],
'Passes when non-conditional required field has a value' => [
[
'required-text-field-3' => 'some text',
'radio-option-3' => 'Y',
'conditional-required-text-2' => 'this text',
'conditional-required-text-3' => ''
],
true
],
'Fails when nested conditional required is displayed but not completed' => [
[
'required-text-field-3' => 'some text',
'radio-option-3' => 'Y',
'conditional-required-text-2' => 'Show more',
'conditional-required-text-3' => ''
],
false
],
'Passes when nested conditional required field has a value' => [
[
'required-text-field-3' => 'some text',
'radio-option-3' => 'Y',
'conditional-required-text-2' => 'Show more',
'conditional-required-text-3' => 'more text'
],
true
]
];
}
/**
* @param string $data
* @param array $expected
* @dataProvider dataProviderValidationOfNestedConditionalRequiredFields
*/
public function testValidationOfNestedConditionalRequiredFields($data, $expected)
{
$page = $this->objFromFixture(UserDefinedForm::class, 'required-nested-custom-rules-form');
$this->assertEquals(4, $page->Fields()->count());
$validator = $this->getValidatorFromPage($page);
$this->assertNotNull($validator);
$this->assertFalse(
$validator->php([]),
'Fails when non-conditional required field is empty'
);
$this->assertTrue(
$validator->php(
[
'required-text-field-3' => 'some text',
'radio-option-2' => 'N',
'conditional-required-text-2' => '',
'conditional-required-text-3' => ''
]
),
'Passes when non-conditional required field has a value'
);
$this->assertFalse(
$validator->php(
[
'required-text-field-3' => 'some text',
'radio-option-2' => 'Y',
'conditional-required-text-2' => '',
'conditional-required-text-3' => ''
]
),
'Fails when conditional required is displayed but not completed'
);
$this->assertTrue(
$validator->php(
[
'required-text-field-3' => 'some text',
'radio-option-3' => 'Y',
'conditional-required-text-2' => 'this text',
'conditional-required-text-3' => ''
]
),
'Passes when non-conditional required field has a value'
);
$this->assertFalse(
$validator->php(
[
'required-text-field-3' => 'some text',
'radio-option-3' => 'Y',
'conditional-required-text-2' => 'Show more',
'conditional-required-text-3' => ''
]
),
'Fails when nested conditional required is displayed but not completed'
);
$this->assertTrue(
$validator->php(
[
'required-text-field-3' => 'some text',
'radio-option-3' => 'Y',
'conditional-required-text-2' => 'Show more',
'conditional-required-text-3' => 'more text'
]
),
'Passes when nested conditional required field has a value'
);
$this->assertEquals($expected, $validator->php($data));
}
}

View File

@ -61,23 +61,85 @@ class EditableCustomRuleTest extends SapphireTest
$this->assertSame('userform.field.hide', $rule1->toggleDisplayEvent('hide', true));
}
public function dataProviderValidateAgainstFormData()
{
return [
'IsNotBlank with blank value' =>
['IsNotBlank', '', '', false],
'IsNotBlank with nopn-blank value' =>
['IsNotBlank', '', 'something', true],
'IsBlank with blank value' =>
['IsBlank', '', '', true],
'IsBlank with nopn-blank value' =>
['IsBlank', '', 'something', false],
'HasValue with blank value' =>
['HasValue', 'NZ', '', false],
'HasValue with correct value' =>
['HasValue', 'NZ', 'NZ', true],
'HasValue with incorrect value' =>
['HasValue', 'NZ', 'UK', false],
'ValueNot with blank value' =>
['ValueNot', 'NZ', '', true],
'ValueNot with targeted value' =>
['ValueNot', 'NZ', 'NZ', false],
'ValueNot with non-targeted value' =>
['ValueNot', 'NZ', 'UK', true],
'ValueLessThan with value below target' =>
['ValueLessThan', '0', '-0.00001', true],
'ValueLessThan with value equal to target' =>
['ValueLessThan', '0', '0', false],
'ValueLessThan with value greater to target' =>
['ValueLessThan', '0', '0.0001', false],
'ValueLessThanEqual with value below target' =>
['ValueLessThanEqual', '0', '-0.00001', true],
'ValueLessThanEqual with value equal to target' =>
['ValueLessThanEqual', '0', '0', true],
'ValueLessThanEqual with value greater to target' =>
['ValueLessThanEqual', '0', '0.0001', false],
'ValueGreaterThan with value below target' =>
['ValueGreaterThan', '0', '-0.00001', false],
'ValueGreaterThan with value equal to target' =>
['ValueGreaterThan', '0', '0', false],
'ValueGreaterThan with value greater to target' =>
['ValueGreaterThan', '0', '0.0001', true],
'ValueGreaterThanEqual with value below target' =>
['ValueGreaterThanEqual', '0', '-0.00001', false],
'ValueGreaterThanEqual with value equal to target' =>
['ValueGreaterThanEqual', '0', '0', true],
'ValueGreaterThanEqual with value greater to target' =>
['ValueGreaterThanEqual', '0', '0.0001', true],
];
}
/**
* Test that methods are returned for manipulating the presence of the "hide" CSS class depending
* on whether the field should be hidden or shown
* @dataProvider dataProviderValidateAgainstFormData
*/
public function testValidateAgainstFormData()
public function testValidateAgainstFormData($condition, $targetValue, $value, $expected)
{
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1');
$rule1->ConditionOption = $condition;
$rule1->FieldValue = $targetValue;
$this->assertFalse($rule1->validateAgainstFormData([]));
$this->assertFalse($rule1->validateAgainstFormData(['CountrySelection' => 'AU']));
$this->assertTrue($rule1->validateAgainstFormData(['CountrySelection' => 'NZ']));
$this->assertFalse(
$rule1->validateAgainstFormData([]),
'Unset value always returns false no matter the rule'
);
$rule2 = $this->objFromFixture(EditableCustomRule::class, 'rule2');
$this->assertEquals(
$expected,
$rule1->validateAgainstFormData(['CountrySelection' => $value])
);
}
$this->assertFalse($rule2->validateAgainstFormData([]));
$this->assertFalse($rule2->validateAgainstFormData(['CountryTextSelection' => 0]));
$this->assertFalse($rule2->validateAgainstFormData(['CountryTextSelection' => 1]));
$this->assertTrue($rule2->validateAgainstFormData(['CountryTextSelection' => 2]));
/**
* @expectedException LogicException
*/
public function testValidateAgainstFormDataWithNonSenseRule()
{
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1');
$rule1->ConditionOption = 'NonSenseRule';
$rule1->validateAgainstFormData(['CountrySelection' => 'booya']);
}
}

View File

@ -241,7 +241,7 @@ SilverStripe\UserForms\Model\EditableFormField\EditableRadioField:
- =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-y
- =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-n
radio-field-3:
Name: radio-option-2
Name: radio-option-3
Title: Radio Option 3
Options:
- =>SilverStripe\UserForms\Model\EditableFormField\EditableOption.option-y-2