From 0a7076744e90f929f7de4a14544ed56b5bb6a403 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Thu, 23 Mar 2017 19:52:35 -0300 Subject: [PATCH 1/5] [REST API] Fixed settings/options rest_base --- ...ass-wc-rest-setting-options-controller.php | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/includes/api/class-wc-rest-setting-options-controller.php b/includes/api/class-wc-rest-setting-options-controller.php index 96fd48b7bbe..9b4915c384c 100644 --- a/includes/api/class-wc-rest-setting-options-controller.php +++ b/includes/api/class-wc-rest-setting-options-controller.php @@ -1,6 +1,6 @@ [\w-]+)'; /** * Register routes. @@ -40,7 +40,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * @since 3.0.0 */ public function register_routes() { - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)', array( + register_rest_route( $this->namespace, '/' . $this->rest_base, array( 'args' => array( 'group' => array( 'description' => __( 'Settings group ID.', 'woocommerce' ), @@ -55,23 +55,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { 'schema' => array( $this, 'get_public_item_schema' ), ) ); - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)/batch', array( - 'args' => array( - 'group' => array( - 'description' => __( 'Settings group ID.', 'woocommerce' ), - 'type' => 'string', - ), - ), - array( - 'methods' => WP_REST_Server::EDITABLE, - 'callback' => array( $this, 'batch_items' ), - 'permission_callback' => array( $this, 'update_items_permissions_check' ), - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), - ), - 'schema' => array( $this, 'get_public_batch_schema' ), - ) ); - - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)/(?P[\w-]+)', array( + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)', array( 'args' => array( 'group' => array( 'description' => __( 'Settings group ID.', 'woocommerce' ), @@ -95,6 +79,22 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); + + register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( + 'args' => array( + 'group' => array( + 'description' => __( 'Settings group ID.', 'woocommerce' ), + 'type' => 'string', + ), + ), + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array( $this, 'batch_items' ), + 'permission_callback' => array( $this, 'update_items_permissions_check' ), + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), + ), + 'schema' => array( $this, 'get_public_batch_schema' ), + ) ); } /** @@ -316,13 +316,13 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * @return array Links for the given setting. */ protected function prepare_links( $setting_id, $group_id ) { - $base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $group_id; + $base = str_replace( '(?P[\w-]+)', $group_id, $this->rest_base ); $links = array( 'self' => array( - 'href' => rest_url( trailingslashit( $base ) . $setting_id ), + 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $base, $setting_id ) ), ), 'collection' => array( - 'href' => rest_url( $base ), + 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $base ) ), ), ); From fb80482d73eccd6f54d9fca5facc1acaf5804004 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Thu, 23 Mar 2017 19:56:56 -0300 Subject: [PATCH 2/5] Fixed rest_base --- includes/api/class-wc-rest-setting-options-controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/api/class-wc-rest-setting-options-controller.php b/includes/api/class-wc-rest-setting-options-controller.php index 9b4915c384c..d8f30b26be2 100644 --- a/includes/api/class-wc-rest-setting-options-controller.php +++ b/includes/api/class-wc-rest-setting-options-controller.php @@ -32,7 +32,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * * @var string */ - protected $rest_base = 'settings/(?P[\w-]+)'; + protected $rest_base = 'settings/(?P[\w-]+)'; /** * Register routes. @@ -316,7 +316,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * @return array Links for the given setting. */ protected function prepare_links( $setting_id, $group_id ) { - $base = str_replace( '(?P[\w-]+)', $group_id, $this->rest_base ); + $base = str_replace( '(?P[\w-]+)', $group_id, $this->rest_base ); $links = array( 'self' => array( 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $base, $setting_id ) ), From c24882f5d2bd2005a729d179a9160e4946f429f4 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Thu, 23 Mar 2017 20:05:16 -0300 Subject: [PATCH 3/5] Apply change on rest_base to WC_REST_Setting_Options_Controller methods --- includes/api/class-wc-rest-setting-options-controller.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/api/class-wc-rest-setting-options-controller.php b/includes/api/class-wc-rest-setting-options-controller.php index d8f30b26be2..a3b207456aa 100644 --- a/includes/api/class-wc-rest-setting-options-controller.php +++ b/includes/api/class-wc-rest-setting-options-controller.php @@ -105,7 +105,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * @return WP_Error|WP_REST_Response */ public function get_item( $request ) { - $setting = $this->get_setting( $request['group'], $request['id'] ); + $setting = $this->get_setting( $request['group_id'], $request['id'] ); if ( is_wp_error( $setting ) ) { return $setting; @@ -124,7 +124,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * @return WP_Error|WP_REST_Response */ public function get_items( $request ) { - $settings = $this->get_group_settings( $request['group'] ); + $settings = $this->get_group_settings( $request['group_id'] ); if ( is_wp_error( $settings ) ) { return $settings; @@ -255,7 +255,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { * @return WP_Error|WP_REST_Response */ public function update_item( $request ) { - $setting = $this->get_setting( $request['group'], $request['id'] ); + $setting = $this->get_setting( $request['group_id'], $request['id'] ); if ( is_wp_error( $setting ) ) { return $setting; @@ -303,7 +303,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { $data = $this->add_additional_fields_to_object( $data, $request ); $data = $this->filter_response_by_context( $data, empty( $request['context'] ) ? 'view' : $request['context'] ); $response = rest_ensure_response( $data ); - $response->add_links( $this->prepare_links( $data['id'], $request['group'] ) ); + $response->add_links( $this->prepare_links( $data['id'], $request['group_id'] ) ); return $response; } From ffe1deba517305e84733811086e03bb72a2256fe Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Thu, 23 Mar 2017 20:37:42 -0300 Subject: [PATCH 4/5] Disable setting options endpoint in CLI --- includes/cli/class-wc-cli-runner.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/cli/class-wc-cli-runner.php b/includes/cli/class-wc-cli-runner.php index 33fb7e062ab..e9c93a24179 100644 --- a/includes/cli/class-wc-cli-runner.php +++ b/includes/cli/class-wc-cli-runner.php @@ -19,9 +19,9 @@ class WC_CLI_Runner { */ private static $disabled_endpoints = array( 'settings', - 'settings/(?P[\w-]+)', - 'settings/(?P[\w-]+)/batch', - 'settings/(?P[\w-]+)/(?P[\w-]+)', + 'settings/(?P[\w-]+)', + 'settings/(?P[\w-]+)/batch', + 'settings/(?P[\w-]+)/(?P[\w-]+)', 'system_status', 'system_status/tools', 'system_status/tools/(?P[\w-]+)', From b1023c23c68e5a94c98e3e5d4242f1c114f531bf Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Thu, 23 Mar 2017 20:45:19 -0300 Subject: [PATCH 5/5] Fixed setting options endpoint tests --- ...ass-wc-rest-setting-options-controller.php | 32 +++++++++---------- tests/unit-tests/api/settings.php | 15 +++++---- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/includes/api/class-wc-rest-setting-options-controller.php b/includes/api/class-wc-rest-setting-options-controller.php index a3b207456aa..8b26ff5d2b8 100644 --- a/includes/api/class-wc-rest-setting-options-controller.php +++ b/includes/api/class-wc-rest-setting-options-controller.php @@ -55,6 +55,22 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { 'schema' => array( $this, 'get_public_item_schema' ), ) ); + register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( + 'args' => array( + 'group' => array( + 'description' => __( 'Settings group ID.', 'woocommerce' ), + 'type' => 'string', + ), + ), + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array( $this, 'batch_items' ), + 'permission_callback' => array( $this, 'update_items_permissions_check' ), + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), + ), + 'schema' => array( $this, 'get_public_batch_schema' ), + ) ); + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P[\w-]+)', array( 'args' => array( 'group' => array( @@ -79,22 +95,6 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller { ), 'schema' => array( $this, 'get_public_item_schema' ), ) ); - - register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( - 'args' => array( - 'group' => array( - 'description' => __( 'Settings group ID.', 'woocommerce' ), - 'type' => 'string', - ), - ), - array( - 'methods' => WP_REST_Server::EDITABLE, - 'callback' => array( $this, 'batch_items' ), - 'permission_callback' => array( $this, 'update_items_permissions_check' ), - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), - ), - 'schema' => array( $this, 'get_public_batch_schema' ), - ) ); } /** diff --git a/tests/unit-tests/api/settings.php b/tests/unit-tests/api/settings.php index cb1192ab4ed..635fdce9ce0 100644 --- a/tests/unit-tests/api/settings.php +++ b/tests/unit-tests/api/settings.php @@ -28,8 +28,8 @@ class Settings extends WC_REST_Unit_Test_Case { public function test_register_routes() { $routes = $this->server->get_routes(); $this->assertArrayHasKey( '/wc/v2/settings', $routes ); - $this->assertArrayHasKey( '/wc/v2/settings/(?P[\w-]+)', $routes ); - $this->assertArrayHasKey( '/wc/v2/settings/(?P[\w-]+)/(?P[\w-]+)', $routes ); + $this->assertArrayHasKey( '/wc/v2/settings/(?P[\w-]+)', $routes ); + $this->assertArrayHasKey( '/wc/v2/settings/(?P[\w-]+)/(?P[\w-]+)', $routes ); } /** @@ -276,13 +276,14 @@ class Settings extends WC_REST_Unit_Test_Case { $request->set_body_params( array( 'update' => array( array( - 'id' => 'woocommerce_shop_page_display', - 'value' => 'both', + 'id' => 'woocommerce_shop_page_display', + 'value' => 'both', ), ), ) ); $response = $this->server->dispatch( $request ); $data = $response->get_data(); + $this->assertEquals( 'both', $data['update'][0]['value'] ); $this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) ); @@ -373,8 +374,8 @@ class Settings extends WC_REST_Unit_Test_Case { * @since 3.0.0 */ public function test_get_setting_invalid_setting_type() { - // $controller = $this->getMock( 'WC_Rest_Settings_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) ); - $controller = $this->getMockBuilder( 'WC_Rest_Settings_Options_Controller' )->setMethods( array( 'get_group_settings', 'is_setting_type_valid' ) )->getMock(); + // $controller = $this->getMock( 'WC_Rest_Setting_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) ); + $controller = $this->getMockBuilder( 'WC_Rest_Setting_Options_Controller' )->setMethods( array( 'get_group_settings', 'is_setting_type_valid' ) )->getMock(); $controller ->expects( $this->any() ) @@ -433,7 +434,7 @@ class Settings extends WC_REST_Unit_Test_Case { * Test updating a bad setting ID. * * @since 3.0.0 - * @covers WC_Rest_Settings_Options_Controller::update_item + * @covers WC_Rest_Setting_Options_Controller::update_item */ public function test_update_setting_bad_setting_id() { wp_set_current_user( $this->user );