From 09affe558dd9c7b33391d1d9188aa50c701a1a1b Mon Sep 17 00:00:00 2001 From: ramon Date: Fri, 3 Nov 2023 14:25:07 +1100 Subject: [PATCH 1/4] Basic setup: - Adding route for single global styles revisions --- lib/compat/wordpress-6.4/rest-api.php | 8 -- ...global-styles-revisions-controller-6-5.php | 102 ++++++++++++++++++ lib/compat/wordpress-6.5/rest-api.php | 21 ++++ lib/load.php | 4 + 4 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 lib/compat/wordpress-6.5/class-gutenberg-rest-global-styles-revisions-controller-6-5.php create mode 100644 lib/compat/wordpress-6.5/rest-api.php diff --git a/lib/compat/wordpress-6.4/rest-api.php b/lib/compat/wordpress-6.4/rest-api.php index 7c81a6a274c03..4a020b2b5cdfa 100644 --- a/lib/compat/wordpress-6.4/rest-api.php +++ b/lib/compat/wordpress-6.4/rest-api.php @@ -19,11 +19,3 @@ function gutenberg_register_rest_block_patterns_routes() { } add_action( 'rest_api_init', 'gutenberg_register_rest_block_patterns_routes' ); -/** - * Registers the Global Styles Revisions REST API routes. - */ -function gutenberg_register_global_styles_revisions_endpoints() { - $global_styles_revisions_controller = new Gutenberg_REST_Global_Styles_Revisions_Controller_6_4(); - $global_styles_revisions_controller->register_routes(); -} -add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' ); diff --git a/lib/compat/wordpress-6.5/class-gutenberg-rest-global-styles-revisions-controller-6-5.php b/lib/compat/wordpress-6.5/class-gutenberg-rest-global-styles-revisions-controller-6-5.php new file mode 100644 index 0000000000000..e7b2ac85f6e52 --- /dev/null +++ b/lib/compat/wordpress-6.5/class-gutenberg-rest-global-styles-revisions-controller-6-5.php @@ -0,0 +1,102 @@ +namespace, + '/' . $this->parent_base . '/(?P[\d]+)/' . $this->rest_base . '/(?P[\d]+)', + array( + 'args' => array( + 'parent' => array( + 'description' => __( 'The ID for the parent of the global styles revision.' ), + 'type' => 'integer', + ), + 'id' => array( + 'description' => __( 'Unique identifier for the global styles revision.' ), + 'type' => 'integer', + ), + ), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_item' ), + 'permission_callback' => array( $this, 'get_item_permissions_check' ), + 'args' => array( + 'context' => $this->get_context_param( array( 'default' => 'view' ) ), + ), + ), + 'schema' => array( $this, 'get_public_item_schema' ), + ) + ); + parent::register_routes(); + } + + /** + * Retrieves one global styles revision from the collection. + * + * @since 6.5.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. + */ + public function get_item( $request ) { + $parent = $this->get_parent( $request['parent'] ); + if ( is_wp_error( $parent ) ) { + return $parent; + } + + $revision = $this->get_revision( $request['id'] ); + if ( is_wp_error( $revision ) ) { + return $revision; + } + + $response = $this->prepare_item_for_response( $revision, $request ); + return rest_ensure_response( $response ); + } + + /** + * Get the global styles revision, if the ID is valid. + * + * @since 6.5.0 + * + * @param int $id Supplied ID. + * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise. + */ + protected function get_revision( $id ) { + $error = new WP_Error( + 'rest_post_invalid_id', + __( 'Invalid revision ID.' ), + array( 'status' => 404 ) + ); + + if ( (int) $id <= 0 ) { + return $error; + } + + $revision = get_post( (int) $id ); + if ( empty( $revision ) || empty( $revision->ID ) || 'revision' !== $revision->post_type ) { + return $error; + } + + return $revision; + } +} diff --git a/lib/compat/wordpress-6.5/rest-api.php b/lib/compat/wordpress-6.5/rest-api.php new file mode 100644 index 0000000000000..dd372eff7943b --- /dev/null +++ b/lib/compat/wordpress-6.5/rest-api.php @@ -0,0 +1,21 @@ +register_routes(); +} + +add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' ); diff --git a/lib/load.php b/lib/load.php index 5740e43b5f76a..711766dec7dfb 100644 --- a/lib/load.php +++ b/lib/load.php @@ -56,6 +56,10 @@ function gutenberg_is_experiment_enabled( $name ) { require_once __DIR__ . '/compat/wordpress-6.4/rest-api.php'; require_once __DIR__ . '/compat/wordpress-6.4/theme-previews.php'; + // WordPress 6.5 compat. + require_once __DIR__ . '/compat/wordpress-6.5/class-gutenberg-rest-global-styles-revisions-controller-6-5.php'; + require_once __DIR__ . '/compat/wordpress-6.5/rest-api.php'; + // Plugin specific code. require_once __DIR__ . '/class-wp-rest-global-styles-controller-gutenberg.php'; require_once __DIR__ . '/rest-api.php'; From a000125618342722cac8bd0091dca4da965d9df9 Mon Sep 17 00:00:00 2001 From: ramon Date: Fri, 3 Nov 2023 14:28:20 +1100 Subject: [PATCH 2/4] Expected 1 blank line at end of file; 2 found --- lib/compat/wordpress-6.4/rest-api.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/compat/wordpress-6.4/rest-api.php b/lib/compat/wordpress-6.4/rest-api.php index 4a020b2b5cdfa..274eb3af94543 100644 --- a/lib/compat/wordpress-6.4/rest-api.php +++ b/lib/compat/wordpress-6.4/rest-api.php @@ -18,4 +18,3 @@ function gutenberg_register_rest_block_patterns_routes() { $block_patterns->register_routes(); } add_action( 'rest_api_init', 'gutenberg_register_rest_block_patterns_routes' ); - From 7472e683ab9b0f4fa8b5729a02e5f79e8e23f261 Mon Sep 17 00:00:00 2001 From: ramon Date: Mon, 6 Nov 2023 14:34:56 +1100 Subject: [PATCH 3/4] Add test for get_item --- ...lobal-styles-revisions-controller-test.php | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php b/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php index 1b8e672fa7819..2eb1b91116f85 100644 --- a/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php +++ b/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php @@ -242,6 +242,11 @@ public function test_register_routes() { $routes, 'Global style revisions based on the given parentID route does not exist.' ); + $this->assertArrayHasKey( + '/wp/v2/global-styles/(?P[\d]+)/revisions/(?P[\d]+)', + $routes, + 'Single global style revisions based on the given parentID and revision ID route does not exist.' + ); } /** @@ -298,6 +303,20 @@ public function test_get_items() { $this->check_get_revision_response( $data[2], $this->revision_1 ); } + /** + * @covers Gutenberg_REST_Global_Styles_Revisions_Controller_6_4::get_item + */ + public function test_get_item() { + wp_set_current_user( self::$admin_id ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions/' . $this->revision_1_id ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + + $this->assertSame( 200, $response->get_status(), 'Response status is 200.' ); + $this->check_get_revision_response( $data, $this->revision_1 ); + } + /** * @ticket 58524 * @@ -320,6 +339,7 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'modified', $properties, 'Schema properties array has "modified" key.' ); $this->assertArrayHasKey( 'modified_gmt', $properties, 'Schema properties array has "modified_gmt" key.' ); } + /** * @doesNotPerformAssertions */ @@ -327,13 +347,6 @@ public function test_context_param() { // Controller does not implement test_context_param(). } - /** - * @doesNotPerformAssertions - */ - public function test_get_item() { - // Controller does not implement get_item(). - } - /** * @doesNotPerformAssertions */ From ec9b0e4228a9ec4dba2af8d1d0f62f6512708f3c Mon Sep 17 00:00:00 2001 From: ramon Date: Mon, 6 Nov 2023 15:11:26 +1100 Subject: [PATCH 4/4] Add ticket number and 404 test --- ...global-styles-revisions-controller-test.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php b/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php index 2eb1b91116f85..857e8fa297cf1 100644 --- a/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php +++ b/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php @@ -304,6 +304,8 @@ public function test_get_items() { } /** + * @ticket 59810 + * * @covers Gutenberg_REST_Global_Styles_Revisions_Controller_6_4::get_item */ public function test_get_item() { @@ -317,6 +319,22 @@ public function test_get_item() { $this->check_get_revision_response( $data, $this->revision_1 ); } + /** + * @ticket 59810 + * + * @covers Gutenberg_REST_Global_Styles_Revisions_Controller_6_4::get_revision + */ + public function test_get_item_invalid_revision_id_should_error() { + wp_set_current_user( self::$admin_id ); + + $expected_error = 'rest_post_invalid_id'; + $expected_status = 404; + $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions/20000001' ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( $expected_error, $response, $expected_status ); + } + /** * @ticket 58524 *