From 93a9afcf16c0163c3f9b603a731e550d46638f7e Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Mon, 31 Jul 2023 11:22:58 -0300 Subject: [PATCH] fix: roles endpoint args --- .../class-tainacan-rest-roles-controller.php | 39 ++++++++++++------- tests/test-api-roles.php | 6 +++ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/classes/api/endpoints/class-tainacan-rest-roles-controller.php b/src/classes/api/endpoints/class-tainacan-rest-roles-controller.php index c419c4024..68379eb2c 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-roles-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-roles-controller.php @@ -30,7 +30,7 @@ class REST_Roles_Controller extends REST_Controller { 'methods' => \WP_REST_Server::READABLE, 'callback' => array($this, 'get_items'), 'permission_callback' => array($this, 'get_items_permissions_check'), - 'args' => $this->get_endpoint_args(\WP_REST_Server::DELETABLE) + 'args' => $this->get_endpoint_args(\WP_REST_Server::READABLE) ), array( 'methods' => \WP_REST_Server::CREATABLE, @@ -227,8 +227,6 @@ class REST_Roles_Controller extends REST_Controller { ], 400); } - $role = $roles[$role_slug]; - if ( isset($request['name']) ) { $name = esc_html( esc_sql( $request['name'] ) ); @@ -463,33 +461,44 @@ class REST_Roles_Controller extends REST_Controller { * @return array|mixed */ public function get_endpoint_args( $method = null ) { - $endpoint_args = [ - 'role' => [ - 'description' => __( 'Role slug', 'tainacan' ), - 'type' => 'string', - 'required' => true, - ] - ]; + $endpoint_args = []; switch ( $method ) { - case \WP_REST_Server::CREATABLE: case \WP_REST_Server::EDITABLE: + $endpoint_args['role'] = array( + 'description' => __( 'Role slug', 'tainacan' ), + 'type' => 'string', + 'required' => true + ); + $endpoint_args['add_cap'] = array( + 'description' => __( 'the capability slug to be add', 'tainacan' ), + 'type' => 'string', + 'required' => false + ); + $endpoint_args['remove_cap'] = array( + 'description' => __( 'The capability slug to be removed', 'tainacan' ), + 'type' => 'string', + 'required' => false + ); + case \WP_REST_Server::CREATABLE: $endpoint_args['name'] = array( 'description' => __('New role name', 'tainacan'), 'type' => 'string', - 'required' => true + 'required' => $method == \WP_REST_Server::CREATABLE ); $endpoint_args['capabilities'] = array( 'description' => __('Array of capabilities, where the keys are capability slugs and values are booleans', 'tainacan'), 'required' => false, 'validate_callback' => [$this, 'validate_roles_capabilities_arg'] ); - - if ( $method == \WP_REST_Server::CREATABLE ) - unset($endpoint_args['role']); break; case \WP_REST_Server::READABLE: case \WP_REST_Server::DELETABLE: + $endpoint_args['role'] = array( + 'description' => __( 'Role slug', 'tainacan' ), + 'type' => 'string', + 'required' => $method == \WP_REST_Server::DELETABLE + ); break; } diff --git a/tests/test-api-roles.php b/tests/test-api-roles.php index 7acd7fa6c..e95ddd2b6 100644 --- a/tests/test-api-roles.php +++ b/tests/test-api-roles.php @@ -33,6 +33,12 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $data = $name_response->get_data(); $this->assertArrayHasKey('tainacan-new-role', $data); $this->assertEquals('New role', $data['tainacan-new-role']['name']); + + $request->set_query_params(['role' => 'tainacan-new-role']); + $name_response = $this->server->dispatch($request); + $data = $name_response->get_data(); + $this->assertArrayHasKey('tainacan-new-role', $data); + $this->assertEquals('New role', $data['tainacan-new-role']['name']); } public function test_create_remove_roles() {