Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,10 @@ public function get_public_item_schema() {

$schema = $this->get_item_schema();

foreach ( $schema['properties'] as &$property ) {
unset( $property['arg_options'] );
if ( ! empty( $schema['properties'] ) ) {
foreach ( $schema['properties'] as &$property ) {
unset( $property['arg_options'] );
}
}

return $schema;
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ function wp_tests_options( $value ) {
require __DIR__ . '/utils.php';
require __DIR__ . '/spy-rest-server.php';
require __DIR__ . '/class-wp-rest-test-search-handler.php';
require __DIR__ . '/class-wp-rest-test-configurable-controller.php';
require __DIR__ . '/class-wp-fake-block-type.php';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Unit tests covering WP_REST_Controller functionality using a flexible schema.
*
* @package WordPress
* @subpackage REST API
* @since 5.4.0
*/

/**
* WP_REST_Test_Configurable_Controller class.
*
* @group restapi
*
* @since 5.4.0
*/
class WP_REST_Test_Configurable_Controller extends WP_REST_Controller {

/**
* Test schema.
*
* @since 5.4.0
*
* @var array $test_schema
*/
protected $test_schema;

/**
* Class constructor.
*
* @since 5.4.0
*
* @param array $test_schema Schema for use in testing.
*/
public function __construct( $test_schema ) {
$this->test_schema = $test_schema;
}

/**
* Provides the test schema.
*
* @since 5.4.0
*
* @return array Test schema.
*/
public function get_test_schema() {
return $this->test_schema;
}

/**
* Get the item's schema, conforming to JSON Schema.
*
* @since 5.4.0
*
* @return array
*/
public function get_item_schema() {
return $this->add_additional_fields_schema( $this->get_test_schema() );
}
}
33 changes: 33 additions & 0 deletions tests/phpunit/tests/rest-api/rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,39 @@ public function test_filtering_fields_for_response_by_context_returns_fields_wit
$this->assertGreaterThan( 0, $listener->get_call_count( $method ) );
}

/**
* @ticket 48785
*/
public function test_get_public_item_schema_with_properties() {
$schema = ( new WP_REST_Test_Controller() )->get_public_item_schema();

// Double-check that the public item schema set in WP_REST_Test_Controller still has properties.
$this->assertArrayHasKey( 'properties', $schema );

// But arg_options should be removed.
$this->assertArrayNotHasKey( 'arg_options', $schema['properties']['someargoptions'] );
}

/**
* @ticket 48785
*/
public function test_get_public_item_schema_no_properties() {
$controller = new WP_REST_Test_Configurable_Controller(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'foo',
'type' => 'string',
'description' => 'This is my magical endpoint that just returns a string.',
)
);

// Initial check that the test class is working as expected.
$this->assertArrayNotHasKey( 'properties', $controller->get_public_item_schema() );

// Test that the schema lacking 'properties' is returned as expected.
$this->assertEqualSetsWithIndex( $controller->get_public_item_schema(), $controller->get_test_schema() );
}

public function test_add_additional_fields_to_object_respects_fields_param() {
$controller = new WP_REST_Test_Controller();
$request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
Expand Down