1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\ValidationResult; |
6
|
|
|
use SilverStripe\ORM\ValidationException; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
|
9
|
|
|
class ValidationExceptionTest extends SapphireTest |
10
|
|
|
{ |
11
|
|
|
private function arrayContainsArray($expectedSubArray, $array) |
12
|
|
|
{ |
13
|
|
|
foreach ($array as $subArray) { |
14
|
|
|
if ($subArray == $expectedSubArray) { |
15
|
|
|
return true; |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
return false; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Test that ValidationResult object can correctly populate a ValidationException |
23
|
|
|
*/ |
24
|
|
|
public function testCreateFromValidationResult() |
25
|
|
|
{ |
26
|
|
|
$result = new ValidationResult(); |
27
|
|
|
$result->addError('Not a valid result'); |
28
|
|
|
|
29
|
|
|
$exception = new ValidationException($result); |
30
|
|
|
|
31
|
|
|
$this->assertEquals(0, $exception->getCode()); |
32
|
|
|
$this->assertEquals('Not a valid result', $exception->getMessage()); |
33
|
|
|
$this->assertFalse($exception->getResult()->isValid()); |
34
|
|
|
$b = $this->arrayContainsArray([ |
35
|
|
|
'message' => 'Not a valid result', |
36
|
|
|
'messageCast' => ValidationResult::CAST_TEXT, |
37
|
|
|
'messageType' => ValidationResult::TYPE_ERROR, |
38
|
|
|
'fieldName' => null, |
39
|
|
|
], $exception->getResult()->getMessages()); |
40
|
|
|
$this->assertTrue($b, 'Messages array should contain expected messaged'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Test that ValidationResult object with multiple errors can correctly |
45
|
|
|
* populate a ValidationException |
46
|
|
|
*/ |
47
|
|
|
public function testCreateFromComplexValidationResult() |
48
|
|
|
{ |
49
|
|
|
$result = new ValidationResult(); |
50
|
|
|
$result |
51
|
|
|
->addError('Invalid type') |
52
|
|
|
->addError('Out of kiwis'); |
53
|
|
|
$exception = new ValidationException($result); |
54
|
|
|
|
55
|
|
|
$this->assertEquals(0, $exception->getCode()); |
56
|
|
|
$this->assertEquals('Invalid type', $exception->getMessage()); |
57
|
|
|
$this->assertEquals(false, $exception->getResult()->isValid()); |
58
|
|
|
|
59
|
|
|
$b = $this->arrayContainsArray([ |
60
|
|
|
'message' => 'Invalid type', |
61
|
|
|
'messageCast' => ValidationResult::CAST_TEXT, |
62
|
|
|
'messageType' => ValidationResult::TYPE_ERROR, |
63
|
|
|
'fieldName' => null, |
64
|
|
|
], $exception->getResult()->getMessages()); |
65
|
|
|
$this->assertTrue($b, 'Messages array should contain expected messaged'); |
66
|
|
|
|
67
|
|
|
$b = $this->arrayContainsArray([ |
68
|
|
|
'message' => 'Out of kiwis', |
69
|
|
|
'messageCast' => ValidationResult::CAST_TEXT, |
70
|
|
|
'messageType' => ValidationResult::TYPE_ERROR, |
71
|
|
|
'fieldName' => null, |
72
|
|
|
], $exception->getResult()->getMessages()); |
73
|
|
|
$this->assertTrue($b, 'Messages array should contain expected messaged'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Test that a ValidationException created with no contained ValidationResult |
78
|
|
|
* will correctly populate itself with an inferred version |
79
|
|
|
*/ |
80
|
|
|
public function testCreateFromMessage() |
81
|
|
|
{ |
82
|
|
|
$exception = new ValidationException('Error inferred from message', E_USER_ERROR); |
83
|
|
|
|
84
|
|
|
$this->assertEquals(E_USER_ERROR, $exception->getCode()); |
85
|
|
|
$this->assertEquals('Error inferred from message', $exception->getMessage()); |
86
|
|
|
$this->assertFalse($exception->getResult()->isValid()); |
87
|
|
|
|
88
|
|
|
$b = $this->arrayContainsArray([ |
89
|
|
|
'message' => 'Error inferred from message', |
90
|
|
|
'messageCast' => ValidationResult::CAST_TEXT, |
91
|
|
|
'messageType' => ValidationResult::TYPE_ERROR, |
92
|
|
|
'fieldName' => null, |
93
|
|
|
], $exception->getResult()->getMessages()); |
94
|
|
|
$this->assertTrue($b, 'Messages array should contain expected messaged'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Test that ValidationException can be created with both a ValidationResult |
99
|
|
|
* and a custom message |
100
|
|
|
*/ |
101
|
|
|
public function testCreateWithComplexValidationResultAndMessage() |
102
|
|
|
{ |
103
|
|
|
$result = new ValidationResult(); |
104
|
|
|
$result->addError('A spork is not a knife') |
105
|
|
|
->addError('A knife is not a back scratcher'); |
106
|
|
|
$exception = new ValidationException($result, E_USER_WARNING); |
107
|
|
|
|
108
|
|
|
$this->assertEquals(E_USER_WARNING, $exception->getCode()); |
109
|
|
|
$this->assertEquals('A spork is not a knife', $exception->getMessage()); |
110
|
|
|
$this->assertEquals(false, $exception->getResult()->isValid()); |
111
|
|
|
|
112
|
|
|
$b = $this->arrayContainsArray([ |
113
|
|
|
'message' => 'A spork is not a knife', |
114
|
|
|
'messageCast' => ValidationResult::CAST_TEXT, |
115
|
|
|
'messageType' => ValidationResult::TYPE_ERROR, |
116
|
|
|
'fieldName' => null, |
117
|
|
|
], $exception->getResult()->getMessages()); |
118
|
|
|
$this->assertTrue($b, 'Messages array should contain expected messaged'); |
119
|
|
|
|
120
|
|
|
$b = $this->arrayContainsArray([ |
121
|
|
|
'message' => 'A knife is not a back scratcher', |
122
|
|
|
'messageCast' => ValidationResult::CAST_TEXT, |
123
|
|
|
'messageType' => ValidationResult::TYPE_ERROR, |
124
|
|
|
'fieldName' => null, |
125
|
|
|
], $exception->getResult()->getMessages()); |
126
|
|
|
$this->assertTrue($b, 'Messages array should contain expected messaged'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Test combining validation results together |
131
|
|
|
*/ |
132
|
|
|
public function testCombineResults() |
133
|
|
|
{ |
134
|
|
|
$result = new ValidationResult(); |
135
|
|
|
$anotherresult = new ValidationResult(); |
136
|
|
|
$yetanotherresult = new ValidationResult(); |
137
|
|
|
$anotherresult->addError("Eat with your mouth closed", 'bad', "EATING101"); |
138
|
|
|
$yetanotherresult->addError("You didn't wash your hands", 'bad', "BECLEAN", false); |
139
|
|
|
|
140
|
|
|
$this->assertTrue($result->isValid()); |
141
|
|
|
$this->assertFalse($anotherresult->isValid()); |
142
|
|
|
$this->assertFalse($yetanotherresult->isValid()); |
143
|
|
|
|
144
|
|
|
$result->combineAnd($anotherresult) |
145
|
|
|
->combineAnd($yetanotherresult); |
146
|
|
|
$this->assertFalse($result->isValid()); |
147
|
|
|
$this->assertEquals( |
148
|
|
|
[ |
149
|
|
|
'EATING101' => [ |
150
|
|
|
'message' => 'Eat with your mouth closed', |
151
|
|
|
'messageType' => 'bad', |
152
|
|
|
'messageCast' => ValidationResult::CAST_TEXT, |
153
|
|
|
'fieldName' => null, |
154
|
|
|
], |
155
|
|
|
'BECLEAN' => [ |
156
|
|
|
'message' => 'You didn\'t wash your hands', |
157
|
|
|
'messageType' => 'bad', |
158
|
|
|
'messageCast' => ValidationResult::CAST_HTML, |
159
|
|
|
'fieldName' => null, |
160
|
|
|
], |
161
|
|
|
], |
162
|
|
|
$result->getMessages() |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Test that a ValidationException created with no contained ValidationResult |
168
|
|
|
* will correctly populate itself with an inferred version |
169
|
|
|
*/ |
170
|
|
|
public function testValidationResultAddMethods() |
171
|
|
|
{ |
172
|
|
|
$result = new ValidationResult(); |
173
|
|
|
$result->addMessage('A spork is not a knife', 'bad'); |
174
|
|
|
$result->addError('A knife is not a back scratcher'); |
175
|
|
|
$result->addFieldMessage('Title', 'Title is good', 'good'); |
176
|
|
|
$result->addFieldError('Content', 'Content is bad', 'bad'); |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
$this->assertEquals( |
180
|
|
|
[ |
181
|
|
|
[ |
182
|
|
|
'fieldName' => null, |
183
|
|
|
'message' => 'A spork is not a knife', |
184
|
|
|
'messageType' => 'bad', |
185
|
|
|
'messageCast' => ValidationResult::CAST_TEXT, |
186
|
|
|
], |
187
|
|
|
[ |
188
|
|
|
'fieldName' => null, |
189
|
|
|
'message' => 'A knife is not a back scratcher', |
190
|
|
|
'messageType' => 'error', |
191
|
|
|
'messageCast' => ValidationResult::CAST_TEXT, |
192
|
|
|
], |
193
|
|
|
[ |
194
|
|
|
'fieldName' => 'Title', |
195
|
|
|
'message' => 'Title is good', |
196
|
|
|
'messageType' => 'good', |
197
|
|
|
'messageCast' => ValidationResult::CAST_TEXT, |
198
|
|
|
], |
199
|
|
|
[ |
200
|
|
|
'fieldName' => 'Content', |
201
|
|
|
'message' => 'Content is bad', |
202
|
|
|
'messageType' => 'bad', |
203
|
|
|
'messageCast' => ValidationResult::CAST_TEXT, |
204
|
|
|
] |
205
|
|
|
], |
206
|
|
|
$result->getMessages() |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|