2016-03-07 16:46:58 +00:00
|
|
|
<?php
|
|
|
|
namespace WooCommerce\Tests\API;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Settings API Tests
|
|
|
|
* @package WooCommerce\Tests\API
|
2016-03-07 18:24:03 +00:00
|
|
|
* @since 2.7.0
|
2016-03-07 16:46:58 +00:00
|
|
|
*/
|
2016-03-21 18:29:27 +00:00
|
|
|
class Settings extends \WC_Unit_Test_Case {
|
2016-03-07 18:24:03 +00:00
|
|
|
|
2016-03-21 18:29:27 +00:00
|
|
|
protected $server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup our test server, endpoints, and user info.
|
|
|
|
*/
|
2016-03-07 18:24:03 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2016-03-21 18:29:27 +00:00
|
|
|
global $wp_rest_server;
|
|
|
|
$this->server = $wp_rest_server = new \WP_Test_Spy_REST_Server;
|
|
|
|
do_action( 'rest_api_init' );
|
2016-03-07 18:24:03 +00:00
|
|
|
$this->endpoint = new \WC_Rest_Settings_Controller();
|
2016-03-07 21:46:40 +00:00
|
|
|
\WC_Helper_Settings::register();
|
2016-03-07 21:54:28 +00:00
|
|
|
$this->user = $this->factory->user->create( array(
|
|
|
|
'role' => 'administrator',
|
|
|
|
) );
|
2016-03-07 18:24:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 18:29:27 +00:00
|
|
|
/**
|
|
|
|
* Unset the server.
|
|
|
|
*/
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
global $wp_rest_server;
|
|
|
|
$wp_rest_server = null;
|
|
|
|
}
|
|
|
|
|
2016-03-07 18:24:03 +00:00
|
|
|
/**
|
|
|
|
* Test route registration.
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function test_register_routes() {
|
|
|
|
$routes = $this->server->get_routes();
|
2016-03-23 19:36:59 +00:00
|
|
|
$this->assertArrayHasKey( '/wc/v1/settings', $routes );
|
|
|
|
// @todo test others
|
2016-03-07 18:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-23 19:36:59 +00:00
|
|
|
* Test getting all groups.
|
2016-03-07 18:24:03 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
2016-03-23 19:36:59 +00:00
|
|
|
public function test_get_groups() {
|
2016-03-07 21:54:28 +00:00
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
2016-03-23 19:36:59 +00:00
|
|
|
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings' ) );
|
2016-03-07 21:46:40 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2016-03-21 21:45:08 +00:00
|
|
|
$this->assertEquals( 3, count( $data ) );
|
2016-03-07 21:46:40 +00:00
|
|
|
|
2016-03-23 19:36:59 +00:00
|
|
|
$this->check_get_group_response( $data[0], array(
|
2016-03-07 21:46:40 +00:00
|
|
|
'id' => 'test',
|
|
|
|
'label' => 'Test Extension',
|
2016-03-23 19:36:59 +00:00
|
|
|
'parent_id' => '',
|
2016-03-07 21:46:40 +00:00
|
|
|
'description' => 'My awesome test settings.',
|
|
|
|
) );
|
|
|
|
|
2016-03-23 19:36:59 +00:00
|
|
|
$this->check_get_group_response( $data[2], array(
|
2016-03-07 21:46:40 +00:00
|
|
|
'id' => 'coupon-data',
|
|
|
|
'label' => 'Coupon Data',
|
2016-03-23 19:36:59 +00:00
|
|
|
'parent_id' => '',
|
2016-03-07 21:46:40 +00:00
|
|
|
'description' => '',
|
|
|
|
) );
|
2016-03-07 18:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-23 19:36:59 +00:00
|
|
|
* Test /settings without valid permissions/creds.
|
2016-03-07 18:24:03 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
2016-03-23 19:36:59 +00:00
|
|
|
public function test_get_groups_without_permission() {
|
2016-03-07 21:54:28 +00:00
|
|
|
wp_set_current_user( 0 );
|
2016-03-07 18:24:03 +00:00
|
|
|
|
2016-03-23 19:36:59 +00:00
|
|
|
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings' ) );
|
2016-03-07 21:54:28 +00:00
|
|
|
$this->assertEquals( 401, $response->get_status() );
|
2016-03-07 18:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-23 19:36:59 +00:00
|
|
|
* Test /settings/ correctly filters out bad values.
|
2016-03-07 18:24:03 +00:00
|
|
|
* Handles required fields and bogus fields.
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
2016-03-23 19:36:59 +00:00
|
|
|
public function test_get_groups_correctly_filters_values() {
|
2016-03-07 21:54:28 +00:00
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
2016-03-23 19:36:59 +00:00
|
|
|
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings' ) );
|
2016-03-07 21:46:40 +00:00
|
|
|
$data = $response->get_data();
|
2016-03-07 18:24:03 +00:00
|
|
|
|
2016-03-07 21:46:40 +00:00
|
|
|
$this->assertEquals( 'test', $data[0]['id'] );
|
|
|
|
$this->assertArrayNotHasKey( 'bad', $data[0] );
|
2016-03-07 18:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-23 19:36:59 +00:00
|
|
|
* Test /settings schema.
|
2016-03-07 18:24:03 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
2016-03-23 19:36:59 +00:00
|
|
|
public function test_get_group_schema() {
|
|
|
|
$request = new \WP_REST_Request( 'OPTIONS', '/wc/v1/settings' );
|
2016-03-07 21:46:40 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
$properties = $data['schema']['properties'];
|
|
|
|
$this->assertEquals( 4, count( $properties ) );
|
|
|
|
$this->assertArrayHasKey( 'id', $properties );
|
2016-03-23 19:36:59 +00:00
|
|
|
$this->assertArrayHasKey( 'parent_id', $properties );
|
2016-03-07 21:46:40 +00:00
|
|
|
$this->assertArrayHasKey( 'label', $properties );
|
|
|
|
$this->assertArrayHasKey( 'description', $properties );
|
2016-03-07 20:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-23 19:36:59 +00:00
|
|
|
* Test getting a single group.
|
2016-03-07 20:44:07 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
2016-03-23 19:36:59 +00:00
|
|
|
public function test_get_group() {
|
2016-03-07 21:54:28 +00:00
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
2016-03-07 21:46:40 +00:00
|
|
|
// test getting a location that does not exist
|
2016-03-23 19:36:59 +00:00
|
|
|
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/not-real' ) );
|
2016-03-07 21:46:40 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
|
|
|
|
// test getting the 'invalid' location
|
2016-03-23 19:36:59 +00:00
|
|
|
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/invalid' ) );
|
2016-03-07 21:46:40 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
|
|
|
|
// test getting a valid location
|
2016-03-23 19:36:59 +00:00
|
|
|
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/coupon-data' ) );
|
2016-03-07 21:46:40 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
|
2016-03-23 19:36:59 +00:00
|
|
|
$this->check_get_group_response( $data, array(
|
2016-03-07 21:46:40 +00:00
|
|
|
'id' => 'coupon-data',
|
|
|
|
'label' => 'Coupon Data',
|
2016-03-23 19:36:59 +00:00
|
|
|
'parent_id' => '',
|
2016-03-07 21:46:40 +00:00
|
|
|
'description' => '',
|
|
|
|
) );
|
2016-03-23 19:36:59 +00:00
|
|
|
|
|
|
|
// @todo make sure settings are set correctly
|
2016-03-07 20:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-23 19:36:59 +00:00
|
|
|
* Test getting a single group without permission.
|
2016-03-07 20:44:07 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
2016-03-23 19:36:59 +00:00
|
|
|
public function test_get_group_without_permission() {
|
2016-03-07 21:54:28 +00:00
|
|
|
wp_set_current_user( 0 );
|
2016-03-07 20:44:07 +00:00
|
|
|
|
2016-03-23 19:36:59 +00:00
|
|
|
$response = $this->server->dispatch( new \WP_REST_Request( 'GET', '/wc/v1/settings/coupon-data' ) );
|
2016-03-07 21:54:28 +00:00
|
|
|
$this->assertEquals( 401, $response->get_status() );
|
2016-03-07 20:44:07 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 21:46:40 +00:00
|
|
|
/**
|
|
|
|
* Ensure valid location data response.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param array $response
|
|
|
|
* @param array $expected
|
|
|
|
*/
|
2016-03-23 19:36:59 +00:00
|
|
|
protected function check_get_group_response( $response, $expected ) {
|
2016-03-07 21:46:40 +00:00
|
|
|
$this->assertEquals( $expected['id'], $response['id'] );
|
2016-03-23 19:36:59 +00:00
|
|
|
$this->assertEquals( $expected['parent_id'], $response['parent_id'] );
|
2016-03-07 21:46:40 +00:00
|
|
|
$this->assertEquals( $expected['label'], $response['label'] );
|
|
|
|
$this->assertEquals( $expected['description'], $response['description'] );
|
|
|
|
}
|
2016-03-07 16:46:58 +00:00
|
|
|
|
|
|
|
}
|