WordPress.org

Make WordPress Core

Ticket #48785: 48785.3.diff

File 48785.3.diff, 3.4 KB (added by johnwatkins0, 6 months ago)
  • 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..2ac4df6e68 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                $controller = new WP_REST_Test_Configurable_Controller(
     348                        array(
     349                                '$schema'     => 'http://json-schema.org/draft-04/schema#',
     350                                'title'       => 'foo',
     351                                'type'        => 'string',
     352                                'description' => __( 'This is my magical endpoint that just returns a string.' ),
     353                        )
     354                );
     355
     356                // Initial check that the test class is working as expected.
     357                $this->assertFalse( array_key_exists( 'properties', $controller->get_public_item_schema() ) );
     358
     359                // Test that the schema lacking 'properties' is returned as expected.
     360                $this->assertEquals( $controller->get_public_item_schema(), $controller->get_test_schema() );
     361
     362                // Double-checkthat the schema set in WP_REST_Test_Controller still has properties.
     363                $schema = ( new WP_REST_Test_Controller() )->get_public_item_schema();
     364                $this->assertTrue( array_key_exists( 'properties', $schema ) );
     365
     366                // But arg_options should be removed.
     367                $this->assertFalse( array_key_exists( 'arg_options', $schema['properties']['someargoptions'] ) );
     368        }
     369
    346370        public function test_add_additional_fields_to_object_respects_fields_param() {
    347371                $controller = new WP_REST_Test_Controller();
    348372                $request    = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
  • new file tests/phpunit/tests/rest-api/rest-test-configurable-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-test-configurable-controller.php b/tests/phpunit/tests/rest-api/rest-test-configurable-controller.php
    new file mode 100644
    index 0000000000..e1c3f869c7
    - +  
     1<?php
     2/**
     3 * Unit tests covering WP_REST_Controller functionality using a flexible schema.
     4 *
     5 * @package WordPress
     6 * @subpackage REST API
     7 */
     8
     9/**
     10 * @group restapi
     11 */
     12class WP_REST_Test_Configurable_Controller extends WP_REST_Controller {
     13        /**
     14         * Test schema.
     15         */
     16        protected $test_schema;
     17
     18        /**
     19         * Class constructor.
     20         *
     21         * @param array $schema Schema for use in testing.
     22         */
     23        public function __construct( $test_schema ) {
     24                $this->test_schema = $test_schema;
     25        }
     26
     27        /**
     28         * Provides the test schema.
     29         *
     30         * @return array Test schema.
     31         */
     32        public function get_test_schema() {
     33                return $this->test_schema;
     34        }
     35
     36        /**
     37         * Get the item's schema, conforming to JSON Schema.
     38         *
     39         * @return array
     40         */
     41        public function get_item_schema() {
     42                return $this->add_additional_fields_schema( $this->get_test_schema() );
     43        }
     44
     45}