WordPress.org

Make WordPress Core

Ticket #48785: 48785.diff

File 48785.diff, 2.7 KB (added by johnwatkins0, 6 months ago)

Includes the previously attached change with unit tests covering the change as well as get_public_item_schema generally

  • src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
    index dd40d93d2b..b32f012542 100644
    a b abstract class WP_REST_Controller { 
    344344
    345345                $schema = $this->get_item_schema();
    346346
    347                 foreach ( $schema['properties'] as &$property ) {
    348                         unset( $property['arg_options'] );
     347                if ( ! empty( $schema['properties'] ) ) {
     348                        foreach ( $schema['properties'] as &$property ) {
     349                                unset( $property['arg_options'] );
     350                        }
    349351                }
    350352
    351353                return $schema;
  • tests/phpunit/tests/rest-api/rest-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-controller.php b/tests/phpunit/tests/rest-api/rest-controller.php
    index 91e1d0c5b7..226855d301 100644
    a b class WP_Test_REST_Controller extends WP_Test_REST_TestCase { 
    343343                $this->assertGreaterThan( 0, $listener->get_call_count( $method ) );
    344344        }
    345345
     346        public function test_get_public_item_schema() {
     347                global $test_get_public_item_schema_test_schema;
     348
     349                $test_get_public_item_schema_test_schema = [
     350                        '$schema'     => 'http://json-schema.org/draft-04/schema#',
     351                        'title'       => 'foo',
     352                        'type'        => 'string',
     353                        'description' => __( 'This is my magical endpoint that just returns a string.' ),
     354                ];
     355
     356                $controller = new class extends WP_REST_Test_Controller {
     357                        public function get_item_schema() {
     358                                global $test_get_public_item_schema_test_schema;
     359
     360                                return $this->add_additional_fields_schema( $test_get_public_item_schema_test_schema );
     361                        }
     362                };
     363
     364                // Initial check that the test class is working as expected.
     365                $this->assertFalse( array_key_exists( 'properties', $controller->get_public_item_schema() ) );
     366
     367                // Test that the schema lacking 'properties' is returned as expected.
     368                $this->assertEquals( $controller->get_public_item_schema(), $test_get_public_item_schema_test_schema );
     369
     370                // Double that the schema set in WP_REST_Test_Controller still has properties.
     371                $schema = ( new WP_REST_Test_Controller() )->get_public_item_schema();
     372                $this->assertTrue( array_key_exists( 'properties', $schema ) );
     373
     374                // But arg_options should be removed.
     375                $this->assertFalse( array_key_exists( 'arg_options', $schema['properties']['someargoptions'] ) );
     376
     377                unset( $GLOBALS['test_get_public_item_schema_test_schema'] );
     378        }
     379
    346380        public function test_add_additional_fields_to_object_respects_fields_param() {
    347381                $controller = new WP_REST_Test_Controller();
    348382                $request    = new WP_REST_Request( 'GET', '/wp/v2/testroute' );