From 43aea55af4528b9eb13573446f74dddbe398e0ce Mon Sep 17 00:00:00 2001 From: rvpatel Date: Thu, 22 Oct 2026 01:14:32 +0530 Subject: [PATCH] Tests: Add direct tests for `_wp_json_schema_matches_type()`. The helper added in [64592] was only exercised indirectly through `wp_prepare_json_value_for_client()`. Add a data provider covering explicit and inherited types, anyOf/oneOf branches, nested combining keywords, and the case where the type and a valid value are found in different branches. Follow-up to [64592]. See #66267. --- tests/phpunit/tests/json-schema.php | 173 ++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) diff --git a/tests/phpunit/tests/json-schema.php b/tests/phpunit/tests/json-schema.php index 07f12d8bdee09..4b1fdd7ac3b2e 100644 --- a/tests/phpunit/tests/json-schema.php +++ b/tests/phpunit/tests/json-schema.php @@ -983,4 +983,177 @@ public function test_wp_prepare_json_value_for_client_uses_pattern_properties() $this->assertSame( '{"item-1":{}}', wp_json_encode( $prepared ) ); } + + /** + * Data provider for test_wp_json_schema_matches_type(). + * + * @return array, 2: string, 3: bool}> + */ + public static function data_wp_json_schema_matches_type() { + return array( + 'type allows the requested type' => array( + array(), + array( 'type' => array( 'object', 'array' ) ), + 'array', + true, + ), + 'type does not allow the requested type' => array( + array(), + array( 'type' => 'object' ), + 'array', + false, + ), + 'value fails validation as the requested type' => array( + array(), + array( + 'type' => 'array', + 'minItems' => 1, + ), + 'array', + false, + ), + 'schema without type or combining keywords' => array( + array(), + array( 'minItems' => 0 ), + 'array', + false, + ), + 'anyOf with a matching branch' => array( + array(), + array( + 'anyOf' => array( + array( 'type' => 'object' ), + array( 'type' => 'array' ), + ), + ), + 'array', + true, + ), + 'anyOf with the type and a valid value in different branches' => array( + array(), + array( + 'anyOf' => array( + array( + 'type' => 'array', + 'minItems' => 1, + ), + array( 'type' => 'object' ), + ), + ), + 'array', + false, + ), + 'oneOf without a branch of the requested type' => array( + array(), + array( + 'oneOf' => array( + array( 'type' => 'object' ), + array( 'type' => 'string' ), + ), + ), + 'array', + false, + ), + 'oneOf with several matching branches' => array( + array(), + array( + 'oneOf' => array( + array( 'type' => 'array' ), + array( + 'type' => 'array', + 'maxItems' => 1, + ), + ), + ), + 'array', + false, + ), + 'anyOf and oneOf must both match' => array( + array(), + array( + 'anyOf' => array( + array( 'type' => 'array' ), + ), + 'oneOf' => array( + array( 'type' => 'object' ), + ), + ), + 'array', + false, + ), + 'untyped branch inherits an allowing parent type' => array( + array(), + array( + 'type' => array( 'object', 'array' ), + 'anyOf' => array( + array( 'maxItems' => 1 ), + ), + ), + 'array', + true, + ), + 'untyped branch inherits a rejecting parent type' => array( + array(), + array( + 'type' => array( 'object', 'string' ), + 'anyOf' => array( + array( 'maxProperties' => 1 ), + ), + ), + 'array', + false, + ), + 'only non-array branches' => array( + array(), + array( + 'anyOf' => array( 'array' ), + ), + 'array', + false, + ), + 'nested combining keywords' => array( + array( 'a' ), + array( + 'oneOf' => array( + array( + 'anyOf' => array( + array( + 'type' => 'array', + 'minItems' => 2, + ), + array( + 'type' => 'array', + 'items' => array( 'type' => 'string' ), + ), + ), + ), + array( 'type' => 'string' ), + ), + ), + 'array', + true, + ), + 'object requested for an empty array' => array( + array(), + array( 'type' => array( 'object', 'array' ) ), + 'object', + true, + ), + ); + } + + /** + * Tests whether a value matches a schema through a given type. + * + * @ticket 66267 + * @dataProvider data_wp_json_schema_matches_type + * + * @param mixed $value The value to validate. + * @param array $schema The schema to inspect. + * @param string $type The type to match. + * @param bool $expected Whether the value is expected to match. + */ + public function test_wp_json_schema_matches_type( $value, $schema, $type, $expected ) { + $this->assertSame( $expected, _wp_json_schema_matches_type( $value, $schema, $type ) ); + } }