Skip to content

Commit 67850a9

Browse files
committed
Tests: Expand JSON Schema value preparation coverage
Test nested `oneOf` schemas with multiple matching branches and combined `anyOf`/`oneOf` schemas through `wp_prepare_json_value_for_client()`, asserting the resulting JSON representation. Add three basic tests for `_wp_json_schema_matches_type()` while retaining the existing coverage for nesting and type inheritance. Follow-up to [64592]. Props ravipatel, gziolo. See #66267. git-svn-id: https://develop.svn.wordpress.org/trunk@64728 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f72b766 commit 67850a9

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

‎tests/phpunit/tests/json-schema.php‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,43 @@ public static function data_wp_prepare_json_value_for_client() {
816816
),
817817
'[{}]',
818818
),
819+
'array branch whose oneOf matches more than once' => array(
820+
array(),
821+
array(
822+
'type' => array( 'object', 'array' ),
823+
'anyOf' => array(
824+
array(
825+
'oneOf' => array(
826+
array( 'type' => 'array' ),
827+
array(
828+
'type' => 'array',
829+
'maxItems' => 5,
830+
),
831+
),
832+
),
833+
array( 'type' => 'object' ),
834+
),
835+
),
836+
'{}',
837+
),
838+
'branch whose anyOf allows an array but whose oneOf does not' => array(
839+
array(),
840+
array(
841+
'type' => array( 'object', 'array' ),
842+
'anyOf' => array(
843+
array(
844+
'anyOf' => array(
845+
array( 'type' => 'array' ),
846+
),
847+
'oneOf' => array(
848+
array( 'type' => 'object' ),
849+
),
850+
),
851+
array( 'type' => 'array' ),
852+
),
853+
),
854+
'[]',
855+
),
819856
);
820857
}
821858

@@ -983,4 +1020,50 @@ public function test_wp_prepare_json_value_for_client_uses_pattern_properties()
9831020

9841021
$this->assertSame( '{"item-1":{}}', wp_json_encode( $prepared ) );
9851022
}
1023+
1024+
/**
1025+
* Data provider for test_wp_json_schema_matches_type().
1026+
*
1027+
* @return array<string, array{0: mixed, 1: array<string, mixed>, 2: string, 3: bool}>
1028+
*/
1029+
public static function data_wp_json_schema_matches_type() {
1030+
return array(
1031+
'type allows the requested type' => array(
1032+
array(),
1033+
array( 'type' => array( 'object', 'array' ) ),
1034+
'array',
1035+
true,
1036+
),
1037+
'type does not allow the requested type' => array(
1038+
array(),
1039+
array( 'type' => 'object' ),
1040+
'array',
1041+
false,
1042+
),
1043+
'value fails validation as the requested type' => array(
1044+
array(),
1045+
array(
1046+
'type' => 'array',
1047+
'minItems' => 1,
1048+
),
1049+
'array',
1050+
false,
1051+
),
1052+
);
1053+
}
1054+
1055+
/**
1056+
* Tests whether a value matches a schema through a given type.
1057+
*
1058+
* @ticket 66267
1059+
* @dataProvider data_wp_json_schema_matches_type
1060+
*
1061+
* @param mixed $value The value to validate.
1062+
* @param array<string, mixed> $schema The schema to inspect.
1063+
* @param string $type The type to match.
1064+
* @param bool $expected Whether the value is expected to match.
1065+
*/
1066+
public function test_wp_json_schema_matches_type( $value, $schema, $type, $expected ) {
1067+
$this->assertSame( $expected, _wp_json_schema_matches_type( $value, $schema, $type ) );
1068+
}
9861069
}

0 commit comments

Comments
 (0)