Ticket #49648: 49648.1.diff
File 49648.1.diff, 5.9 KB (added by , 11 months ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php index b32f012542..355a12b188 100644
abstract class WP_REST_Controller { 449 449 continue; 450 450 } 451 451 452 if ( ! in_array( $field_name, $requested_fields, true) ) {452 if ( ! rest_is_field_included( $field_name, $requested_fields ) ) { 453 453 continue; 454 454 } 455 455 -
tests/phpunit/tests/rest-api.php
diff --git tests/phpunit/tests/rest-api.php tests/phpunit/tests/rest-api.php index c0eea9804f..64f9065fdd 100644
class Tests_REST_API extends WP_UnitTestCase { 560 560 ); 561 561 } 562 562 563 /** 564 * Ensure inclusion of deeply nested fields may be controlled with request['_fields']. 565 * 566 * @ticket 49648 567 */ 568 public function test_rest_filter_response_fields_deeply_nested_field_filter() { 569 $response = new WP_REST_Response(); 570 571 $response->set_data( 572 array( 573 'field' => array( 574 'a' => array( 575 'i' => 'value i', 576 'ii' => 'value ii', 577 ), 578 'b' => array( 579 'iii' => 'value iii', 580 'iv' => 'value iv', 581 ), 582 ), 583 ) 584 ); 585 $request = array( 586 '_fields' => 'field.a.i,field.b.iv', 587 ); 588 589 $response = rest_filter_response_fields( $response, null, $request ); 590 $this->assertEquals( 591 array( 592 'field' => array( 593 'a' => array( 594 'i' => 'value i', 595 ), 596 'b' => array( 597 'iv' => 'value iv', 598 ), 599 ), 600 ), 601 $response->get_data() 602 ); 603 } 604 563 605 /** 564 606 * Ensure that specifying a single top-level key in _fields includes that field and all children. 565 607 * -
tests/phpunit/tests/rest-api/rest-controller.php
diff --git tests/phpunit/tests/rest-api/rest-controller.php tests/phpunit/tests/rest-api/rest-controller.php index 6de5cf5482..ef39112c00 100644
class WP_Test_REST_Controller extends WP_Test_REST_TestCase { 288 288 $controller->prepare_item_for_response( array(), $request ); 289 289 290 290 $this->assertGreaterThan( 0, $listener->get_call_count( $method ) ); 291 292 global $wp_rest_additional_fields; 293 $wp_rest_additional_fields = array(); 291 294 } 292 295 293 296 public function test_filtering_fields_for_response_by_context_returns_fields_with_no_context() { … … class WP_Test_REST_Controller extends WP_Test_REST_TestCase { 316 319 $controller->prepare_item_for_response( array(), $request ); 317 320 318 321 $this->assertGreaterThan( 0, $listener->get_call_count( $method ) ); 322 323 global $wp_rest_additional_fields; 324 $wp_rest_additional_fields = array(); 319 325 } 320 326 321 327 public function test_filtering_fields_for_response_by_context_returns_fields_with_no_schema() { … … class WP_Test_REST_Controller extends WP_Test_REST_TestCase { 341 347 $controller->prepare_item_for_response( array(), $request ); 342 348 343 349 $this->assertGreaterThan( 0, $listener->get_call_count( $method ) ); 350 351 global $wp_rest_additional_fields; 352 $wp_rest_additional_fields = array(); 344 353 } 345 354 346 355 /** … … class WP_Test_REST_Controller extends WP_Test_REST_TestCase { 415 424 $controller->prepare_item_for_response( $item, $request ); 416 425 417 426 $this->assertTrue( $listener->get_call_count( $method ) > $first_call_count ); 427 428 global $wp_rest_additional_fields; 429 $wp_rest_additional_fields = array(); 430 } 431 432 /** 433 * @dataProvider data_filter_registered_rest_fields 434 * @ticket 49648 435 */ 436 public function test_filter_nested_registered_rest_fields( $filter, $expected ) { 437 $controller = new WP_REST_Test_Controller(); 438 439 register_rest_field( 440 'type', 441 'field', 442 array( 443 'schema' => array( 444 'type' => 'object', 445 'description' => 'A complex object', 446 'context' => array( 'view', 'edit' ), 447 'properties' => array( 448 'a' => array( 449 'i' => 'string', 450 'ii' => 'string', 451 ), 452 'b' => array( 453 'iii' => 'string', 454 'iv' => 'string', 455 ), 456 ), 457 ), 458 'get_callback' => array( $this, 'register_nested_rest_field_get_callback' ), 459 ), 460 ); 461 462 $request = new WP_REST_Request( 'GET', '/wp/v2/testroute' ); 463 $request->set_param( '_fields', $filter ); 464 465 $response = $controller->prepare_item_for_response( array(), $request ); 466 $response = rest_filter_response_fields( $response, rest_get_server(), $request ); 467 468 $this->assertEquals( $expected, $response->get_data() ); 469 470 global $wp_rest_additional_fields; 471 $wp_rest_additional_fields = array(); 472 } 473 474 public function register_nested_rest_field_get_callback() { 475 return array( 476 'a' => array( 477 'i' => 'value i', 478 'ii' => 'value ii', 479 ), 480 'b' => array( 481 'iii' => 'value iii', 482 'iv' => 'value iv', 483 ), 484 ); 485 } 486 487 public function data_filter_nested_registered_rest_fields() { 488 return array( 489 array( 490 'field', 491 array( 492 'field' => array( 493 'a' => array( 494 'i' => 'value i', 495 'ii' => 'value ii', 496 ), 497 'b' => array( 498 'iii' => 'value iii', 499 'iv' => 'value iv', 500 ), 501 ), 502 ), 503 ), 504 array( 505 'field.a', 506 array( 507 'field' => array( 508 'a' => array( 509 'i' => 'value i', 510 'ii' => 'value ii', 511 ), 512 ), 513 ), 514 ), 515 array( 516 'field.b', 517 array( 518 'field' => array( 519 'b' => array( 520 'iii' => 'value iii', 521 'iv' => 'value iv', 522 ), 523 ), 524 ), 525 ), 526 array( 527 'field.a.i,field.b.iv', 528 array( 529 'field' => array( 530 'a' => array( 531 'i' => 'value i', 532 ), 533 'b' => array( 534 'iv' => 'value iv', 535 ), 536 ), 537 ), 538 ), 539 array( 540 'field.a,field.b.iii', 541 array( 542 'field' => array( 543 'a' => array( 544 'i' => 'value i', 545 'ii' => 'value ii', 546 ), 547 'b' => array( 548 'iii' => 'value iii', 549 ), 550 ), 551 ), 552 ), 553 ); 418 554 } 419 555 }