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
349 changes: 333 additions & 16 deletions src/wp-includes/rest-api.php

Large diffs are not rendered by default.

234 changes: 219 additions & 15 deletions tests/phpunit/tests/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ public function test_register_route_with_missing_permission_callback_multiple_wr

public function _dp_rest_filter_response_by_context() {
return array(
'default' => array(
'default' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand All @@ -1106,7 +1106,7 @@ public function _dp_rest_filter_response_by_context() {
),
array( 'first' => 'a' ),
),
'keeps missing context' => array(
'keeps missing context' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand All @@ -1129,7 +1129,7 @@ public function _dp_rest_filter_response_by_context() {
'second' => 'b',
),
),
'removes empty context' => array(
'removes empty context' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand All @@ -1150,7 +1150,7 @@ public function _dp_rest_filter_response_by_context() {
),
array( 'first' => 'a' ),
),
'nested properties' => array(
'nested properties' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand Down Expand Up @@ -1179,7 +1179,7 @@ public function _dp_rest_filter_response_by_context() {
),
array( 'parent' => array( 'child' => 'hi' ) ),
),
'grand child properties' => array(
'grand child properties' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand Down Expand Up @@ -1215,7 +1215,7 @@ public function _dp_rest_filter_response_by_context() {
),
array( 'parent' => array( 'child' => array( 'grand' => 'hi' ) ) ),
),
'array' => array(
'array' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand Down Expand Up @@ -1250,7 +1250,7 @@ public function _dp_rest_filter_response_by_context() {
),
array( 'arr' => array( array( 'visible' => 'hi' ) ) ),
),
'additional properties' => array(
'additional properties' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand Down Expand Up @@ -1284,7 +1284,7 @@ public function _dp_rest_filter_response_by_context() {
),
array( 'additional' => array( 'a' => '1' ) ),
),
'pattern properties' => array(
'pattern properties' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand Down Expand Up @@ -1320,7 +1320,7 @@ public function _dp_rest_filter_response_by_context() {
'0' => '3',
),
),
'multiple types object' => array(
'multiple types object' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand Down Expand Up @@ -1349,7 +1349,7 @@ public function _dp_rest_filter_response_by_context() {
),
array( 'multi' => array( 'a' => '1' ) ),
),
'multiple types array' => array(
'multiple types array' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand Down Expand Up @@ -1384,7 +1384,7 @@ public function _dp_rest_filter_response_by_context() {
),
array( 'multi' => array( array( 'visible' => '1' ) ) ),
),
'does not traverse missing context' => array(
'does not traverse missing context' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand Down Expand Up @@ -1427,7 +1427,7 @@ public function _dp_rest_filter_response_by_context() {
),
),
),
'object with no matching properties' => array(
'object with no matching properties' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand All @@ -1448,7 +1448,7 @@ public function _dp_rest_filter_response_by_context() {
),
array(),
),
'array whose type does not match' => array(
'array whose type does not match' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
Expand All @@ -1468,7 +1468,7 @@ public function _dp_rest_filter_response_by_context() {
),
array( 'arr' => array() ),
),
'array and object type passed object' => array(
'array and object type passed object' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => array( 'array', 'object' ),
Expand Down Expand Up @@ -1506,7 +1506,7 @@ public function _dp_rest_filter_response_by_context() {
'b' => 'bar',
),
),
'array and object type passed array' => array(
'array and object type passed array' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => array( 'array', 'object' ),
Expand Down Expand Up @@ -1547,6 +1547,210 @@ public function _dp_rest_filter_response_by_context() {
),
array(),
),
'anyOf applies the correct schema' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'anyOf' => array(
array(
'properties' => array(
'a' => array(
'type' => 'string',
'context' => array( 'view' ),
),
'b' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
array(
'properties' => array(
'a' => array(
'type' => 'integer',
'context' => array( 'edit' ),
),
'b' => array(
'type' => 'integer',
'context' => array( 'view' ),
),
),
),
),
),
array(
'a' => 1,
'b' => 2,
),
array(
'b' => 2,
),
),
'anyOf is ignored if no valid schema is found' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'anyOf' => array(
array(
'properties' => array(
'a' => array(
'type' => 'string',
'context' => array( 'view' ),
),
'b' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
array(
'properties' => array(
'a' => array(
'type' => 'integer',
'context' => array( 'edit' ),
),
'b' => array(
'type' => 'integer',
'context' => array( 'view' ),
),
),
),
),
),
array(
'a' => true,
'b' => false,
),
array(
'a' => true,
'b' => false,
),
),
'oneOf applies the correct schema' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'oneOf' => array(
array(
'properties' => array(
'a' => array(
'type' => 'string',
'context' => array( 'view' ),
),
'b' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
array(
'properties' => array(
'a' => array(
'type' => 'integer',
'context' => array( 'edit' ),
),
'b' => array(
'type' => 'integer',
'context' => array( 'view' ),
),
),
),
),
),
array(
'a' => 1,
'b' => 2,
),
array(
'b' => 2,
),
),
'oneOf ignored if no valid schema was found' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'anyOf' => array(
array(
'properties' => array(
'a' => array(
'type' => 'string',
'context' => array( 'view' ),
),
'b' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
array(
'properties' => array(
'a' => array(
'type' => 'integer',
'context' => array( 'edit' ),
),
'b' => array(
'type' => 'integer',
'context' => array( 'view' ),
),
),
),
),
),
array(
'a' => true,
'b' => false,
),
array(
'a' => true,
'b' => false,
),
),
'oneOf combined with base' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'properties' => array(
'c' => array(
'type' => 'integer',
'context' => array( 'edit' ),
),
),
'oneOf' => array(
array(
'properties' => array(
'a' => array(
'type' => 'string',
'context' => array( 'view' ),
),
'b' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
array(
'properties' => array(
'a' => array(
'type' => 'integer',
'context' => array( 'edit' ),
),
'b' => array(
'type' => 'integer',
'context' => array( 'view' ),
),
),
),
),
),
array(
'a' => 1,
'b' => 2,
'c' => 3,
),
array(
'b' => 2,
),
),
);
}

Expand Down
Loading