Merge pull request #13751 from woocommerce/fix/rest-api-setting-options-rest-base

[REST API] Fix setting options rest base
This commit is contained in:
Mike Jolley 2017-03-24 10:26:22 +00:00 committed by GitHub
commit 5cb3b9e0ea
3 changed files with 24 additions and 23 deletions

View File

@ -1,6 +1,6 @@
<?php
/**
* REST API Settings Options controller
* REST API Setting Options controller
*
* Handles requests to the /settings/$group/$setting endpoints.
*
@ -15,7 +15,7 @@ if ( ! defined( 'ABSPATH' ) ) {
}
/**
* REST API Settings Options controller class.
* REST API Setting Options controller class.
*
* @package WooCommerce/API
* @extends WC_REST_Controller
@ -32,7 +32,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller {
*
* @var string
*/
protected $rest_base = 'settings';
protected $rest_base = 'settings/(?P<group_id>[\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<group>[\w-]+)', array(
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
'args' => array(
'group' => array(
'description' => __( 'Settings group ID.', 'woocommerce' ),
@ -55,7 +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<group>[\w-]+)/batch', array(
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
'args' => array(
'group' => array(
'description' => __( 'Settings group ID.', 'woocommerce' ),
@ -71,7 +71,7 @@ class WC_REST_Setting_Options_Controller extends WC_REST_Controller {
'schema' => array( $this, 'get_public_batch_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<group>[\w-]+)/(?P<id>[\w-]+)', array(
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\w-]+)', array(
'args' => array(
'group' => array(
'description' => __( 'Settings group ID.', 'woocommerce' ),
@ -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;
}
@ -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<group_id>[\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 ) ),
),
);

View File

@ -19,9 +19,9 @@ class WC_CLI_Runner {
*/
private static $disabled_endpoints = array(
'settings',
'settings/(?P<group>[\w-]+)',
'settings/(?P<group>[\w-]+)/batch',
'settings/(?P<group>[\w-]+)/(?P<id>[\w-]+)',
'settings/(?P<group_id>[\w-]+)',
'settings/(?P<group_id>[\w-]+)/batch',
'settings/(?P<group_id>[\w-]+)/(?P<id>[\w-]+)',
'system_status',
'system_status/tools',
'system_status/tools/(?P<id>[\w-]+)',

View File

@ -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<group>[\w-]+)', $routes );
$this->assertArrayHasKey( '/wc/v2/settings/(?P<group>[\w-]+)/(?P<id>[\w-]+)', $routes );
$this->assertArrayHasKey( '/wc/v2/settings/(?P<group_id>[\w-]+)', $routes );
$this->assertArrayHasKey( '/wc/v2/settings/(?P<group_id>[\w-]+)/(?P<id>[\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 );