Skip to content

Commit

Permalink
Adding unit tests, removing the hardcoded supports array constant in …
Browse files Browse the repository at this point in the history
…entities.js
  • Loading branch information
ramonjd committed Nov 29, 2023
1 parent 602b14c commit f4c6cdc
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 18 deletions.
19 changes: 1 addition & 18 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ export const DEFAULT_ENTITY_KEY = 'id';

const POST_RAW_ATTRIBUTES = [ 'title', 'excerpt', 'content' ];

// A hardcoded list of post types that support revisions.
// Reflects post types in Core's src/wp-includes/post.php.
// @TODO: Ideally this should be fetched from the `/types` REST API's view context.
const POST_TYPE_ENTITIES_WITH_REVISIONS_SUPPORT = [
'post',
'page',
'wp_block',
'wp_navigation',
'wp_template',
'wp_template_part',
];

export const rootEntitiesConfig = [
{
label: __( 'Base' ),
Expand Down Expand Up @@ -299,7 +287,6 @@ async function loadPostTypeEntities() {
const postTypes = await apiFetch( {
path: '/wp/v2/types?context=view',
} );
console.log( 'postTypes', postTypes );
return Object.entries( postTypes ?? {} ).map( ( [ name, postType ] ) => {
const isTemplate = [ 'wp_template', 'wp_template_part' ].includes(
name
Expand All @@ -316,11 +303,7 @@ async function loadPostTypeEntities() {
selection: true,
},
mergedEdits: { meta: true },
supports: {
revisions: POST_TYPE_ENTITIES_WITH_REVISIONS_SUPPORT.includes(
postType?.slug
),
},
supports: postType?.supports,
rawAttributes: POST_RAW_ATTRIBUTES,
getTitle: ( record ) =>
record?.title?.rendered ||
Expand Down
119 changes: 119 additions & 0 deletions phpunit/class-gutenberg-rest-post-types-controller-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Unit tests covering WP_REST_Posts_Types_Controller functionality extensions.
*
* @package WordPress
* @subpackage REST API
*/
class Gutenberg_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcase {
public function set_up() {
register_post_type(
'gutenberg_types_cpt',
array(
'show_in_rest' => true,
'rest_base' => 'gutenberg_types_cpt',
)
);
}

public static function wpTearDownAfterClass() {
unregister_post_type( 'gutenberg_types_cpt' );
}

public function test_get_items_revisions_support_in_view_context() {
$request = new WP_REST_Request( 'GET', '/wp/v2/types' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

// Only returns one support.
$this->assertCount( 1, $data['page']['supports'], 'Only one support item should be returned in view context.' );

$page_supports = get_all_post_type_supports( 'page' );
$this->assertSame( $page_supports['revisions'], $data['page']['supports']['revisions'], 'Revisions support should be returned for `page` post types.' );

$this->assertCount( 1, $data['post']['supports'], 'Only one support item should be returned in view context.' );
$post_supports = get_all_post_type_supports( 'post' );
$this->assertSame( $post_supports['revisions'], $data['post']['supports']['revisions'], 'Revisions support should be returned for `post` post types.' );

// Where revisions are not supported.
$this->assertSame( 'gutenberg_types_cpt', $data['gutenberg_types_cpt']['slug'], 'Custom post should be correctly registered.' );
$this->assertFalse( isset( $data['gutenberg_types_cpt']['supports'] ), 'Custom post should not return supports array in view context.' );
}

public function test_get_item_post_revisions_support_in_view_context() {
$request = new WP_REST_Request( 'GET', '/wp/v2/types/post' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$data = $response->get_data();

// Only returns one support.
$this->assertCount( 1, $data['supports'], 'Only one support item should be returned in view context.' );

$supports_revisions = post_type_supports( 'post', 'revisions' );
$this->assertSame( $supports_revisions, $data['supports']['revisions'], 'Revisions support should be returned for single `post` post types.' );

// Where revisions are not supported.
$request = new WP_REST_Request( 'GET', '/wp/v2/types/gutenberg_types_cpt' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 'gutenberg_types_cpt', $data['slug'], 'Custom post should be correctly registered.' );
$this->assertFalse( isset( $data['supports'] ), 'Custom post should not return supports array in view context.' );
}

/**
* @doesNotPerformAssertions
*/
public function test_context_param() {
// Controller does not implement this method.
}

/**
* @doesNotPerformAssertions
*/
public function test_get_item() {
// Controller does not implement this method.
}

/**
* @doesNotPerformAssertions
*/
public function test_create_item() {
// Controller does not implement this method.
}

/**
* @doesNotPerformAssertions
*/
public function test_update_item() {
// Controller does not implement this method.
}

/**
* @doesNotPerformAssertions
*/
public function test_delete_item() {
// Controller does not implement this method.
}

/**
* @doesNotPerformAssertions
*/
public function test_prepare_item() {
// Controller does not implement this method.
}

/**
* @doesNotPerformAssertions
*/
public function test_get_item_schema() {
// Controller does not implement this method.
}

/**
* @doesNotPerformAssertions
*/
public function test_register_routes() {
// Controller does not implement this method.
}
}

0 comments on commit f4c6cdc

Please sign in to comment.