- Timestamp:
- 05/16/2020 06:41:41 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php
r47753 r47809 3 3 * Unit tests covering schema validation and sanitization functionality. 4 4 * 5 * @package WordPress5 * @package WordPress 6 6 * @subpackage REST API 7 7 */ … … 410 410 $this->assertWPError( rest_validate_value_from_schema( $mb_char . $mb_char . $mb_char, $schema ) ); 411 411 } 412 413 /** 414 * @ticket 48818 415 * @dataProvider data_required_property 416 */ 417 public function test_property_is_required( $data, $expected ) { 418 $schema = array( 419 'type' => 'object', 420 'properties' => array( 421 'my_prop' => array( 422 'type' => 'string', 423 ), 424 'my_required_prop' => array( 425 'type' => 'string', 426 'required' => true, 427 ), 428 ), 429 ); 430 431 $valid = rest_validate_value_from_schema( $data, $schema ); 432 433 if ( $expected ) { 434 $this->assertTrue( $valid ); 435 } else { 436 $this->assertWPError( $valid ); 437 } 438 } 439 440 /** 441 * @ticket 48818 442 * @dataProvider data_required_property 443 */ 444 public function test_property_is_required_v4( $data, $expected ) { 445 $schema = array( 446 'type' => 'object', 447 'properties' => array( 448 'my_prop' => array( 449 'type' => 'string', 450 ), 451 'my_required_prop' => array( 452 'type' => 'string', 453 ), 454 ), 455 'required' => array( 'my_required_prop' ), 456 ); 457 458 $valid = rest_validate_value_from_schema( $data, $schema ); 459 460 if ( $expected ) { 461 $this->assertTrue( $valid ); 462 } else { 463 $this->assertWPError( $valid ); 464 } 465 } 466 467 public function data_required_property() { 468 return array( 469 array( 470 array( 471 'my_required_prop' => 'test', 472 'my_prop' => 'test', 473 ), 474 true, 475 ), 476 array( array( 'my_prop' => 'test' ), false ), 477 array( array(), false ), 478 ); 479 } 480 481 /** 482 * @ticket 48818 483 * @dataProvider data_required_nested_property 484 */ 485 public function test_nested_property_is_required( $data, $expected ) { 486 $schema = array( 487 'type' => 'object', 488 'properties' => array( 489 'my_object' => array( 490 'type' => 'object', 491 'properties' => array( 492 'my_nested_prop' => array( 493 'type' => 'string', 494 ), 495 'my_required_nested_prop' => array( 496 'type' => 'string', 497 'required' => true, 498 ), 499 ), 500 ), 501 ), 502 ); 503 504 $valid = rest_validate_value_from_schema( $data, $schema ); 505 506 if ( $expected ) { 507 $this->assertTrue( $valid ); 508 } else { 509 $this->assertWPError( $valid ); 510 } 511 } 512 513 /** 514 * @ticket 48818 515 * @dataProvider data_required_nested_property 516 */ 517 public function test_nested_property_is_required_v4( $data, $expected ) { 518 $schema = array( 519 'type' => 'object', 520 'properties' => array( 521 'my_object' => array( 522 'type' => 'object', 523 'properties' => array( 524 'my_nested_prop' => array( 525 'type' => 'string', 526 ), 527 'my_required_nested_prop' => array( 528 'type' => 'string', 529 ), 530 ), 531 'required' => array( 'my_required_nested_prop' ), 532 ), 533 ), 534 ); 535 536 $valid = rest_validate_value_from_schema( $data, $schema ); 537 538 if ( $expected ) { 539 $this->assertTrue( $valid ); 540 } else { 541 $this->assertWPError( $valid ); 542 } 543 } 544 545 public function data_required_nested_property() { 546 return array( 547 array( 548 array( 549 'my_object' => array( 550 'my_required_nested_prop' => 'test', 551 'my_nested_prop' => 'test', 552 ), 553 ), 554 true, 555 ), 556 array( 557 array( 558 'my_object' => array( 559 'my_nested_prop' => 'test', 560 ), 561 ), 562 false, 563 ), 564 array( 565 array(), 566 true, 567 ), 568 ); 569 } 570 571 /** 572 * @ticket 48818 573 * @dataProvider data_required_deeply_nested_property 574 */ 575 public function test_deeply_nested_v3_required_property( $value, $expected ) { 576 $schema = array( 577 'type' => 'object', 578 'properties' => array( 579 'propA' => array( 580 'type' => 'object', 581 'required' => true, 582 'properties' => array( 583 'propB' => array( 584 'type' => 'object', 585 'required' => true, 586 'properties' => array( 587 'propC' => array( 588 'type' => 'string', 589 'required' => true, 590 ), 591 'propD' => array( 592 'type' => 'string', 593 ), 594 ), 595 ), 596 ), 597 ), 598 ), 599 ); 600 601 $valid = rest_validate_value_from_schema( $value, $schema ); 602 603 if ( $expected ) { 604 $this->assertTrue( $valid ); 605 } else { 606 $this->assertWPError( $valid ); 607 } 608 } 609 610 /** 611 * @ticket 48818 612 * @dataProvider data_required_deeply_nested_property 613 */ 614 public function test_deeply_nested_v4_required_property( $value, $expected ) { 615 $schema = array( 616 'type' => 'object', 617 'required' => array( 'propA' ), 618 'properties' => array( 619 'propA' => array( 620 'type' => 'object', 621 'required' => array( 'propB' ), 622 'properties' => array( 623 'propB' => array( 624 'type' => 'object', 625 'required' => array( 'propC' ), 626 'properties' => array( 627 'propC' => array( 628 'type' => 'string', 629 ), 630 'propD' => array( 631 'type' => 'string', 632 ), 633 ), 634 ), 635 ), 636 ), 637 ), 638 ); 639 640 $valid = rest_validate_value_from_schema( $value, $schema ); 641 642 if ( $expected ) { 643 $this->assertTrue( $valid ); 644 } else { 645 $this->assertWPError( $valid ); 646 } 647 } 648 649 /** 650 * @ticket 48818 651 * @dataProvider data_required_deeply_nested_property 652 */ 653 public function test_deeply_nested_mixed_version_required_property( $value, $expected ) { 654 $schema = array( 655 'type' => 'object', 656 'required' => array( 'propA' ), 657 'properties' => array( 658 'propA' => array( 659 'type' => 'object', 660 'required' => array( 'propB' ), 661 'properties' => array( 662 'propB' => array( 663 'type' => 'object', 664 'properties' => array( 665 'propC' => array( 666 'type' => 'string', 667 'required' => true, 668 ), 669 'propD' => array( 670 'type' => 'string', 671 ), 672 ), 673 ), 674 ), 675 ), 676 ), 677 ); 678 679 $valid = rest_validate_value_from_schema( $value, $schema ); 680 681 if ( $expected ) { 682 $this->assertTrue( $valid ); 683 } else { 684 $this->assertWPError( $valid ); 685 } 686 } 687 688 public function data_required_deeply_nested_property() { 689 return array( 690 array( 691 array(), 692 false, 693 ), 694 array( 695 array( 696 'propA' => array(), 697 ), 698 false, 699 ), 700 array( 701 array( 702 'propA' => array( 703 'propB' => array(), 704 ), 705 ), 706 false, 707 ), 708 array( 709 array( 710 'propA' => array( 711 'propB' => array( 712 'propD' => 'd', 713 ), 714 ), 715 ), 716 false, 717 ), 718 array( 719 array( 720 'propA' => array( 721 'propB' => array( 722 'propC' => 'c', 723 ), 724 ), 725 ), 726 true, 727 ), 728 ); 729 } 412 730 }
Note: See TracChangeset
for help on using the changeset viewer.