Skip to content

Commit

Permalink
Patch and reorganise some test
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime-rainville committed Jan 12, 2020
1 parent 8d0a5dd commit d280c54
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 79 deletions.
2 changes: 2 additions & 0 deletions code/Model/EditableCustomRule.php
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\UserForms\Model;

use InvalidArgumentException;
use LogicException;
use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\Control\Controller;
Expand Down Expand Up @@ -237,6 +238,7 @@ public function buildExpression()
*
* @param array $data Submitted form data
* @return boolean
* @throws LogicException Invalid ConditionOption is set for this rule.
*/
public function validateAgainstFormData($data)
{
Expand Down
136 changes: 67 additions & 69 deletions tests/Form/UserFormsRequiredFieldsTest.php
Expand Up @@ -29,55 +29,44 @@ public function testUsesUserFormsRequiredFieldsValidator()
$this->assertInstanceOf(UserFormsRequiredFields::class, $validator, 'Uses UserFormsRequiredFields validator');
}

public function testValidationOfConditionalRequiredFields()
public function dataProviderValidationOfConditionalRequiredFields()
{
$page = $this->objFromFixture(UserDefinedForm::class, 'required-custom-rules-form');
$validator = $this->getValidatorFromPage($page);
$this->assertNotNull($validator);

$this->assertFalse(
$validator->php([]),
'Fails when non-conditional required field is empty'
);

$this->assertTrue(
$validator->php(
return [
'Passes when non-conditional required field has a value' => [
[
'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(
],
true
],
'Fails when conditional required is displayed but not completed' => [
[
'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(
],
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'
]
),
'Passes when conditional required field has a value'
);
],
true
]
];
}

public function testValidationOfNestedConditionalRequiredFields()
/**
* @param $data
* @param $expected
* @dataProvider dataProviderValidationOfConditionalRequiredFields
*/
public function testValidationOfConditionalRequiredFields($data, $expected)
{
$page = $this->objFromFixture(UserDefinedForm::class, 'required-nested-custom-rules-form');
$this->assertEquals(4, $page->Fields()->count());
$page = $this->objFromFixture(UserDefinedForm::class, 'required-custom-rules-form');
$validator = $this->getValidatorFromPage($page);
$this->assertNotNull($validator);

Expand All @@ -86,64 +75,73 @@ public function testValidationOfNestedConditionalRequiredFields()
'Fails when non-conditional required field is empty'
);

$this->assertTrue(
$validator->php(
$this->assertEquals($expected, $validator->php($data));
}

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-2' => 'N',
'radio-option-3' => 'N',
'conditional-required-text-2' => '',
'conditional-required-text-3' => ''
]
),
'Passes when non-conditional required field has a value'
);

$this->assertFalse(
$validator->php(
],
true
],
'Fails when conditional required is displayed but not completed' => [
[
'required-text-field-3' => 'some text',
'radio-option-2' => 'Y',
'radio-option-3' => 'Y',
'conditional-required-text-2' => '',
'conditional-required-text-3' => ''
]
),
'Fails when conditional required is displayed but not completed'
);

$this->assertTrue(
$validator->php(
],
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' => ''
]
),
'Passes when non-conditional required field has a value'
);

$this->assertFalse(
$validator->php(
],
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' => ''
]
),
'Fails when nested conditional required is displayed but not completed'
);

$this->assertTrue(
$validator->php(
],
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'
]
),
'Passes when nested conditional required field has a value'
);
],
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->assertEquals($expected, $validator->php($data));
}
}
80 changes: 71 additions & 9 deletions tests/Model/EditableCustomRuleTest.php
Expand Up @@ -61,23 +61,85 @@ public function testToggleDisplayEvent()
$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']);
}
}
2 changes: 1 addition & 1 deletion tests/UserFormsTest.yml
Expand Up @@ -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
Expand Down

0 comments on commit d280c54

Please sign in to comment.