PluginProbe
QuadMenu – Mega Menu / 3.3.8
QuadMenu – Mega Menu v3.3.8
3.3.8 3.3.7 3.3.6 trunk 1.8.4 1.8.5 1.8.7 1.8.8 1.8.9 1.9.0 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 88 releases
quadmenu / vendor / franmastromarino / wp-orm / tests / Helpers / GetSanitizedDataTest.php

GetSanitizedDataTest.php in QuadMenu – Mega Menu 3.3.8, at vendor/franmastromarino/wp-orm/tests/Helpers/GetSanitizedDataTest.php

505 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace QuadLayers\WP_Orm\Tests\V2\Helpers;
4
5 use PHPUnit\Framework\TestCase;
6
7 use function QuadLayers\WP_Orm\V2\Helpers\getSanitizedData;
8
9 class GetSanitizedDataTest extends TestCase
10 {
11 private array $schema;
12
13 public function setUp(): void
14 {
15 $this->schema = [
16 'id' => [
17 'type' => 'integer',
18 'default' => 0
19 ],
20 'key1' => [
21 'type' => 'string',
22 'default' => 'key1_default_value1'
23 ],
24 'key2' => [
25 'type' => 'boolean',
26 'default' => false
27 ],
28 'key3' => [
29 'type' => 'object',
30 'default' => (object) [
31 'key3_1' => 'key3_default_value1',
32 'key3_2' => 'key3_default_value2'
33 ],
34 'properties' => [
35 'key3_1' => [
36 'type' => 'string',
37 'default' => 'key3_default_value1'
38 ],
39 'key3_2' => [
40 'type' => 'string',
41 'default' => 'key3_default_value2'
42 ],
43 ]
44 ],
45 'key4' => [
46 'type' => 'number',
47 'default' => 1
48 ],
49 'key5' => [
50 'type' => 'number'
51 ],
52 'key6' => [
53 'type' => 'array',
54 'default' => [
55 'key6_1' => 'key6_default_value1',
56 'key6_2' => 'key6_default_value2'
57 ],
58 'properties' => [
59 'key6_1' => [
60 'type' => 'string',
61 'default' => 'key6_default_value1'
62 ],
63 'key6_2' => [
64 'type' => 'string',
65 'default' => 'key6_default_value2'
66 ],
67 ]
68 ],
69 ];
70
71 }
72
73 public function testGetSanitizedDataStrict()
74 {
75
76 $data = [
77 'id' => 0,
78 'key1' => 'value1_1',
79 'key2' => true,
80 'key3' => [
81 'key3_1' => 'test',
82 'key3_2' => 'test'
83 ],
84 'key4' => '1'
85 ];
86
87 $result = getSanitizedData($data, $this->schema, true);
88
89 $expected = [
90 'id' => 0,
91 'key1' => 'value1_1',
92 'key2' => true
93 ];
94
95 $this->assertSame($expected, $result);
96 }
97
98 public function testGetSanitizedDataNonStrictArrayToObject()
99 {
100
101 $data = [
102 'id' => 0,
103 'key1' => 'value1_1',
104 'key2' => 1,
105 'key3' => [
106 'key3_1' => 'value_3',
107 'key3_2' => 'value_4'
108 ],
109 'key4' => '1',
110 'key5' => '-1'
111 ];
112
113 $expected = [
114 'id' => 0,
115 'key1' => 'value1_1',
116 'key2' => true,
117 'key3' => (object) [
118 'key3_1' => 'value_3',
119 'key3_2' => 'value_4'
120 ],
121 'key4' => 1,
122 'key5' => -1,
123 'key6' => [
124 'key6_1' => 'key6_default_value1',
125 'key6_2' => 'key6_default_value2'
126 ]
127 ];
128
129 $result = getSanitizedData($data, $this->schema, false);
130
131 $this->assertEquals($expected, $result);
132 }
133
134 public function testGetSanitizedDataNonStrictObjectToArray()
135 {
136
137 $data = [
138 'id' => 0,
139 'key1' => 'value1_1',
140 'key2' => 1,
141 'key3' => [
142 'key3_1' => 'value_3',
143 'key3_2' => 'value_4'
144 ],
145 'key4' => '1',
146 'key5' => '-1',
147 'key6' => (object) [
148 'key6_1' => 'value_3',
149 'key6_2' => 'value_4'
150 ]
151 ];
152
153 $result = getSanitizedData($data, $this->schema, false);
154
155 $expected = [
156 'id' => 0,
157 'key1' => 'value1_1',
158 'key2' => true,
159 'key3' => (object) [
160 'key3_1' => 'value_3',
161 'key3_2' => 'value_4'
162 ],
163 'key4' => 1,
164 'key5' => -1,
165 'key6' => [
166 'key6_1' => 'value_3',
167 'key6_2' => 'value_4'
168 ]
169 ];
170
171 $this->assertEquals($expected, $result);
172 }
173 public function testGetSanitizedDataWithUnsupportedType()
174 {
175 $this->expectException(\InvalidArgumentException::class);
176 $this->expectExceptionMessage("Unsupported type 'unsupported_type' in schema for key 'unsupported_key'");
177
178 $data = [
179 'unsupported_key' => 'value',
180 ];
181
182 $schema = [
183 'unsupported_key' => [
184 'type' => 'unsupported_type',
185 'default' => 'default'
186 ],
187 ];
188
189 getSanitizedData($data, $schema, false);
190 }
191
192 public function testGetSanitizedDataNonStrictWithInvalidNumber()
193 {
194 $data = [
195 'key' => 'not a number'
196 ];
197
198 $schema = [
199 'key' => [
200 'type' => 'number',
201 'default' => 0
202 ],
203 ];
204
205 $expected = [
206 'key' => 0
207 ];
208
209 $result = getSanitizedData($data, $schema, false);
210
211 $this->assertSame($expected, $result);
212 }
213
214 public function testGetSanitizedDataStrictWithInvalidNumber()
215 {
216 $data = [
217 'key' => 'not a number'
218 ];
219
220 $schema = [
221 'key' => [
222 'type' => 'number',
223 'default' => 0
224 ],
225 ];
226
227 $expected = [];
228
229 $result = getSanitizedData($data, $schema, true);
230
231 $this->assertSame($expected, $result);
232 }
233
234 public function testGetSanitizedDataNonStrictWithFloatNumber()
235 {
236 $data = [
237 'key' => '10.5'
238 ];
239
240 $schema = [
241 'key' => [
242 'type' => 'number',
243 'default' => 0
244 ],
245 ];
246
247 $expected = [
248 'key' => 10.5
249 ];
250
251 $result = getSanitizedData($data, $schema, false);
252
253 $this->assertEquals($expected, $result);
254 }
255
256 public function testGetSanitizedDataStrictWithFloatNumber()
257 {
258 $data = [
259 'key' => '10.5'
260 ];
261
262 $schema = [
263 'key' => [
264 'type' => 'number',
265 'default' => 0
266 ],
267 ];
268
269 $expected = [];
270
271 $result = getSanitizedData($data, $schema, true);
272
273 $this->assertEquals($expected, $result);
274 }
275
276 public function testGetSanitizedDataNonStrictWithInvalidBoolean()
277 {
278 $data = [
279 'key' => 'not a boolean'
280 ];
281
282 $schema = [
283 'key' => [
284 'type' => 'boolean',
285 'default' => false
286 ],
287 ];
288
289 $expected = [
290 'key' => false
291 ];
292
293 $result = getSanitizedData($data, $schema, false);
294
295 $this->assertEquals($expected, $result);
296 }
297
298 public function testGetSanitizedDataStrictWithInvalidBoolean()
299 {
300 $data = [
301 'key' => 'not a boolean'
302 ];
303
304 $schema = [
305 'key' => [
306 'type' => 'boolean',
307 'default' => false
308 ],
309 ];
310
311 $expected = [];
312
313 $result = getSanitizedData($data, $schema, true);
314
315 $this->assertEquals($expected, $result);
316 }
317
318 public function testGetSanitizedDataStrictMissingValue()
319 {
320 $data = [
321 'key1' => 'value1',
322 ];
323
324 $schema = [
325 'key1' => [
326 'type' => 'string',
327 'default' => 'default1'
328 ],
329 'key2' => [
330 'type' => 'string',
331 'default' => 'default2'
332 ],
333 ];
334
335 $expected = [
336 'key1' => 'value1',
337 ];
338
339 $result = getSanitizedData($data, $schema, true);
340
341 $this->assertEquals($expected, $result);
342 }
343
344 public function testGetSanitizedDataNonStrictMissingValue()
345 {
346 $data = [
347 'key1' => 'value1',
348 ];
349
350 $schema = [
351 'key1' => [
352 'type' => 'string',
353 'default' => 'default1'
354 ],
355 'key2' => [
356 'type' => 'string',
357 'default' => 'default2'
358 ],
359 ];
360
361 $expected = [
362 'key1' => 'value1',
363 'key2' => 'default2'
364 ];
365
366 $result = getSanitizedData($data, $schema, false);
367
368 $this->assertEquals($expected, $result);
369 }
370
371 public function testGetSanitizedDataNonStrictInvalidType()
372 {
373 $data = [
374 'key' => 'not a number'
375 ];
376
377 $schema = [
378 'key' => [
379 'type' => 'number',
380 'default' => 0
381 ],
382 ];
383
384 $expected = [
385 'key' => 0
386 ];
387
388 $result = getSanitizedData($data, $schema, false);
389
390 $this->assertEquals($expected, $result);
391 }
392
393 public function testNonStringValueForStringFieldNonStrict()
394 {
395 $data = ['key1' => 1];
396 $schema = ['key1' => ['type' => 'string', 'default' => 'default1']];
397
398 $expected = ['key1' => '1'];
399
400 $this->assertEquals($expected, getSanitizedData($data, $schema, false));
401 }
402
403 public function testNonStringValueForStringFieldStrict()
404 {
405 $data = ['key1' => 1];
406 $schema = ['key1' => ['type' => 'string', 'default' => 'default1']];
407
408 $expected = [];
409
410 $this->assertEquals($expected, getSanitizedData($data, $schema, true));
411 }
412
413 public function testNonObjectValueForObjectFieldNonStrict()
414 {
415 $data = [
416 'key1' => 'string'
417 ];
418
419 $schema = [
420 'key1' => [
421 'type' => 'object',
422 'default' => (object) [
423 'key' => 'default'
424 ]
425 ]
426 ];
427
428 $expected = [
429 'key1' => (object) ['key' => 'default']
430 ];
431
432 $this->assertEquals($expected, getSanitizedData($data, $schema, false));
433 }
434
435 public function testNonObjectValueForObjectFieldStrict()
436 {
437 $data = ['key1' => 'string'];
438 $schema = ['key1' => ['type' => 'object', 'default' => (object) ['key' => 'default']]];
439
440 $expected = [];
441
442 $this->assertEquals($expected, getSanitizedData($data, $schema, true));
443 }
444
445 public function testNonIntegerValueForIntegerFieldNonStrict()
446 {
447 $data = ['key1' => 'string'];
448 $schema = ['key1' => ['type' => 'integer', 'default' => 0]];
449
450 $expected = ['key1' => 0];
451
452 $this->assertEquals($expected, getSanitizedData($data, $schema, false));
453 }
454
455 public function testNonIntegerValueForIntegerFieldStrict()
456 {
457 $data = ['key1' => 'string'];
458 $schema = ['key1' => ['type' => 'integer', 'default' => 0]];
459
460 $expected = [];
461
462 $this->assertEquals($expected, getSanitizedData($data, $schema, true));
463 }
464
465 public function testStringTrueFalseForBooleanFieldNonStrict()
466 {
467 $data = ['key1' => 'true', 'key2' => 'false'];
468 $schema = ['key1' => ['type' => 'boolean', 'default' => false], 'key2' => ['type' => 'boolean', 'default' => false]];
469
470 $expected = ['key1' => true, 'key2' => false];
471
472 $this->assertEquals($expected, getSanitizedData($data, $schema, false));
473 }
474
475 public function testStringTrueFalseForBooleanFieldStrict()
476 {
477 $data = ['key1' => 'true', 'key2' => 'false'];
478 $schema = ['key1' => ['type' => 'boolean', 'default' => false], 'key2' => ['type' => 'boolean', 'default' => false]];
479
480 $expected = [];
481
482 $this->assertEquals($expected, getSanitizedData($data, $schema, true));
483 }
484
485 public function testNonBooleanValueForBooleanFieldNonStrict()
486 {
487 $data = ['key1' => 'string'];
488 $schema = ['key1' => ['type' => 'boolean', 'default' => false]];
489
490 $expected = ['key1' => false];
491
492 $this->assertEquals($expected, getSanitizedData($data, $schema, false));
493 }
494
495 public function testNonBooleanValueForBooleanFieldStrict()
496 {
497 $data = ['key1' => 'string'];
498 $schema = ['key1' => ['type' => 'boolean', 'default' => false]];
499
500 $expected = [];
501
502 $this->assertEquals($expected, getSanitizedData($data, $schema, true));
503 }
504 }
505