2016-03-07 16:46:58 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-19 18:24:05 +00:00
|
|
|
* Settings API Tests.
|
|
|
|
*
|
2016-03-07 16:46:58 +00:00
|
|
|
* @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-07-26 21:19:50 +00:00
|
|
|
|
|
|
|
class Settings extends WC_REST_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-06-16 20:11:50 +00:00
|
|
|
$this->endpoint = new WC_Rest_Settings_Options_Controller();
|
2016-06-08 21:01:30 +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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test route registration.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-03-07 18:24:03 +00:00
|
|
|
* @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 );
|
2016-03-24 21:01:22 +00:00
|
|
|
$this->assertArrayHasKey( '/wc/v1/settings/(?P<group>[\w-]+)', $routes );
|
2016-06-08 19:08:49 +00:00
|
|
|
$this->assertArrayHasKey( '/wc/v1/settings/(?P<group>[\w-]+)/(?P<id>[\w-]+)', $routes );
|
2016-03-07 18:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-23 19:36:59 +00:00
|
|
|
* Test getting all groups.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
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-06-08 21:01:30 +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-28 19:04:26 +00:00
|
|
|
$this->assertContains( 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-06-08 19:08:49 +00:00
|
|
|
'sub_groups' => array( 'sub-test' ),
|
|
|
|
'_links' => array(
|
|
|
|
'item' => array(
|
|
|
|
array(
|
|
|
|
'href' => rest_url( '/wc/v1/settings/test' ),
|
|
|
|
'embeddable' => true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2016-03-28 19:04:26 +00:00
|
|
|
), $data );
|
2016-03-07 21:46:40 +00:00
|
|
|
|
2016-03-28 19:04:26 +00:00
|
|
|
$this->assertContains( array(
|
2016-03-24 21:01:22 +00:00
|
|
|
'id' => 'sub-test',
|
|
|
|
'label' => 'Sub test',
|
|
|
|
'parent_id' => 'test',
|
|
|
|
'description' => '',
|
2016-03-28 19:04:26 +00:00
|
|
|
'sub_groups' => array(),
|
2016-06-08 19:08:49 +00:00
|
|
|
'_links' => array(
|
|
|
|
'item' => array(
|
|
|
|
array(
|
|
|
|
'href' => rest_url( '/wc/v1/settings/sub-test' ),
|
|
|
|
'embeddable' => true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2016-03-28 19:04:26 +00:00
|
|
|
), $data );
|
2016-03-07 18:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-23 19:36:59 +00:00
|
|
|
* Test /settings without valid permissions/creds.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
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-06-08 21:01:30 +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-06-13 20:27:54 +00:00
|
|
|
/**
|
|
|
|
* Test /settings without valid permissions/creds.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-06-13 20:27:54 +00:00
|
|
|
* @since 2.7.0
|
2016-06-16 20:19:15 +00:00
|
|
|
* @covers WC_Rest_Settings_Controller::get_items
|
2016-06-13 20:27:54 +00:00
|
|
|
*/
|
|
|
|
public function test_get_groups_none_registered() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
remove_all_filters( 'woocommerce_settings_groups' );
|
|
|
|
|
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings' ) );
|
|
|
|
$this->assertEquals( 500, $response->get_status() );
|
|
|
|
|
|
|
|
WC_Helper_Settings::register();
|
|
|
|
}
|
|
|
|
|
2016-03-07 18:24:03 +00:00
|
|
|
/**
|
2016-03-24 21:01:22 +00:00
|
|
|
* Test groups schema.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
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() {
|
2016-06-08 21:01:30 +00:00
|
|
|
$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'];
|
2016-03-28 19:04:26 +00:00
|
|
|
$this->assertEquals( 5, count( $properties ) );
|
2016-03-07 21:46:40 +00:00
|
|
|
$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-28 19:04:26 +00:00
|
|
|
$this->assertArrayHasKey( 'sub_groups', $properties );
|
2016-03-07 20:44:07 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 21:01:22 +00:00
|
|
|
/**
|
|
|
|
* Test settings schema.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-03-24 21:01:22 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function test_get_setting_schema() {
|
2016-06-08 21:01:30 +00:00
|
|
|
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/settings/test/woocommerce_shop_page_display' );
|
2016-03-24 21:01:22 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
$properties = $data['schema']['properties'];
|
|
|
|
$this->assertEquals( 8, count( $properties ) );
|
|
|
|
$this->assertArrayHasKey( 'id', $properties );
|
|
|
|
$this->assertArrayHasKey( 'label', $properties );
|
|
|
|
$this->assertArrayHasKey( 'description', $properties );
|
|
|
|
$this->assertArrayHasKey( 'default', $properties );
|
|
|
|
$this->assertArrayHasKey( 'tip', $properties );
|
|
|
|
$this->assertArrayHasKey( 'placeholder', $properties );
|
|
|
|
$this->assertArrayHasKey( 'type', $properties );
|
|
|
|
$this->assertArrayHasKey( 'options', $properties );
|
|
|
|
}
|
|
|
|
|
2016-03-07 20:44:07 +00:00
|
|
|
/**
|
2016-03-23 19:36:59 +00:00
|
|
|
* Test getting a single group.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
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-06-13 20:27:54 +00:00
|
|
|
// test route callback receiving an empty group id
|
|
|
|
$result = $this->endpoint->get_group_settings( '' );
|
|
|
|
$this->assertIsWPError( $result );
|
|
|
|
|
2016-03-24 21:01:22 +00:00
|
|
|
// test getting a group that does not exist
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/not-real' ) );
|
2016-03-07 21:46:40 +00:00
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
|
2016-03-24 21:01:22 +00:00
|
|
|
// test getting the 'invalid' group
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/invalid' ) );
|
2016-03-07 21:46:40 +00:00
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
|
2016-03-24 21:01:22 +00:00
|
|
|
// test getting a valid group
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/general' ) );
|
2016-03-07 21:46:40 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2016-06-08 19:08:49 +00:00
|
|
|
$this->assertContains( array(
|
|
|
|
'id' => 'woocommerce_demo_store',
|
|
|
|
'label' => 'Store Notice',
|
|
|
|
'description' => 'Enable site-wide store notice text',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'default' => 'no',
|
|
|
|
'value' => 'no',
|
|
|
|
'_links' => array(
|
|
|
|
'self' => array(
|
|
|
|
array(
|
|
|
|
'href' => rest_url( '/wc/v1/settings/general/woocommerce_demo_store' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
|
|
|
'href' => rest_url( '/wc/v1/settings/general' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
), $data );
|
2016-03-28 19:04:26 +00:00
|
|
|
|
2016-03-24 21:01:22 +00:00
|
|
|
// test getting a valid group with settings attached to it
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$data = $response->get_data();
|
2016-06-08 19:08:49 +00:00
|
|
|
$this->assertEquals( 2, count( $data ) );
|
|
|
|
$this->assertEquals( 'woocommerce_shop_page_display', $data[0]['id'] );
|
|
|
|
$this->assertEmpty( $data[0]['value'] );
|
|
|
|
$this->assertEquals( 'woocommerce_enable_lightbox', $data[1]['id'] );
|
|
|
|
$this->assertEquals( 'yes', $data[1]['value'] );
|
2016-03-07 20:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-23 19:36:59 +00:00
|
|
|
* Test getting a single group without permission.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
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-06-08 21:01:30 +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
|
|
|
/**
|
2016-03-24 21:01:22 +00:00
|
|
|
* Test updating a single setting.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-03-24 21:01:22 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function test_update_setting() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// test defaults first
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test/woocommerce_shop_page_display' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( '', $data['value'] );
|
|
|
|
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test/woocommerce_enable_lightbox' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 'yes', $data['value'] );
|
|
|
|
|
|
|
|
// test updating shop display setting
|
2016-06-08 21:01:30 +00:00
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$request->set_body_params( array(
|
|
|
|
'value' => 'both',
|
|
|
|
) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 'both', $data['value'] );
|
|
|
|
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) );
|
|
|
|
|
2016-06-08 21:01:30 +00:00
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$request->set_body_params( array(
|
|
|
|
'value' => 'subcategories',
|
|
|
|
) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 'subcategories', $data['value'] );
|
|
|
|
$this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) );
|
|
|
|
|
2016-06-08 21:01:30 +00:00
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$request->set_body_params( array(
|
|
|
|
'value' => '',
|
|
|
|
) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( '', $data['value'] );
|
|
|
|
$this->assertEquals( '', get_option( 'woocommerce_shop_page_display' ) );
|
|
|
|
|
|
|
|
// test updating ligtbox
|
2016-06-08 21:01:30 +00:00
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'test', 'woocommerce_enable_lightbox' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$request->set_body_params( array(
|
|
|
|
'value' => 'no',
|
|
|
|
) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 'no', $data['value'] );
|
|
|
|
$this->assertEquals( 'no', get_option( 'woocommerce_enable_lightbox' ) );
|
|
|
|
|
2016-06-08 21:01:30 +00:00
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'test', 'woocommerce_enable_lightbox' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$request->set_body_params( array(
|
|
|
|
'value' => 'yes',
|
|
|
|
) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 'yes', $data['value'] );
|
|
|
|
$this->assertEquals( 'yes', get_option( 'woocommerce_enable_lightbox' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test updating multiple settings at once.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-03-24 21:01:22 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function test_update_settings() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// test defaults first
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$data = $response->get_data();
|
2016-06-08 19:08:49 +00:00
|
|
|
$this->assertEquals( '', $data[0]['value'] );
|
|
|
|
$this->assertEquals( 'yes', $data[1]['value'] );
|
2016-03-24 21:01:22 +00:00
|
|
|
|
|
|
|
// test setting both at once
|
2016-06-08 21:01:30 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v1/settings/test/batch' );
|
2016-03-24 21:01:22 +00:00
|
|
|
$request->set_body_params( array(
|
2016-06-08 19:08:49 +00:00
|
|
|
'update' => array(
|
|
|
|
array(
|
|
|
|
'id' => 'woocommerce_shop_page_display',
|
|
|
|
'value' => 'both',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 'woocommerce_enable_lightbox',
|
|
|
|
'value' => 'no',
|
|
|
|
),
|
2016-03-24 21:01:22 +00:00
|
|
|
),
|
|
|
|
) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
2016-06-08 19:08:49 +00:00
|
|
|
$this->assertEquals( 'both', $data['update'][0]['value'] );
|
2016-03-24 21:01:22 +00:00
|
|
|
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) );
|
2016-06-08 19:08:49 +00:00
|
|
|
$this->assertEquals( 'no', $data['update'][1]['value'] );
|
2016-03-24 21:01:22 +00:00
|
|
|
$this->assertEquals( 'no', get_option( 'woocommerce_enable_lightbox' ) );
|
|
|
|
|
|
|
|
// test updating one, but making sure the other value stays the same
|
2016-06-08 21:01:30 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v1/settings/test/batch' );
|
2016-03-24 21:01:22 +00:00
|
|
|
$request->set_body_params( array(
|
2016-06-08 19:08:49 +00:00
|
|
|
'update' => array(
|
|
|
|
array(
|
|
|
|
'id' => 'woocommerce_shop_page_display',
|
|
|
|
'value' => 'subcategories',
|
|
|
|
),
|
2016-03-24 21:01:22 +00:00
|
|
|
),
|
|
|
|
) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
2016-06-08 19:08:49 +00:00
|
|
|
$this->assertEquals( 'subcategories', $data['update'][0]['value'] );
|
2016-03-24 21:01:22 +00:00
|
|
|
$this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) );
|
2016-06-08 19:08:49 +00:00
|
|
|
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test' ) );
|
2016-06-08 19:08:49 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 'no', $data[1]['value'] );
|
2016-03-24 21:01:22 +00:00
|
|
|
$this->assertEquals( 'no', get_option( 'woocommerce_enable_lightbox' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting a single setting.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-03-24 21:01:22 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function test_get_setting() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// test getting an invalid setting from a group that does not exist
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/not-real/woocommerce_enable_lightbox' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
|
|
|
|
// test getting an invalid setting from a group that does exist
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/invalid/invalid' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
|
|
|
|
// test getting a valid setting
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test/woocommerce_enable_lightbox' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
|
|
|
|
$this->assertEquals( 'woocommerce_enable_lightbox', $data['id'] );
|
|
|
|
$this->assertEquals( 'Product Image Gallery', $data['label'] );
|
|
|
|
$this->assertEquals( 'yes', $data['default'] );
|
|
|
|
$this->assertEquals( 'Product gallery images will open in a lightbox.', $data['tip'] );
|
|
|
|
$this->assertEquals( 'checkbox', $data['type'] );
|
|
|
|
$this->assertEquals( 'yes', $data['value'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting a single setting without valid user permissions.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-03-24 21:01:22 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function test_get_setting_without_permission() {
|
|
|
|
wp_set_current_user( 0 );
|
|
|
|
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/test/woocommerce_enable_lightbox' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$this->assertEquals( 401, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
2016-06-13 20:27:54 +00:00
|
|
|
/**
|
|
|
|
* Tests the GET single setting route handler receiving an empty setting ID.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
2016-06-13 20:27:54 +00:00
|
|
|
*/
|
|
|
|
public function test_get_setting_empty_setting_id() {
|
|
|
|
$result = $this->endpoint->get_setting( 'test', '' );
|
|
|
|
|
|
|
|
$this->assertIsWPError( $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the GET single setting route handler receiving an invalid setting ID.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
2016-06-13 20:27:54 +00:00
|
|
|
*/
|
|
|
|
public function test_get_setting_invalid_setting_id() {
|
|
|
|
$result = $this->endpoint->get_setting( 'test', 'invalid' );
|
|
|
|
|
|
|
|
$this->assertIsWPError( $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the GET single setting route handler encountering an invalid setting type.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
2016-06-13 20:27:54 +00:00
|
|
|
*/
|
|
|
|
public function test_get_setting_invalid_setting_type() {
|
2016-06-16 20:11:50 +00:00
|
|
|
$controller = $this->getMock( 'WC_Rest_Settings_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) );
|
2016-06-13 20:27:54 +00:00
|
|
|
|
|
|
|
$controller
|
|
|
|
->expects( $this->any() )
|
|
|
|
->method( 'get_group_settings' )
|
|
|
|
->will( $this->returnValue( WC_Helper_Settings::register_test_settings( array() ) ) );
|
|
|
|
|
|
|
|
$controller
|
|
|
|
->expects( $this->any() )
|
|
|
|
->method( 'is_setting_type_valid' )
|
|
|
|
->will( $this->returnValue( false ) );
|
|
|
|
|
|
|
|
$result = $controller->get_setting( 'test', 'woocommerce_enable_lightbox' );
|
|
|
|
|
|
|
|
$this->assertIsWPError( $result );
|
|
|
|
}
|
2016-03-24 21:01:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test updating a single setting without valid user permissions.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-03-24 21:01:22 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function test_update_setting_without_permission() {
|
|
|
|
wp_set_current_user( 0 );
|
|
|
|
|
2016-06-08 21:01:30 +00:00
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'test', 'woocommerce_enable_lightbox' ) );
|
2016-03-24 21:01:22 +00:00
|
|
|
$request->set_body_params( array(
|
|
|
|
'value' => 'yes',
|
|
|
|
) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertEquals( 401, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test updating multiple settings without valid user permissions.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-03-24 21:01:22 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function test_update_settings_without_permission() {
|
|
|
|
wp_set_current_user( 0 );
|
|
|
|
|
2016-06-08 21:01:30 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v1/settings/test/batch' );
|
2016-03-24 21:01:22 +00:00
|
|
|
$request->set_body_params( array(
|
2016-06-08 19:08:49 +00:00
|
|
|
'update' => array(
|
|
|
|
array(
|
|
|
|
'id' => 'woocommerce_shop_page_display',
|
|
|
|
'value' => 'subcategories',
|
|
|
|
),
|
2016-03-24 21:01:22 +00:00
|
|
|
),
|
|
|
|
) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertEquals( 401, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
2016-06-13 20:27:54 +00:00
|
|
|
/**
|
|
|
|
* Test updating a bad setting ID.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-06-13 20:27:54 +00:00
|
|
|
* @since 2.7.0
|
2016-06-16 20:11:50 +00:00
|
|
|
* @covers WC_Rest_Settings_Options_Controller::update_item
|
2016-06-13 20:27:54 +00:00
|
|
|
*/
|
|
|
|
public function test_update_setting_bad_setting_id() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v1/settings/test/invalid' );
|
|
|
|
$request->set_body_params( array(
|
|
|
|
'value' => 'test',
|
|
|
|
) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
2016-03-28 19:04:26 +00:00
|
|
|
/**
|
2016-06-08 19:08:49 +00:00
|
|
|
* Tests our classic setting registration to make sure settings added for WP-Admin are available over the API.
|
2016-07-19 18:24:05 +00:00
|
|
|
*
|
2016-03-28 19:04:26 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function test_classic_settings() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// Make sure the group is properly registered
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/products' ) );
|
2016-03-28 19:04:26 +00:00
|
|
|
$data = $response->get_data();
|
2016-06-08 19:08:49 +00:00
|
|
|
$this->assertTrue( is_array( $data ) );
|
2016-03-28 19:04:26 +00:00
|
|
|
$this->assertContains( array(
|
2016-06-08 19:08:49 +00:00
|
|
|
'id' => 'woocommerce_downloads_require_login',
|
|
|
|
'label' => 'Access Restriction',
|
2016-03-28 19:04:26 +00:00
|
|
|
'description' => 'Downloads require login',
|
2016-06-08 19:08:49 +00:00
|
|
|
'type' => 'checkbox',
|
|
|
|
'default' => 'no',
|
|
|
|
'tip' => 'This setting does not apply to guest purchases.',
|
|
|
|
'value' => 'no',
|
|
|
|
'_links' => array(
|
|
|
|
'self' => array(
|
|
|
|
array(
|
|
|
|
'href' => rest_url( '/wc/v1/settings/products/woocommerce_downloads_require_login' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
|
|
|
'href' => rest_url( '/wc/v1/settings/products' ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
), $data );
|
2016-03-28 19:04:26 +00:00
|
|
|
|
|
|
|
// test get single
|
2016-06-08 21:01:30 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/settings/products/woocommerce_dimension_unit' ) );
|
2016-03-28 19:04:26 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 'cm', $data['default'] );
|
|
|
|
|
|
|
|
// test update
|
2016-06-08 21:01:30 +00:00
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v1/settings/%s/%s', 'products', 'woocommerce_dimension_unit' ) );
|
2016-03-28 19:04:26 +00:00
|
|
|
$request->set_body_params( array(
|
|
|
|
'value' => 'yd',
|
|
|
|
) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 'yd', $data['value'] );
|
|
|
|
$this->assertEquals( 'yd', get_option(' woocommerce_dimension_unit' ) );
|
|
|
|
}
|
|
|
|
|
2016-03-07 16:46:58 +00:00
|
|
|
}
|