Changeset 62441
- Timestamp:
- 06/01/2026 12:13:15 PM (2 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/abilities-api/wpAbility.php
r62440 r62441 26 26 'type' => 'number', 27 27 'description' => 'The result of performing a math operation.', 28 'required' => true,29 28 ), 30 29 'execute_callback' => static function (): int { … … 275 274 'type' => array( 'null', 'integer' ), 276 275 'description' => 'The null or integer to convert to integer.', 277 'required' => true,278 276 ), 279 277 static function ( $input ): int { … … 287 285 'type' => 'boolean', 288 286 'description' => 'The boolean to convert to integer.', 289 'required' => true,290 287 ), 291 288 static function ( bool $input ): int { … … 299 296 'type' => 'integer', 300 297 'description' => 'The integer to add 5 to.', 301 'required' => true,302 298 ), 303 299 static function ( int $input ): int { … … 311 307 'type' => 'number', 312 308 'description' => 'The floating number to round.', 313 'required' => true,314 309 ), 315 310 static function ( float $input ): int { … … 323 318 'type' => 'string', 324 319 'description' => 'The string to measure the length of.', 325 'required' => true,326 320 ), 327 321 static function ( string $input ): int { … … 362 356 'type' => 'array', 363 357 'description' => 'An array containing two numbers to add.', 364 'required' => true,365 358 'minItems' => 2, 366 359 'maxItems' => 2, … … 402 395 403 396 $this->assertSame( $result, $ability->execute( $input ) ); 397 } 398 399 /** 400 * Data provider for top-level `required` validation behavior. 401 * 402 * Each schema variant is paired with both a valid and an invalid input so the 403 * inert behavior of a top-level `required` boolean — and the meaningful 404 * behavior of a draft-04 `required` array on an object — are sealed. 405 * 406 * @return array<string, array{0: array, 1: mixed, 2: bool}> Data sets. 407 */ 408 public function data_validate_input_top_level_required() { 409 $required_true = array( 410 'type' => 'string', 411 'required' => true, 412 ); 413 $required_false = array( 414 'type' => 'string', 415 'required' => false, 416 ); 417 $required_unset = array( 418 'type' => 'string', 419 ); 420 $object_required = array( 421 'type' => 'object', 422 'properties' => array( 423 'a' => array( 'type' => 'integer' ), 424 'b' => array( 'type' => 'integer' ), 425 ), 426 'required' => array( 'a', 'b' ), 427 ); 428 429 return array( 430 // A top-level `required: true` is inert: only `type` is enforced. 431 'required true: valid input' => array( $required_true, 'hello', true ), 432 'required true: invalid input' => array( $required_true, 123, false ), 433 434 // A top-level `required: false` is equally inert and does not permit null. 435 'required false: valid input' => array( $required_false, 'hello', true ), 436 'required false: invalid input' => array( $required_false, 123, false ), 437 'required false: null still invalid' => array( $required_false, null, false ), 438 439 // Omitting `required` behaves identically to setting it. 440 'required unset: valid input' => array( $required_unset, 'hello', true ), 441 'required unset: invalid input' => array( $required_unset, 123, false ), 442 443 // A draft-04 `required` array on an object type IS honored. 444 'object required array: valid input' => array( 445 $object_required, 446 array( 447 'a' => 1, 448 'b' => 2, 449 ), 450 true, 451 ), 452 'object required array: invalid input' => array( $object_required, array( 'a' => 1 ), false ), 453 ); 454 } 455 456 /** 457 * Tests how a top-level `required` keyword is handled during input validation. 458 * 459 * For a non-object root type, a top-level `required` flag is inert: validation 460 * gates solely on `type`, so the outcome is identical whether `required` is 461 * `true`, `false`, or omitted — and `required: false` notably does not make a 462 * `null` value acceptable. For an object root type, a draft-04 `required` array 463 * of property names is honored and enforces the presence of those properties. 464 * 465 * @ticket 64955 466 * 467 * @covers WP_Ability::validate_input 468 * 469 * @dataProvider data_validate_input_top_level_required 470 * 471 * @param array $input_schema The input schema under test. 472 * @param mixed $input The input value to validate. 473 * @param bool $is_valid Whether the input is expected to pass validation. 474 */ 475 public function test_validate_input_top_level_required( $input_schema, $input, $is_valid ) { 476 $ability = new WP_Ability( 477 self::$test_ability_name, 478 array_merge( 479 self::$test_ability_properties, 480 array( 'input_schema' => $input_schema ) 481 ) 482 ); 483 484 $result = $ability->validate_input( $input ); 485 486 if ( $is_valid ) { 487 $this->assertTrue( $result, 'Expected the input to pass validation.' ); 488 } else { 489 $this->assertWPError( $result, 'Expected the input to fail validation.' ); 490 } 404 491 } 405 492 … … 467 554 'type' => 'string', 468 555 'description' => 'Test input string.', 469 'required' => true,470 556 ), 471 557 'execute_callback' => $execute_callback, … … 562 648 'type' => 'integer', 563 649 'description' => 'Test input parameter.', 564 'required' => true,565 650 ), 566 651 'execute_callback' => static function ( int $input ): int { … … 646 731 'type' => 'integer', 647 732 'description' => 'Test input parameter.', 648 'required' => true,649 733 ), 650 734 'execute_callback' => static function ( int $input ): int { … … 814 898 'type' => 'string', 815 899 'description' => 'Expected string output.', 816 'required' => true,817 900 ), 818 901 'execute_callback' => static function (): int { … … 857 940 'type' => 'string', 858 941 'description' => 'Test input string.', 859 'required' => true,860 942 ), 861 943 'output_schema' => array( 862 944 'type' => 'integer', 863 945 'description' => 'Result integer.', 864 'required' => true,865 946 ), 866 947 'execute_callback' => static function ( string $input ): int { … … 899 980 'type' => 'string', 900 981 'description' => 'Test input string.', 901 'required' => true,902 982 ), 903 983 'execute_callback' => static function ( string $input ) { … … 935 1015 'type' => 'integer', 936 1016 'description' => 'Test input integer.', 937 'required' => true,938 1017 ), 939 1018 'output_schema' => array( 940 1019 'type' => 'integer', 941 1020 'description' => 'Result integer.', 942 'required' => true,943 1021 ), 944 1022 'execute_callback' => static function ( int $input ): int { … … 1106 1184 'type' => 'integer', 1107 1185 'description' => 'Test input integer.', 1108 'required' => true,1109 1186 ), 1110 1187 'execute_callback' => static function (): int { … … 1273 1350 'type' => 'integer', 1274 1351 'description' => 'Test input integer.', 1275 'required' => true,1276 1352 ), 1277 1353 'execute_callback' => static function ( int $input ): int { … … 1417 1493 'type' => 'string', 1418 1494 'description' => 'Test input string.', 1419 'required' => true,1420 1495 ), 1421 1496 'execute_callback' => static function ( string $input ): int { … … 1455 1530 'type' => 'integer', 1456 1531 'description' => 'Test input integer.', 1457 'required' => true,1458 1532 ), 1459 1533 'output_schema' => array( 1460 1534 'type' => 'integer', 1461 1535 'description' => 'Result integer.', 1462 'required' => true,1463 1536 ), 1464 1537 'execute_callback' => static function () { … … 1497 1570 'type' => 'integer', 1498 1571 'description' => 'Test input integer.', 1499 'required' => true,1500 1572 ), 1501 1573 'execute_callback' => static function ( int $input ): int { … … 1535 1607 'type' => 'integer', 1536 1608 'description' => 'Test input integer.', 1537 'required' => true,1538 1609 ), 1539 1610 'execute_callback' => static function ( int $input ): int { … … 1573 1644 'type' => 'integer', 1574 1645 'description' => 'The result integer.', 1575 'required' => true,1576 1646 ), 1577 1647 'execute_callback' => static function (): int { … … 1611 1681 'type' => 'string', 1612 1682 'description' => 'The result string.', 1613 'required' => true,1614 1683 ), 1615 1684 'execute_callback' => static function (): int { … … 1648 1717 'type' => 'string', 1649 1718 'description' => 'The result string.', 1650 'required' => true,1651 1719 ), 1652 1720 'execute_callback' => static function (): int { … … 1686 1754 'type' => 'string', 1687 1755 'description' => 'The result string.', 1688 'required' => true,1689 1756 ), 1690 1757 'execute_callback' => static function (): int { … … 1806 1873 'type' => 'integer', 1807 1874 'description' => 'Int input.', 1808 'required' => true,1809 1875 ), 1810 1876 'execute_callback' => static function ( int $input ): int {
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)