WordPress.org

Make WordPress Core

Ticket #48785: 48785.4.diff

File 48785.4.diff, 3.7 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..b605b92735 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        /**
     347         * Tests get_public_item_schema for an array-type schema.
     348         *
     349         * @ticket 48785
     350         */
     351        public function test_get_public_item_schema_with_properties() {
     352                $schema = ( new WP_REST_Test_Controller() )->get_public_item_schema();
     353
     354                // Double-check that the public item schema set in WP_REST_Test_Controller still has properties.
     355                $this->assertTrue( array_key_exists( 'properties', $schema ) );
     356
     357                // But arg_options should be removed.
     358                $this->assertFalse( array_key_exists( 'arg_options', $schema['properties']['someargoptions'] ) );
     359        }
     360
     361        /**
     362         * Tests get_public_item_schema for a string-type schema.
     363         *
     364         * @ticket 48785
     365         */
     366        public function test_get_public_item_schema_no_properties() {
     367                $controller = new WP_REST_Test_Configurable_Controller(
     368                        array(
     369                                '$schema'     => 'http://json-schema.org/draft-04/schema#',
     370                                'title'       => 'foo',
     371                                'type'        => 'string',
     372                                'description' => __( 'This is my magical endpoint that just returns a string.' ),
     373                        )
     374                );
     375
     376                // Initial check that the test class is working as expected.
     377                $this->assertFalse( array_key_exists( 'properties', $controller->get_public_item_schema() ) );
     378
     379                // Test that the schema lacking 'properties' is returned as expected.
     380                $this->assertEquals( $controller->get_public_item_schema(), $controller->get_test_schema() );
     381        }
     382
    346383        public function test_add_additional_fields_to_object_respects_fields_param() {
    347384                $controller = new WP_REST_Test_Controller();
    348385                $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}