From c946a5846d0e34f1c3b17ba73c5a7824d1e45814 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Tue, 25 Mar 2025 18:28:18 +0530 Subject: [PATCH 1/4] Update schema --- .../endpoints/class-wp-rest-global-styles-controller.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php index 51c1ac29b8294..7b9f176c5bd07 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php @@ -88,7 +88,7 @@ public function register_routes() { // Lists/updates a single global style variation based on the given id. register_rest_route( $this->namespace, - '/' . $this->rest_base . '/(?P[\/\w-]+)', + '/' . $this->rest_base . '/(?P[\/\d+]+)', array( array( 'methods' => WP_REST_Server::READABLE, @@ -96,9 +96,8 @@ public function register_routes() { 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'args' => array( 'id' => array( - 'description' => __( 'The id of a template' ), - 'type' => 'string', - 'sanitize_callback' => array( $this, '_sanitize_global_styles_callback' ), + 'description' => __( 'ID of global styles config.' ), + 'type' => 'integer', ), ), ), @@ -464,7 +463,7 @@ public function get_item_schema() { 'properties' => array( 'id' => array( 'description' => __( 'ID of global styles config.' ), - 'type' => 'string', + 'type' => 'integer', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), From 6e01efbc2788a99f9633697b54ff96c12c93f2aa Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Tue, 25 Mar 2025 18:55:27 +0530 Subject: [PATCH 2/4] REST API: Add tests for global styles endpoint integer ID type --- .../rest-global-styles-controller.php | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php index b55c7c3d606eb..749507b9a8ea5 100644 --- a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php +++ b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php @@ -776,4 +776,54 @@ public function test_assign_edit_css_action_admin() { $this->assertArrayHasKey( 'https://api.w.org/action-edit-css', $links ); } } + + /** + * Test that the route accepts integer IDs. + * + * @ticket 61911 + */ + public function test_global_styles_route_accepts_integer_id() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertEquals( 200, $response->get_status() ); + + $data = $response->get_data(); + $this->assertIsInt( $data['id'] ); + $this->assertSame( self::$global_styles_id, $data['id'] ); + } + + /** + * Test that the schema defines ID as an integer. + * + * @ticket 61911 + */ + public function test_global_styles_schema_id_type() { + $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/global-styles/' . self::$global_styles_id ); + $response = rest_get_server()->dispatch( $request ); + + $data = $response->get_data(); + $schema = $data['schema']; + + $this->assertArrayHasKey( 'properties', $schema ); + $this->assertArrayHasKey( 'id', $schema['properties'] ); + $this->assertArrayHasKey( 'type', $schema['properties']['id'] ); + $this->assertSame( 'integer', $schema['properties']['id']['type'] ); + } + + /** + * Test that the route argument schema defines ID as an integer. + * + * @ticket 61911 + */ + public function test_global_styles_route_args_schema() { + $routes = rest_get_server()->get_routes(); + $route_data = $routes['/wp/v2/global-styles/(?P[\/\d+]+)']; + + $this->assertArrayHasKey( 'args', $route_data[0] ); + $this->assertArrayHasKey( 'id', $route_data[0]['args'] ); + $this->assertArrayHasKey( 'type', $route_data[0]['args']['id'] ); + $this->assertSame( 'integer', $route_data[0]['args']['id']['type'] ); + } } From b0a6e92b7310ca1c0f77e58dacb17ea1d15f7a27 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Tue, 25 Mar 2025 18:57:52 +0530 Subject: [PATCH 3/4] REST API: Update the existing tests --- .../tests/rest-api/rest-global-styles-controller.php | 6 +++--- tests/phpunit/tests/rest-api/rest-schema-setup.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php index 749507b9a8ea5..59986e597c71b 100644 --- a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php +++ b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php @@ -133,13 +133,13 @@ public function filter_theme_file_uri( $file ) { public function test_register_routes() { $routes = rest_get_server()->get_routes(); $this->assertArrayHasKey( - '/wp/v2/global-styles/(?P[\/\w-]+)', + '/wp/v2/global-styles/(?P[\/\d+]+)', $routes, 'Single global style based on the given ID route does not exist' ); $this->assertCount( 2, - $routes['/wp/v2/global-styles/(?P[\/\w-]+)'], + $routes['/wp/v2/global-styles/(?P[\/\d+]+)'], 'Single global style based on the given ID route does not have exactly two elements' ); $this->assertArrayHasKey( @@ -408,7 +408,7 @@ public function data_get_theme_item_invalid_theme_dirname() { // Themes deep in subdirectories. '2 subdirectories deep' => array( 'theme_dirname' => 'subdir/subsubdir/mytheme', - 'expected' => 'rest_global_styles_not_found', + 'expected' => 'rest_no_route', ), ); } diff --git a/tests/phpunit/tests/rest-api/rest-schema-setup.php b/tests/phpunit/tests/rest-api/rest-schema-setup.php index 03dcb0631f133..7f8de5f0dd83d 100644 --- a/tests/phpunit/tests/rest-api/rest-schema-setup.php +++ b/tests/phpunit/tests/rest-api/rest-schema-setup.php @@ -133,7 +133,7 @@ public function test_expected_routes_in_schema() { '/wp/v2/users/(?P(?:[\\d]+|me))/application-passwords/(?P[\\w\\-]+)', '/wp/v2/comments', '/wp/v2/comments/(?P[\\d]+)', - '/wp/v2/global-styles/(?P[\/\w-]+)', + '/wp/v2/global-styles/(?P[\/\d+]+)', '/wp/v2/global-styles/(?P[\d]+)/revisions', '/wp/v2/global-styles/(?P[\d]+)/revisions/(?P[\d]+)', '/wp/v2/global-styles/themes/(?P[\/\s%\w\.\(\)\[\]\@_\-]+)/variations', From b11c81b30d9c9e2e9b96199c88cae6170a154986 Mon Sep 17 00:00:00 2001 From: himanshupathak95 Date: Tue, 25 Mar 2025 19:23:52 +0530 Subject: [PATCH 4/4] REST API: Updates QUnit fixtures --- tests/qunit/fixtures/wp-api-generated.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index f87397b20a449..268f03c3b71fd 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -6896,7 +6896,7 @@ mockedApiResponse.Schema = { } ] }, - "/wp/v2/global-styles/(?P[\\/\\w-]+)": { + "/wp/v2/global-styles/(?P[\\/\\d+]+)": { "namespace": "wp/v2", "methods": [ "GET", @@ -6914,8 +6914,8 @@ mockedApiResponse.Schema = { }, "args": { "id": { - "description": "The id of a template", - "type": "string", + "description": "ID of global styles config.", + "type": "integer", "required": false } }