Skip to content

Commit

Permalink
Changed duped error code
Browse files Browse the repository at this point in the history
Adding parent id revision parent_id mismatch invalid test to the templates revisions controller tests

Remove specific mention of "post" to discern between other post types, e.g., "template"
  • Loading branch information
ramonjd committed Dec 20, 2023
1 parent 6d489eb commit ff84ef6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ public function get_item( $request ) {

if ( (int) $parent->ID !== (int) $revision->post_parent ) {
return new WP_Error(
'rest_post_invalid_parent',
'rest_revision_parent_id_mismatch',
/* translators: %d: A post revision id. */
sprintf( __( 'Parent post does not have a revision with id of "%d"' ), $request['id'] ),
sprintf( __( 'Parent does not have a revision with id of "%d"' ), $request['id'] ),
array( 'status' => 404 )
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/rest-api/rest-revisions-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function test_get_item_invalid_parent_id() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_2_1_id );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
$this->assertErrorResponse( 'rest_revision_parent_id_mismatch', $response, 404 );
}

public function test_delete_item() {
Expand Down
48 changes: 48 additions & 0 deletions tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class Tests_REST_wpRestTemplateRevisionsController extends WP_Test_REST_Controll
*/
const TEMPLATE_NAME = 'my_template';

/**
* @var string
*/
const TEMPLATE_NAME_2 = 'my_template_2';

/**
* @var string
*/
Expand Down Expand Up @@ -51,6 +56,15 @@ class Tests_REST_wpRestTemplateRevisionsController extends WP_Test_REST_Controll
*/
private static $template_post;

/**
* Template post.
*
* @since 6.5.0
*
* @var WP_Post
*/
private static $template_post_2;

/**
* @var array
*/
Expand Down Expand Up @@ -123,6 +137,26 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
'post_content' => 'Content revision #5',
)
);

// Create a new template post to test the get_item method.
self::$template_post_2 = $factory->post->create_and_get(
array(
'post_type' => self::PARENT_POST_TYPE,
'post_name' => self::TEMPLATE_NAME_2,
'post_title' => 'My Template 2',
'post_content' => 'Content 2',
'post_excerpt' => 'Description of my template 2',
'tax_input' => array(
'wp_theme' => array(
self::TEST_THEME,
),
),
)
);
wp_set_post_terms( self::$template_post_2->ID, self::TEST_THEME, 'wp_theme' );

var_dump( self::$template_post->ID );
var_dump( self::$template_post_2->ID );
}

/**
Expand Down Expand Up @@ -336,6 +370,20 @@ public function test_get_item_not_found() {
$this->assertErrorResponse( 'rest_post_invalid_parent', $response, WP_Http::NOT_FOUND );
}

/**
* @ticket 59875
*/
public function test_get_item_invalid_parent_id() {
wp_set_current_user( self::$admin_id );
$revisions = wp_get_post_revisions( self::$template_post, array( 'fields' => 'ids' ) );
$revision_id = array_shift( $revisions );

$request = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME_2 . '/revisions/' . $revision_id );

$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_revision_parent_id_mismatch', $response, 404 );
}

/**
* @covers WP_REST_Template_Revisions_Controller::prepare_item_for_response
* @ticket 56922
Expand Down

0 comments on commit ff84ef6

Please sign in to comment.