Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Created January 3, 2020 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingo-d/d243d556187295e6b34d156d53107115 to your computer and use it in GitHub Desktop.
Save dingo-d/d243d556187295e6b34d156d53107115 to your computer and use it in GitHub Desktop.
A test for API response
<?php
/**
* Class Api_Docs_Page
*
* @package My_Plugin\Routes\Endpoints
*/
namespace My_Plugin\Tests\Routes\Endpoints;
use WP_REST_Request;
use WP_UnitTestCase;
use Tests\Factories\Segment_Taxonomy_Factory;
use Tests\Factories\System_Taxonomy_Factory;
use My_Plugin\Custom_Post_Type\Documentation;
use My_Plugin\Custom_Post_Type\Documentation_Functionality;
use My_Plugin\Custom_Taxonomy\Segments_Taxonomy;
use My_Plugin\Custom_Taxonomy\Systems_Taxonomy;
/**
* Class that tests the /wp-json/wp/v2/pages?slug=api-docs response.
*/
class Api_Docs_Page extends WP_UnitTestCase {
private $author_id;
private $api_docs_page_id;
/**
* Test suite setUp method
*/
public function setUp() {
parent::setUp();
// Set up pretty permalinks so that the rest route works.
$this->set_permalink_structure( '/%postname%/' );
flush_rewrite_rules();
$_REQUEST['_wpnonce_create-user'] = wp_create_nonce( 'create-user' );
$this->author_id = $this->factory->user->create(
[
'user_email' => 'test@test.com',
'role' => 'administrator',
]
);
$this->api_docs_page_id = $this->factory->post->create(
[
'post_title' => 'API Docs',
'post_type' => 'page',
]
);
// ACF Option Fields that have to be filled.
update_field( 'api_docs_page', $this->api_docs_page_id, 'options' );
// Create the documentation posts, related taxonomies and all the fields.
$segment_term_factory = new Segment_Taxonomy_Factory();
$segment_city = $segment_term_factory->create_and_get(
[
'name' => 'City',
'slug' => 'city',
]
);
$segment_landmark = $segment_term_factory->create_and_get(
[
'name' => 'Landmark',
'slug' => 'landmark',
]
);
$system_term_factory = new System_Taxonomy_Factory();
$system_city = $system_term_factory->create_and_get(
[
'name' => 'Interact City',
'slug' => 'interact-city',
]
);
$system_landmark = $system_term_factory->create_and_get(
[
'name' => 'Interact Landmark',
'slug' => 'interact-landmark',
]
);
$doc_post_assets = $this->factory->post->create(
[
'post_title' => 'Assets',
'post_type' => Documentation::POST_TYPE_SLUG,
]
);
$doc_post_landmark = $this->factory->post->create(
[
'post_title' => 'Light Control Landmark',
'post_type' => Documentation::POST_TYPE_SLUG,
]
);
$doc_post_light_control_city = $this->factory->post->create(
[
'post_title' => 'Light Control',
'post_type' => Documentation::POST_TYPE_SLUG,
]
);
$doc_post_light_control_city_child = $this->factory->post->create(
[
'post_title' => 'Introduction',
'post_type' => Documentation::POST_TYPE_SLUG,
'post_parent' => $doc_post_light_control_city,
]
);
update_post_meta( $doc_post_assets, '_wp_page_template', 'default' );
update_post_meta( $doc_post_landmark, '_wp_page_template', 'default' );
update_post_meta( $doc_post_light_control_city, '_wp_page_template', 'default' );
update_post_meta( $doc_post_light_control_city_child, '_wp_page_template', 'docs-details-template' );
// Set post thumbnails.
$city_attachment_id = $this->factory->attachment->create_upload_object(
dirname( __FILE__, 5 ) . '/_files/api_city-control@2x.png',
$doc_post_light_control_city
);
$asset_attachment_id = $this->factory->attachment->create_upload_object(
dirname( __FILE__, 5 ) . '/_files/api_assets@2x.png',
$doc_post_assets
);
$landmark_attachment_id = $this->factory->attachment->create_upload_object(
dirname( __FILE__, 5 ) . '/_files/system_landmark@2x.png',
$doc_post_landmark
);
set_post_thumbnail( $doc_post_light_control_city, $city_attachment_id );
set_post_thumbnail( $doc_post_assets, $asset_attachment_id );
set_post_thumbnail( $doc_post_landmark, $landmark_attachment_id );
// Add taxonomy terms to documentation posts.
wp_set_post_terms( $doc_post_assets, [ $segment_landmark->term_id ], Segments_Taxonomy::TAXONOMY_SLUG );
wp_set_post_terms( $doc_post_assets, [ $system_landmark->term_id ], Systems_Taxonomy::TAXONOMY_SLUG );
wp_set_post_terms( $doc_post_light_control_city, [ $segment_city->term_id ], Segments_Taxonomy::TAXONOMY_SLUG );
wp_set_post_terms( $doc_post_light_control_city, [ $system_city->term_id ], Systems_Taxonomy::TAXONOMY_SLUG );
wp_set_post_terms( $doc_post_landmark, [ $segment_landmark->term_id ], Segments_Taxonomy::TAXONOMY_SLUG );
wp_set_post_terms( $doc_post_landmark, [ $system_landmark->term_id ], Systems_Taxonomy::TAXONOMY_SLUG );
update_post_meta( $doc_post_assets, Documentation_Functionality::DOCUMENTATION_ORDER, 0 );
update_post_meta( $doc_post_light_control_city, Documentation_Functionality::DOCUMENTATION_ORDER, 1 );
update_post_meta( $doc_post_landmark, Documentation_Functionality::DOCUMENTATION_ORDER, 2 );
// Add custom fields. First we need to add the documentation posts section to our API Docs page.
update_field( 'sections', [ 'simple_content_section' ], $this->api_docs_page_id );
update_post_meta( $this->api_docs_page_id, 'sections', [ 'simple_content_section' ] ); // Workaround. Dirty, but works.
update_field( 'sections_0_items', ['documentation_posts'], $this->api_docs_page_id );
update_field( 'sections_0_utilities_background_color', '#f5f5f5', $this->api_docs_page_id );
// ACF Fields of the documentation posts themselves.
update_field( 'documentation_overview', 'Short description' , $doc_post_assets );
update_field( 'documentation_file', '' , $doc_post_assets );
update_field( 'enable_playground', '1', $doc_post_assets );
update_field( 'hide_models', '0', $doc_post_assets );
update_field( 'visible_in_sidebar_only', '0', $doc_post_assets );
update_field( 'soap', '0', $doc_post_assets );
update_field( 'hide_systems_list', '0', $doc_post_assets );
update_field( 'host_url_from_file', '0', $doc_post_assets );
update_field( 'service_name', 'landmark', $doc_post_assets );
update_field( 'legacy_name', '', $doc_post_assets );
update_field( 'system_credentials_username', '', $doc_post_assets );
update_field( 'system_credentials_password', '', $doc_post_assets );
update_field( 'documentation_overview', 'Short description.' , $doc_post_light_control_city );
update_field( 'documentation_file', '' , $doc_post_light_control_city );
update_field( 'enable_playground', '0', $doc_post_light_control_city );
update_field( 'hide_models', '1', $doc_post_light_control_city );
update_field( 'visible_in_sidebar_only', '1', $doc_post_light_control_city );
update_field( 'soap', '0', $doc_post_light_control_city );
update_field( 'hide_systems_list', '0', $doc_post_light_control_city );
update_field( 'host_url_from_file', '0', $doc_post_light_control_city );
update_field( 'service_name', 'city', $doc_post_light_control_city );
update_field( 'legacy_name', '', $doc_post_light_control_city );
update_field( 'system_credentials_username', '', $doc_post_light_control_city );
update_field( 'system_credentials_password', '', $doc_post_light_control_city );
update_field( 'documentation_overview', 'Short description.' , $doc_post_assets );
update_field( 'documentation_file', '' , $doc_post_assets );
update_field( 'enable_playground', '1', $doc_post_assets );
update_field( 'hide_models', '1', $doc_post_assets );
update_field( 'visible_in_sidebar_only', '0', $doc_post_assets );
update_field( 'soap', '0', $doc_post_assets );
update_field( 'hide_systems_list', '1', $doc_post_assets );
update_field( 'host_url_from_file', '0', $doc_post_assets );
update_field( 'service_name', 'landmark', $doc_post_assets );
update_field( 'legacy_name', '', $doc_post_assets );
update_field( 'system_credentials_username', '', $doc_post_assets );
update_field( 'system_credentials_password', '', $doc_post_assets );
// Child post.
update_field( 'show_related_docs_sidebar', 1, $doc_post_light_control_city_child );
update_field( 'default_child', 1, $doc_post_light_control_city_child );
}
/**
* Test suite tearDown method
*/
public function tearDown() {
parent::tearDown();
}
/**
* Test if the response is correct
*/
public function test_api_docs_page_response() {
$request = new WP_REST_Request( 'GET', "/wp/v2/pages/{$this->api_docs_page_id}" );
$response = rest_get_server()->dispatch( $request );
$page_data = $response->get_data();
error_log( print_r( $page_data, true ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment