1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\UserForms\Tests\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
6
|
|
|
use SilverStripe\UserForms\Model\EditableCustomRule; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class EditableCustomRulesTest |
10
|
|
|
*/ |
11
|
|
|
class EditableCustomRuleTest extends SapphireTest |
12
|
|
|
{ |
13
|
|
|
protected static $fixture_file = 'EditableCustomRuleTest.yml'; |
14
|
|
|
|
15
|
|
|
public function testBuildExpression() |
16
|
|
|
{ |
17
|
|
|
/** @var EditableCustomRule $rule1 */ |
18
|
|
|
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1'); |
19
|
|
|
$result1 = $rule1->buildExpression(); |
20
|
|
|
|
21
|
|
|
//Dropdowns expect change event |
22
|
|
|
$this->assertEquals('change', $result1['event']); |
23
|
|
|
$this->assertNotEmpty($result1['operation']); |
24
|
|
|
|
25
|
|
|
//Check for equals sign |
26
|
|
|
$this->assertContains('==', $result1['operation']); |
27
|
|
|
|
28
|
|
|
/** @var EditableCustomRule $rule2 */ |
29
|
|
|
$rule2 = $this->objFromFixture(EditableCustomRule::class, 'rule2'); |
30
|
|
|
$result2 = $rule2->buildExpression(); |
31
|
|
|
|
32
|
|
|
//TextField expect change event |
33
|
|
|
$this->assertEquals('keyup', $result2['event']); |
34
|
|
|
$this->assertNotEmpty($result2['operation']); |
35
|
|
|
|
36
|
|
|
//Check for greater than sign |
37
|
|
|
$this->assertContains('>', $result2['operation']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Test that methods are returned for manipulating the presence of the "hide" CSS class depending |
42
|
|
|
* on whether the field should be hidden or shown |
43
|
|
|
*/ |
44
|
|
|
public function testToggleDisplayText() |
45
|
|
|
{ |
46
|
|
|
/** @var EditableCustomRule $rule1 */ |
47
|
|
|
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1'); |
48
|
|
|
$this->assertSame('addClass("hide")', $rule1->toggleDisplayText('show')); |
49
|
|
|
$this->assertSame('removeClass("hide")', $rule1->toggleDisplayText('hide')); |
50
|
|
|
$this->assertSame('removeClass("hide")', $rule1->toggleDisplayText('show', true)); |
51
|
|
|
$this->assertSame('addClass("hide")', $rule1->toggleDisplayText('hide', true)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testToggleDisplayEvent() |
55
|
|
|
{ |
56
|
|
|
/** @var EditableCustomRule $rule1 */ |
57
|
|
|
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1'); |
58
|
|
|
$this->assertSame('userform.field.hide', $rule1->toggleDisplayEvent('show')); |
59
|
|
|
$this->assertSame('userform.field.show', $rule1->toggleDisplayEvent('hide')); |
60
|
|
|
$this->assertSame('userform.field.show', $rule1->toggleDisplayEvent('show', true)); |
61
|
|
|
$this->assertSame('userform.field.hide', $rule1->toggleDisplayEvent('hide', true)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function dataProviderValidateAgainstFormData() |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'IsNotBlank with blank value' => |
68
|
|
|
['IsNotBlank', '', '', false], |
69
|
|
|
'IsNotBlank with nopn-blank value' => |
70
|
|
|
['IsNotBlank', '', 'something', true], |
71
|
|
|
'IsBlank with blank value' => |
72
|
|
|
['IsBlank', '', '', true], |
73
|
|
|
'IsBlank with nopn-blank value' => |
74
|
|
|
['IsBlank', '', 'something', false], |
75
|
|
|
'HasValue with blank value' => |
76
|
|
|
['HasValue', 'NZ', '', false], |
77
|
|
|
'HasValue with correct value' => |
78
|
|
|
['HasValue', 'NZ', 'NZ', true], |
79
|
|
|
'HasValue with incorrect value' => |
80
|
|
|
['HasValue', 'NZ', 'UK', false], |
81
|
|
|
'ValueNot with blank value' => |
82
|
|
|
['ValueNot', 'NZ', '', true], |
83
|
|
|
'ValueNot with targeted value' => |
84
|
|
|
['ValueNot', 'NZ', 'NZ', false], |
85
|
|
|
'ValueNot with non-targeted value' => |
86
|
|
|
['ValueNot', 'NZ', 'UK', true], |
87
|
|
|
'ValueLessThan with value below target' => |
88
|
|
|
['ValueLessThan', '0', '-0.00001', true], |
89
|
|
|
'ValueLessThan with value equal to target' => |
90
|
|
|
['ValueLessThan', '0', '0', false], |
91
|
|
|
'ValueLessThan with value greater to target' => |
92
|
|
|
['ValueLessThan', '0', '0.0001', false], |
93
|
|
|
'ValueLessThanEqual with value below target' => |
94
|
|
|
['ValueLessThanEqual', '0', '-0.00001', true], |
95
|
|
|
'ValueLessThanEqual with value equal to target' => |
96
|
|
|
['ValueLessThanEqual', '0', '0', true], |
97
|
|
|
'ValueLessThanEqual with value greater to target' => |
98
|
|
|
['ValueLessThanEqual', '0', '0.0001', false], |
99
|
|
|
'ValueGreaterThan with value below target' => |
100
|
|
|
['ValueGreaterThan', '0', '-0.00001', false], |
101
|
|
|
'ValueGreaterThan with value equal to target' => |
102
|
|
|
['ValueGreaterThan', '0', '0', false], |
103
|
|
|
'ValueGreaterThan with value greater to target' => |
104
|
|
|
['ValueGreaterThan', '0', '0.0001', true], |
105
|
|
|
'ValueGreaterThanEqual with value below target' => |
106
|
|
|
['ValueGreaterThanEqual', '0', '-0.00001', false], |
107
|
|
|
'ValueGreaterThanEqual with value equal to target' => |
108
|
|
|
['ValueGreaterThanEqual', '0', '0', true], |
109
|
|
|
'ValueGreaterThanEqual with value greater to target' => |
110
|
|
|
['ValueGreaterThanEqual', '0', '0.0001', true], |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Test that methods are returned for manipulating the presence of the "hide" CSS class depending |
116
|
|
|
* on whether the field should be hidden or shown |
117
|
|
|
* @dataProvider dataProviderValidateAgainstFormData |
118
|
|
|
*/ |
119
|
|
|
public function testValidateAgainstFormData($condition, $targetValue, $value, $expected) |
120
|
|
|
{ |
121
|
|
|
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1'); |
122
|
|
|
$rule1->ConditionOption = $condition; |
123
|
|
|
$rule1->FieldValue = $targetValue; |
124
|
|
|
|
125
|
|
|
$this->assertFalse( |
126
|
|
|
$rule1->validateAgainstFormData([]), |
127
|
|
|
'Unset value always returns false no matter the rule' |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$this->assertEquals( |
131
|
|
|
$expected, |
132
|
|
|
$rule1->validateAgainstFormData(['CountrySelection' => $value]) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @expectedException LogicException |
138
|
|
|
*/ |
139
|
|
|
public function testValidateAgainstFormDataWithNonSenseRule() |
140
|
|
|
{ |
141
|
|
|
$rule1 = $this->objFromFixture(EditableCustomRule::class, 'rule1'); |
142
|
|
|
$rule1->ConditionOption = 'NonSenseRule'; |
143
|
|
|
$rule1->validateAgainstFormData(['CountrySelection' => 'booya']); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|