|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Dev\Tests\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Page; |
|
|
|
|
|
|
6
|
|
|
use SilverStripe\Core\Config\Config; |
|
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
8
|
|
|
use SilverStripe\Dev\Validation\RelationValidationService; |
|
9
|
|
|
|
|
10
|
|
|
class RelationValidationTest extends SapphireTest |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected static $extra_dataobjects = [ |
|
16
|
|
|
Team::class, |
|
17
|
|
|
Member::class, |
|
18
|
|
|
Hat::class, |
|
19
|
|
|
Freelancer::class, |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string|null $class |
|
24
|
|
|
* @param string|null $field |
|
25
|
|
|
* @param array $value |
|
26
|
|
|
* @param array $expected |
|
27
|
|
|
* @dataProvider validateCasesProvider |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testValidation(?string $class, ?string $field, array $value, array $expected): void |
|
30
|
|
|
{ |
|
31
|
|
|
if ($class && $field) { |
|
32
|
|
|
Config::modify()->set($class, $field, $value); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$data = RelationValidationService::singleton()->inspectClasses([ |
|
36
|
|
|
Team::class, |
|
37
|
|
|
Member::class, |
|
38
|
|
|
Hat::class, |
|
39
|
|
|
Freelancer::class, |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertSame($expected, $data); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $class |
|
47
|
|
|
* @param string|null $relation |
|
48
|
|
|
* @param array $config |
|
49
|
|
|
* @param bool $expected |
|
50
|
|
|
* @dataProvider ignoredClassesProvider |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testIgnoredClass(string $class, ?string $relation, array $config, bool $expected): void |
|
53
|
|
|
{ |
|
54
|
|
|
$service = RelationValidationService::singleton(); |
|
55
|
|
|
|
|
56
|
|
|
foreach ($config as $name => $value) { |
|
57
|
|
|
$service->config()->set($name, $value); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$result = $service->isIgnored($class, $relation); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertEquals($expected, $result); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function validateCasesProvider(): array |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
|
|
'correct setup' => [ |
|
69
|
|
|
null, |
|
70
|
|
|
null, |
|
71
|
|
|
[], |
|
72
|
|
|
[], |
|
73
|
|
|
], |
|
74
|
|
|
'ambiguous has_one - no relation name' => [ |
|
75
|
|
|
Hat::class, |
|
76
|
|
|
'belongs_to', |
|
77
|
|
|
[ |
|
78
|
|
|
'Hatter' => Member::class, |
|
79
|
|
|
], |
|
80
|
|
|
[ |
|
81
|
|
|
'SilverStripe\Dev\Tests\Validation\Member / Hat : Back relation not found or ambiguous (needs class.relation format)', |
|
82
|
|
|
'SilverStripe\Dev\Tests\Validation\Hat / Hatter : Relation is not in the expected format (needs class.relation format)', |
|
83
|
|
|
], |
|
84
|
|
|
], |
|
85
|
|
|
'ambiguous has_one - incorrect relation name' => [ |
|
86
|
|
|
Hat::class, |
|
87
|
|
|
'belongs_to', |
|
88
|
|
|
[ |
|
89
|
|
|
'Hatter' => Member::class . '.ObviouslyWrong', |
|
90
|
|
|
], |
|
91
|
|
|
[ |
|
92
|
|
|
'SilverStripe\Dev\Tests\Validation\Member / Hat : Back relation not found or ambiguous (needs class.relation format)', |
|
93
|
|
|
'SilverStripe\Dev\Tests\Validation\Hat / Hatter : Back relation not found', |
|
94
|
|
|
], |
|
95
|
|
|
], |
|
96
|
|
|
'ambiguous has_one - too many relations' => [ |
|
97
|
|
|
Hat::class, |
|
98
|
|
|
'belongs_to', |
|
99
|
|
|
[ |
|
100
|
|
|
'Hatter' => Member::class . '.Hat', |
|
101
|
|
|
'HatterCopy' => Member::class . '.Hat', |
|
102
|
|
|
], |
|
103
|
|
|
[ |
|
104
|
|
|
'SilverStripe\Dev\Tests\Validation\Member / Hat : Back relation is ambiguous', |
|
105
|
|
|
], |
|
106
|
|
|
], |
|
107
|
|
|
'invalid has one' => [ |
|
108
|
|
|
Member::class, |
|
109
|
|
|
'has_one', |
|
110
|
|
|
[ |
|
111
|
|
|
'HomeTeam' => Team::class . '.UnnecessaryRelation', |
|
112
|
|
|
'Hat' => Hat::class, |
|
113
|
|
|
], |
|
114
|
|
|
[ |
|
115
|
|
|
'SilverStripe\Dev\Tests\Validation\Member / HomeTeam : Relation SilverStripe\Dev\Tests\Validation\Team.UnnecessaryRelation is not in the expected format (needs class only format).' |
|
116
|
|
|
], |
|
117
|
|
|
], |
|
118
|
|
|
'ambiguous has_many - no relation name' => [ |
|
119
|
|
|
Team::class, |
|
120
|
|
|
'has_many', |
|
121
|
|
|
[ |
|
122
|
|
|
'Members' => Member::class, |
|
123
|
|
|
'FreelancerMembers' => Freelancer::class . '.TemporaryTeam', |
|
124
|
|
|
], |
|
125
|
|
|
[ |
|
126
|
|
|
'SilverStripe\Dev\Tests\Validation\Team / Members : Relation is not in the expected format (needs class.relation format)', |
|
127
|
|
|
'SilverStripe\Dev\Tests\Validation\Member / HomeTeam : Back relation not found or ambiguous (needs class.relation format)', |
|
128
|
|
|
], |
|
129
|
|
|
], |
|
130
|
|
|
'ambiguous has_many - incorrect relation name' => [ |
|
131
|
|
|
Team::class, |
|
132
|
|
|
'has_many', |
|
133
|
|
|
[ |
|
134
|
|
|
'Members' => Member::class . '.ObviouslyWrong', |
|
135
|
|
|
'FreelancerMembers' => Freelancer::class . '.TemporaryTeam', |
|
136
|
|
|
], |
|
137
|
|
|
[ |
|
138
|
|
|
'SilverStripe\Dev\Tests\Validation\Team / Members : Back relation not found or ambiguous (needs class.relation format)', |
|
139
|
|
|
'SilverStripe\Dev\Tests\Validation\Member / HomeTeam : Back relation not found or ambiguous (needs class.relation format)', |
|
140
|
|
|
], |
|
141
|
|
|
], |
|
142
|
|
|
'ambiguous has_many - too many relations' => [ |
|
143
|
|
|
Team::class, |
|
144
|
|
|
'has_many', |
|
145
|
|
|
[ |
|
146
|
|
|
'Members' => Member::class . '.HomeTeam', |
|
147
|
|
|
'MembersCopy' => Member::class . '.HomeTeam', |
|
148
|
|
|
'FreelancerMembers' => Freelancer::class . '.TemporaryTeam', |
|
149
|
|
|
], |
|
150
|
|
|
[ |
|
151
|
|
|
'SilverStripe\Dev\Tests\Validation\Member / HomeTeam : Back relation is ambiguous', |
|
152
|
|
|
], |
|
153
|
|
|
], |
|
154
|
|
|
'ambiguous many_many - no relation name' => [ |
|
155
|
|
|
Hat::class, |
|
156
|
|
|
'belongs_many_many', |
|
157
|
|
|
[ |
|
158
|
|
|
'TeamHats' => Team::class, |
|
159
|
|
|
], |
|
160
|
|
|
[ |
|
161
|
|
|
'SilverStripe\Dev\Tests\Validation\Team / ReserveHats : Back relation not found or ambiguous (needs class.relation format)', |
|
162
|
|
|
'SilverStripe\Dev\Tests\Validation\Hat / TeamHats : Relation is not in the expected format (needs class.relation format)', |
|
163
|
|
|
], |
|
164
|
|
|
], |
|
165
|
|
|
'ambiguous many_many - incorrect relation name' => [ |
|
166
|
|
|
Hat::class, |
|
167
|
|
|
'belongs_many_many', |
|
168
|
|
|
[ |
|
169
|
|
|
'TeamHats' => Team::class . '.ObviouslyWrong', |
|
170
|
|
|
], |
|
171
|
|
|
[ |
|
172
|
|
|
'SilverStripe\Dev\Tests\Validation\Team / ReserveHats : Back relation not found or ambiguous (needs class.relation format)', |
|
173
|
|
|
'SilverStripe\Dev\Tests\Validation\Hat / TeamHats : Back relation not found', |
|
174
|
|
|
], |
|
175
|
|
|
], |
|
176
|
|
|
'ambiguous many_many - too many relations' => [ |
|
177
|
|
|
Hat::class, |
|
178
|
|
|
'belongs_many_many', |
|
179
|
|
|
[ |
|
180
|
|
|
'TeamHats' => Team::class . '.ReserveHats', |
|
181
|
|
|
'TeamHatsCopy' => Team::class . '.ReserveHats', |
|
182
|
|
|
], |
|
183
|
|
|
[ |
|
184
|
|
|
'SilverStripe\Dev\Tests\Validation\Team / ReserveHats : Back relation is ambiguous', |
|
185
|
|
|
], |
|
186
|
|
|
], |
|
187
|
|
|
'ambiguous many_many through - no relation name' => [ |
|
188
|
|
|
Member::class, |
|
189
|
|
|
'belongs_many_many', |
|
190
|
|
|
[ |
|
191
|
|
|
'FreelancerTeams' => Team::class, |
|
192
|
|
|
], |
|
193
|
|
|
[ |
|
194
|
|
|
'SilverStripe\Dev\Tests\Validation\Team / Freelancers : Back relation not found or ambiguous (needs class.relation format)', |
|
195
|
|
|
'SilverStripe\Dev\Tests\Validation\Member / FreelancerTeams : Relation is not in the expected format (needs class.relation format)', |
|
196
|
|
|
], |
|
197
|
|
|
], |
|
198
|
|
|
'ambiguous many_many through - incorrect relation name' => [ |
|
199
|
|
|
Member::class, |
|
200
|
|
|
'belongs_many_many', |
|
201
|
|
|
[ |
|
202
|
|
|
'FreelancerTeams' => Team::class . '.ObviouslyWrong', |
|
203
|
|
|
], |
|
204
|
|
|
[ |
|
205
|
|
|
'SilverStripe\Dev\Tests\Validation\Team / Freelancers : Back relation not found or ambiguous (needs class.relation format)', |
|
206
|
|
|
'SilverStripe\Dev\Tests\Validation\Member / FreelancerTeams : Back relation not found', |
|
207
|
|
|
], |
|
208
|
|
|
], |
|
209
|
|
|
'ambiguous many_many through - too many relations' => [ |
|
210
|
|
|
Member::class, |
|
211
|
|
|
'belongs_many_many', |
|
212
|
|
|
[ |
|
213
|
|
|
'FreelancerTeams' => Team::class . '.Freelancers', |
|
214
|
|
|
'FreelancerTeamsCopy' => Team::class . '.Freelancers', |
|
215
|
|
|
], |
|
216
|
|
|
[ |
|
217
|
|
|
'SilverStripe\Dev\Tests\Validation\Team / Freelancers : Back relation is ambiguous', |
|
218
|
|
|
], |
|
219
|
|
|
], |
|
220
|
|
|
]; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function ignoredClassesProvider(): array |
|
224
|
|
|
{ |
|
225
|
|
|
return [ |
|
226
|
|
|
'class default' => [ |
|
227
|
|
|
Team::class, |
|
228
|
|
|
null, |
|
229
|
|
|
[], |
|
230
|
|
|
true, |
|
231
|
|
|
], |
|
232
|
|
|
'class relation default' => [ |
|
233
|
|
|
Team::class, |
|
234
|
|
|
'Members', |
|
235
|
|
|
[], |
|
236
|
|
|
true, |
|
237
|
|
|
], |
|
238
|
|
|
'page should by included by default (empty namespace)' => [ |
|
239
|
|
|
Page::class, |
|
240
|
|
|
null, |
|
241
|
|
|
[], |
|
242
|
|
|
false, |
|
243
|
|
|
], |
|
244
|
|
|
'class relation via allow rules' => [ |
|
245
|
|
|
Team::class, |
|
246
|
|
|
'Members', |
|
247
|
|
|
[ |
|
248
|
|
|
'allow_rules' => ['app' => 'SilverStripe\Dev\Tests\Validation'], |
|
249
|
|
|
], |
|
250
|
|
|
false, |
|
251
|
|
|
], |
|
252
|
|
|
'class included via allow rules but overwritten by deny rules' => [ |
|
253
|
|
|
Team::class, |
|
254
|
|
|
null, |
|
255
|
|
|
[ |
|
256
|
|
|
'allow_rules' => ['app' => 'SilverStripe\Dev\Tests\Validation'], |
|
257
|
|
|
'deny_rules' => [Team::class], |
|
258
|
|
|
], |
|
259
|
|
|
true, |
|
260
|
|
|
], |
|
261
|
|
|
'class relation included via allow rules but overwritten by deny rules' => [ |
|
262
|
|
|
Team::class, |
|
263
|
|
|
'Members', |
|
264
|
|
|
[ |
|
265
|
|
|
'allow_rules' => ['app' => 'SilverStripe\Dev\Tests\Validation'], |
|
266
|
|
|
'deny_rules' => [Team::class], |
|
267
|
|
|
], |
|
268
|
|
|
true, |
|
269
|
|
|
], |
|
270
|
|
|
'class relation included via allow rules but overwritten by deny relations' => [ |
|
271
|
|
|
Team::class, |
|
272
|
|
|
'Members', |
|
273
|
|
|
[ |
|
274
|
|
|
'allow_rules' => ['app' => 'SilverStripe\Dev\Tests\Validation'], |
|
275
|
|
|
'deny_relations' => [Team::class . '.Members'], |
|
276
|
|
|
], |
|
277
|
|
|
true, |
|
278
|
|
|
], |
|
279
|
|
|
'class relation included via allow rules and not overwritten by deny relations of other class' => [ |
|
280
|
|
|
Member::class, |
|
281
|
|
|
'HomeTeam', |
|
282
|
|
|
[ |
|
283
|
|
|
'allow_rules' => ['app' => 'SilverStripe\Dev\Tests\Validation'], |
|
284
|
|
|
'deny_rules' => [Team::class], |
|
285
|
|
|
'deny_relations' => [Team::class . '.Members'], |
|
286
|
|
|
], |
|
287
|
|
|
false, |
|
288
|
|
|
], |
|
289
|
|
|
]; |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths