Skip to content

Commit

Permalink
Add further test coverage for the _embedded element.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion committed Aug 21, 2024
1 parent d9fb3ee commit 00dd91c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 15 deletions.
34 changes: 25 additions & 9 deletions tests/bin/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
42 changes: 36 additions & 6 deletions tests/output/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph -->',
'post_status' => 'publish',
] );

wp_insert_post( [
'post_type' => 'wp_navigation',
'post_title' => 'Navigation',
'post_title' => 'Navigation Title',
'post_content' => '<!-- wp:navigation-link {"label":"Title","type":"page","id":123,"url":"/title/","kind":"post-type"} /-->',
'post_status' => 'publish',
] );
Expand Down

0 comments on commit 00dd91c

Please sign in to comment.