WordPress.org

Make WordPress Core

Ticket #48785: 48785.5.diff

File 48785.5.diff, 3.9 KB (added by birgire, 6 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 dd40d93..b32f012 100644
    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 tests/phpunit/tests/rest-api/rest-controller.php tests/phpunit/tests/rest-api/rest-controller.php
    index 91e1d0c..89ec29e 100644
    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->assertArrayHasKey( 'properties', $schema );
     356
     357                // But arg_options should be removed.
     358                $this->assertArrayNotHasKey( '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->assertArrayNotHasKey( 'properties', $controller->get_public_item_schema() );
     378
     379                // Test that the schema lacking 'properties' is returned as expected.
     380                $this->assertEqualSetsWithIndex( $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 tests/phpunit/tests/rest-api/rest-test-configurable-controller.php tests/phpunit/tests/rest-api/rest-test-configurable-controller.php
    new file mode 100644
    index 0000000..372c5b8
    - +  
     1<?php
     2/**
     3 * Unit tests covering WP_REST_Controller functionality using a flexible schema.
     4 *
     5 * @package WordPress
     6 * @subpackage REST API
     7 * @since 5.4.0
     8 */
     9
     10/**
     11 * WP_REST_Test_Configurable_Controller class.
     12 *
     13 * @group restapi
     14 *
     15 * @since 5.4.0
     16 */
     17class WP_REST_Test_Configurable_Controller extends WP_REST_Controller {
     18
     19        /**
     20         * Test schema.
     21         *
     22         * @since 5.4.0
     23         *
     24         * @var array $test_schema
     25         */
     26        protected $test_schema;
     27
     28        /**
     29         * Class constructor.
     30         *
     31         * @since 5.4.0
     32         *
     33         * @param array $schema Schema for use in testing.
     34         */
     35        public function __construct( $test_schema ) {
     36                $this->test_schema = $test_schema;
     37        }
     38
     39        /**
     40         * Provides the test schema.
     41         *
     42         * @since 5.4.0
     43         *
     44         * @return array Test schema.
     45         */
     46        public function get_test_schema() {
     47                return $this->test_schema;
     48        }
     49
     50        /**
     51         * Get the item's schema, conforming to JSON Schema.
     52         *
     53         * @since 5.4.0
     54         *
     55         * @return array
     56         */
     57        public function get_item_schema() {
     58                return $this->add_additional_fields_schema( $this->get_test_schema() );
     59        }
     60
     61}