From 00dd91c08a19ecd9a90b6bba1bd3d0b40840a123 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Wed, 21 Aug 2024 23:58:20 +0100 Subject: [PATCH] Add further test coverage for the `_embedded` element. --- tests/bin/test.sh | 34 +++++++++++++++++++++++++--------- tests/output/post.php | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/tests/bin/test.sh b/tests/bin/test.sh index 4eb0d72..261541b 100755 --- a/tests/bin/test.sh +++ b/tests/bin/test.sh @@ -21,12 +21,36 @@ function validate_schema() { function modify_schema() { local file="$1" local changes="$2" + local condition="$3" + + if [[ "$condition" != "" ]] + then + if [[ $(./node_modules/node-jq/bin/jq -e "$condition" "$file") == false ]] + then + return + fi + fi + ./node_modules/node-jq/bin/jq --tab "$changes" "$file" > tmp mv tmp "$file" } +function cleanup() { + for file in schemas/rest-api/*.json + do + if [[ "${IGNORE_FILES[*]}" =~ "${file}" ]] + then + continue + fi + modify_schema "$file" 'del(.unevaluatedProperties)' + modify_schema "$file" 'del(.properties._embedded.additionalProperties)' + done +} + IGNORE_FILES=("schemas/rest-api/error.json") +trap cleanup EXIT + for file in schemas/*.json do validate_schema "$file" @@ -39,18 +63,10 @@ do continue fi modify_schema "$file" '. + { "unevaluatedProperties": false }' + modify_schema "$file" '.properties._embedded += { "additionalProperties": false }' '.properties._embedded != null' done for file in schemas/rest-api/collections/*.json do validate_schema "$file" done - -for file in schemas/rest-api/*.json -do - if [[ "${IGNORE_FILES[*]}" =~ "${file}" ]] - then - continue - fi - modify_schema "$file" 'del(.unevaluatedProperties)' -done diff --git a/tests/output/post.php b/tests/output/post.php index 1281c4f..35a2281 100644 --- a/tests/output/post.php +++ b/tests/output/post.php @@ -2,28 +2,58 @@ namespace WPJsonSchemas; -wp_insert_post( [ +$parent_post = wp_insert_post( [ 'post_type' => 'post', - 'post_title' => 'Title', + 'post_title' => 'Post Title', 'post_status' => 'publish', +], true ); + +if ( is_wp_error( $parent_post ) ) { + throw new \Exception( $parent_post->get_error_message() ); +} + +$attachment_id = wp_insert_attachment( + [ + 'post_title' => 'I am an attachment with a parent', + ], + false, + $parent_post, + true, +); + +if ( is_wp_error( $attachment_id ) ) { + throw new \Exception( $attachment_id->get_error_message() ); +} + +$thumb = update_post_meta( $parent_post, '_thumbnail_id', $attachment_id ); + +if ( ! $thumb ) { + throw new \Exception( "Failed to set thumbnail for post {$parent_post}" ); +} + +$parent_page = wp_insert_post( [ + 'post_type' => 'page', + 'post_title' => 'Parent Title', + 'post_status' => 'draft', ] ); wp_insert_post( [ 'post_type' => 'page', - 'post_title' => 'Title', - 'post_status' => 'draft', + 'post_title' => 'Child Title', + 'post_status' => 'publish', + 'post_parent' => $parent_page, ] ); wp_insert_post( [ 'post_type' => 'wp_block', - 'post_title' => 'Title', + 'post_title' => 'Block Title', 'post_content' => '

Hello

', 'post_status' => 'publish', ] ); wp_insert_post( [ 'post_type' => 'wp_navigation', - 'post_title' => 'Navigation', + 'post_title' => 'Navigation Title', 'post_content' => '', 'post_status' => 'publish', ] );