WordPress.org

Make WordPress Core

Ticket #48785: 48785.2.diff

File 48785.2.diff, 3.1 KB (added by johnwatkins0, 6 months ago)

Made unit test PHP5-compatible

  • 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..9f70e21455 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_String_Schema_Controller();
     348
     349                // Initial check that the test class is working as expected.
     350                $this->assertFalse( array_key_exists( 'properties', $controller->get_public_item_schema() ) );
     351
     352                // Test that the schema lacking 'properties' is returned as expected.
     353                $this->assertEquals( $controller->get_public_item_schema(), $controller::get_test_schema() );
     354
     355                // Double that the schema set in WP_REST_Test_Controller still has properties.
     356                $schema = ( new WP_REST_Test_Controller() )->get_public_item_schema();
     357                $this->assertTrue( array_key_exists( 'properties', $schema ) );
     358
     359                // But arg_options should be removed.
     360                $this->assertFalse( array_key_exists( 'arg_options', $schema['properties']['someargoptions'] ) );
     361        }
     362
    346363        public function test_add_additional_fields_to_object_respects_fields_param() {
    347364                $controller = new WP_REST_Test_Controller();
    348365                $request    = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
  • new file tests/phpunit/tests/rest-api/rest-test-string-schema-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-test-string-schema-controller.php b/tests/phpunit/tests/rest-api/rest-test-string-schema-controller.php
    new file mode 100644
    index 0000000000..d550f3a069
    - +  
     1<?php
     2/**
     3 * Unit tests covering WP_REST_Controller functionality when the schema does not have properties.
     4 *
     5 * @package WordPress
     6 * @subpackage REST API
     7 */
     8
     9/**
     10 * @group restapi
     11 */
     12class WP_REST_String_Schema_Controller extends WP_REST_Controller {
     13        public static function get_test_schema() {
     14                return array(
     15                        '$schema'     => 'http://json-schema.org/draft-04/schema#',
     16                        'title'       => 'foo',
     17                        'type'        => 'string',
     18                        'description' => __( 'This is my magical endpoint that just returns a string.' ),
     19                );
     20        }
     21
     22        /**
     23         * Get the item's schema, conforming to JSON Schema.
     24         *
     25         * @return array
     26         */
     27        public function get_item_schema() {
     28                return $this->add_additional_fields_schema( static::get_test_schema() );
     29        }
     30
     31}