| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\ParamValidator; |
| 4 |
|
| 5 |
use DomainException; |
| 6 |
use InvalidArgumentException; |
| 7 |
use PHPUnit\Framework\TestCase; |
| 8 |
use Psr\Container\ContainerInterface; |
| 9 |
use stdClass; |
| 10 |
use UnexpectedValueException; |
| 11 |
use Wikimedia\Message\DataMessageValue; |
| 12 |
use Wikimedia\Message\MessageValue; |
| 13 |
use Wikimedia\Message\ParamType; |
| 14 |
use Wikimedia\Message\ScalarParam; |
| 15 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 16 |
use Wikimedia\ParamValidator\ParamValidator; |
| 17 |
use Wikimedia\ParamValidator\SimpleCallbacks; |
| 18 |
use Wikimedia\ParamValidator\TypeDef; |
| 19 |
use Wikimedia\ParamValidator\ValidationException; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class ParamValidatorTest extends TestCase { |
| 25 |
|
| 26 |
public function testTypeRegistration() { |
| 27 |
$validator = new ParamValidator( |
| 28 |
new SimpleCallbacks( [] ), |
| 29 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ) |
| 30 |
); |
| 31 |
$this->assertSame( array_keys( ParamValidator::STANDARD_TYPES ), $validator->knownTypes() ); |
| 32 |
|
| 33 |
$validator = new ParamValidator( |
| 34 |
new SimpleCallbacks( [] ), |
| 35 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 36 |
[ 'typeDefs' => [ 'foo' => [], 'bar' => [] ] ] |
| 37 |
); |
| 38 |
$validator->addTypeDef( 'baz', [] ); |
| 39 |
try { |
| 40 |
$validator->addTypeDef( 'baz', [] ); |
| 41 |
$this->fail( 'Expected exception not thrown' ); |
| 42 |
} catch ( InvalidArgumentException $ex ) { |
| 43 |
} |
| 44 |
$validator->overrideTypeDef( 'bar', null ); |
| 45 |
$validator->overrideTypeDef( 'baz', [] ); |
| 46 |
$this->assertSame( [ 'foo', 'baz' ], $validator->knownTypes() ); |
| 47 |
|
| 48 |
$this->assertTrue( $validator->hasTypeDef( 'foo' ) ); |
| 49 |
$this->assertFalse( $validator->hasTypeDef( 'bar' ) ); |
| 50 |
$this->assertTrue( $validator->hasTypeDef( 'baz' ) ); |
| 51 |
$this->assertFalse( $validator->hasTypeDef( 'bazz' ) ); |
| 52 |
} |
| 53 |
|
| 54 |
public function testGetTypeDef() { |
| 55 |
$callbacks = new SimpleCallbacks( [] ); |
| 56 |
$factory = $this->getMockBuilder( ObjectFactory::class ) |
| 57 |
->setConstructorArgs( [ $this->getMockForAbstractClass( ContainerInterface::class ) ] ) |
| 58 |
->onlyMethods( [ 'createObject' ] ) |
| 59 |
->getMock(); |
| 60 |
$factory->method( 'createObject' ) |
| 61 |
->willReturnCallback( function ( $spec, $options ) use ( $callbacks ) { |
| 62 |
$this->assertIsArray( $spec ); |
| 63 |
$this->assertSame( |
| 64 |
[ 'extraArgs' => [ $callbacks ], 'assertClass' => TypeDef::class ], $options |
| 65 |
); |
| 66 |
$ret = $this->getMockBuilder( TypeDef::class ) |
| 67 |
->setConstructorArgs( [ $callbacks ] ) |
| 68 |
->getMockForAbstractClass(); |
| 69 |
return $ret; |
| 70 |
} ); |
| 71 |
$validator = new ParamValidator( $callbacks, $factory ); |
| 72 |
|
| 73 |
$def = $validator->getTypeDef( 'boolean' ); |
| 74 |
$this->assertInstanceOf( TypeDef::class, $def ); |
| 75 |
|
| 76 |
$def = $validator->getTypeDef( [] ); |
| 77 |
$this->assertInstanceOf( TypeDef::class, $def ); |
| 78 |
|
| 79 |
$def = $validator->getTypeDef( 'missing' ); |
| 80 |
$this->assertNull( $def ); |
| 81 |
} |
| 82 |
|
| 83 |
public function testGetTypeDef_caching() { |
| 84 |
$callbacks = new SimpleCallbacks( [] ); |
| 85 |
|
| 86 |
$mb = $this->getMockBuilder( TypeDef::class ) |
| 87 |
->setConstructorArgs( [ $callbacks ] ); |
| 88 |
$def1 = $mb->getMockForAbstractClass(); |
| 89 |
$def2 = $mb->getMockForAbstractClass(); |
| 90 |
$this->assertNotSame( $def1, $def2, 'consistency check' ); |
| 91 |
|
| 92 |
$factory = $this->getMockBuilder( ObjectFactory::class ) |
| 93 |
->setConstructorArgs( [ $this->getMockForAbstractClass( ContainerInterface::class ) ] ) |
| 94 |
->onlyMethods( [ 'createObject' ] ) |
| 95 |
->getMock(); |
| 96 |
$factory->expects( $this->once() )->method( 'createObject' )->willReturn( $def1 ); |
| 97 |
|
| 98 |
$validator = new ParamValidator( $callbacks, $factory, [ 'typeDefs' => [ |
| 99 |
'foo' => [], |
| 100 |
'bar' => $def2, |
| 101 |
] ] ); |
| 102 |
|
| 103 |
$this->assertSame( $def1, $validator->getTypeDef( 'foo' ) ); |
| 104 |
|
| 105 |
|
| 106 |
$this->assertSame( $def1, $validator->getTypeDef( 'foo' ) ); |
| 107 |
|
| 108 |
|
| 109 |
$this->assertSame( $def2, $validator->getTypeDef( 'bar' ) ); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function testGetTypeDef_error() { |
| 115 |
$validator = new ParamValidator( |
| 116 |
new SimpleCallbacks( [] ), |
| 117 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 118 |
[ 'typeDefs' => [ 'foo' => [ 'class' => stdClass::class ] ] ] |
| 119 |
); |
| 120 |
$this->expectException( UnexpectedValueException::class ); |
| 121 |
$this->expectExceptionMessage( |
| 122 |
"Expected instance of Wikimedia\ParamValidator\TypeDef, got stdClass" ); |
| 123 |
$validator->getTypeDef( 'foo' ); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
public function testNormalizeSettings( $input, $expect ) { |
| 128 |
$callbacks = new SimpleCallbacks( [] ); |
| 129 |
|
| 130 |
$mb = $this->getMockBuilder( TypeDef::class ) |
| 131 |
->setConstructorArgs( [ $callbacks ] ) |
| 132 |
->onlyMethods( [ 'normalizeSettings' ] ); |
| 133 |
$mock1 = $mb->getMockForAbstractClass(); |
| 134 |
$mock1->method( 'normalizeSettings' )->willReturnCallback( static function ( $s ) { |
| 135 |
$s['foo'] = 'FooBar!'; |
| 136 |
return $s; |
| 137 |
} ); |
| 138 |
$mock2 = $mb->getMockForAbstractClass(); |
| 139 |
$mock2->method( 'normalizeSettings' )->willReturnCallback( static function ( $s ) { |
| 140 |
$s['bar'] = 'FooBar!'; |
| 141 |
return $s; |
| 142 |
} ); |
| 143 |
|
| 144 |
$validator = new ParamValidator( |
| 145 |
$callbacks, |
| 146 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 147 |
[ 'typeDefs' => [ 'foo' => $mock1, 'bar' => $mock2 ] ] |
| 148 |
); |
| 149 |
|
| 150 |
$this->assertSame( $expect, $validator->normalizeSettings( $input ) ); |
| 151 |
} |
| 152 |
|
| 153 |
public static function provideNormalizeSettings() { |
| 154 |
return [ |
| 155 |
'Plain value' => [ |
| 156 |
'ok?', |
| 157 |
[ ParamValidator::PARAM_DEFAULT => 'ok?', ParamValidator::PARAM_TYPE => 'string' ], |
| 158 |
], |
| 159 |
'Simple array' => [ |
| 160 |
[ 'test' => 'ok?' ], |
| 161 |
[ 'test' => 'ok?', ParamValidator::PARAM_TYPE => 'NULL' ], |
| 162 |
], |
| 163 |
'A type with overrides' => [ |
| 164 |
[ ParamValidator::PARAM_TYPE => 'foo', 'test' => 'ok?' ], |
| 165 |
[ ParamValidator::PARAM_TYPE => 'foo', 'test' => 'ok?', 'foo' => 'FooBar!' ], |
| 166 |
], |
| 167 |
]; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
public function testCheckSettings( $settings, array $expect ): void { |
| 172 |
$callbacks = new SimpleCallbacks( [] ); |
| 173 |
|
| 174 |
$mb = $this->getMockBuilder( TypeDef::class ) |
| 175 |
->setConstructorArgs( [ $callbacks ] ) |
| 176 |
->onlyMethods( [ 'checkSettings' ] ); |
| 177 |
$mock1 = $mb->getMockForAbstractClass(); |
| 178 |
$mock1->method( 'checkSettings' )->willReturnCallback( |
| 179 |
static function ( string $name, $settings, array $options, array $ret ) { |
| 180 |
$ret['allowedKeys'][] = 'XXX-test'; |
| 181 |
if ( isset( $settings['XXX-test'] ) ) { |
| 182 |
$ret['issues']['XXX-test'] = 'XXX-test was ' . $settings['XXX-test']; |
| 183 |
} |
| 184 |
return $ret; |
| 185 |
} |
| 186 |
); |
| 187 |
$mock2 = $mb->getMockForAbstractClass(); |
| 188 |
$mock2->method( 'checkSettings' )->willReturnCallback( |
| 189 |
static function ( string $name, $settings, array $options, array $ret ) { |
| 190 |
return $ret; |
| 191 |
} |
| 192 |
); |
| 193 |
|
| 194 |
$validator = new ParamValidator( |
| 195 |
$callbacks, |
| 196 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 197 |
[ 'typeDefs' => [ 'foo' => $mock1, 'NULL' => $mock2 ] + ParamValidator::STANDARD_TYPES ] |
| 198 |
); |
| 199 |
|
| 200 |
$this->assertEquals( $expect, $validator->checkSettings( 'dummy', $settings, [] ) ); |
| 201 |
} |
| 202 |
|
| 203 |
public static function provideCheckSettings(): array { |
| 204 |
$normalKeys = [ |
| 205 |
ParamValidator::PARAM_TYPE, ParamValidator::PARAM_DEFAULT, ParamValidator::PARAM_REQUIRED, |
| 206 |
ParamValidator::PARAM_ISMULTI, ParamValidator::PARAM_SENSITIVE, ParamValidator::PARAM_DEPRECATED, |
| 207 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES, |
| 208 |
]; |
| 209 |
$multiKeys = [ ...$normalKeys, |
| 210 |
ParamValidator::PARAM_ISMULTI_LIMIT1, ParamValidator::PARAM_ISMULTI_LIMIT2, |
| 211 |
ParamValidator::PARAM_ALL, ParamValidator::PARAM_ALLOW_DUPLICATES |
| 212 |
]; |
| 213 |
$multiEnumKeys = [ ...$multiKeys, TypeDef\EnumDef::PARAM_DEPRECATED_VALUES ]; |
| 214 |
|
| 215 |
return [ |
| 216 |
'Basic test' => [ |
| 217 |
null, |
| 218 |
[ |
| 219 |
'issues' => [], |
| 220 |
'allowedKeys' => $normalKeys, |
| 221 |
'messages' => [], |
| 222 |
], |
| 223 |
], |
| 224 |
'Basic multi-value' => [ |
| 225 |
[ |
| 226 |
ParamValidator::PARAM_ISMULTI => true, |
| 227 |
], |
| 228 |
[ |
| 229 |
'issues' => [], |
| 230 |
'allowedKeys' => $multiKeys, |
| 231 |
'messages' => [], |
| 232 |
], |
| 233 |
], |
| 234 |
'Test with everything' => [ |
| 235 |
[ |
| 236 |
ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd' ], |
| 237 |
ParamValidator::PARAM_DEFAULT => 'a|b', |
| 238 |
ParamValidator::PARAM_REQUIRED => true, |
| 239 |
ParamValidator::PARAM_ISMULTI => true, |
| 240 |
ParamValidator::PARAM_SENSITIVE => false, |
| 241 |
ParamValidator::PARAM_DEPRECATED => false, |
| 242 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES => true, |
| 243 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 10, |
| 244 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 20, |
| 245 |
ParamValidator::PARAM_ALL => 'all', |
| 246 |
ParamValidator::PARAM_ALLOW_DUPLICATES => true, |
| 247 |
], |
| 248 |
[ |
| 249 |
'issues' => [], |
| 250 |
'allowedKeys' => $multiEnumKeys, |
| 251 |
'messages' => [], |
| 252 |
], |
| 253 |
], |
| 254 |
'Lots of bad types' => [ |
| 255 |
[ |
| 256 |
ParamValidator::PARAM_TYPE => false, |
| 257 |
ParamValidator::PARAM_REQUIRED => 1, |
| 258 |
ParamValidator::PARAM_ISMULTI => 1, |
| 259 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => '10', |
| 260 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => '100', |
| 261 |
ParamValidator::PARAM_ALL => [], |
| 262 |
ParamValidator::PARAM_ALLOW_DUPLICATES => 1, |
| 263 |
ParamValidator::PARAM_SENSITIVE => 1, |
| 264 |
ParamValidator::PARAM_DEPRECATED => 1, |
| 265 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES => 1, |
| 266 |
], |
| 267 |
[ |
| 268 |
'issues' => [ |
| 269 |
ParamValidator::PARAM_TYPE => 'PARAM_TYPE must be a string or array, got boolean', |
| 270 |
ParamValidator::PARAM_REQUIRED => 'PARAM_REQUIRED must be boolean, got integer', |
| 271 |
ParamValidator::PARAM_ISMULTI => 'PARAM_ISMULTI must be boolean, got integer', |
| 272 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 'PARAM_ISMULTI_LIMIT1 must be an integer, got string', |
| 273 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 'PARAM_ISMULTI_LIMIT2 must be an integer, got string', |
| 274 |
ParamValidator::PARAM_ALL => 'PARAM_ALL must be a string or boolean, got array', |
| 275 |
ParamValidator::PARAM_ALLOW_DUPLICATES |
| 276 |
=> 'PARAM_ALLOW_DUPLICATES must be boolean, got integer', |
| 277 |
ParamValidator::PARAM_SENSITIVE => 'PARAM_SENSITIVE must be boolean, got integer', |
| 278 |
ParamValidator::PARAM_DEPRECATED => 'PARAM_DEPRECATED must be boolean, got integer', |
| 279 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES |
| 280 |
=> 'PARAM_IGNORE_UNRECOGNIZED_VALUES must be boolean, got integer', |
| 281 |
], |
| 282 |
'allowedKeys' => $multiKeys, |
| 283 |
'messages' => [], |
| 284 |
], |
| 285 |
], |
| 286 |
'Multi-value stuff is ignored without ISMULTI' => [ |
| 287 |
[ |
| 288 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => '10', |
| 289 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => '100', |
| 290 |
ParamValidator::PARAM_ALL => [], |
| 291 |
ParamValidator::PARAM_ALLOW_DUPLICATES => 1, |
| 292 |
], |
| 293 |
[ |
| 294 |
'issues' => [], |
| 295 |
'allowedKeys' => $normalKeys, |
| 296 |
'messages' => [], |
| 297 |
], |
| 298 |
], |
| 299 |
'PARAM_TYPE is not registered' => [ |
| 300 |
[ ParamValidator::PARAM_TYPE => 'xyz' ], |
| 301 |
[ |
| 302 |
'issues' => [ |
| 303 |
ParamValidator::PARAM_TYPE => 'Unknown/unregistered PARAM_TYPE "xyz"', |
| 304 |
], |
| 305 |
'allowedKeys' => $normalKeys, |
| 306 |
'messages' => [], |
| 307 |
], |
| 308 |
], |
| 309 |
'PARAM_DEFAULT value doesn\'t validate' => [ |
| 310 |
[ |
| 311 |
ParamValidator::PARAM_TYPE => [ 'a', 'b', 'c', 'd' ], |
| 312 |
ParamValidator::PARAM_DEFAULT => 'a|b', |
| 313 |
], |
| 314 |
[ |
| 315 |
'issues' => [ |
| 316 |
ParamValidator::PARAM_DEFAULT => 'Value for PARAM_DEFAULT does not validate (code badvalue)', |
| 317 |
], |
| 318 |
'allowedKeys' => [ ...$normalKeys, TypeDef\EnumDef::PARAM_DEPRECATED_VALUES ], |
| 319 |
'messages' => [], |
| 320 |
], |
| 321 |
], |
| 322 |
'PARAM_ISMULTI_LIMIT1 out of range' => [ |
| 323 |
[ |
| 324 |
ParamValidator::PARAM_ISMULTI => true, |
| 325 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 0, |
| 326 |
], |
| 327 |
[ |
| 328 |
'issues' => [ |
| 329 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 'PARAM_ISMULTI_LIMIT1 must be greater than 0, got 0', |
| 330 |
], |
| 331 |
'allowedKeys' => $multiKeys, |
| 332 |
'messages' => [], |
| 333 |
], |
| 334 |
], |
| 335 |
'PARAM_ISMULTI_LIMIT2 out of range' => [ |
| 336 |
[ |
| 337 |
ParamValidator::PARAM_ISMULTI => true, |
| 338 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 100, |
| 339 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 10, |
| 340 |
], |
| 341 |
[ |
| 342 |
'issues' => [ |
| 343 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 'PARAM_ISMULTI_LIMIT2 must be greater than or equal to PARAM_ISMULTI_LIMIT1, but 10 < 100', |
| 344 |
], |
| 345 |
'allowedKeys' => $multiKeys, |
| 346 |
'messages' => [], |
| 347 |
], |
| 348 |
], |
| 349 |
'PARAM_ISMULTI_LIMIT1 = LIMIT2 is ok' => [ |
| 350 |
[ |
| 351 |
ParamValidator::PARAM_ISMULTI => true, |
| 352 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 10, |
| 353 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 10, |
| 354 |
], |
| 355 |
[ |
| 356 |
'issues' => [], |
| 357 |
'allowedKeys' => $multiKeys, |
| 358 |
'messages' => [], |
| 359 |
], |
| 360 |
], |
| 361 |
'PARAM_ALL false is ok with non-enumerated type' => [ |
| 362 |
[ |
| 363 |
ParamValidator::PARAM_ISMULTI => true, |
| 364 |
ParamValidator::PARAM_ALL => false, |
| 365 |
], |
| 366 |
[ |
| 367 |
'issues' => [], |
| 368 |
'allowedKeys' => $multiKeys, |
| 369 |
'messages' => [], |
| 370 |
], |
| 371 |
], |
| 372 |
'PARAM_ALL true is not ok with non-enumerated type' => [ |
| 373 |
[ |
| 374 |
ParamValidator::PARAM_ISMULTI => true, |
| 375 |
ParamValidator::PARAM_ALL => true, |
| 376 |
], |
| 377 |
[ |
| 378 |
'issues' => [ |
| 379 |
ParamValidator::PARAM_ALL => 'PARAM_ALL cannot be used with non-enumerated types', |
| 380 |
], |
| 381 |
'allowedKeys' => $multiKeys, |
| 382 |
'messages' => [], |
| 383 |
], |
| 384 |
], |
| 385 |
'PARAM_ALL string is not ok with non-enumerated type' => [ |
| 386 |
[ |
| 387 |
ParamValidator::PARAM_ISMULTI => true, |
| 388 |
ParamValidator::PARAM_ALL => 'all', |
| 389 |
], |
| 390 |
[ |
| 391 |
'issues' => [ |
| 392 |
ParamValidator::PARAM_ALL => 'PARAM_ALL cannot be used with non-enumerated types', |
| 393 |
], |
| 394 |
'allowedKeys' => $multiKeys, |
| 395 |
'messages' => [], |
| 396 |
], |
| 397 |
], |
| 398 |
'PARAM_ALL true value collision' => [ |
| 399 |
[ |
| 400 |
ParamValidator::PARAM_TYPE => [ 'a', 'b', '*' ], |
| 401 |
ParamValidator::PARAM_ISMULTI => true, |
| 402 |
ParamValidator::PARAM_ALL => true, |
| 403 |
], |
| 404 |
[ |
| 405 |
'issues' => [ |
| 406 |
ParamValidator::PARAM_ALL => 'Value for PARAM_ALL conflicts with an enumerated value', |
| 407 |
], |
| 408 |
'allowedKeys' => $multiEnumKeys, |
| 409 |
'messages' => [], |
| 410 |
], |
| 411 |
], |
| 412 |
'PARAM_ALL string value collision' => [ |
| 413 |
[ |
| 414 |
ParamValidator::PARAM_TYPE => [ 'a', 'b', 'all' ], |
| 415 |
ParamValidator::PARAM_ISMULTI => true, |
| 416 |
ParamValidator::PARAM_ALL => 'all', |
| 417 |
], |
| 418 |
[ |
| 419 |
'issues' => [ |
| 420 |
ParamValidator::PARAM_ALL => 'Value for PARAM_ALL conflicts with an enumerated value', |
| 421 |
], |
| 422 |
'allowedKeys' => $multiEnumKeys, |
| 423 |
'messages' => [], |
| 424 |
], |
| 425 |
], |
| 426 |
'TypeDef is called' => [ |
| 427 |
[ |
| 428 |
ParamValidator::PARAM_TYPE => 'foo', |
| 429 |
'XXX-test' => '!!!', |
| 430 |
], |
| 431 |
[ |
| 432 |
'issues' => [ |
| 433 |
'XXX-test' => 'XXX-test was !!!', |
| 434 |
], |
| 435 |
'allowedKeys' => array_merge( $normalKeys, [ 'XXX-test' ] ), |
| 436 |
'messages' => [], |
| 437 |
], |
| 438 |
], |
| 439 |
]; |
| 440 |
} |
| 441 |
|
| 442 |
public function testCheckSettings_noEnum(): void { |
| 443 |
$callbacks = new SimpleCallbacks( [] ); |
| 444 |
$validator = new ParamValidator( |
| 445 |
$callbacks, |
| 446 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 447 |
[ 'typeDefs' => [] ] |
| 448 |
); |
| 449 |
|
| 450 |
$this->assertEquals( |
| 451 |
[ ParamValidator::PARAM_TYPE => 'Unknown/unregistered PARAM_TYPE "enum"' ], |
| 452 |
$validator->checkSettings( 'dummy', [ ParamValidator::PARAM_TYPE => [ 'xyz' ] ], [] )['issues'] |
| 453 |
); |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
public function testExplodeMultiValue( $value, $limit, $expect ) { |
| 458 |
$this->assertSame( $expect, ParamValidator::explodeMultiValue( $value, $limit ) ); |
| 459 |
} |
| 460 |
|
| 461 |
public static function provideExplodeMultiValue() { |
| 462 |
return [ |
| 463 |
[ 'foobar', 100, [ 'foobar' ] ], |
| 464 |
[ 'foo|bar|baz', 100, [ 'foo', 'bar', 'baz' ] ], |
| 465 |
[ "\x1Ffoo\x1Fbar\x1Fbaz", 100, [ 'foo', 'bar', 'baz' ] ], |
| 466 |
[ 'foo|bar|baz', 2, [ 'foo', 'bar|baz' ] ], |
| 467 |
[ "\x1Ffoo\x1Fbar\x1Fbaz", 2, [ 'foo', "bar\x1Fbaz" ] ], |
| 468 |
[ '|bar|baz', 100, [ '', 'bar', 'baz' ] ], |
| 469 |
[ "\x1F\x1Fbar\x1Fbaz", 100, [ '', 'bar', 'baz' ] ], |
| 470 |
[ '', 100, [] ], |
| 471 |
[ "\x1F", 100, [] ], |
| 472 |
]; |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
public function testImplodeMultiValue( $value, $expect ) { |
| 477 |
$this->assertSame( $expect, ParamValidator::implodeMultiValue( $value ) ); |
| 478 |
} |
| 479 |
|
| 480 |
public static function provideImplodeMultiValue() { |
| 481 |
return [ |
| 482 |
[ [], '' ], |
| 483 |
[ [ '' ], '|' ], |
| 484 |
[ [ '', '' ], '|' ], |
| 485 |
[ [ 'foo', '' ], 'foo|' ], |
| 486 |
[ [ '', 'foo' ], '|foo' ], |
| 487 |
[ [ '', 'foo', '' ], '|foo|' ], |
| 488 |
[ [ 'foobar' ], 'foobar' ], |
| 489 |
[ [ 'foo', 'bar' ], 'foo|bar' ], |
| 490 |
[ [ 'foo|bar' ], "\x1ffoo|bar" ], |
| 491 |
[ [ 'foo', '|', 'bar' ], "\x1ffoo\x1f|\x1fbar" ], |
| 492 |
]; |
| 493 |
} |
| 494 |
|
| 495 |
public function testGetValue_badType() { |
| 496 |
$validator = new ParamValidator( |
| 497 |
new SimpleCallbacks( [] ), |
| 498 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 499 |
[ 'typeDefs' => [] ] |
| 500 |
); |
| 501 |
$this->expectException( DomainException::class ); |
| 502 |
$this->expectExceptionMessage( "Param foo's type is unknown - string" ); |
| 503 |
$validator->getValue( 'foo', 'default', [] ); |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
public function testGetValue( |
| 508 |
$settings, $parseLimit, $get, $value, $isSensitive, $isDeprecated |
| 509 |
) { |
| 510 |
$callbacks = new SimpleCallbacks( $get ); |
| 511 |
$dummy = (object)[]; |
| 512 |
$options = [ $dummy ]; |
| 513 |
|
| 514 |
$settings += [ |
| 515 |
ParamValidator::PARAM_TYPE => 'xyz', |
| 516 |
ParamValidator::PARAM_DEFAULT => null, |
| 517 |
]; |
| 518 |
|
| 519 |
$mockDef = $this->getMockBuilder( TypeDef::class ) |
| 520 |
->setConstructorArgs( [ $callbacks ] ) |
| 521 |
->getMockForAbstractClass(); |
| 522 |
|
| 523 |
$expectOptions = $options; |
| 524 |
if ( $get === [] && isset( $settings[ParamValidator::PARAM_DEFAULT] ) ) { |
| 525 |
$expectOptions['is-default'] = true; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
$validator = $this->getMockBuilder( ParamValidator::class ) |
| 530 |
->setConstructorArgs( [ |
| 531 |
$callbacks, |
| 532 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 533 |
[ 'typeDefs' => [ 'xyz' => $mockDef ] ] |
| 534 |
] ) |
| 535 |
->onlyMethods( [ 'validateValue' ] ) |
| 536 |
->getMock(); |
| 537 |
$validator->expects( $this->once() )->method( 'validateValue' ) |
| 538 |
->with( |
| 539 |
$this->identicalTo( 'foobar' ), |
| 540 |
$this->identicalTo( $value ), |
| 541 |
$this->identicalTo( $settings ), |
| 542 |
$this->identicalTo( $expectOptions ) |
| 543 |
) |
| 544 |
->willReturn( $dummy ); |
| 545 |
|
| 546 |
$this->assertSame( $dummy, $validator->getValue( 'foobar', $settings, $options ) ); |
| 547 |
|
| 548 |
$expectConditions = []; |
| 549 |
if ( $isSensitive ) { |
| 550 |
$expectConditions[] = [ |
| 551 |
'message' => DataMessageValue::new( 'paramvalidator-param-sensitive', [], 'param-sensitive' ) |
| 552 |
->plaintextParams( 'foobar', $value ), |
| 553 |
'name' => 'foobar', |
| 554 |
'value' => $value, |
| 555 |
'settings' => $settings, |
| 556 |
]; |
| 557 |
} |
| 558 |
if ( $isDeprecated ) { |
| 559 |
$expectConditions[] = [ |
| 560 |
'message' => DataMessageValue::new( 'paramvalidator-param-deprecated', [], 'param-deprecated' ) |
| 561 |
->plaintextParams( 'foobar', $value ), |
| 562 |
'name' => 'foobar', |
| 563 |
'value' => $value, |
| 564 |
'settings' => $settings, |
| 565 |
]; |
| 566 |
} |
| 567 |
$this->assertEquals( $expectConditions, $callbacks->getRecordedConditions() ); |
| 568 |
} |
| 569 |
|
| 570 |
public static function provideGetValue() { |
| 571 |
$sen = [ ParamValidator::PARAM_SENSITIVE => true ]; |
| 572 |
$dep = [ ParamValidator::PARAM_DEPRECATED => true ]; |
| 573 |
$dflt = [ ParamValidator::PARAM_DEFAULT => 'DeFaUlT' ]; |
| 574 |
$arr = [ ParamValidator::PARAM_ISMULTI => true, ParamValidator::PARAM_TYPE => 'integer' ]; |
| 575 |
|
| 576 |
|
| 577 |
return [ |
| 578 |
'Simple case' => [ [], false, [ 'foobar' => '!!!' ], '!!!', false, false ], |
| 579 |
'Not provided' => [ $sen + $dep, false, [], null, false, false ], |
| 580 |
'Not provided, default' => [ $sen + $dep + $dflt, true, [], 'DeFaUlT', false, false ], |
| 581 |
'Provided' => [ $dflt, false, [ 'foobar' => 'XYZ' ], 'XYZ', false, false ], |
| 582 |
'Provided, sensitive' => [ $sen, false, [ 'foobar' => 'XYZ' ], 'XYZ', true, false ], |
| 583 |
'Provided, deprecated' => [ $dep, false, [ 'foobar' => 'XYZ' ], 'XYZ', false, true ], |
| 584 |
'Provided array' => [ $dflt, false, [ 'foobar' => [ 'XYZ' ] ], [ 'XYZ' ], false, false ], |
| 585 |
'Multivalue as array' => [ $dflt, false, [ 'foobar' => [ 1, 2, 3 ] ], [ 1, 2, 3 ], false, false ], |
| 586 |
]; |
| 587 |
} |
| 588 |
|
| 589 |
public function testValidateValue_badType() { |
| 590 |
$validator = new ParamValidator( |
| 591 |
new SimpleCallbacks( [] ), |
| 592 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 593 |
[ 'typeDefs' => [] ] |
| 594 |
); |
| 595 |
$this->expectException( DomainException::class ); |
| 596 |
$this->expectExceptionMessage( "Param foo's type is unknown - string" ); |
| 597 |
$validator->validateValue( 'foo', null, 'default', [] ); |
| 598 |
} |
| 599 |
|
| 600 |
public function testValidateValue_multiValueMustBeArray() { |
| 601 |
$validator = new ParamValidator( |
| 602 |
new SimpleCallbacks( [ |
| 603 |
'foo' => 'x|y|z' |
| 604 |
] ), |
| 605 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 606 |
[ 'typeDefs' => ParamValidator::STANDARD_TYPES ] |
| 607 |
); |
| 608 |
|
| 609 |
$options = [ |
| 610 |
ParamValidator::OPT_ENFORCE_JSON_TYPES => true |
| 611 |
]; |
| 612 |
|
| 613 |
$settings = [ |
| 614 |
ParamValidator::PARAM_TYPE => 'integer', |
| 615 |
ParamValidator::PARAM_ISMULTI => true, |
| 616 |
]; |
| 617 |
|
| 618 |
$this->expectException( ValidationException::class ); |
| 619 |
$this->expectExceptionMessage( 'multivalue-must-be-array' ); |
| 620 |
$validator->validateValue( 'foo', 'x|y|z', $settings, $options ); |
| 621 |
} |
| 622 |
|
| 623 |
public static function provideMismatchValueToType() { |
| 624 |
yield 'array provided but not multi-value' => [ |
| 625 |
'foo', |
| 626 |
[ 'foobar' ], |
| 627 |
[ ParamValidator::PARAM_TYPE => 'string' ], |
| 628 |
ValidationException::class, |
| 629 |
"Validation of `foo` failed: badvalue", |
| 630 |
]; |
| 631 |
|
| 632 |
yield 'string provided but multi-value set' => [ |
| 633 |
'foo', |
| 634 |
[ 'foobar' ], |
| 635 |
[ ParamValidator::PARAM_TYPE => 'integer' ], |
| 636 |
ValidationException::class, |
| 637 |
"Validation of `foo` failed: badvalue", |
| 638 |
]; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
public function testValidateValue_valueToTypeMismatch( |
| 643 |
$name, |
| 644 |
$value, |
| 645 |
$settings, |
| 646 |
$expectedException, |
| 647 |
$expectedExceptionMsg |
| 648 |
) { |
| 649 |
$validator = new ParamValidator( |
| 650 |
new SimpleCallbacks( [] ), |
| 651 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 652 |
[ 'typeDefs' => ParamValidator::STANDARD_TYPES ] |
| 653 |
); |
| 654 |
|
| 655 |
$this->expectException( $expectedException ); |
| 656 |
$this->expectExceptionMessage( $expectedExceptionMsg ); |
| 657 |
$validator->validateValue( $name, $value, $settings, [] ); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
public function testValidateValue( |
| 662 |
$value, $settings, $highLimits, $valuesList, $calls, $expect, $expectConditions = [], |
| 663 |
$constructorOptions = [] |
| 664 |
) { |
| 665 |
$callbacks = new SimpleCallbacks( [] ); |
| 666 |
$settings += [ |
| 667 |
ParamValidator::PARAM_TYPE => 'xyz', |
| 668 |
ParamValidator::PARAM_DEFAULT => null, |
| 669 |
]; |
| 670 |
$dummy = (object)[]; |
| 671 |
$options = [ $dummy, 'useHighLimits' => $highLimits ]; |
| 672 |
$eOptions = $options; |
| 673 |
$eOptions2 = $eOptions; |
| 674 |
if ( $valuesList !== null ) { |
| 675 |
$eOptions2['values-list'] = $valuesList; |
| 676 |
} |
| 677 |
|
| 678 |
$mockDef = $this->getMockBuilder( TypeDef::class ) |
| 679 |
->setConstructorArgs( [ $callbacks ] ) |
| 680 |
->onlyMethods( [ 'validate', 'getEnumValues' ] ) |
| 681 |
->getMockForAbstractClass(); |
| 682 |
$mockDef->method( 'getEnumValues' ) |
| 683 |
->with( |
| 684 |
$this->identicalTo( 'foobar' ), $this->identicalTo( $settings ), $this->identicalTo( $eOptions ) |
| 685 |
) |
| 686 |
->willReturn( [ 'a', 'b', 'c', 'd', 'e', 'f' ] ); |
| 687 |
$mockDef->expects( $this->exactly( count( $calls ) ) )->method( 'validate' )->willReturnCallback( |
| 688 |
function ( $n, $v, $s, $o ) use ( $settings, $eOptions2, $calls ) { |
| 689 |
$this->assertSame( 'foobar', $n ); |
| 690 |
$this->assertSame( $settings, $s ); |
| 691 |
$this->assertSame( $eOptions2, $o ); |
| 692 |
|
| 693 |
if ( !array_key_exists( $v, $calls ) ) { |
| 694 |
$this->fail( "Called with unexpected value '$v'" ); |
| 695 |
} |
| 696 |
if ( $calls[$v] === null ) { |
| 697 |
throw new ValidationException( |
| 698 |
DataMessageValue::new( 'XXX-from-test', [], 'badvalue' ), |
| 699 |
$n, $v, $s |
| 700 |
); |
| 701 |
} |
| 702 |
return $calls[$v]; |
| 703 |
} |
| 704 |
); |
| 705 |
|
| 706 |
$validator = new ParamValidator( |
| 707 |
$callbacks, |
| 708 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 709 |
$constructorOptions + [ 'typeDefs' => [ 'xyz' => $mockDef ] ] |
| 710 |
); |
| 711 |
|
| 712 |
if ( $expect instanceof ValidationException ) { |
| 713 |
try { |
| 714 |
$validator->validateValue( 'foobar', $value, $settings, $options ); |
| 715 |
$this->fail( 'Expected exception not thrown' ); |
| 716 |
} catch ( ValidationException $ex ) { |
| 717 |
$this->assertSame( |
| 718 |
$expect->getFailureMessage()->getCode(), |
| 719 |
$ex->getFailureMessage()->getCode() |
| 720 |
); |
| 721 |
$this->assertSame( |
| 722 |
$expect->getFailureMessage()->getData(), |
| 723 |
$ex->getFailureMessage()->getData() |
| 724 |
); |
| 725 |
} |
| 726 |
} else { |
| 727 |
$this->assertSame( |
| 728 |
$expect, $validator->validateValue( 'foobar', $value, $settings, $options ) |
| 729 |
); |
| 730 |
foreach ( $expectConditions as $k => $c ) { |
| 731 |
$expectConditions[$k]['settings'] = $settings; |
| 732 |
} |
| 733 |
$this->assertEquals( $expectConditions, $callbacks->getRecordedConditions() ); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
public static function provideValidateValue() { |
| 738 |
|
| 739 |
|
| 740 |
return [ |
| 741 |
'No value' => [ null, [], false, null, [], null ], |
| 742 |
'No value, required' => [ |
| 743 |
null, |
| 744 |
[ ParamValidator::PARAM_REQUIRED => true ], |
| 745 |
false, |
| 746 |
null, |
| 747 |
[], |
| 748 |
new ValidationException( |
| 749 |
DataMessageValue::new( 'paramvalidator-missingparam', [], 'missingparam' ) |
| 750 |
->plaintextParams( 'foobar' ), |
| 751 |
'foobar', null, [] |
| 752 |
), |
| 753 |
], |
| 754 |
'Non-multi value' => [ 'abc', [], false, null, [ 'abc' => 'def' ], 'def' ], |
| 755 |
'Simple multi value' => [ |
| 756 |
'a|b|c|d', |
| 757 |
[ ParamValidator::PARAM_ISMULTI => true ], |
| 758 |
false, |
| 759 |
[ 'a', 'b', 'c', 'd' ], |
| 760 |
[ 'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D' ], |
| 761 |
[ 'A', 'B', 'C', 'D' ], |
| 762 |
], |
| 763 |
'Array multi value' => [ |
| 764 |
[ 'a', 'b', 'c', 'd' ], |
| 765 |
[ ParamValidator::PARAM_ISMULTI => true ], |
| 766 |
false, |
| 767 |
[ 'a', 'b', 'c', 'd' ], |
| 768 |
[ 'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D' ], |
| 769 |
[ 'A', 'B', 'C', 'D' ], |
| 770 |
], |
| 771 |
'Multi value with PARAM_ALL' => [ |
| 772 |
'*', |
| 773 |
[ ParamValidator::PARAM_ISMULTI => true, ParamValidator::PARAM_ALL => true ], |
| 774 |
false, |
| 775 |
null, |
| 776 |
[], |
| 777 |
[ 'a', 'b', 'c', 'd', 'e', 'f' ], |
| 778 |
], |
| 779 |
'Multi value with PARAM_ALL = "x"' => [ |
| 780 |
'x', |
| 781 |
[ ParamValidator::PARAM_ISMULTI => true, ParamValidator::PARAM_ALL => "x" ], |
| 782 |
false, |
| 783 |
null, |
| 784 |
[], |
| 785 |
[ 'a', 'b', 'c', 'd', 'e', 'f' ], |
| 786 |
], |
| 787 |
'Multi value with PARAM_ALL = "x", passing "*"' => [ |
| 788 |
'*', |
| 789 |
[ ParamValidator::PARAM_ISMULTI => true, ParamValidator::PARAM_ALL => "x" ], |
| 790 |
false, |
| 791 |
[ '*' ], |
| 792 |
[ '*' => '?' ], |
| 793 |
[ '?' ], |
| 794 |
], |
| 795 |
|
| 796 |
'Too many values' => [ |
| 797 |
'a|b|c|d', |
| 798 |
[ |
| 799 |
ParamValidator::PARAM_ISMULTI => true, |
| 800 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 2, |
| 801 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 4, |
| 802 |
], |
| 803 |
false, |
| 804 |
null, |
| 805 |
[], |
| 806 |
new ValidationException( |
| 807 |
DataMessageValue::new( 'paramvalidator-toomanyvalues', [], 'toomanyvalues', [ |
| 808 |
'parameter' => 'foobar', |
| 809 |
'limit' => 2, |
| 810 |
'lowlimit' => 2, |
| 811 |
'highlimit' => 4, |
| 812 |
] )->plaintextParams( 'foobar', 'a|b|c|d' )->numParams( 2 ), |
| 813 |
'foobar', 'a|b|c|d', [] |
| 814 |
), |
| 815 |
], |
| 816 |
'Too many values as array' => [ |
| 817 |
[ 'a', 'b', 'c', 'd' ], |
| 818 |
[ |
| 819 |
ParamValidator::PARAM_ISMULTI => true, |
| 820 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 2, |
| 821 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 4, |
| 822 |
], |
| 823 |
false, |
| 824 |
null, |
| 825 |
[], |
| 826 |
new ValidationException( |
| 827 |
DataMessageValue::new( 'paramvalidator-toomanyvalues', [], 'toomanyvalues', [ |
| 828 |
'parameter' => 'foobar', |
| 829 |
'limit' => 2, |
| 830 |
'lowlimit' => 2, |
| 831 |
'highlimit' => 4, |
| 832 |
] )->plaintextParams( 'foobar', 'a|b|c|d' )->numParams( 2 ), |
| 833 |
'foobar', [ 'a', 'b', 'c', 'd' ], [] |
| 834 |
), |
| 835 |
], |
| 836 |
'Not too many values for highlimits' => [ |
| 837 |
'a|b|c|d', |
| 838 |
[ |
| 839 |
ParamValidator::PARAM_ISMULTI => true, |
| 840 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 2, |
| 841 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 4, |
| 842 |
], |
| 843 |
true, |
| 844 |
[ 'a', 'b', 'c', 'd' ], |
| 845 |
[ 'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D' ], |
| 846 |
[ 'A', 'B', 'C', 'D' ], |
| 847 |
], |
| 848 |
'Too many values for highlimits' => [ |
| 849 |
'a|b|c|d|e', |
| 850 |
[ |
| 851 |
ParamValidator::PARAM_ISMULTI => true, |
| 852 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 2, |
| 853 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 4, |
| 854 |
], |
| 855 |
true, |
| 856 |
null, |
| 857 |
[], |
| 858 |
new ValidationException( |
| 859 |
DataMessageValue::new( 'paramvalidator-toomanyvalues', [], 'toomanyvalues', [ |
| 860 |
'parameter' => 'foobar', |
| 861 |
'limit' => 4, |
| 862 |
'lowlimit' => 2, |
| 863 |
'highlimit' => 4, |
| 864 |
] )->plaintextParams( 'foobar', 'a|b|c|d|e' )->numParams( 4 ), |
| 865 |
'foobar', 'a|b|c|d|e', [] |
| 866 |
), |
| 867 |
], |
| 868 |
|
| 869 |
'Too many values via default' => [ |
| 870 |
'a|b|c|d', |
| 871 |
[ |
| 872 |
ParamValidator::PARAM_ISMULTI => true, |
| 873 |
], |
| 874 |
false, |
| 875 |
null, |
| 876 |
[], |
| 877 |
new ValidationException( |
| 878 |
DataMessageValue::new( 'paramvalidator-toomanyvalues', [], 'toomanyvalues', [ |
| 879 |
'parameter' => 'foobar', |
| 880 |
'limit' => 2, |
| 881 |
'lowlimit' => 2, |
| 882 |
'highlimit' => 4, |
| 883 |
] )->plaintextParams( 'foobar', 'a|b|c|d' )->numParams( 2 ), |
| 884 |
'foobar', 'a|b|c|d|e', [] |
| 885 |
), |
| 886 |
[], |
| 887 |
[ 'ismultiLimits' => [ 2, 4 ] ], |
| 888 |
], |
| 889 |
'Not too many values for highlimits via default' => [ |
| 890 |
'a|b|c|d', |
| 891 |
[ |
| 892 |
ParamValidator::PARAM_ISMULTI => true, |
| 893 |
], |
| 894 |
true, |
| 895 |
[ 'a', 'b', 'c', 'd' ], |
| 896 |
[ 'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D' ], |
| 897 |
[ 'A', 'B', 'C', 'D' ], |
| 898 |
[], |
| 899 |
[ 'ismultiLimits' => [ 2, 4 ] ], |
| 900 |
], |
| 901 |
'Too many values for highlimits via default' => [ |
| 902 |
'a|b|c|d|e', |
| 903 |
[ |
| 904 |
ParamValidator::PARAM_ISMULTI => true, |
| 905 |
], |
| 906 |
true, |
| 907 |
null, |
| 908 |
[], |
| 909 |
new ValidationException( |
| 910 |
DataMessageValue::new( 'paramvalidator-toomanyvalues', [], 'toomanyvalues', [ |
| 911 |
'parameter' => 'foobar', |
| 912 |
'limit' => 4, |
| 913 |
'lowlimit' => 2, |
| 914 |
'highlimit' => 4, |
| 915 |
] )->plaintextParams( 'foobar', 'a|b|c|d|e' )->numParams( 4 ), |
| 916 |
'foobar', 'a|b|c|d|e', [] |
| 917 |
), |
| 918 |
[], |
| 919 |
[ 'ismultiLimits' => [ 2, 4 ] ], |
| 920 |
], |
| 921 |
'U+001F-style multi value when not multi' => [ |
| 922 |
"\x1fa", |
| 923 |
[ ParamValidator::PARAM_ISMULTI => false ], |
| 924 |
false, |
| 925 |
null, |
| 926 |
[], |
| 927 |
new ValidationException( |
| 928 |
DataMessageValue::new( 'paramvalicator-notmulti', [], 'badvalue' ) |
| 929 |
->plaintextParams( 'foobar', "\x1fa" ), |
| 930 |
'foobar', "\x1fa", [] |
| 931 |
), |
| 932 |
], |
| 933 |
|
| 934 |
'Invalid values' => [ |
| 935 |
'a|b|c|d', |
| 936 |
[ ParamValidator::PARAM_ISMULTI => true ], |
| 937 |
false, |
| 938 |
[ 'a', 'b', 'c', 'd' ], |
| 939 |
[ 'a' => 'A', 'b' => null, ], |
| 940 |
new ValidationException( |
| 941 |
DataMessageValue::new( 'XXX-from-test', [], 'badvalue' ), |
| 942 |
'foobar', 'b', [] |
| 943 |
), |
| 944 |
], |
| 945 |
'Ignored unrecognized values' => [ |
| 946 |
'a|b|c|d', |
| 947 |
[ |
| 948 |
ParamValidator::PARAM_ISMULTI => true, |
| 949 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES => true, |
| 950 |
], |
| 951 |
false, |
| 952 |
[ 'a', 'b', 'c', 'd' ], |
| 953 |
[ 'a' => 'A', 'b' => null, 'c' => null, 'd' => 'D' ], |
| 954 |
[ 'A', 'D' ], |
| 955 |
[ |
| 956 |
[ |
| 957 |
'message' => DataMessageValue::new( |
| 958 |
'paramvalidator-unrecognizedvalues', [], 'unrecognizedvalues', [ 'values' => [ 'b', 'c' ] ] |
| 959 |
) |
| 960 |
->plaintextParams( 'foobar', 'a|b|c|d' ) |
| 961 |
->commaListParams( [ |
| 962 |
new ScalarParam( ParamType::PLAINTEXT, 'b' ), |
| 963 |
new ScalarParam( ParamType::PLAINTEXT, 'c' ), |
| 964 |
] ) |
| 965 |
->numParams( 2 ), |
| 966 |
'name' => 'foobar', |
| 967 |
'value' => 'a|b|c|d', |
| 968 |
], |
| 969 |
], |
| 970 |
], |
| 971 |
'Ignored unrecognized values, passed as array' => [ |
| 972 |
[ 'a', 'b', 'c', 'd' ], |
| 973 |
[ |
| 974 |
ParamValidator::PARAM_ISMULTI => true, |
| 975 |
ParamValidator::PARAM_IGNORE_UNRECOGNIZED_VALUES => true, |
| 976 |
], |
| 977 |
false, |
| 978 |
[ 'a', 'b', 'c', 'd' ], |
| 979 |
[ 'a' => 'A', 'b' => null, 'c' => null, 'd' => 'D' ], |
| 980 |
[ 'A', 'D' ], |
| 981 |
[ |
| 982 |
[ |
| 983 |
'message' => DataMessageValue::new( |
| 984 |
'paramvalidator-unrecognizedvalues', [], 'unrecognizedvalues', [ 'values' => [ 'b', 'c' ] ] |
| 985 |
) |
| 986 |
->plaintextParams( 'foobar', 'a|b|c|d' ) |
| 987 |
->commaListParams( [ |
| 988 |
new ScalarParam( ParamType::PLAINTEXT, 'b' ), |
| 989 |
new ScalarParam( ParamType::PLAINTEXT, 'c' ), |
| 990 |
] ) |
| 991 |
->numParams( 2 ), |
| 992 |
'name' => 'foobar', |
| 993 |
'value' => 'a|b|c|d', |
| 994 |
], |
| 995 |
], |
| 996 |
], |
| 997 |
]; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
public function testGetParamInfo( |
| 1002 |
$settings, array $expect, array $typeDefReturns = [], array $options = [] |
| 1003 |
) { |
| 1004 |
$callbacks = new SimpleCallbacks( [] ); |
| 1005 |
|
| 1006 |
$mb = $this->getMockBuilder( TypeDef::class ) |
| 1007 |
->setConstructorArgs( [ $callbacks ] ) |
| 1008 |
->onlyMethods( [ 'getParamInfo' ] ); |
| 1009 |
$mock = $mb->getMockForAbstractClass(); |
| 1010 |
$mock->method( 'getParamInfo' )->willReturn( $typeDefReturns ); |
| 1011 |
|
| 1012 |
$validator = new ParamValidator( |
| 1013 |
$callbacks, |
| 1014 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 1015 |
[ 'typeDefs' => [ 'foo' => $mock ] ] |
| 1016 |
); |
| 1017 |
$settings += [ ParamValidator::PARAM_TYPE => 'foo' ]; |
| 1018 |
$this->assertSame( $expect, $validator->getParamInfo( 'test', $settings, $options ) ); |
| 1019 |
} |
| 1020 |
|
| 1021 |
public static function provideGetParamInfo() { |
| 1022 |
return [ |
| 1023 |
'Basic test' => [ |
| 1024 |
[], |
| 1025 |
[ |
| 1026 |
'type' => 'foo', |
| 1027 |
'required' => false, |
| 1028 |
'multi' => false, |
| 1029 |
], |
| 1030 |
], |
| 1031 |
'Various settings' => [ |
| 1032 |
[ |
| 1033 |
ParamValidator::PARAM_REQUIRED => true, |
| 1034 |
ParamValidator::PARAM_DEPRECATED => true, |
| 1035 |
ParamValidator::PARAM_SENSITIVE => true, |
| 1036 |
ParamValidator::PARAM_DEFAULT => 1234, |
| 1037 |
], |
| 1038 |
[ |
| 1039 |
'type' => 'foo', |
| 1040 |
'required' => true, |
| 1041 |
'deprecated' => true, |
| 1042 |
'sensitive' => true, |
| 1043 |
'default' => 1234, |
| 1044 |
'multi' => false, |
| 1045 |
], |
| 1046 |
], |
| 1047 |
'Multi-value' => [ |
| 1048 |
[ |
| 1049 |
ParamValidator::PARAM_ISMULTI => true, |
| 1050 |
ParamValidator::PARAM_ALLOW_DUPLICATES => true, |
| 1051 |
ParamValidator::PARAM_ALL => true, |
| 1052 |
], |
| 1053 |
[ |
| 1054 |
'type' => 'foo', |
| 1055 |
'required' => false, |
| 1056 |
'multi' => true, |
| 1057 |
'lowlimit' => 50, |
| 1058 |
'highlimit' => 500, |
| 1059 |
'limit' => 50, |
| 1060 |
'allowsduplicates' => true, |
| 1061 |
'allspecifier' => ParamValidator::ALL_DEFAULT_STRING, |
| 1062 |
], |
| 1063 |
], |
| 1064 |
'Multi-value, high limit' => [ |
| 1065 |
[ |
| 1066 |
ParamValidator::PARAM_ISMULTI => true, |
| 1067 |
], |
| 1068 |
[ |
| 1069 |
'type' => 'foo', |
| 1070 |
'required' => false, |
| 1071 |
'multi' => true, |
| 1072 |
'lowlimit' => 50, |
| 1073 |
'highlimit' => 500, |
| 1074 |
'limit' => 500, |
| 1075 |
], |
| 1076 |
[], |
| 1077 |
[ 'useHighLimits' => true ], |
| 1078 |
], |
| 1079 |
'Multi-value, custom limits and all string' => [ |
| 1080 |
[ |
| 1081 |
ParamValidator::PARAM_ISMULTI => true, |
| 1082 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 10, |
| 1083 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 15, |
| 1084 |
ParamValidator::PARAM_ALL => 'all', |
| 1085 |
], |
| 1086 |
[ |
| 1087 |
'type' => 'foo', |
| 1088 |
'required' => false, |
| 1089 |
'multi' => true, |
| 1090 |
'lowlimit' => 10, |
| 1091 |
'highlimit' => 15, |
| 1092 |
'limit' => 10, |
| 1093 |
'allspecifier' => 'all', |
| 1094 |
], |
| 1095 |
], |
| 1096 |
'Multi-value, screwy custom limits' => [ |
| 1097 |
[ |
| 1098 |
ParamValidator::PARAM_ISMULTI => true, |
| 1099 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 10, |
| 1100 |
], |
| 1101 |
[ |
| 1102 |
'type' => 'foo', |
| 1103 |
'required' => false, |
| 1104 |
'multi' => true, |
| 1105 |
'lowlimit' => 50, |
| 1106 |
'highlimit' => 50, |
| 1107 |
'limit' => 50, |
| 1108 |
], |
| 1109 |
], |
| 1110 |
'Overrides from the TypeDef' => [ |
| 1111 |
[ |
| 1112 |
ParamValidator::PARAM_SENSITIVE => true, |
| 1113 |
ParamValidator::PARAM_DEFAULT => 1234, |
| 1114 |
], |
| 1115 |
[ |
| 1116 |
'type' => 'foo', |
| 1117 |
'required' => false, |
| 1118 |
'sensitive' => true, |
| 1119 |
'multi' => false, |
| 1120 |
'foobar' => [ 'baz' ], |
| 1121 |
], |
| 1122 |
[ 'default' => null, 'foobar' => [ 'baz' ] ], |
| 1123 |
] |
| 1124 |
]; |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
public function testGetHelpInfo( |
| 1129 |
$settings, array $expect, array $typeDefReturns = [], array $options = [] |
| 1130 |
) { |
| 1131 |
$callbacks = new SimpleCallbacks( [] ); |
| 1132 |
|
| 1133 |
$mb = $this->getMockBuilder( TypeDef::class ) |
| 1134 |
->setConstructorArgs( [ $callbacks ] ) |
| 1135 |
->onlyMethods( [ 'getHelpInfo', 'getEnumValues' ] ); |
| 1136 |
$mock = $mb->getMockForAbstractClass(); |
| 1137 |
$mock->method( 'getHelpInfo' )->willReturn( $typeDefReturns ); |
| 1138 |
$mock->method( 'getEnumValues' )->willReturn( $settings['values'] ?? null ); |
| 1139 |
|
| 1140 |
$validator = new ParamValidator( |
| 1141 |
$callbacks, |
| 1142 |
new ObjectFactory( $this->getMockForAbstractClass( ContainerInterface::class ) ), |
| 1143 |
[ 'typeDefs' => [ 'foo' => $mock ] ] |
| 1144 |
); |
| 1145 |
$settings += [ ParamValidator::PARAM_TYPE => 'foo' ]; |
| 1146 |
|
| 1147 |
$info = []; |
| 1148 |
foreach ( $validator->getHelpInfo( 'test', $settings, $options ) as $k => $m ) { |
| 1149 |
$this->assertInstanceOf( MessageValue::class, $m ); |
| 1150 |
$info[$k] = $m->dump(); |
| 1151 |
} |
| 1152 |
$this->assertSame( $expect, $info ); |
| 1153 |
} |
| 1154 |
|
| 1155 |
public static function provideGetHelpInfo() { |
| 1156 |
return [ |
| 1157 |
'Basic test' => [ |
| 1158 |
[], |
| 1159 |
[], |
| 1160 |
], |
| 1161 |
'Various settings' => [ |
| 1162 |
[ |
| 1163 |
ParamValidator::PARAM_REQUIRED => true, |
| 1164 |
ParamValidator::PARAM_DEPRECATED => true, |
| 1165 |
ParamValidator::PARAM_SENSITIVE => true, |
| 1166 |
ParamValidator::PARAM_DEFAULT => 1234, |
| 1167 |
], |
| 1168 |
[ |
| 1169 |
ParamValidator::PARAM_DEPRECATED => '<message key="paramvalidator-help-deprecated"></message>', |
| 1170 |
ParamValidator::PARAM_REQUIRED => '<message key="paramvalidator-help-required"></message>', |
| 1171 |
ParamValidator::PARAM_DEFAULT => '<message key="paramvalidator-help-default"><plaintext>1234</plaintext></message>', |
| 1172 |
], |
| 1173 |
], |
| 1174 |
'Multi-value' => [ |
| 1175 |
[ |
| 1176 |
ParamValidator::PARAM_ISMULTI => true, |
| 1177 |
ParamValidator::PARAM_ALLOW_DUPLICATES => true, |
| 1178 |
ParamValidator::PARAM_ALL => true, |
| 1179 |
ParamValidator::PARAM_DEFAULT => '', |
| 1180 |
], |
| 1181 |
[ |
| 1182 |
ParamValidator::PARAM_ISMULTI => '<message key="paramvalidator-help-multi-separate"></message>', |
| 1183 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => '<message key="paramvalidator-help-multi-max"><num>50</num><num>500</num></message>', |
| 1184 |
ParamValidator::PARAM_ALL => '<message key="paramvalidator-help-multi-all"><plaintext>*</plaintext></message>', |
| 1185 |
ParamValidator::PARAM_DEFAULT => '<message key="paramvalidator-help-default-empty"></message>', |
| 1186 |
], |
| 1187 |
], |
| 1188 |
'Multi-value, high limit' => [ |
| 1189 |
[ |
| 1190 |
ParamValidator::PARAM_ISMULTI => true, |
| 1191 |
], |
| 1192 |
[ |
| 1193 |
ParamValidator::PARAM_ISMULTI => '<message key="paramvalidator-help-multi-separate"></message>', |
| 1194 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => '<message key="paramvalidator-help-multi-max"><num>50</num><num>500</num></message>', |
| 1195 |
], |
| 1196 |
[], |
| 1197 |
[ 'useHighLimits' => true ], |
| 1198 |
], |
| 1199 |
'Multi-value, custom limits and all string' => [ |
| 1200 |
[ |
| 1201 |
ParamValidator::PARAM_ISMULTI => true, |
| 1202 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 10, |
| 1203 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 15, |
| 1204 |
ParamValidator::PARAM_ALL => 'all', |
| 1205 |
], |
| 1206 |
[ |
| 1207 |
ParamValidator::PARAM_ISMULTI => '<message key="paramvalidator-help-multi-separate"></message>', |
| 1208 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => '<message key="paramvalidator-help-multi-max"><num>10</num><num>15</num></message>', |
| 1209 |
ParamValidator::PARAM_ALL => '<message key="paramvalidator-help-multi-all"><plaintext>all</plaintext></message>', |
| 1210 |
], |
| 1211 |
], |
| 1212 |
'Multi-value, screwy custom limits' => [ |
| 1213 |
[ |
| 1214 |
ParamValidator::PARAM_ISMULTI => true, |
| 1215 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 10, |
| 1216 |
], |
| 1217 |
[ |
| 1218 |
ParamValidator::PARAM_ISMULTI => '<message key="paramvalidator-help-multi-separate"></message>', |
| 1219 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => '<message key="paramvalidator-help-multi-max-simple"><num>50</num></message>', |
| 1220 |
], |
| 1221 |
], |
| 1222 |
'Multi-value, not enough values to meet the limit' => [ |
| 1223 |
[ |
| 1224 |
'values' => [ 'a', 'b', 'c', 'd' ], |
| 1225 |
ParamValidator::PARAM_ISMULTI => true, |
| 1226 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 4, |
| 1227 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 2, |
| 1228 |
], |
| 1229 |
[ |
| 1230 |
ParamValidator::PARAM_ISMULTI => '<message key="paramvalidator-help-multi-separate"></message>', |
| 1231 |
], |
| 1232 |
], |
| 1233 |
'Multi-value, enough values to meet the limit' => [ |
| 1234 |
[ |
| 1235 |
'values' => [ 'a', 'b', 'c', 'd', 'e' ], |
| 1236 |
ParamValidator::PARAM_ISMULTI => true, |
| 1237 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 4, |
| 1238 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 10, |
| 1239 |
], |
| 1240 |
[ |
| 1241 |
ParamValidator::PARAM_ISMULTI => '<message key="paramvalidator-help-multi-separate"></message>', |
| 1242 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => '<message key="paramvalidator-help-multi-max"><num>4</num><num>10</num></message>', |
| 1243 |
], |
| 1244 |
], |
| 1245 |
'Multi-value, not enough values to meet the limit but dups allowed' => [ |
| 1246 |
[ |
| 1247 |
'values' => [ 'a', 'b', 'c', 'd' ], |
| 1248 |
ParamValidator::PARAM_ISMULTI => true, |
| 1249 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => 4, |
| 1250 |
ParamValidator::PARAM_ISMULTI_LIMIT2 => 2, |
| 1251 |
ParamValidator::PARAM_ALLOW_DUPLICATES => true, |
| 1252 |
], |
| 1253 |
[ |
| 1254 |
ParamValidator::PARAM_ISMULTI => '<message key="paramvalidator-help-multi-separate"></message>', |
| 1255 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => '<message key="paramvalidator-help-multi-max-simple"><num>4</num></message>', |
| 1256 |
], |
| 1257 |
], |
| 1258 |
'Overrides from the TypeDef' => [ |
| 1259 |
[ |
| 1260 |
ParamValidator::PARAM_REQUIRED => true, |
| 1261 |
ParamValidator::PARAM_ISMULTI => true, |
| 1262 |
ParamValidator::PARAM_DEFAULT => 1234, |
| 1263 |
], |
| 1264 |
[ |
| 1265 |
ParamValidator::PARAM_REQUIRED => '<message key="paramvalidator-help-required"></message>', |
| 1266 |
ParamValidator::PARAM_TYPE => '<message key="paramvalidator-help-type-help"></message>', |
| 1267 |
ParamValidator::PARAM_ISMULTI_LIMIT1 => '<message key="paramvalidator-help-multi-max"><num>50</num><num>500</num></message>', |
| 1268 |
'foobar' => '<message key="foobaz"></message>', |
| 1269 |
ParamValidator::PARAM_DEFAULT => '<message key="paramvalidator-help-default"><text>XX</text></message>', |
| 1270 |
], |
| 1271 |
[ |
| 1272 |
ParamValidator::PARAM_DEFAULT => MessageValue::new( 'paramvalidator-help-default', [ 'XX' ] ), |
| 1273 |
'foobar' => MessageValue::new( 'foobaz' ), |
| 1274 |
ParamValidator::PARAM_ISMULTI => null, |
| 1275 |
ParamValidator::PARAM_TYPE => MessageValue::new( 'paramvalidator-help-type-help' ), |
| 1276 |
], |
| 1277 |
] |
| 1278 |
]; |
| 1279 |
} |
| 1280 |
|
| 1281 |
} |