From ae49d86c2070ac1bc06461a52f52c1d5a42301bb Mon Sep 17 00:00:00 2001 From: ramon Date: Wed, 20 Dec 2023 14:01:11 +1100 Subject: [PATCH] Changed duped error code Adding parent id revision parent_id mismatch invalid test to the templates revisions controller tests --- .../class-wp-rest-revisions-controller.php | 2 +- .../rest-api/rest-revisions-controller.php | 2 +- .../wpRestTemplateRevisionsController.php | 48 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php index 605a4e984763e..41ae5c805d66a 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php @@ -405,7 +405,7 @@ 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'] ), array( 'status' => 404 ) diff --git a/tests/phpunit/tests/rest-api/rest-revisions-controller.php b/tests/phpunit/tests/rest-api/rest-revisions-controller.php index ca7794127847f..00dacbf36bb72 100644 --- a/tests/phpunit/tests/rest-api/rest-revisions-controller.php +++ b/tests/phpunit/tests/rest-api/rest-revisions-controller.php @@ -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() { diff --git a/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php b/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php index 91370b21c726b..253819253f4a4 100644 --- a/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php +++ b/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php @@ -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 */ @@ -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 */ @@ -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 ); } /** @@ -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