Debian Codegraph @

tests/phpunit/unit/includes/Rest/Validator/ValidatorTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest;
4
5 use Exception;
6 use InvalidArgumentException;
7 use MediaWiki\ParamValidator\TypeDef\ArrayDef;
8 use MediaWiki\Permissions\Authority;
9 use MediaWiki\Rest\Handler;
10 use MediaWiki\Rest\HttpException;
11 use MediaWiki\Rest\LocalizedHttpException;
12 use MediaWiki\Rest\RequestData;
13 use MediaWiki\Rest\Validator\BodyValidator;
14 use MediaWiki\Rest\Validator\JsonBodyValidator;
15 use MediaWiki\Rest\Validator\NullBodyValidator;
16 use MediaWiki\Rest\Validator\ParamValidatorCallbacks;
17 use MediaWiki\Rest\Validator\Validator;
18 use MediaWiki\Tests\Unit\DummyServicesTrait;
19 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
20 use MediaWikiUnitTestCase;
21 use PHPUnit\Framework\MockObject\MockObject;
22 use Wikimedia\Message\ListParam;
23 use Wikimedia\Message\ListType;
24 use Wikimedia\Message\MessageValue;
25 use Wikimedia\ParamValidator\ParamValidator;
26
27 /**
28 * @covers \MediaWiki\Rest\Validator\Validator
29 * @covers \MediaWiki\Rest\Validator\ParamValidatorCallbacks
30 */
31 class ValidatorTest extends MediaWikiUnitTestCase {
32 use DummyServicesTrait;
33 use MockAuthorityTrait;
34
35 public static function provideValidateBody() {
36 $bodyData = [
37 'kittens' => 'cute',
38 'number' => 5,
39 ];
40
41 $emptyBodyValidator = new NullBodyValidator();
42 $nonEmptyBodyValidator = @new JsonBodyValidator( [
43 'kittens' => [
44 'rest-param-source' => 'body',
45 ParamValidator::PARAM_TYPE => 'string',
46 ParamValidator::PARAM_REQUIRED => true,
47 ],
48 'number' => [
49 'rest-param-source' => 'body',
50 ParamValidator::PARAM_TYPE => 'integer',
51 ParamValidator::PARAM_REQUIRED => true,
52 ]
53 ] );
54
55 $emptyBodyParams = [ 'bodyContents' => '' ];
56 $nonEmptyBodyParams = [
57 'bodyContents' => json_encode( (object)$bodyData ),
58 'headers' => [
59 'Content-Type' => 'application/json'
60 ]
61 ];
62 $nonEmptyBodyUnknownTypeParams = [
63 'bodyContents' => json_encode( (object)$bodyData ),
64 ];
65
66 // Validator::validateBody() normalizes method for case and leading/trailing whitespace.
67 // Use various permutations herein to confirm that normalization is happy.
68 yield 'GET request with empty body' => [
69 $emptyBodyValidator,
70 new RequestData( $emptyBodyParams + [ 'method' => 'GET' ] ),
71 null
72 ];
73
74 yield 'GET request with non-empty body' => [
75 $emptyBodyValidator,
76 new RequestData( $nonEmptyBodyParams + [ 'method' => 'get' ] ),
77 null
78 ];
79
80 yield 'HEAD request with empty body' => [
81 $emptyBodyValidator,
82 new RequestData( $emptyBodyParams + [ 'method' => 'HEAD' ] ),
83 null
84 ];
85
86 yield 'HEAD request with non-empty body' => [
87 $emptyBodyValidator,
88 new RequestData( $nonEmptyBodyParams + [ 'method' => 'Head' ] ),
89 null
90
91 ];
92 yield 'DELETE request with empty body' => [
93 $emptyBodyValidator,
94 new RequestData( $emptyBodyParams + [ 'method' => 'DELETE' ] ),
95 null
96 ];
97
98 yield 'DELETE request with non-empty body' => [
99 $nonEmptyBodyValidator,
100 new RequestData( $nonEmptyBodyParams + [ 'method' => 'DELETE ' ] ),
101 $bodyData
102 ];
103
104 yield 'POST request with empty body' => [
105 $nonEmptyBodyValidator,
106 new RequestData( $emptyBodyParams + [ 'method' => 'POST' ] ),
107 null
108 ];
109
110 yield 'POST request with unknown type' => [
111 $emptyBodyValidator,
112 new RequestData( $nonEmptyBodyUnknownTypeParams + [ 'method' => 'POST' ] ),
113 HttpException::class
114 ];
115
116 yield 'POST request with non-empty body' => [
117 $nonEmptyBodyValidator,
118 new RequestData( $nonEmptyBodyParams + [ 'method' => ' POST' ] ),
119 $bodyData
120 ];
121
122 yield 'PUT request with empty body' => [
123 $nonEmptyBodyValidator,
124 new RequestData( $emptyBodyParams + [ 'method' => 'PUT' ] ),
125 null
126 ];
127
128 yield 'PUT request with unknown type' => [
129 $emptyBodyValidator,
130 new RequestData( $nonEmptyBodyUnknownTypeParams + [ 'method' => 'PUT' ] ),
131 HttpException::class
132 ];
133
134 yield 'PUT request with non-empty body' => [
135 $nonEmptyBodyValidator,
136 new RequestData( $nonEmptyBodyParams + [ 'method' => ' put ' ] ),
137 $bodyData
138 ];
139 }
140
141 /**
142 * If $expected is a string, it must be the name of the expected exception class.
143 * Otherwise, it must match the returned body.
144 *
145 * @dataProvider provideValidateBody
146 */
147 public function testValidateBody( BodyValidator $bodyValidator, RequestData $requestData, $expected ) {
148 $this->hideDeprecated( 'MediaWiki\Rest\Validator\Validator::validateBody' );
149
150 $objectFactory = $this->getDummyObjectFactory();
151
152 /** @var Handler|MockObject $handler */
153 $handler = $this->getMockBuilder( Handler::class )
154 ->onlyMethods( [ 'getBodyValidator', 'execute' ] )
155 ->getMock();
156 $handler->method( 'getBodyValidator' )->willReturn( $bodyValidator );
157
158 $validator = new Validator( $objectFactory, $requestData, $this->mockAnonNullAuthority() );
159
160 if ( is_string( $expected ) ) {
161 $this->expectException( $expected );
162 }
163
164 $actual = $validator->validateBody( $requestData, $handler );
165 $this->assertEquals( $expected, $actual );
166 }
167
168 public static function provideParameterSpec() {
169 yield 'simple path parameter' => [
170 [
171 ParamValidator::PARAM_TYPE => 'string',
172 Validator::PARAM_SOURCE => 'path',
173 ],
174 [
175 'schema' => [
176 'type' => 'string',
177 ],
178 'required' => true,
179 'description' => 'test parameter',
180 'in' => 'path',
181 'name' => 'test',
182 ],
183 ];
184
185 yield 'optional query parameter' => [
186 [
187 ParamValidator::PARAM_TYPE => 'float',
188 Validator::PARAM_SOURCE => 'query',
189 ParamValidator::PARAM_REQUIRED => false,
190 ],
191 [
192 'schema' => [
193 'type' => 'number',
194 'format' => 'float',
195 ],
196 'required' => false,
197 'description' => 'test parameter',
198 'in' => 'query',
199 'name' => 'test',
200 ]
201 ];
202
203 yield 'array parameter' => [
204 [
205 ParamValidator::PARAM_TYPE => 'array',
206 Validator::PARAM_SOURCE => 'body',
207 Validator::PARAM_DESCRIPTION => 'just a test',
208 ParamValidator::PARAM_REQUIRED => true,
209 ArrayDef::PARAM_SCHEMA => [
210 'type' => 'array',
211 'items' => [
212 'type' => 'object'
213 ]
214 ]
215 ],
216 [
217 'schema' => [
218 'type' => 'array',
219 'items' => [
220 'type' => 'object'
221 ]
222 ],
223 'required' => true,
224 'description' => 'just a test',
225 'in' => 'body',
226 'name' => 'test',
227 ]
228 ];
229
230 yield 'enum parameter' => [
231 [
232 ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c' ],
233 Validator::PARAM_SOURCE => 'body',
234 Validator::PARAM_DESCRIPTION => 'just a test',
235 ParamValidator::PARAM_REQUIRED => true,
236 ],
237 [
238 'schema' => [
239 'type' => 'string',
240 'enum' => [ 'a', 'b', 'c' ],
241 ],
242 'required' => true,
243 'description' => 'just a test',
244 'in' => 'body',
245 'name' => 'test',
246 ]
247 ];
248
249 yield 'empty enum (should not happen)' => [
250 [
251 ParamValidator::PARAM_TYPE => [],
252 Validator::PARAM_SOURCE => 'body',
253 Validator::PARAM_DESCRIPTION => 'just a test',
254 ParamValidator::PARAM_REQUIRED => true,
255 ],
256 [
257 'schema' => [
258 'type' => 'string',
259 'enum' => [ '' ], // hacky
260 ],
261 'required' => true,
262 'description' => 'just a test',
263 'in' => 'body',
264 'name' => 'test',
265 ]
266 ];
267
268 // Should not happen, but we shouldn't let things explode either.
269 yield 'timestamp, missing source (should not happen)' => [
270 [
271 ParamValidator::PARAM_TYPE => 'timestamp',
272 ],
273 [
274 'schema' => [
275 'type' => 'string',
276 'format' => 'mw-timestamp',
277 ],
278 'required' => false,
279 'description' => 'test parameter',
280 'in' => 'unspecified',
281 'name' => 'test',
282 ]
283 ];
284
285 // Should not happen, but we shouldn't let things explode either.
286 yield 'missing type, pretend not required' => [
287 [
288 Validator::PARAM_SOURCE => 'path',
289 ParamValidator::PARAM_REQUIRED => false,
290 ],
291 [
292 'schema' => [
293 'type' => 'string',
294 ],
295 'required' => true, // path params are always required
296 'description' => 'test parameter',
297 'in' => 'path',
298 'name' => 'test',
299 ]
300 ];
301 }
302
303 /**
304 * @dataProvider provideParameterSpec
305 * @param array $paramSetting
306 * @param array $expectedSpec
307 */
308 public function testParameterSpec( $paramSetting, $expectedSpec ) {
309 $spec = Validator::getParameterSpec( 'test', $paramSetting );
310 $this->assertArrayEquals( $expectedSpec, $spec, false, true );
311 }
312
313 /**
314 * @param string $source
315 * @param string $requestDataKey
316 *
317 * @return \Generator
318 */
319 private static function generateParamValidationCases( string $source, $requestDataKey ): \Generator {
320 yield "default $source parameter is null" => [
321 [
322 'defaultparam' => [
323 ParamValidator::PARAM_TYPE => 'string',
324 Validator::PARAM_SOURCE => $source,
325 ]
326 ],
327 new RequestData( [ 'pathParams' => [] ] ),
328 [ 'defaultparam' => null ]
329 ];
330 yield "missing required $source parameter raises Exception" => [
331 [
332 'requiredparam' => [
333 ParamValidator::PARAM_TYPE => 'string',
334 ParamValidator::PARAM_REQUIRED => true,
335 Validator::PARAM_SOURCE => $source,
336 ]
337 ],
338 new RequestData( [] ),
339 new LocalizedHttpException(
340 new MessageValue( 'paramvalidator-missingparam' ),
341 400,
342 [
343 'error' => 'parameter-validation-failed',
344 'name' => 'requiredparam',
345 'value' => null,
346 'failureCode' => 'missingparam',
347 'failureData' => null,
348 ]
349 )
350 ];
351 yield "$source parameter" => [
352 [
353 'param' => [
354 ParamValidator::PARAM_TYPE => 'string',
355 Validator::PARAM_SOURCE => $source,
356 ]
357 ],
358 new RequestData( [ $requestDataKey => [ 'param' => 'test' ] ] ),
359 [ 'param' => 'test' ]
360 ];
361 yield "return default $source param set" => [
362 [
363 'param' => [
364 ParamValidator::PARAM_TYPE => 'string',
365 Validator::PARAM_SOURCE => $source,
366 ParamValidator::PARAM_DEFAULT => "default$source"
367 ]
368 ],
369 new RequestData( [] ),
370 [ "param" => "default$source" ]
371 ];
372 yield "throw on malformed $source param" => [
373 [
374 'param' => [
375 ParamValidator::PARAM_TYPE => 'string',
376 Validator::PARAM_SOURCE => $source,
377 ]
378 ],
379 new RequestData( [ $requestDataKey => [ 'param' => [] ] ] ),
380 new LocalizedHttpException(
381 new MessageValue( 'paramvalidator-notmulti' ),
382 400,
383 [
384 'error' => 'parameter-validation-failed',
385 'name' => 'param',
386 'value' => [],
387 'failureCode' => 'badvalue',
388 'failureData' => null,
389 ]
390 )
391 ];
392 }
393
394 public static function provideValidateParams() {
395 $sources = [ 'path', 'query' ];
396 $paramNames = [
397 "path" => "pathParams",
398 "query" => "queryParams",
399 "post" => "postParams"
400 ];
401 foreach ( $sources as $source ) {
402 $cases = self::generateParamValidationCases( $source, $paramNames[ $source ] );
403 foreach ( $cases as $name => $case ) {
404 yield $name => $case;
405 }
406 }
407 yield 'ignore body param' => [
408 [
409 'foo' => [
410 ParamValidator::PARAM_TYPE => 'string',
411 Validator::PARAM_SOURCE => 'body',
412 ]
413 ],
414 new RequestData( [ 'parsedBody' => [ 'foo' => 'test' ] ] ),
415 []
416 ];
417 yield 'unknown source' => [
418 [
419 'unknown source' => [
420 ParamValidator::PARAM_TYPE => 'string',
421 Validator::PARAM_SOURCE => 'unknown',
422 ]
423 ],
424 new RequestData( [ 'parsedBody' => [ 'simplebodyparam1' => 'test' ] ] ),
425 new InvalidArgumentException( "Invalid source 'unknown'" )
426 ];
427 }
428
429 /**
430 * If $expected is a string, it must be the name of the expected exception class.
431 * Otherwise, it must match the returned body.
432 *
433 * @dataProvider provideValidateParams
434 */
435 public function testValidateParams( $paramSetting, RequestData $requestData, $expected ) {
436 $objectFactory = $this->getDummyObjectFactory();
437 $validator = new Validator( $objectFactory, $requestData, $this->mockAnonNullAuthority() );
438
439 if ( $expected instanceof Exception ) {
440 $this->expectException( get_class( $expected ) );
441 $this->expectExceptionCode( $expected->getCode() );
442 $this->expectExceptionMessage( $expected->getMessage() );
443 }
444
445 $actual = $validator->validateParams( $paramSetting );
446 $this->assertEquals( $expected, $actual );
447 }
448
449 public static function provideValidateBodyParams() {
450 $cases = self::generateParamValidationCases( 'body', 'parsedBody' );
451 foreach ( $cases as $name => $case ) {
452 yield $name => $case;
453 }
454
455 yield 'ignore path param' => [
456 [
457 'foo' => [
458 ParamValidator::PARAM_TYPE => 'string',
459 Validator::PARAM_SOURCE => 'path',
460 ]
461 ],
462 new RequestData( [ 'pathParams' => [ 'foo' => 'test' ] ] ),
463 [] // The parameter from a non-body source should be ignored.
464 ];
465
466 yield 'empty string' => [
467 [
468 'foo' => [
469 ParamValidator::PARAM_TYPE => 'string',
470 ParamValidator::PARAM_REQUIRED => true,
471 Validator::PARAM_SOURCE => 'body',
472 ]
473 ],
474 new RequestData( [ 'parsedBody' => [ 'foo' => '' ] ] ),
475 [ 'foo' => '' ]
476 ];
477
478 yield 'valid integer (strict)' => [
479 [
480 'foo' => [
481 ParamValidator::PARAM_TYPE => 'integer',
482 Validator::PARAM_SOURCE => 'body',
483 ]
484 ],
485 new RequestData( [ 'parsedBody' => [ 'foo' => 123 ] ] ),
486 [ 'foo' => 123 ]
487 ];
488
489 yield 'string as integer (lenient)' => [
490 [
491 'foo' => [
492 ParamValidator::PARAM_TYPE => 'integer',
493 Validator::PARAM_SOURCE => 'body',
494 ]
495 ],
496 new RequestData( [ 'parsedBody' => [ 'foo' => '123' ] ] ),
497 [ 'foo' => 123 ],
498 false, // don't enforce types
499 ];
500
501 yield 'string as integer (strict)' => [
502 [
503 'foo' => [
504 ParamValidator::PARAM_TYPE => 'integer',
505 Validator::PARAM_SOURCE => 'body',
506 ]
507 ],
508 new RequestData( [ 'parsedBody' => [ 'foo' => '123' ] ] ),
509 new LocalizedHttpException(
510 new MessageValue( 'paramvalidator-badinteger-type' ),
511 400,
512 [
513 'error' => 'parameter-validation-failed',
514 'name' => 'foo',
515 'value' => '123',
516 'failureCode' => 'badinteger-type',
517 'failureData' => null,
518 ]
519 ),
520 ];
521
522 yield 'integer instead of string' => [
523 [
524 'foo' => [
525 ParamValidator::PARAM_TYPE => 'string',
526 Validator::PARAM_SOURCE => 'body',
527 ]
528 ],
529 new RequestData( [ 'parsedBody' => [ 'foo' => 1234 ] ] ),
530 new LocalizedHttpException(
531 new MessageValue( 'paramvalidator-needstring' ),
532 400,
533 [
534 'error' => 'parameter-validation-failed',
535 'name' => 'foo',
536 'value' => '1234',
537 'failureCode' => 'needstring',
538 'failureData' => null,
539 ]
540 ),
541 ];
542
543 yield 'array instead of string' => [
544 [
545 'foo' => [
546 ParamValidator::PARAM_TYPE => 'string',
547 Validator::PARAM_SOURCE => 'body',
548 ]
549 ],
550 new RequestData( [ 'parsedBody' => [ 'foo' => [ 1, 2, 3 ] ] ] ),
551 new LocalizedHttpException(
552 new MessageValue( 'paramvalidator-notmulti' ),
553 400,
554 [
555 'error' => 'parameter-validation-failed',
556 'name' => 'foo',
557 'value' => [ 1, 2, 3 ],
558 'failureCode' => 'badvalue',
559 'failureData' => null,
560 ]
561 ),
562 ];
563
564 yield "valid complex value" => [
565 [
566 'foo' => [
567 ParamValidator::PARAM_TYPE => 'array',
568 Validator::PARAM_SOURCE => 'body',
569 ParamValidator::PARAM_REQUIRED => true
570 ]
571 ],
572 new RequestData( [ 'parsedBody' => [
573 'foo' => [ 'x' => 1 ] // this is a complex value
574 ] ] ),
575 [ 'foo' => [ 'x' => 1 ] ]
576 ];
577
578 yield "invalid complex value" => [
579 [
580 'foo' => [
581 ParamValidator::PARAM_TYPE => 'array',
582 Validator::PARAM_SOURCE => 'body',
583 ParamValidator::PARAM_REQUIRED => true
584 ]
585 ],
586 new RequestData( [ 'parsedBody' => [
587 'foo' => 'xyzzy' // not a complex value
588 ] ] ),
589 new LocalizedHttpException(
590 new MessageValue( 'paramvalidator-notarray' ),
591 400,
592 [
593 'error' => 'parameter-validation-failed',
594 'name' => 'foo',
595 'value' => 'xyzzy',
596 'failureCode' => 'notarray',
597 'failureData' => null,
598 ]
599 ),
600 ];
601
602 yield "default complex value" => [
603 [
604 'foo' => [
605 ParamValidator::PARAM_TYPE => 'array',
606 Validator::PARAM_SOURCE => 'body',
607 ParamValidator::PARAM_DEFAULT => []
608 ]
609 ],
610 new RequestData( [] ),
611 [ 'foo' => [] ]
612 ];
613 }
614
615 /**
616 * @dataProvider provideValidateBodyParams
617 */
618 public function testValidateBodyParams(
619 $paramSetting,
620 RequestData $requestData,
621 $expected,
622 $enforceTypes = true
623 ) {
624 $objectFactory = $this->getDummyObjectFactory();
625 $validator = new Validator( $objectFactory, $requestData, $this->mockAnonNullAuthority() );
626
627 try {
628 $actual = $validator->validateBodyParams( $paramSetting, $enforceTypes );
629
630 if ( $expected instanceof Exception ) {
631 $this->fail( 'Expected exception: ' . $expected );
632 }
633
634 $this->assertEquals( $expected, $actual );
635 } catch ( LocalizedHttpException $ex ) {
636 if ( $expected instanceof LocalizedHttpException ) {
637 $this->assertInstanceOf( get_class( $expected ), $ex );
638 $this->assertSame( $expected->getCode(), $ex->getCode() );
639 $this->assertStringContainsString( 'rest-body-validation-error', $ex->getMessage() );
640
641 // Look at the original validation error that is wrapped inside
642 // the rest-body-validation-error as a parameter.
643 $param = $ex->getMessageValue()->getParams()[0];
644 /** @var MessageValue $validationMessage */
645 $validationMessage = $param->getValue();
646 $this->assertSame( $expected->getMessageValue()->getKey(), $validationMessage->getKey() );
647
648 $this->assertSame( $expected->getErrorData(), $ex->getErrorData() );
649 } else {
650 $this->fail( 'Unexpected exception: ' . $ex );
651 }
652 }
653 }
654
655 public function testValidateParams_post() {
656 $this->expectDeprecationAndContinue( '/The "post" source is deprecated/' );
657
658 $requestData = new RequestData( [ 'postParams' => [ 'foo' => 'bar' ] ] );
659 $paramSetting = [
660 'foo' => [
661 ParamValidator::PARAM_TYPE => 'string',
662 Validator::PARAM_SOURCE => 'post',
663 ]
664 ];
665
666 $objectFactory = $this->getDummyObjectFactory();
667 $validator = new Validator( $objectFactory, $requestData, $this->mockAnonNullAuthority() );
668
669 $actual = $validator->validateParams( $paramSetting );
670 $this->assertSame( [ 'foo' => 'bar' ], $actual );
671 }
672
673 public static function provideDetectExtraneousBodyFields() {
674 yield 'known body params' => [
675 [
676 'foo' => [
677 ParamValidator::PARAM_TYPE => 'string',
678 Validator::PARAM_SOURCE => 'body',
679 ],
680 'pathfoo' => [
681 ParamValidator::PARAM_TYPE => 'string',
682 Validator::PARAM_SOURCE => 'path',
683 ],
684 ],
685 new RequestData( [ 'parsedBody' => [ 'foo' => 'test' ] ] )
686 ];
687 yield 'no known body params' => [
688 [
689 'pathfoo' => [
690 ParamValidator::PARAM_TYPE => 'string',
691 Validator::PARAM_SOURCE => 'path',
692 ],
693 ],
694 new RequestData( [ 'parsedBody' => [ 'foo' => 'test' ] ] )
695 ];
696 yield 'extraneous body params' => [
697 [
698 'foo' => [
699 ParamValidator::PARAM_TYPE => 'string',
700 Validator::PARAM_SOURCE => 'body',
701 ],
702 ],
703 new RequestData( [ 'parsedBody' => [ 'foo' => 'test', 'xyzzy' => 123 ] ] ),
704 new LocalizedHttpException(
705 new MessageValue(
706 'rest-extraneous-body-fields',
707 [ new ListParam( ListType::COMMA, array_keys( [ 'xyzzy' ] ) ) ]
708 ),
709 400,
710 [
711 'error' => 'parameter-validation-failed',
712 'failureCode' => 'extraneous-body-fields',
713 'name' => 'xyzzy',
714 'failureData' => [ 'xyzzy' ]
715 ]
716 )
717 ];
718 }
719
720 /**
721 * @dataProvider provideDetectExtraneousBodyFields
722 */
723 public function testDetectExtraneousBodyFields( $paramSetting, RequestData $requestData, $expected = null ) {
724 $objectFactory = $this->getDummyObjectFactory();
725 $validator = new Validator( $objectFactory, $requestData, $this->mockAnonNullAuthority() );
726
727 try {
728 $validator->detectExtraneousBodyFields(
729 $paramSetting,
730 $requestData->getParsedBody()
731 );
732
733 if ( $expected instanceof Exception ) {
734 $this->fail( 'Expected exception: ' . $expected );
735 }
736
737 // all is fine
738 $this->addToAssertionCount( 1 );
739 } catch ( LocalizedHttpException $ex ) {
740 if ( $expected instanceof LocalizedHttpException ) {
741 $this->assertInstanceOf( get_class( $expected ), $ex );
742 $this->assertSame( $expected->getCode(), $ex->getCode() );
743 $this->assertSame( $expected->getErrorData(), $ex->getErrorData() );
744 $this->assertStringContainsString( $expected->getMessage(), $ex->getMessage() );
745 $this->assertStringContainsString(
746 $expected->getMessageValue()->getKey(),
747 $ex->getMessageValue()->getKey()
748 );
749 } else {
750 $this->fail( 'Unexpected exception: ' . $ex );
751 }
752 }
753 }
754
755 public function provideGetValue() {
756 return [
757 // Test case 0: Parameter exists in source and no normalization required
758 [
759 'source' => 'query',
760 'requestData' => new RequestData( [ 'queryParams' => [ 'param1' => 'value1' ] ] ),
761 'options' => [],
762 'expected' => 'value1'
763 ],
764
765 // Test case 1: Parameter exists in source and normalization required
766 [
767 'source' => 'query',
768 'requestData' => new RequestData( [ 'queryParams' => [ 'param1' => "L\u{0061}\u{0308}rm" ] ] ),
769 'options' => [],
770 'expected' => "L\u{00E4}rm"
771 ],
772
773 // Test case 2: Parameter exists in source and normalization required
774 [
775 'source' => 'query',
776 'requestData' => new RequestData( [ 'queryParams' => [ 'param1' => "Foo\0" ] ] ),
777 'options' => [],
778 'expected' => "Foo\u{FFFD}"
779 ],
780 //
781 // // Test case 3: Parameter does not exist, default used
782 [
783 'source' => 'query',
784 'requestData' => new RequestData( [] ),
785 'options' => [],
786 'expected' => 'default'
787 ],
788 //
789 // // Test case 4: Parameter source is body (No normalization)
790 [
791 'source' => 'body',
792 'requestData' => new RequestData( [ 'parsedBody' => [ 'param1' => "L\u{0061}\u{0308}rm" ] ] ),
793 'options' => [],
794 'expected' => "L\u{0061}\u{0308}rm"
795 ],
796 //
797 // // Test case 5: raw option is set (No normalization)
798 [
799 'source' => 'query',
800 'requestData' => new RequestData( [ 'queryParams' => [ 'param1' => "L\u{0061}\u{0308}rm" ] ] ),
801 'options' => [ 'raw' => true ],
802 'expected' => "L\u{0061}\u{0308}rm"
803 ],
804
805 // Test case 6: Parameter source is not specified (Expect InvalidArgumentException)
806 [
807 'source' => null,
808 'requestData' => new RequestData( [ 'queryParams' => [ 'param1' => "L\u{0061}\u{0308}rm" ] ] ),
809 'options' => [],
810 'expected' => null,
811 'expectedException' => InvalidArgumentException::class
812 ],
813 ];
814 }
815
816 /**
817 * @dataProvider provideGetValue
818 */
819 public function testGetValue( $source, RequestData $requestData, $options, $expected, $expectedException = null ) {
820 $validatorCallbacks = new ParamValidatorCallbacks(
821 $requestData,
822 $this->createMock( Authority::class ),
823 );
824
825 // Set options
826 $options['source'] = $source;
827
828 // Test case
829 if ( $expectedException !== null ) {
830 $this->expectException( $expectedException );
831 }
832
833 $result = $validatorCallbacks->getValue( 'param1', 'default', $options );
834 $this->assertSame( $expected, $result );
835 }
836 }