Created
June 15, 2016 15:21
-
-
Save nylen/246da68b6f309ff8392e0321e462bf12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Test WordPress.com REST API custom taxonomies on a Jetpack site. | |
error_reporting( E_ALL & ~E_NOTICE ); | |
$site_id = $argv[1]; | |
$oauth2_token = $argv[2]; | |
if ( ! $site_id || ! $oauth2_token ) { | |
print "Usage: php $argv[0] site_id oauth2_token\n"; | |
exit( 2 ); | |
} | |
function check( $condition, $message = null, $vars = null ) { | |
if ( is_array( $message ) ) { | |
$vars = $message; | |
$message = null; | |
} | |
// Don't try this at home | |
extract( $GLOBALS ); | |
if ( is_array( $vars ) ) { | |
extract( $vars ); | |
} | |
print "check( $condition )\n"; | |
// Or this | |
if ( ! eval( "return $condition;" ) ) { | |
print "Assertion failed: $condition\n"; | |
list( $expected, $actual ) = explode( ' === ', $condition ); | |
if ( ! empty( $actual ) ) { | |
$expected = eval( "return $expected;" ); | |
$actual = eval( "return $actual;" ); | |
print "Expected '$expected' but got '$actual'\n"; | |
} | |
if ( $message ) { | |
print "$message\n"; | |
} | |
exit( 1 ); | |
} | |
} | |
function do_api_request( $method, $path, $content = '' ) { | |
global $oauth2_token; | |
if ( is_array( $content ) ) { | |
$content = json_encode( $content ); | |
} else if ( false === $content ) { | |
$check_response_code = false; | |
$content = ''; | |
} else { | |
$check_response_code = true; | |
} | |
print "$method $path ...\n"; | |
$options = array( | |
'http' => array( | |
'ignore_errors' => true, | |
'method' => $method, | |
'header' => array( | |
'Authorization: Bearer ' . $oauth2_token | |
), | |
'content' => $content | |
) | |
); | |
$context = stream_context_create( $options ); | |
$response = file_get_contents( | |
"https://public-api.wordpress.com$path?http_envelope=true", | |
false, | |
$context | |
); | |
$response = json_decode( $response ); | |
if ( $check_response_code ) { | |
check( | |
'200 === $response->code', | |
json_encode( $response->body ), | |
compact( 'response' ) | |
); | |
} | |
return $response->body; | |
} | |
// Test for the presence of the 'book' CPT. | |
$post_types = do_api_request( | |
'GET', | |
"/rest/v1.1/sites/$site_id/post-types" | |
)->post_types; | |
$book_post_type = array_values( array_filter( $post_types, function( $post_type ) { | |
return ( 'book' === $post_type->name ); | |
} ) )[0]; | |
check( | |
'"book" === $book_post_type->name', | |
'"book" CPT is not present on this site. Install the plugin from ' | |
. 'https://cloudup.com/cIsFtBaQaGA and activate it.' | |
); | |
// Test for the presence of the genre taxonomy. | |
$taxonomies = do_api_request( | |
'GET', | |
"/rest/v1.1/sites/$site_id/post-types/book/taxonomies" | |
)->taxonomies; | |
$genre_taxonomy = array_values( array_filter( $taxonomies, function( $taxonomy ) { | |
return ( 'genre' === $taxonomy->name ); | |
} ) )[0]; | |
check( '"genre" === $genre_taxonomy->name' ); | |
$test_term_name = 'Ribs + Chicken (Jetpack Test)'; | |
$test_term_slug = 'ribs-chicken-jetpack-test'; | |
function test_list_genre_taxonomy_and_cleanup( $should_find_term ) { | |
global $site_id, $test_term_name, $test_term_slug; | |
// Test the members of the genre taxonomy. | |
$genre_terms = do_api_request( | |
'GET', | |
"/rest/v1.1/sites/$site_id/taxonomies/genre/terms" | |
)->terms; | |
check( 'is_array( $genre_terms )', compact( 'genre_terms' ) ); | |
$test_term = null; | |
$slug_to_delete = null; | |
// If our test term is already present, then delete it. | |
foreach ( $genre_terms as $term ) { | |
if ( $term->name === $test_term_name ) { | |
$test_term = $term; | |
$slug_to_delete = $term->slug; | |
} | |
} | |
if ( $should_find_term ) { | |
check( | |
'$test_term_slug === $test_term->slug', | |
'Could not find expected genre term.', | |
compact( 'test_term', 'test_term_slug' ) | |
); | |
} | |
if ( $slug_to_delete ) { | |
$response = do_api_request( | |
'POST', | |
"/rest/v1.1/sites/$site_id/taxonomies/genre/terms/slug:$slug_to_delete/delete" | |
); | |
} | |
} | |
test_list_genre_taxonomy_and_cleanup( false ); | |
// Test creating a new genre term. | |
$test_term = do_api_request( | |
'POST', | |
"/rest/v1.1/sites/$site_id/taxonomies/genre/terms/new", | |
array( | |
'name' => $test_term_name, | |
'description' => 'BBQ' | |
) | |
); | |
check( '$test_term_name === $test_term->name' ); | |
check( '$test_term_slug === $test_term->slug' ); | |
check( '"BBQ" === $test_term->description' ); | |
// Test updating a genre term. | |
$test_term = do_api_request( | |
'POST', | |
"/rest/v1.1/sites/$site_id/taxonomies/genre/terms/slug:$test_term_slug", | |
array( | |
'description' => 'BBQ is delicious' | |
) | |
); | |
check( '$test_term_name === $test_term->name' ); | |
check( '$test_term_slug === $test_term->slug' ); | |
check( '"BBQ is delicious" === $test_term->description' ); | |
$test_post_slug = 'ribs-chicken-jetpack-test-post'; | |
function test_get_book_and_cleanup( $should_find_book ) { | |
global $site_id, $test_post_slug; | |
// If our test post is already present, then delete it. | |
$test_post = do_api_request( | |
'GET', | |
"/rest/v1.1/sites/$site_id/posts/slug:$test_post_slug", | |
false | |
); | |
if ( $should_find_book ) { | |
check( | |
'$test_post_slug === $test_post->slug', | |
'Could not find expected book post.', | |
compact( 'test_post', 'test_post_slug' ) | |
); | |
} | |
if ( isset( $test_post->ID ) ) { | |
// Send it to the trash... | |
do_api_request( | |
'POST', | |
"/rest/v1.1/sites/$site_id/posts/$test_post->ID/delete" | |
); | |
// ...and delete it | |
do_api_request( | |
'POST', | |
"/rest/v1.1/sites/$site_id/posts/$test_post->ID/delete", | |
false | |
); | |
} | |
} | |
test_get_book_and_cleanup( false ); | |
// Test creating a post using the new term. | |
$test_post = do_api_request( | |
'POST', | |
"/rest/v1.2/sites/$site_id/posts/new", | |
array( | |
'type' => 'book', | |
'title' => 'Test book from Jetpack', | |
'content' => 'All about BBQ', | |
'slug' => $test_post_slug, | |
'terms' => array( | |
'genre' => array( $test_term_name ) | |
) | |
) | |
); | |
check( '$test_post_slug === $test_post->slug' ); | |
check( '$test_term_slug === $test_post->terms->genre->{$test_term_name}->slug' ); | |
// Test updating a post to remove the genre term (this time using the v1.1 | |
// endpoint). | |
$test_post = do_api_request( | |
'POST', | |
"/rest/v1.1/sites/$site_id/posts/$test_post->ID", | |
array( | |
'terms' => array( | |
'genre' => array() | |
) | |
) | |
); | |
$test_post_genre_terms = get_object_vars( $test_post->terms->genre ); | |
check( 'is_array( $test_post_genre_terms )' ); | |
check( '0 === count( $test_post_genre_terms )' ); | |
// Clean up our mess. | |
test_list_genre_taxonomy_and_cleanup( true ); | |
test_get_book_and_cleanup( true ); | |
// All tests passed. | |
print "Everything looks OK! 🎉\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment