Add a test for settings groups, make sure any bad keys/values are filtered out, update docs.
This commit is contained in:
parent
785e43a207
commit
f425a3735a
|
@ -54,7 +54,7 @@ class WC_Rest_Settings_Controller extends WP_Rest_Controller {
|
|||
*/
|
||||
public function permissions_check( $request ) {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
// return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot access settings.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot access settings.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -129,7 +129,10 @@ class WC_Rest_Settings_Controller extends WP_Rest_Controller {
|
|||
$groups = apply_filters( 'woocommerce_settings_groups_' . $location['id'], array() );
|
||||
if ( ! empty( $groups ) ) {
|
||||
foreach ( $groups as $group ) {
|
||||
$location['groups'][] = array( 'id' => $group['id'], 'label' => $group['label'], 'description' => $group['description'] );
|
||||
$location['groups'][] = array_intersect_key(
|
||||
$group,
|
||||
array_flip( array_filter( array_keys( $group ), array( $this, 'filter_group_keys' ) ) )
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -144,7 +147,7 @@ class WC_Rest_Settings_Controller extends WP_Rest_Controller {
|
|||
}
|
||||
|
||||
/**
|
||||
* Callback for Allowed keys for each location response.
|
||||
* Callback for allowed keys for each location response.
|
||||
* @since 2.7.0
|
||||
* @param string $key Key to check
|
||||
* @return boolean
|
||||
|
@ -153,6 +156,16 @@ class WC_Rest_Settings_Controller extends WP_Rest_Controller {
|
|||
return in_array( $key, array( 'id', 'type', 'label', 'description', 'groups' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for allowed keys for each group.
|
||||
* @since 2.7.0
|
||||
* @param string $key Key to check
|
||||
* @return boolean
|
||||
*/
|
||||
public function filter_group_keys( $key ) {
|
||||
return in_array( $key, array( 'id', 'label', 'description' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get supported query parameters for locations.
|
||||
* @since 2.7.0
|
||||
|
|
|
@ -20,7 +20,7 @@ The coupon data box is considered location and would be represented like so:
|
|||
}
|
||||
|
||||
|
||||
There are 4 fields that make up a location:
|
||||
There are 4 fields that make up all locations:
|
||||
|
||||
* _id_: A unique identifier that can be used to link settings together. This shoud be a unique (for the whole system) value. Prefixing with your plugin slug is recommended for non-core changes. Alphanumeric string that contains no spaces. Required.
|
||||
* _type_: Context for where the settings in this location are going to be displayed. Right now core accepts 'page' for settings pages (pages currently under WooCommerce > Settings), 'metabox' (for metabox grouped settings like Coupon Data - this name is subject to change as this API develops), and 'shipping-zone' for settings associated with shipping zone settings. Alphanumeric string that contains no spaces. Required, defaults to 'page'.
|
||||
|
@ -29,6 +29,9 @@ There are 4 fields that make up a location:
|
|||
|
||||
Any other fields passed will be stripped out before the JSON response is sent back to the client.
|
||||
|
||||
Locations with type 'page' can also have an associated set of 'groups' which are sub sections/pages. See [groups.md](groups.md).
|
||||
Pages will return an array of groups in their response.
|
||||
|
||||
## Registering a Location
|
||||
|
||||
Locations can be registered with the `woocommerce_settings_locations` filter:
|
||||
|
@ -53,4 +56,4 @@ There is an optional ?type parameter that allows you to return only locations fo
|
|||
|
||||
### GET /settings/locations/$id
|
||||
|
||||
Returns information on a single location.
|
||||
Returns information on a single location + any associated groups.
|
|
@ -13,6 +13,7 @@ class WC_Helper_Settings {
|
|||
*/
|
||||
public static function register() {
|
||||
add_filter( 'woocommerce_settings_locations', array( 'WC_Helper_Settings', 'register_locations' ) );
|
||||
add_filter( 'woocommerce_settings_groups_test', array( 'WC_Helper_Settings', 'register_test_groups' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -29,6 +30,12 @@ class WC_Helper_Settings {
|
|||
'label' => __( 'Test Extension', 'woocommerce' ),
|
||||
'description' => __( 'My awesome test settings.', 'woocommerce' ),
|
||||
);
|
||||
$locations[] = array(
|
||||
'id' => 'test-2',
|
||||
'type' => 'page',
|
||||
'label' => __( 'Test Extension', 'woocommerce' ),
|
||||
'description' => '',
|
||||
);
|
||||
$locations[] = array(
|
||||
'id' => 'coupon-data',
|
||||
'type' => 'metabox',
|
||||
|
@ -43,4 +50,22 @@ class WC_Helper_Settings {
|
|||
return $locations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers some example groups for the 'test' page.
|
||||
* @since 2.7.0
|
||||
* @param array $groups
|
||||
* @return array
|
||||
*/
|
||||
public static function register_test_groups( $groups ) {
|
||||
$groups[] = array(
|
||||
'id' => 'general',
|
||||
'label' => __( 'General', 'woocommerce' ),
|
||||
);
|
||||
$groups[] = array(
|
||||
'id' => 'inventory',
|
||||
'label' => __( 'Inventory', 'woocommerce' ),
|
||||
);
|
||||
return $groups;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ class Settings extends \WC_Unit_Test_Case {
|
|||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $data ) );
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
|
||||
$this->check_get_location_response( $data[0], array(
|
||||
'id' => 'test',
|
||||
|
@ -63,7 +63,7 @@ class Settings extends \WC_Unit_Test_Case {
|
|||
'description' => 'My awesome test settings.',
|
||||
) );
|
||||
|
||||
$this->check_get_location_response( $data[1], array(
|
||||
$this->check_get_location_response( $data[2], array(
|
||||
'id' => 'coupon-data',
|
||||
'type' => 'metabox',
|
||||
'label' => 'Coupon Data',
|
||||
|
@ -108,7 +108,7 @@ class Settings extends \WC_Unit_Test_Case {
|
|||
$request->set_param( 'type', 'not-a-real-type' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 2, count( $data ) ); // all results
|
||||
$this->assertEquals( 3, count( $data ) ); // all results
|
||||
|
||||
$request = new \WP_REST_Request( 'GET', '/wc/v1/settings/locations' );
|
||||
$request->set_param( 'type', 'page' );
|
||||
|
@ -116,14 +116,7 @@ class Settings extends \WC_Unit_Test_Case {
|
|||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $data ) );
|
||||
|
||||
$this->check_get_location_response( $data[0], array(
|
||||
'id' => 'test',
|
||||
'type' => 'page',
|
||||
'label' => 'Test Extension',
|
||||
'description' => 'My awesome test settings.',
|
||||
) );
|
||||
$this->assertEquals( 2, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -184,6 +177,31 @@ class Settings extends \WC_Unit_Test_Case {
|
|||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test settings groups (for pages)
|
||||
* @since 2.7.0
|
||||
*/
|
||||
public function test_settings_groups() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test getting a non page location
|
||||
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/locations/coupon-data' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayNotHasKey( 'groups', $data );
|
||||
|
||||
// test getting a page with no groups
|
||||
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/locations/test-2' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'groups', $data );
|
||||
$this->assertEmpty( $data['groups'] );
|
||||
|
||||
// test getting a page with groups
|
||||
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/locations/test' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertArrayHasKey( 'groups', $data );
|
||||
$this->assertEquals( 2, count( $data['groups'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure valid location data response.
|
||||
* @since 2.7.0
|
||||
|
|
Loading…
Reference in New Issue