Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API: Add tests for global styles endpoint integer ID type #8585

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,16 @@ 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<id>[\/\w-]+)',
'/' . $this->rest_base . '/(?P<id>[\/\d+]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'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',
),
),
),
Expand Down Expand Up @@ -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,
),
Expand Down
56 changes: 53 additions & 3 deletions tests/phpunit/tests/rest-api/rest-global-styles-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<id>[\/\w-]+)',
'/wp/v2/global-styles/(?P<id>[\/\d+]+)',
$routes,
'Single global style based on the given ID route does not exist'
);
$this->assertCount(
2,
$routes['/wp/v2/global-styles/(?P<id>[\/\w-]+)'],
$routes['/wp/v2/global-styles/(?P<id>[\/\d+]+)'],
'Single global style based on the given ID route does not have exactly two elements'
);
$this->assertArrayHasKey(
Expand Down Expand Up @@ -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',
),
);
}
Expand Down Expand Up @@ -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<id>[\/\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'] );
}
}
2 changes: 1 addition & 1 deletion tests/phpunit/tests/rest-api/rest-schema-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function test_expected_routes_in_schema() {
'/wp/v2/users/(?P<user_id>(?:[\\d]+|me))/application-passwords/(?P<uuid>[\\w\\-]+)',
'/wp/v2/comments',
'/wp/v2/comments/(?P<id>[\\d]+)',
'/wp/v2/global-styles/(?P<id>[\/\w-]+)',
'/wp/v2/global-styles/(?P<id>[\/\d+]+)',
'/wp/v2/global-styles/(?P<parent>[\d]+)/revisions',
'/wp/v2/global-styles/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',
Expand Down
6 changes: 3 additions & 3 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -6896,7 +6896,7 @@ mockedApiResponse.Schema = {
}
]
},
"/wp/v2/global-styles/(?P<id>[\\/\\w-]+)": {
"/wp/v2/global-styles/(?P<id>[\\/\\d+]+)": {
"namespace": "wp/v2",
"methods": [
"GET",
Expand All @@ -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
}
}
Expand Down
Loading