More setting location tests.

This commit is contained in:
Justin Shreve 2016-03-07 13:46:40 -08:00
parent 5ee4b5d4dd
commit 21b66f94ff
4 changed files with 154 additions and 10 deletions

View File

@ -72,12 +72,7 @@ class WC_Rest_Settings_Controller extends WP_Rest_Controller {
*/ */
public function get_locations( $request ) { public function get_locations( $request ) {
$locations = apply_filters( 'woocommerce_settings_locations', array() ); $locations = apply_filters( 'woocommerce_settings_locations', array() );
$defaults = array( $defaults = $this->get_location_defaults();
'id' => null,
'type' => 'page',
'label' => null,
'description' => '',
);
$filtered_locations = array(); $filtered_locations = array();
foreach ( $locations as $location ) { foreach ( $locations as $location ) {
$location = wp_parse_args( $location, $defaults ); $location = wp_parse_args( $location, $defaults );
@ -119,6 +114,8 @@ class WC_Rest_Settings_Controller extends WP_Rest_Controller {
} }
$location = $locations[ $index_key ]; $location = $locations[ $index_key ];
$defaults = $this->get_location_defaults();
$location = wp_parse_args( $location, $defaults );
if ( is_null( $location['id'] ) || is_null( $location['label'] ) || is_null( $location['type'] ) ) { if ( is_null( $location['id'] ) || is_null( $location['label'] ) || is_null( $location['type'] ) ) {
return new WP_Error( 'rest_setting_location_invalid_id', __( 'Invalid location id.' ), array( 'status' => 404 ) ); return new WP_Error( 'rest_setting_location_invalid_id', __( 'Invalid location id.' ), array( 'status' => 404 ) );
} }
@ -212,6 +209,21 @@ class WC_Rest_Settings_Controller extends WP_Rest_Controller {
return apply_filters( 'woocommerce_settings_location_types', array( 'page', 'metabox', 'shipping-zone' ) ); return apply_filters( 'woocommerce_settings_location_types', array( 'page', 'metabox', 'shipping-zone' ) );
} }
/**
* Returns default settings for the various locations. null means the field is required.
* @todo move this?
* @since 2.7.0
* @return array
*/
protected function get_location_defaults() {
return array(
'id' => null,
'type' => 'page',
'label' => null,
'description' => '',
);
}
/** /**
* Returns the array key for a specific location ID so it can be pulled out of the 'locations' array. * Returns the array key for a specific location ID so it can be pulled out of the 'locations' array.
* @todo move this? * @todo move this?
@ -221,7 +233,7 @@ class WC_Rest_Settings_Controller extends WP_Rest_Controller {
*/ */
protected function get_array_key_from_location_id( $locations, $id ) { protected function get_array_key_from_location_id( $locations, $id ) {
foreach ( $locations as $key => $location ) { foreach ( $locations as $key => $location ) {
if ( $id === $location['id'] ) { if ( ! empty( $location['id'] ) && $id === $location['id'] ) {
return $key; return $key;
} }
} }

View File

@ -112,6 +112,7 @@ class WC_Unit_Tests_Bootstrap {
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-order.php' ); require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-order.php' );
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-shipping-zones.php' ); require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-shipping-zones.php' );
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-payment-token.php' ); require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-payment-token.php' );
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-settings.php' );
/** /**
* WP-API * WP-API

View File

@ -0,0 +1,46 @@
<?php
/**
* Class WC_Helper_Settings.
*
* This helper class should ONLY be used for unit tests!
*/
class WC_Helper_Settings {
/**
* Hooks in some dummy data for testing the settings REST API.
* @since 2.7.0
*/
public static function register() {
add_filter( 'woocommerce_settings_locations', array( 'WC_Helper_Settings', 'register_locations' ) );
}
/**
* Registers some example locations, including invalid ones that should not show up in JSON responses.
* @since 2.7.0
* @param array $locations
* @return array
*/
public static function register_locations( $locations ) {
$locations[] = array(
'id' => 'test',
'type' => 'page',
'bad' => 'value',
'label' => __( 'Test Extension', 'woocommerce' ),
'description' => __( 'My awesome test settings.', 'woocommerce' ),
);
$locations[] = array(
'id' => 'coupon-data',
'type' => 'metabox',
'label' => __( 'Coupon Data', 'woocommerce' ),
);
$locations[] = array(
'label' => __( 'Invalid', 'woocommerce' ),
);
$locations[] = array(
'id' => 'invalid',
);
return $locations;
}
}

View File

@ -11,6 +11,7 @@ class Settings extends \WP_Test_REST_Controller_Testcase {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->endpoint = new \WC_Rest_Settings_Controller(); $this->endpoint = new \WC_Rest_Settings_Controller();
\WC_Helper_Settings::register();
} }
/** /**
@ -27,7 +28,25 @@ class Settings extends \WP_Test_REST_Controller_Testcase {
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_locations() { public function test_get_locations() {
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/locations' ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 2, count( $data ) );
$this->check_get_location_response( $data[0], array(
'id' => 'test',
'type' => 'page',
'label' => 'Test Extension',
'description' => 'My awesome test settings.',
) );
$this->check_get_location_response( $data[1], array(
'id' => 'coupon-data',
'type' => 'metabox',
'label' => 'Coupon Data',
'description' => '',
) );
} }
/** /**
@ -43,8 +62,12 @@ class Settings extends \WP_Test_REST_Controller_Testcase {
* Handles required fields and bogus fields. * Handles required fields and bogus fields.
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_locations_filters_values() { public function test_get_locations_correctly_filters_values() {
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/locations' ) );
$data = $response->get_data();
$this->assertEquals( 'test', $data[0]['id'] );
$this->assertArrayNotHasKey( 'bad', $data[0] );
} }
/** /**
@ -52,15 +75,42 @@ class Settings extends \WP_Test_REST_Controller_Testcase {
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_locations_with_type() { public function test_get_locations_with_type() {
$request = new \WP_REST_Request( 'GET', '/wc/v1/settings/locations' );
$request->set_param( 'type', 'not-a-real-type' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 2, count( $data ) ); // all results
$request = new \WP_REST_Request( 'GET', '/wc/v1/settings/locations' );
$request->set_param( 'type', 'page' );
$response = $this->server->dispatch( $request );
$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.',
) );
} }
/** /**
* Test /settings/locations schema. * Test /settings/locations schema.
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_item_schema() { public function test_get_location_schema() {
$request = new \WP_REST_Request( 'OPTIONS', '/wc/v1/settings/locations' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 4, count( $properties ) );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'type', $properties );
$this->assertArrayHasKey( 'label', $properties );
$this->assertArrayHasKey( 'description', $properties );
} }
/** /**
@ -68,7 +118,28 @@ class Settings extends \WP_Test_REST_Controller_Testcase {
* @since 2.7.0 * @since 2.7.0
*/ */
public function test_get_location() { public function test_get_location() {
// test getting a location that does not exist
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/locations/not-real' ) );
$data = $response->get_data();
$this->assertEquals( 404, $response->get_status() );
// test getting the 'invalid' location
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/locations/invalid' ) );
$data = $response->get_data();
$this->assertEquals( 404, $response->get_status() );
// test getting a valid location
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/locations/coupon-data' ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->check_get_location_response( $data, array(
'id' => 'coupon-data',
'type' => 'metabox',
'label' => 'Coupon Data',
'description' => '',
) );
} }
/** /**
@ -86,5 +157,19 @@ class Settings extends \WP_Test_REST_Controller_Testcase {
public function test_update_item() { } public function test_update_item() { }
public function test_delete_item() { } public function test_delete_item() { }
public function test_prepare_item() { } public function test_prepare_item() { }
public function test_get_item_schema() { }
/**
* Ensure valid location data response.
* @since 2.7.0
* @param array $response
* @param array $expected
*/
protected function check_get_location_response( $response, $expected ) {
$this->assertEquals( $expected['id'], $response['id'] );
$this->assertEquals( $expected['type'], $response['type'] );
$this->assertEquals( $expected['label'], $response['label'] );
$this->assertEquals( $expected['description'], $response['description'] );
}
} }