2014-09-01 06:04:02 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* WC API Unit Test Case
|
2014-09-01 06:04:02 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* Provides REST API specific setup/tear down/assert methods, along with some helper.
|
|
|
|
* functions.
|
2014-09-01 06:04:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
*/
|
|
|
|
class WC_API_Unit_Test_Case extends WC_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Setup the test case case.
|
2014-09-01 06:04:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
* @see WC_Unit_Test_Case::setUp()
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// load API classes
|
|
|
|
WC()->api->includes();
|
|
|
|
|
|
|
|
// set user
|
2014-09-01 20:03:20 +00:00
|
|
|
$this->user_id = $this->factory->user->create( array( 'role' => 'shop_manager' ) );
|
|
|
|
wp_set_current_user( $this->user_id );
|
2014-09-01 06:04:02 +00:00
|
|
|
|
|
|
|
// this isn't used, but it causes a warning unless set
|
|
|
|
$_SERVER['REQUEST_METHOD'] = null;
|
|
|
|
|
|
|
|
// mock the API server to prevent headers from being sent
|
2016-08-27 02:39:16 +00:00
|
|
|
$this->mock_server = $this->getMock( 'WC_API_Server', array( 'header' ), array( '/' ) );
|
2014-09-01 06:04:02 +00:00
|
|
|
|
|
|
|
WC()->api->register_resources( $this->mock_server );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Assert the given response is an API error with a specific code and status.
|
2014-09-01 06:04:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
* @param string $code error code, e.g. `woocommerce_api_user_cannot_read_orders_count`
|
|
|
|
* @param int|null $status HTTP status code associated with error, e.g. 400
|
|
|
|
* @param WP_Error $response
|
|
|
|
* @param string $message optional message to render when assertion fails
|
|
|
|
*/
|
|
|
|
public function assertHasAPIError( $code, $status = null, $response, $message = '' ) {
|
|
|
|
|
|
|
|
$this->assertWPError( $response, $message );
|
|
|
|
|
|
|
|
// code
|
|
|
|
$this->assertEquals( $code, $response->get_error_code(), $message );
|
|
|
|
|
|
|
|
// status
|
|
|
|
$data = $response->get_error_data();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'status', $data, $message );
|
|
|
|
$this->assertEquals( $status, $data['status'], $message );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Disable the given capability for the current user, used for testing.
|
|
|
|
* permission checking.
|
2014-09-01 06:04:02 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
* @param string $capability, e.g. `read_private_shop_orders`
|
|
|
|
*/
|
|
|
|
protected function disable_capability( $capability ) {
|
|
|
|
|
|
|
|
$user = wp_get_current_user();
|
|
|
|
$user->add_cap( $capability, false );
|
|
|
|
|
|
|
|
// flush capabilities, see https://core.trac.wordpress.org/ticket/28374
|
|
|
|
$user->get_role_caps();
|
|
|
|
$user->update_user_level_from_caps();
|
|
|
|
}
|
|
|
|
}
|