3 namespace Wikimedia\ParamValidator
;
5 use Psr\Container\ContainerInterface
;
6 use Wikimedia\ObjectFactory
;
9 * @covers Wikimedia\ParamValidator\ParamValidator
11 class ParamValidatorTest
extends \PHPUnit\Framework\TestCase
{
13 public function testTypeRegistration() {
14 $validator = new ParamValidator(
15 new SimpleCallbacks( [] ),
16 new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface
::class ) )
18 $this->assertSame( array_keys( ParamValidator
::$STANDARD_TYPES ), $validator->knownTypes() );
20 $validator = new ParamValidator(
21 new SimpleCallbacks( [] ),
22 new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface
::class ) ),
23 [ 'typeDefs' => [ 'foo' => [], 'bar' => [] ] ]
25 $validator->addTypeDef( 'baz', [] );
27 $validator->addTypeDef( 'baz', [] );
28 $this->fail( 'Expected exception not thrown' );
29 } catch ( \InvalidArgumentException
$ex ) {
31 $validator->overrideTypeDef( 'bar', null );
32 $validator->overrideTypeDef( 'baz', [] );
33 $this->assertSame( [ 'foo', 'baz' ], $validator->knownTypes() );
35 $this->assertTrue( $validator->hasTypeDef( 'foo' ) );
36 $this->assertFalse( $validator->hasTypeDef( 'bar' ) );
37 $this->assertTrue( $validator->hasTypeDef( 'baz' ) );
38 $this->assertFalse( $validator->hasTypeDef( 'bazz' ) );
41 public function testGetTypeDef() {
42 $callbacks = new SimpleCallbacks( [] );
43 $factory = $this->getMockBuilder( ObjectFactory
::class )
44 ->setConstructorArgs( [ $this->getMockForAbstractClass( ContainerInterface
::class ) ] )
45 ->setMethods( [ 'createObject' ] )
47 $factory->method( 'createObject' )
48 ->willReturnCallback( function ( $spec, $options ) use ( $callbacks ) {
49 $this->assertInternalType( 'array', $spec );
51 [ 'extraArgs' => [ $callbacks ], 'assertClass' => TypeDef
::class ], $options
53 $ret = $this->getMockBuilder( TypeDef
::class )
54 ->setConstructorArgs( [ $callbacks ] )
55 ->getMockForAbstractClass();
59 $validator = new ParamValidator( $callbacks, $factory );
61 $def = $validator->getTypeDef( 'boolean' );
62 $this->assertInstanceOf( TypeDef
::class, $def );
63 $this->assertSame( ParamValidator
::$STANDARD_TYPES['boolean'], $def->spec
);
65 $def = $validator->getTypeDef( [] );
66 $this->assertInstanceOf( TypeDef
::class, $def );
67 $this->assertSame( ParamValidator
::$STANDARD_TYPES['enum'], $def->spec
);
69 $def = $validator->getTypeDef( 'missing' );
70 $this->assertNull( $def );
73 public function testGetTypeDef_caching() {
74 $callbacks = new SimpleCallbacks( [] );
76 $mb = $this->getMockBuilder( TypeDef
::class )
77 ->setConstructorArgs( [ $callbacks ] );
78 $def1 = $mb->getMockForAbstractClass();
79 $def2 = $mb->getMockForAbstractClass();
80 $this->assertNotSame( $def1, $def2, 'sanity check' );
82 $factory = $this->getMockBuilder( ObjectFactory
::class )
83 ->setConstructorArgs( [ $this->getMockForAbstractClass( ContainerInterface
::class ) ] )
84 ->setMethods( [ 'createObject' ] )
86 $factory->expects( $this->once() )->method( 'createObject' )->willReturn( $def1 );
88 $validator = new ParamValidator( $callbacks, $factory, [ 'typeDefs' => [
93 $this->assertSame( $def1, $validator->getTypeDef( 'foo' ) );
95 // Second call doesn't re-call ObjectFactory
96 $this->assertSame( $def1, $validator->getTypeDef( 'foo' ) );
98 // When registered a TypeDef directly, doesn't call ObjectFactory
99 $this->assertSame( $def2, $validator->getTypeDef( 'bar' ) );
103 * @expectedException \UnexpectedValueException
104 * @expectedExceptionMessage Expected instance of Wikimedia\ParamValidator\TypeDef, got stdClass
106 public function testGetTypeDef_error() {
107 $validator = new ParamValidator(
108 new SimpleCallbacks( [] ),
109 new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface
::class ) ),
110 [ 'typeDefs' => [ 'foo' => [ 'class' => \stdClass
::class ] ] ]
112 $validator->getTypeDef( 'foo' );
115 /** @dataProvider provideNormalizeSettings */
116 public function testNormalizeSettings( $input, $expect ) {
117 $callbacks = new SimpleCallbacks( [] );
119 $mb = $this->getMockBuilder( TypeDef
::class )
120 ->setConstructorArgs( [ $callbacks ] )
121 ->setMethods( [ 'normalizeSettings' ] );
122 $mock1 = $mb->getMockForAbstractClass();
123 $mock1->method( 'normalizeSettings' )->willReturnCallback( function ( $s ) {
124 $s['foo'] = 'FooBar!';
127 $mock2 = $mb->getMockForAbstractClass();
128 $mock2->method( 'normalizeSettings' )->willReturnCallback( function ( $s ) {
129 $s['bar'] = 'FooBar!';
133 $validator = new ParamValidator(
135 new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface
::class ) ),
136 [ 'typeDefs' => [ 'foo' => $mock1, 'bar' => $mock2 ] ]
139 $this->assertSame( $expect, $validator->normalizeSettings( $input ) );
142 public static function provideNormalizeSettings() {
146 [ ParamValidator
::PARAM_DEFAULT
=> 'ok?', ParamValidator
::PARAM_TYPE
=> 'string' ],
150 [ 'test' => 'ok?', ParamValidator
::PARAM_TYPE
=> 'NULL' ],
152 'A type with overrides' => [
153 [ ParamValidator
::PARAM_TYPE
=> 'foo', 'test' => 'ok?' ],
154 [ ParamValidator
::PARAM_TYPE
=> 'foo', 'test' => 'ok?', 'foo' => 'FooBar!' ],
159 /** @dataProvider provideExplodeMultiValue */
160 public function testExplodeMultiValue( $value, $limit, $expect ) {
161 $this->assertSame( $expect, ParamValidator
::explodeMultiValue( $value, $limit ) );
164 public static function provideExplodeMultiValue() {
166 [ 'foobar', 100, [ 'foobar' ] ],
167 [ 'foo|bar|baz', 100, [ 'foo', 'bar', 'baz' ] ],
168 [ "\x1Ffoo\x1Fbar\x1Fbaz", 100, [ 'foo', 'bar', 'baz' ] ],
169 [ 'foo|bar|baz', 2, [ 'foo', 'bar|baz' ] ],
170 [ "\x1Ffoo\x1Fbar\x1Fbaz", 2, [ 'foo', "bar\x1Fbaz" ] ],
171 [ '|bar|baz', 100, [ '', 'bar', 'baz' ] ],
172 [ "\x1F\x1Fbar\x1Fbaz", 100, [ '', 'bar', 'baz' ] ],
179 * @expectedException DomainException
180 * @expectedExceptionMessage Param foo's type is unknown - string
182 public function testGetValue_badType() {
183 $validator = new ParamValidator(
184 new SimpleCallbacks( [] ),
185 new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface
::class ) ),
188 $validator->getValue( 'foo', 'default', [] );
191 /** @dataProvider provideGetValue */
192 public function testGetValue(
193 $settings, $parseLimit, $get, $value, $isSensitive, $isDeprecated
195 $callbacks = new SimpleCallbacks( $get );
197 $options = [ $dummy ];
200 ParamValidator
::PARAM_TYPE
=> 'xyz',
201 ParamValidator
::PARAM_DEFAULT
=> null,
204 $mockDef = $this->getMockBuilder( TypeDef
::class )
205 ->setConstructorArgs( [ $callbacks ] )
206 ->getMockForAbstractClass();
208 // Mock the validateValue method so we can test only getValue
209 $validator = $this->getMockBuilder( ParamValidator
::class )
210 ->setConstructorArgs( [
212 new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface
::class ) ),
213 [ 'typeDefs' => [ 'xyz' => $mockDef ] ]
215 ->setMethods( [ 'validateValue' ] )
217 $validator->expects( $this->once() )->method( 'validateValue' )
219 $this->identicalTo( 'foobar' ),
220 $this->identicalTo( $value ),
221 $this->identicalTo( $settings ),
222 $this->identicalTo( $options )
224 ->willReturn( $dummy );
226 $this->assertSame( $dummy, $validator->getValue( 'foobar', $settings, $options ) );
228 $expectConditions = [];
229 if ( $isSensitive ) {
230 $expectConditions[] = new ValidationException(
231 'foobar', $value, $settings, 'param-sensitive', []
234 if ( $isDeprecated ) {
235 $expectConditions[] = new ValidationException(
236 'foobar', $value, $settings, 'param-deprecated', []
239 $this->assertEquals( $expectConditions, $callbacks->getRecordedConditions() );
242 public static function provideGetValue() {
243 $sen = [ ParamValidator
::PARAM_SENSITIVE
=> true ];
244 $dep = [ ParamValidator
::PARAM_DEPRECATED
=> true ];
245 $dflt = [ ParamValidator
::PARAM_DEFAULT
=> 'DeFaUlT' ];
247 'Simple case' => [ [], false, [ 'foobar' => '!!!' ], '!!!', false, false ],
248 'Not provided' => [ $sen +
$dep, false, [], null, false, false ],
249 'Not provided, default' => [ $sen +
$dep +
$dflt, true, [], 'DeFaUlT', false, false ],
250 'Provided' => [ $dflt, false, [ 'foobar' => 'XYZ' ], 'XYZ', false, false ],
251 'Provided, sensitive' => [ $sen, false, [ 'foobar' => 'XYZ' ], 'XYZ', true, false ],
252 'Provided, deprecated' => [ $dep, false, [ 'foobar' => 'XYZ' ], 'XYZ', false, true ],
253 'Provided array' => [ $dflt, false, [ 'foobar' => [ 'XYZ' ] ], [ 'XYZ' ], false, false ],
258 * @expectedException DomainException
259 * @expectedExceptionMessage Param foo's type is unknown - string
261 public function testValidateValue_badType() {
262 $validator = new ParamValidator(
263 new SimpleCallbacks( [] ),
264 new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface
::class ) ),
267 $validator->validateValue( 'foo', null, 'default', [] );
270 /** @dataProvider provideValidateValue */
271 public function testValidateValue(
272 $value, $settings, $highLimits, $valuesList, $calls, $expect, $expectConditions = [],
273 $constructorOptions = []
275 $callbacks = new SimpleCallbacks( [] );
277 ParamValidator
::PARAM_TYPE
=> 'xyz',
278 ParamValidator
::PARAM_DEFAULT
=> null,
281 $options = [ $dummy, 'useHighLimits' => $highLimits ];
282 $eOptions = $options;
283 $eOptions2 = $eOptions;
284 if ( $valuesList !== null ) {
285 $eOptions2['values-list'] = $valuesList;
288 $mockDef = $this->getMockBuilder( TypeDef
::class )
289 ->setConstructorArgs( [ $callbacks ] )
290 ->setMethods( [ 'validate', 'getEnumValues' ] )
291 ->getMockForAbstractClass();
292 $mockDef->method( 'getEnumValues' )
294 $this->identicalTo( 'foobar' ), $this->identicalTo( $settings ), $this->identicalTo( $eOptions )
296 ->willReturn( [ 'a', 'b', 'c', 'd', 'e', 'f' ] );
297 $mockDef->expects( $this->exactly( count( $calls ) ) )->method( 'validate' )->willReturnCallback(
298 function ( $n, $v, $s, $o ) use ( $settings, $eOptions2, $calls ) {
299 $this->assertSame( 'foobar', $n );
300 $this->assertSame( $settings, $s );
301 $this->assertSame( $eOptions2, $o );
303 if ( !array_key_exists( $v, $calls ) ) {
304 $this->fail( "Called with unexpected value '$v'" );
306 if ( $calls[$v] === null ) {
307 throw new ValidationException( $n, $v, $s, 'badvalue', [] );
313 $validator = new ParamValidator(
315 new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface
::class ) ),
316 $constructorOptions +
[ 'typeDefs' => [ 'xyz' => $mockDef ] ]
319 if ( $expect instanceof ValidationException
) {
321 $validator->validateValue( 'foobar', $value, $settings, $options );
322 $this->fail( 'Expected exception not thrown' );
323 } catch ( ValidationException
$ex ) {
324 $this->assertSame( $expect->getFailureCode(), $ex->getFailureCode() );
325 $this->assertSame( $expect->getFailureData(), $ex->getFailureData() );
329 $expect, $validator->validateValue( 'foobar', $value, $settings, $options )
333 foreach ( $callbacks->getRecordedConditions() as $c ) {
334 $conditions[] = array_merge( [ $c->getFailureCode() ], $c->getFailureData() );
336 $this->assertSame( $expectConditions, $conditions );
340 public static function provideValidateValue() {
342 'No value' => [ null, [], false, null, [], null ],
343 'No value, required' => [
345 [ ParamValidator
::PARAM_REQUIRED
=> true ],
349 new ValidationException( 'foobar', null, [], 'missingparam', [] ),
351 'Non-multi value' => [ 'abc', [], false, null, [ 'abc' => 'def' ], 'def' ],
352 'Simple multi value' => [
354 [ ParamValidator
::PARAM_ISMULTI
=> true ],
356 [ 'a', 'b', 'c', 'd' ],
357 [ 'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D' ],
358 [ 'A', 'B', 'C', 'D' ],
360 'Array multi value' => [
361 [ 'a', 'b', 'c', 'd' ],
362 [ ParamValidator
::PARAM_ISMULTI
=> true ],
364 [ 'a', 'b', 'c', 'd' ],
365 [ 'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D' ],
366 [ 'A', 'B', 'C', 'D' ],
368 'Multi value with PARAM_ALL' => [
370 [ ParamValidator
::PARAM_ISMULTI
=> true, ParamValidator
::PARAM_ALL
=> true ],
374 [ 'a', 'b', 'c', 'd', 'e', 'f' ],
376 'Multi value with PARAM_ALL = "x"' => [
378 [ ParamValidator
::PARAM_ISMULTI
=> true, ParamValidator
::PARAM_ALL
=> "x" ],
382 [ 'a', 'b', 'c', 'd', 'e', 'f' ],
384 'Multi value with PARAM_ALL = "x", passing "*"' => [
386 [ ParamValidator
::PARAM_ISMULTI
=> true, ParamValidator
::PARAM_ALL
=> "x" ],
393 'Too many values' => [
396 ParamValidator
::PARAM_ISMULTI
=> true,
397 ParamValidator
::PARAM_ISMULTI_LIMIT1
=> 2,
398 ParamValidator
::PARAM_ISMULTI_LIMIT2
=> 4,
403 new ValidationException( 'foobar', 'a|b|c|d', [], 'toomanyvalues', [ 'limit' => 2 ] ),
405 'Too many values as array' => [
406 [ 'a', 'b', 'c', 'd' ],
408 ParamValidator
::PARAM_ISMULTI
=> true,
409 ParamValidator
::PARAM_ISMULTI_LIMIT1
=> 2,
410 ParamValidator
::PARAM_ISMULTI_LIMIT2
=> 4,
415 new ValidationException(
416 'foobar', [ 'a', 'b', 'c', 'd' ], [], 'toomanyvalues', [ 'limit' => 2 ]
419 'Not too many values for highlimits' => [
422 ParamValidator
::PARAM_ISMULTI
=> true,
423 ParamValidator
::PARAM_ISMULTI_LIMIT1
=> 2,
424 ParamValidator
::PARAM_ISMULTI_LIMIT2
=> 4,
427 [ 'a', 'b', 'c', 'd' ],
428 [ 'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D' ],
429 [ 'A', 'B', 'C', 'D' ],
431 'Too many values for highlimits' => [
434 ParamValidator
::PARAM_ISMULTI
=> true,
435 ParamValidator
::PARAM_ISMULTI_LIMIT1
=> 2,
436 ParamValidator
::PARAM_ISMULTI_LIMIT2
=> 4,
441 new ValidationException( 'foobar', 'a|b|c|d|e', [], 'toomanyvalues', [ 'limit' => 4 ] ),
444 'Too many values via default' => [
447 ParamValidator
::PARAM_ISMULTI
=> true,
452 new ValidationException( 'foobar', 'a|b|c|d', [], 'toomanyvalues', [ 'limit' => 2 ] ),
454 [ 'ismultiLimits' => [ 2, 4 ] ],
456 'Not too many values for highlimits via default' => [
459 ParamValidator
::PARAM_ISMULTI
=> true,
462 [ 'a', 'b', 'c', 'd' ],
463 [ 'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D' ],
464 [ 'A', 'B', 'C', 'D' ],
466 [ 'ismultiLimits' => [ 2, 4 ] ],
468 'Too many values for highlimits via default' => [
471 ParamValidator
::PARAM_ISMULTI
=> true,
476 new ValidationException( 'foobar', 'a|b|c|d|e', [], 'toomanyvalues', [ 'limit' => 4 ] ),
478 [ 'ismultiLimits' => [ 2, 4 ] ],
481 'Invalid values' => [
483 [ ParamValidator
::PARAM_ISMULTI
=> true ],
485 [ 'a', 'b', 'c', 'd' ],
486 [ 'a' => 'A', 'b' => null ],
487 new ValidationException( 'foobar', 'b', [], 'badvalue', [] ),
489 'Ignored invalid values' => [
492 ParamValidator
::PARAM_ISMULTI
=> true,
493 ParamValidator
::PARAM_IGNORE_INVALID_VALUES
=> true,
496 [ 'a', 'b', 'c', 'd' ],
497 [ 'a' => 'A', 'b' => null, 'c' => null, 'd' => 'D' ],
500 [ 'unrecognizedvalues', 'values' => [ 'b', 'c' ] ],