From a472915b11386cedb59f7111c5336aede82b114d Mon Sep 17 00:00:00 2001 From: leogermani Date: Tue, 10 Dec 2019 16:51:05 -0300 Subject: [PATCH] fix capabilities validation api roles #274 --- .../class-tainacan-rest-roles-controller.php | 2 +- tests/test-api-roles.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/api/endpoints/class-tainacan-rest-roles-controller.php b/src/api/endpoints/class-tainacan-rest-roles-controller.php index 7a22fb556..7e92eb7bb 100644 --- a/src/api/endpoints/class-tainacan-rest-roles-controller.php +++ b/src/api/endpoints/class-tainacan-rest-roles-controller.php @@ -333,7 +333,7 @@ class REST_Roles_Controller extends REST_Controller { public function validate_roles_capabilities_arg($value, $request, $param) { if ( is_array($value) ) { foreach ($value as $cap => $val) { - if ( ! in_array($cap, \tainacan_roles()->get_all_caps_slugs() ) ) { + if ( ! in_array( \tainacan_roles()->get_cap_generic_name($cap), \tainacan_roles()->get_all_caps_slugs() ) ) { return false; } } diff --git a/tests/test-api-roles.php b/tests/test-api-roles.php index fcbf0c03a..60c3a9cb2 100644 --- a/tests/test-api-roles.php +++ b/tests/test-api-roles.php @@ -156,6 +156,22 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals( 400, $response->get_status() ); + + $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role'); + + $request->set_query_params( + [ + 'name' => 'Changed name', + 'capabilities' => [ + 'tnc_col_23_edit_items' => true + ] + ] + ); + + $response = $this->server->dispatch($request); + + $this->assertEquals( 200, $response->get_status() ); + } public function test_get_role() {