Merge pull request #11756 from woothemes/update/coupon-api-crud

Update Coupon API to use CRUD
This commit is contained in:
Claudio Sanches 2016-08-23 19:51:27 -03:00 committed by GitHub
commit 5e069a4f4e
5 changed files with 868 additions and 357 deletions

View File

@ -149,28 +149,28 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
$coupon = new WC_Coupon( $code );
$data = array(
'id' => $coupon->id,
'code' => $coupon->code,
'id' => $coupon->get_id(),
'code' => $coupon->get_code(),
'date_created' => wc_rest_prepare_date_response( $post->post_date_gmt ),
'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt ),
'discount_type' => $coupon->type,
'description' => $post->post_excerpt,
'amount' => wc_format_decimal( $coupon->coupon_amount, 2 ),
'expiry_date' => ( ! empty( $coupon->expiry_date ) ) ? wc_rest_prepare_date_response( $coupon->expiry_date ) : null,
'usage_count' => (int) $coupon->usage_count,
'individual_use' => ( 'yes' === $coupon->individual_use ),
'product_ids' => array_map( 'absint', (array) $coupon->product_ids ),
'exclude_product_ids' => array_map( 'absint', (array) $coupon->exclude_product_ids ),
'usage_limit' => ( ! empty( $coupon->usage_limit ) ) ? $coupon->usage_limit : null,
'usage_limit_per_user' => ( ! empty( $coupon->usage_limit_per_user ) ) ? $coupon->usage_limit_per_user : null,
'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items,
'free_shipping' => $coupon->enable_free_shipping(),
'product_categories' => array_map( 'absint', (array) $coupon->product_categories ),
'excluded_product_categories' => array_map( 'absint', (array) $coupon->exclude_product_categories ),
'exclude_sale_items' => $coupon->exclude_sale_items(),
'minimum_amount' => wc_format_decimal( $coupon->minimum_amount, 2 ),
'maximum_amount' => wc_format_decimal( $coupon->maximum_amount, 2 ),
'email_restrictions' => $coupon->customer_email,
'discount_type' => $coupon->get_discount_type(),
'description' => $coupon->get_description(),
'amount' => wc_format_decimal( $coupon->get_amount(), 2 ),
'expiry_date' => ( ! empty( $coupon->get_expiry_date() ) ) ? wc_rest_prepare_date_response( $coupon->get_expiry_date() ) : null,
'usage_count' => (int) $coupon->get_usage_count(),
'individual_use' => $coupon->get_individual_use(),
'product_ids' => array_map( 'absint', (array) $coupon->get_product_ids() ),
'exclude_product_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_ids() ),
'usage_limit' => ( ! empty( $coupon->get_usage_limit() ) ) ? $coupon->get_usage_limit() : null,
'usage_limit_per_user' => ( ! empty( $coupon->get_usage_limit_per_user() ) ) ? $coupon->get_usage_limit_per_user() : null,
'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(),
'free_shipping' => $coupon->get_free_shipping(),
'product_categories' => array_map( 'absint', (array) $coupon->get_product_categories() ),
'excluded_product_categories' => array_map( 'absint', (array) $coupon->get_excluded_product_categories() ),
'exclude_sale_items' => $coupon->get_exclude_sale_items(),
'minimum_amount' => wc_format_decimal( $coupon->get_minimum_amount(), 2 ),
'maximum_amount' => wc_format_decimal( $coupon->get_maximum_amount(), 2 ),
'email_restrictions' => $coupon->get_email_restrictions(),
'used_by' => $coupon->get_used_by(),
);
@ -205,17 +205,18 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
protected function prepare_item_for_database( $request ) {
global $wpdb;
$data = new stdClass;
// ID.
if ( isset( $request['id'] ) ) {
$data->ID = absint( $request['id'] );
$code = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE id = %d AND post_type = 'shop_coupon' AND post_status = 'publish'", $request['id'] ) );
$coupon = new WC_Coupon( $code );
} else {
$coupon = new WC_Coupon();
}
$schema = $this->get_item_schema();
// Validate required POST fields.
if ( 'POST' === $request->get_method() && empty( $data->ID ) ) {
if ( 'POST' === $request->get_method() && empty( $coupon->get_id() ) ) {
if ( empty( $request['code'] ) ) {
return new WP_Error( 'woocommerce_rest_empty_coupon_code', sprintf( __( 'The coupon code cannot be empty.', 'woocommerce' ), 'code' ), array( 'status' => 400 ) );
}
@ -224,7 +225,7 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
// Code.
if ( ! empty( $schema['properties']['code'] ) && ! empty( $request['code'] ) ) {
$coupon_code = apply_filters( 'woocommerce_coupon_code', $request['code'] );
$id = isset( $data->ID ) ? $data->ID : 0;
$id = $coupon->get_id() ? $coupon->get_id() : 0;
// Check for duplicate coupon codes.
$coupon_found = $wpdb->get_var( $wpdb->prepare( "
@ -240,29 +241,14 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
return new WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
}
$data->post_title = $coupon_code;
$coupon->set_code( $coupon_code );
}
// Content.
$data->post_content = '';
// Coupon description (excerpt).
if ( ! empty( $schema['properties']['description'] ) && isset( $request['description'] ) ) {
$data->post_excerpt = wp_filter_post_kses( $request['description'] );
$coupon->set_description( wp_filter_post_kses( $request['description'] ) );
}
// Post type.
$data->post_type = $this->post_type;
// Post status.
$data->post_status = 'publish';
// Comment status.
$data->comment_status = 'closed';
// Ping status.
$data->ping_status = 'closed';
/**
* Filter the query_vars used in `get_items` for the constructed query.
*
@ -273,7 +259,90 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
* for inserting or updating the database.
* @param WP_REST_Request $request Request object.
*/
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $data, $request );
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $coupon, $request );
}
/**
* Create a single item.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function create_item( $request ) {
if ( ! empty( $request['id'] ) ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_exists", sprintf( __( 'Cannot create existing %s.', 'woocommerce' ), $this->post_type ), array( 'status' => 400 ) );
}
$coupon_id = $this->save_coupon( $request );
if ( is_wp_error( $coupon_id ) ) {
return $coupon_id;
}
$post = get_post( $coupon_id );
$this->update_additional_fields_for_object( $post, $request );
$this->add_post_meta_fields( $post, $request );
/**
* Fires after a single item is created or updated via the REST API.
*
* @param object $post Inserted object (not a WP_Post object).
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating item, false when updating.
*/
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, true );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $post, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post->ID ) ) );
return $response;
}
/**
* Update a single coupon.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function update_item( $request ) {
$post_id = (int) $request['id'];
if ( empty( $post_id ) || $this->post_type !== get_post_type( $post_id ) ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
}
$coupon_id = $this->save_coupon( $request );
if ( is_wp_error( $coupon_id ) ) {
return $coupon_id;
}
$post = get_post( $coupon_id );
$this->update_additional_fields_for_object( $post, $request );
$this->update_post_meta_fields( $post, $request );
/**
* Fires after a single item is created or updated via the REST API.
*
* @param object $post Inserted object (not a WP_Post object).
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating item, false when updating.
*/
do_action( "woocommerce_rest_insert_{$this->post_type}", $post, $request, false );
$request->set_param( 'context', 'edit' );
$response = $this->prepare_item_for_response( $post, $request );
return rest_ensure_response( $response );
}
/**
* Saves a coupon to the database.
*/
public function save_coupon( $request ) {
$coupon = $this->prepare_item_for_database( $request );
$coupon->save();
return $coupon->get_id();
}
/**
@ -323,24 +392,28 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
$data = wp_parse_args( $data, $defaults );
// Set coupon meta.
update_post_meta( $post->ID, 'discount_type', $data['discount_type'] );
update_post_meta( $post->ID, 'coupon_amount', wc_format_decimal( $data['amount'] ) );
update_post_meta( $post->ID, 'individual_use', ( true === $data['individual_use'] ) ? 'yes' : 'no' );
update_post_meta( $post->ID, 'product_ids', implode( ',', array_filter( array_map( 'intval', $data['product_ids'] ) ) ) );
update_post_meta( $post->ID, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $data['exclude_product_ids'] ) ) ) );
update_post_meta( $post->ID, 'usage_limit', absint( $data['usage_limit'] ) );
update_post_meta( $post->ID, 'usage_limit_per_user', absint( $data['usage_limit_per_user'] ) );
update_post_meta( $post->ID, 'limit_usage_to_x_items', absint( $data['limit_usage_to_x_items'] ) );
update_post_meta( $post->ID, 'usage_count', absint( $data['usage_count'] ) );
update_post_meta( $post->ID, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $data['expiry_date'] ) ) );
update_post_meta( $post->ID, 'free_shipping', ( true === $data['free_shipping'] ) ? 'yes' : 'no' );
update_post_meta( $post->ID, 'product_categories', array_filter( array_map( 'intval', $data['product_categories'] ) ) );
update_post_meta( $post->ID, 'exclude_product_categories', array_filter( array_map( 'intval', $data['excluded_product_categories'] ) ) );
update_post_meta( $post->ID, 'exclude_sale_items', ( true === $data['exclude_sale_items'] ) ? 'yes' : 'no' );
update_post_meta( $post->ID, 'minimum_amount', wc_format_decimal( $data['minimum_amount'] ) );
update_post_meta( $post->ID, 'maximum_amount', wc_format_decimal( $data['maximum_amount'] ) );
update_post_meta( $post->ID, 'customer_email', array_filter( array_map( 'sanitize_email', $data['email_restrictions'] ) ) );
if ( ! empty( $post->post_title ) ) {
$coupon = new WC_Coupon( $post->post_title );
// Set coupon meta.
$coupon->set_discount_type( $data['discount_type'] );
$coupon->set_amount( $data['amount'] );
$coupon->set_individual_use( $data['individual_use'] );
$coupon->set_product_ids( $data['product_ids'] );
$coupon->set_excluded_product_ids( $data['exclude_product_ids'] );
$coupon->set_usage_limit( $data['usage_limit'] );
$coupon->set_usage_limit_per_user( $data['usage_limit_per_user'] );
$coupon->set_limit_usage_to_x_items( $data['limit_usage_to_x_items'] );
$coupon->set_usage_count( $data['usage_count'] );
$coupon->set_expiry_date( $data['expiry_date'] );
$coupon->set_free_shipping( $data['free_shipping'] );
$coupon->set_product_categories( $data['product_categories'] );
$coupon->set_excluded_product_categories( $data['excluded_product_categories'] );
$coupon->set_exclude_sale_items( $data['exclude_sale_items'] );
$coupon->set_minimum_amount( $data['minimum_amount'] );
$coupon->set_maximum_amount( $data['maximum_amount'] );
$coupon->set_email_restrictions( $data['email_restrictions'] );
$coupon->save();
}
return true;
}
@ -353,70 +426,75 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
* @return bool|WP_Error
*/
protected function update_post_meta_fields( $post, $request ) {
$coupon = new WC_Coupon( $post->post_title );
if ( isset( $request['amount'] ) ) {
update_post_meta( $post->ID, 'coupon_amount', wc_format_decimal( $request['amount'] ) );
$coupon->set_amount( $request['amount'] );
}
if ( isset( $request['individual_use'] ) ) {
update_post_meta( $post->ID, 'individual_use', ( true === $request['individual_use'] ) ? 'yes' : 'no' );
$coupon->set_individual_use( $request['individual_use'] );
}
if ( isset( $request['product_ids'] ) ) {
update_post_meta( $post->ID, 'product_ids', implode( ',', array_filter( array_map( 'intval', $request['product_ids'] ) ) ) );
$coupon->set_product_ids( $request['product_ids'] );
}
if ( isset( $request['exclude_product_ids'] ) ) {
update_post_meta( $post->ID, 'exclude_product_ids', implode( ',', array_filter( array_map( 'intval', $request['exclude_product_ids'] ) ) ) );
$coupon->set_excluded_product_ids( $request['exclude_product_ids'] );
}
if ( isset( $request['usage_limit'] ) ) {
update_post_meta( $post->ID, 'usage_limit', absint( $request['usage_limit'] ) );
$coupon->set_usage_limit( $request['usage_limit'] );
}
if ( isset( $request['usage_limit_per_user'] ) ) {
update_post_meta( $post->ID, 'usage_limit_per_user', absint( $request['usage_limit_per_user'] ) );
$coupon->set_usage_limit_per_user( $request['usage_limit_per_user'] );
}
if ( isset( $request['limit_usage_to_x_items'] ) ) {
update_post_meta( $post->ID, 'limit_usage_to_x_items', absint( $request['limit_usage_to_x_items'] ) );
$coupon->set_limit_usage_to_x_items( $request['limit_usage_to_x_items'] );
}
if ( isset( $request['usage_count'] ) ) {
update_post_meta( $post->ID, 'usage_count', absint( $request['usage_count'] ) );
$coupon->set_usage_count( $request['usage_count'] );
}
if ( isset( $request['expiry_date'] ) ) {
update_post_meta( $post->ID, 'expiry_date', $this->get_coupon_expiry_date( wc_clean( $request['expiry_date'] ) ) );
$coupon->set_expiry_date( $request['expiry_date'] );
}
if ( isset( $request['free_shipping'] ) ) {
update_post_meta( $post->ID, 'free_shipping', ( true === $request['free_shipping'] ) ? 'yes' : 'no' );
$coupon->set_free_shipping( $request['free_shipping'] );
}
if ( isset( $request['product_categories'] ) ) {
update_post_meta( $post->ID, 'product_categories', array_filter( array_map( 'intval', $request['product_categories'] ) ) );
$coupon->set_product_categories( $request['product_categories'] );
}
if ( isset( $request['excluded_product_categories'] ) ) {
update_post_meta( $post->ID, 'exclude_product_categories', array_filter( array_map( 'intval', $request['excluded_product_categories'] ) ) );
$coupon->set_excluded_product_categories( $request['excluded_product_categories'] );
}
if ( isset( $request['exclude_sale_items'] ) ) {
update_post_meta( $post->ID, 'exclude_sale_items', ( true === $request['exclude_sale_items'] ) ? 'yes' : 'no' );
$coupon->set_exclude_sale_items( $request['exclude_sale_items'] );
}
if ( isset( $request['minimum_amount'] ) ) {
update_post_meta( $post->ID, 'minimum_amount', wc_format_decimal( $request['minimum_amount'] ) );
$coupon->set_minimum_amount( $request['minimum_amount'] );
}
if ( isset( $request['maximum_amount'] ) ) {
update_post_meta( $post->ID, 'maximum_amount', wc_format_decimal( $request['maximum_amount'] ) );
$coupon->set_maximum_amount( $request['maximum_amount'] );
}
if ( isset( $request['email_restrictions'] ) ) {
update_post_meta( $post->ID, 'customer_email', array_filter( array_map( 'sanitize_email', $request['email_restrictions'] ) ) );
$coupon->set_email_restrictions( $request['email_restrictions'] );
}
$coupon->save();
return true;
}

View File

@ -571,7 +571,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
* @since 2.7.0
* @param array $emails
*/
public function set_email_restrictions( $emails ) {
public function set_email_restrictions( $emails = array() ) {
$this->_data['customer_email'] = array_map( 'sanitize_email', $emails );
}
@ -646,7 +646,8 @@ class WC_Coupon extends WC_Legacy_Coupon {
$this->set_exclude_sale_items( $exclude_sale_items );
$this->set_minimum_amount( get_post_meta( $coupon_id, 'minimum_amount', true ) );
$this->set_maximum_amount( get_post_meta( $coupon_id, 'maximum_amount', true ) );
$this->set_email_restrictions( get_post_meta( $coupon_id, 'customer_email', true ) );
$email_restrictions = get_post_meta( $coupon_id, 'customer_email', true ) ;
$this->set_email_restrictions( ! empty( $email_restrictions ) ? $email_restrictions : array() );
$this->set_used_by( (array) get_post_meta( $coupon_id, '_used_by' ) );
$this->read_meta_data();

View File

@ -4,318 +4,429 @@
* @package WooCommerce\Tests\API
* @since 2.7.0
*/
class WC_Tests_API_Coupons extends WC_API_Unit_Test_Case {
class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
/** @var WC_API_Coupons instance */
protected $endpoint;
/**
* Setup test coupon data.
* @see WC_API_UnitTestCase::setup()
* @since 2.7.0
*/
public function setUp() {
parent::setUp();
$this->endpoint = WC()->api->WC_API_Coupons;
$this->coupon = WC_Helper_Coupon::create_coupon();
}
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Coupons_Controller();
$this->user = $this->factory->user->create( array(
'role' => 'administrator',
) );
}
/**
* Ensure valid coupon data response.
* @since 2.7.0
* @param array $response
* @param WC_Coupon $coupon
*/
protected function check_get_coupon_response( $response, $coupon ) {
$this->assertEquals( (int) $coupon->get_id(), $response['id'] );
$this->assertEquals( $coupon->get_code(), $response['code'] );
$this->assertEquals( $coupon->get_discount_type(), $response['type'] );
$this->assertEquals( $coupon->get_amount(), $response['amount'] );
$this->assertEquals( $coupon->get_individual_use(), $response['individual_use'] );
$this->assertEquals( $coupon->get_product_ids(), $response['product_ids'] );
$this->assertEquals( $coupon->get_excluded_product_ids(), $response['exclude_product_ids'] );
$this->assertEquals( (int) $coupon->get_usage_limit(), $response['usage_limit'] );
$this->assertEquals( (int) $coupon->get_usage_limit_per_user(), $response['usage_limit_per_user'] );
$this->assertEquals( (int) $coupon->get_limit_usage_to_x_items(), $response['limit_usage_to_x_items'] );
$this->assertEquals( (int) $coupon->get_usage_count(), $response['usage_count'] );
$this->assertEquals( $coupon->get_expiry_date(), $response['expiry_date'] );
$this->assertEquals( $coupon->get_free_shipping(), $response['enable_free_shipping'] );
$this->assertEquals( $coupon->get_product_categories(), $response['product_category_ids'] );
$this->assertEquals( $coupon->get_excluded_product_categories(), $response['exclude_product_category_ids'] );
$this->assertEquals( $coupon->get_exclude_sale_items(), $response['exclude_sale_items'] );
$this->assertEquals( wc_format_decimal( $coupon->get_minimum_amount(), 2 ), $response['minimum_amount'] );
$this->assertEquals( wc_format_decimal( $coupon->get_maximum_amount(), 2 ), $response['maximum_amount'] );
$this->assertEquals( $coupon->get_email_restrictions(), $response['customer_emails'] );
$this->assertEquals( $coupon->get_description(), $response['description'] );
$this->assertArrayHasKey( 'created_at', $response );
$this->assertArrayHasKey( 'updated_at', $response );
}
/**
* Test route registration.
* @since 2.7.0
*/
public function test_register_routes() {
$routes = $this->server->get_routes();
$this->assertArrayHasKey( '/wc/v1/coupons', $routes );
$this->assertArrayHasKey( '/wc/v1/coupons/(?P<id>[\d]+)', $routes );
$this->assertArrayHasKey( '/wc/v1/coupons/batch', $routes );
}
/**
* Get default arguments for creating/editing a coupon.
* @since 2.7.0
* @param array $args
* @return array
*/
protected function get_defaults( $args = array() ) {
$defaults = array(
'code' => 'api-dummycoupon',
'description' => 'Test API Coupon',
'amount' => '5',
'type' => 'percent',
);
return array( 'coupon' => wp_parse_args( $args, $defaults ) );
}
/**
* Test getting coupons.
* @since 2.7.0
*/
public function test_get_coupons() {
wp_set_current_user( $this->user );
/**
* Clears out the post title from our post data before inserting the coupon into the database.
* @since 2.7.0
* @param array $data
* @return array
*/
public function clear_code_from_post_data( $data ) {
$data['post_title'] = '';
return $data;
}
$coupon_1 = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
$post_1 = get_post( $coupon_1->get_id() );
$coupon_2 = WC_Helper_Coupon::create_coupon( 'dummycoupon-2' );
/**
* Test route registration.
* @since 2.7.0
*/
public function test_register_routes() {
$routes = $this->endpoint->register_routes( array() );
$this->assertArrayHasKey( '/coupons', $routes );
$this->assertArrayHasKey( '/coupons/count', $routes );
$this->assertArrayHasKey( '/coupons/(?P<id>\d+)', $routes );
$this->assertArrayHasKey( '/coupons/code/(?P<code>\w[\w\s\-]*)', $routes );
$this->assertArrayHasKey( '/coupons/bulk', $routes );
}
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons' ) );
$coupons = $response->get_data();
/**
* Test GET /coupons/{id}.
* @since 2.7.0
*/
public function test_get_coupon() {
// invalid ID
$response = $this->endpoint->get_coupon( 0 );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_id', 404, $response );
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 2, count( $coupons ) );
$this->assertContains( array(
'id' => $coupon_1->get_id(),
'code' => 'dummycoupon-1',
'amount' => '1.00',
'date_created' => wc_rest_prepare_date_response( $post_1->post_date_gmt ),
'date_modified' => wc_rest_prepare_date_response( $post_1->post_modified_gmt ),
'discount_type' => 'fixed_cart',
'description' => 'This is a dummy coupon',
'expiry_date' => '',
'usage_count' => 0,
'individual_use' => false,
'product_ids' => array(),
'exclude_product_ids' => array(),
'usage_limit' => '',
'usage_limit_per_user' => '',
'limit_usage_to_x_items' => 0,
'free_shipping' => false,
'product_categories' => array(),
'excluded_product_categories' => array(),
'exclude_sale_items' => false,
'minimum_amount' => '0.00',
'maximum_amount' => '0.00',
'email_restrictions' => array(),
'used_by' => array(),
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v1/coupons/'. $coupon_1->get_id() ),
),
),
'collection' => array(
array(
'href' => rest_url( '/wc/v1/coupons' ),
),
),
),
), $coupons );
}
// valid request
$response = $this->endpoint->get_coupon( $this->coupon->get_id() );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupon', $response );
$this->check_get_coupon_response( $response['coupon'], $this->coupon );
}
/**
* Test getting coupons without valid permissions.
* @since 2.7.0
*/
public function test_get_coupons_without_permission() {
wp_set_current_user( 0 );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons' ) );
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test GET /coupons/{id} without valid permissions.
* @since 2.7.0
*/
public function test_get_coupon_without_permission() {
$this->disable_capability( 'read_private_shop_coupons' );
$response = $this->endpoint->get_coupon( $this->coupon->get_id() );
$this->assertHasAPIError( 'woocommerce_api_user_cannot_read_coupon', 401, $response );
}
/**
* Test getting a single coupon.
* @since 2.7.0
*/
public function test_get_coupon() {
wp_set_current_user( $this->user );
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
$post = get_post( $coupon->get_id() );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons/' . $coupon->get_id() ) );
$data = $response->get_data();
/**
* Test GET /coupons/code/{code}.
* @since 2.7.0
*/
public function test_get_get_coupon_by_code() {
// invalid ID
$response = $this->endpoint->get_coupon_by_code( 'bogus' );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_code', 404, $response );
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( array(
'id' => $coupon->get_id(),
'code' => 'dummycoupon-1',
'amount' => '1.00',
'date_created' => wc_rest_prepare_date_response( $post->post_date_gmt ),
'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt ),
'discount_type' => 'fixed_cart',
'description' => 'This is a dummy coupon',
'expiry_date' => null,
'usage_count' => 0,
'individual_use' => false,
'product_ids' => array(),
'exclude_product_ids' => array(),
'usage_limit' => null,
'usage_limit_per_user' => null,
'limit_usage_to_x_items' => 0,
'free_shipping' => false,
'product_categories' => array(),
'excluded_product_categories' => array(),
'exclude_sale_items' => false,
'minimum_amount' => '0.00',
'maximum_amount' => '0.00',
'email_restrictions' => array(),
'used_by' => array(),
), $data );
}
// valid request
$response = $this->endpoint->get_coupon_by_code( 'dummycoupon' );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupon', $response );
$this->check_get_coupon_response( $response['coupon'], $this->coupon );
}
/**
* Test getting a single coupon with an invalid ID.
* @since 2.7.0
*/
public function test_get_coupon_invalid_id() {
wp_set_current_user( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons/0' ) );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test GET /coupons/code/{code} without valid permissions.
* @since 2.7.0
*/
public function test_get_coupon_by_code_without_permission() {
$this->disable_capability( 'read_private_shop_coupons' );
$response = $this->endpoint->get_coupon( $this->coupon->get_id() );
$this->assertHasAPIError( 'woocommerce_api_user_cannot_read_coupon', 401, $response );
}
/**
* Test getting a single coupon without valid permissions.
* @since 2.7.0
*/
public function test_get_coupon_without_permission() {
wp_set_current_user( 0 );
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons/' . $coupon->get_id() ) );
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test GET /coupons.
* @since 2.7.0
*/
public function test_get_coupons() {
$response = $this->endpoint->get_coupons();
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupons', $response );
$this->assertCount( 1, $response['coupons'] );
$this->check_get_coupon_response( $response['coupons'][0], $this->coupon );
}
/**
* Test creating a single coupon.
* @since 2.7.0
*/
public function test_create_coupon() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'POST', '/wc/v1/coupons' );
$request->set_body_params( array(
'code' => 'test',
'amount' => '5.00',
'discount_type' => 'fixed_product',
'description' => 'Test',
'usage_limit' => 10,
) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
/**
* Test GET /coupons without valid permissions.
* @since 2.7.0
*/
public function test_get_coupons_without_permission() {
$this->disable_capability( 'read_private_shop_coupons' );
$response = $this->endpoint->get_coupons();
$this->assertArrayHasKey( 'coupons', $response );
$this->assertEmpty( $response['coupons'] );
}
$this->assertEquals( 201, $response->get_status() );
$this->assertEquals( array(
'id' => $data['id'],
'code' => 'test',
'amount' => '5.00',
'date_created' => $data['date_created'],
'date_modified' => $data['date_modified'],
'discount_type' => 'fixed_product',
'description' => 'Test',
'expiry_date' => null,
'usage_count' => 0,
'individual_use' => false,
'product_ids' => array(),
'exclude_product_ids' => array(),
'usage_limit' => 10,
'usage_limit_per_user' => null,
'limit_usage_to_x_items' => 0,
'free_shipping' => false,
'product_categories' => array(),
'excluded_product_categories' => array(),
'exclude_sale_items' => false,
'minimum_amount' => '0.00',
'maximum_amount' => '0.00',
'email_restrictions' => array(),
'used_by' => array(),
), $data );
}
/**
* Test GET /coupons/count.
* @since 2.7.0
*/
public function test_get_coupons_count() {
$response = $this->endpoint->get_coupons_count();
$this->assertArrayHasKey( 'count', $response );
$this->assertEquals( 1, $response['count'] );
}
/**
* Test creating a single coupon with invalid fields.
* @since 2.7.0
*/
public function test_create_coupon_invalid_fields() {
wp_set_current_user( $this->user );
/**
* Test GET /coupons/count without valid permissions.
* @since 2.7.0
*/
public function test_get_coupons_count_without_permission() {
$this->disable_capability( 'read_private_shop_coupons' );
$response = $this->endpoint->get_coupons_count();
$this->assertHasAPIError( 'woocommerce_api_user_cannot_read_coupons_count', 401, $response );
}
// test no code...
$request = new WP_REST_Request( 'POST', '/wc/v1/coupons' );
$request->set_body_params( array(
'amount' => '5.00',
'discount_type' => 'fixed_product',
) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
/**
* Test POST /coupons.
* @since 2.7.0
*/
public function test_create_coupon() {
$response = $this->endpoint->create_coupon( $this->get_defaults() );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupon', $response );
$this->check_get_coupon_response( $response['coupon'], new WC_Coupon( $response['coupon']['code'] ) );
}
$this->assertEquals( 400, $response->get_status() );
}
/**
* Test POST /coupons without valid permissions.
* @since 2.7.0
*/
public function test_create_coupon_without_permission() {
$this->disable_capability( 'publish_shop_coupons' );
$response = $this->endpoint->create_coupon( $this->get_defaults() );
$this->assertHasAPIError( 'woocommerce_api_user_cannot_create_coupon', 401, $response );
}
/**
* Test creating a single coupon without valid permissions.
* @since 2.7.0
*/
public function test_create_coupon_without_permission() {
wp_set_current_user( 0 );
/**
* Test an empty coupon code for POST /coupons.
* @since 2.7.0
*/
public function test_create_coupon_empty_code() {
$response = $this->endpoint->create_coupon( $this->get_defaults( array( 'code' => null ) ) );
$this->assertHasAPIError( 'woocommerce_api_missing_coupon_code', 400, $response );
}
// test no code...
$request = new WP_REST_Request( 'POST', '/wc/v1/coupons' );
$request->set_body_params( array(
'code' => 'fail',
'amount' => '5.00',
'discount_type' => 'fixed_product',
) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
/**
* Test an empty or invalid discount type for POST /coupons.
* @since 2.7.0
*/
public function test_create_coupon_invalid_discount_type() {
// empty
$response = $this->endpoint->create_coupon( $this->get_defaults( array( 'type' => null ) ) );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_type', 400, $response );
// invalid
$response = $this->endpoint->create_coupon( $this->get_defaults( array( 'type' => 'bogus' ) ) );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_type', 400, $response );
}
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test wp_insert_post() failure for POST /coupons.
* @since 2.7.0
*/
public function test_create_coupon_insert_post_failure() {
add_filter( 'wp_insert_post_empty_content', '__return_true' );
add_filter( 'wp_insert_post_data', array( $this, 'clear_code_from_post_data' ) );
$response = $this->endpoint->create_coupon( $this->get_defaults( array( 'description' => null, 'code' => '' ) ) );
$this->assertHasAPIError( 'woocommerce_api_cannot_create_coupon', 400, $response );
}
/**
* Test updating a single coupon.
* @since 2.7.0
*/
public function test_update_coupon() {
wp_set_current_user( $this->user );
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
$post = get_post( $coupon->get_id() );
/**
* Test PUT /coupons/{id}.
* @since 2.7.0
*/
public function test_edit_coupon() {
// invalid ID
$response = $this->endpoint->edit_coupon( 0, $this->get_defaults() );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_id', 404, $response );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v1/coupons/' . $coupon->get_id() ) );
$data = $response->get_data();
$this->assertEquals( 'This is a dummy coupon', $data['description'] );
$this->assertEquals( 'fixed_cart', $data['discount_type'] );
$this->assertEquals( '1.00', $data['amount'] );
$args = array(
'description' => rand_str(),
'code' => rand_str(),
);
$request = new WP_REST_Request( 'PUT', '/wc/v1/coupons/' . $coupon->get_id() );
$request->set_body_params( array(
'amount' => '10.00',
'description' => 'New description',
) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
// valid request
$response = $this->endpoint->edit_coupon( $this->coupon->get_id(), $this->get_defaults( $args ) );
$this->assertEquals( '10.00', $data['amount'] );
$this->assertEquals( 'New description', $data['description'] );
$this->assertEquals( 'fixed_cart', $data['discount_type'] );
}
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupon', $response );
$this->check_get_coupon_response( $response['coupon'], new WC_Coupon( $response['coupon']['code'] ) );
}
/**
* Test updating a single coupon with an invalid ID.
* @since 2.7.0
*/
public function test_update_coupon_invalid_id() {
wp_set_current_user( $this->user );
/**
* Test PUT /coupons/{id} without valid permissions.
*
* @since 2.2
*/
public function test_edit_coupon_without_permission() {
$this->disable_capability( 'edit_published_shop_coupons' );
$response = $this->endpoint->edit_coupon( $this->coupon->get_id(), $this->get_defaults() );
$this->assertHasAPIError( 'woocommerce_api_user_cannot_edit_coupon', 401, $response );
}
$request = new WP_REST_Request( 'PUT', '/wc/v1/coupons/0' );
$request->set_body_params( array(
'code' => 'tester',
'amount' => '10.00',
'description' => 'New description',
) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
/**
* Test DELETE /coupons/{id}.
*
* @since 2.2
*/
public function test_delete_coupon() {
$response = $this->endpoint->delete_coupon( 0 );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_id', 404, $response );
$this->assertEquals( 400, $response->get_status() );
}
$response = $this->endpoint->delete_coupon( $this->coupon->get_id() );
$this->assertArrayHasKey( 'message', $response );
$this->assertEquals( 'Deleted coupon', $response['message'] );
}
/**
* Test updating a single coupon without valid permissions.
* @since 2.7.0
*/
public function test_update_coupon_without_permission() {
wp_set_current_user( 0 );
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
$post = get_post( $coupon->get_id() );
/**
* Test POST /coupons/bulk.
* @since 2.7.0
*/
public function test_create_coupon_bulk() {
$test_coupon_data = $this->get_defaults();
$test_coupon_data_2 = $this->get_defaults( array( 'code' => time() ) );
$coupons = array( 'coupons' => array( $test_coupon_data['coupon'], $test_coupon_data_2['coupon'] ) );
$response = $this->endpoint->bulk( $coupons );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupons', $response );
$this->assertCount( 2, $response['coupons'] );
$this->check_get_coupon_response( $response['coupons'][0], new WC_Coupon( $response['coupons'][0]['code'] ) );
$this->check_get_coupon_response( $response['coupons'][1], new WC_Coupon( $response['coupons'][1]['code'] ) );
}
$request = new WP_REST_Request( 'PUT', '/wc/v1/coupons/' . $coupon->get_id() );
$request->set_body_params( array(
'amount' => '10.00',
'description' => 'New description',
) );
$response = $this->server->dispatch( $request );
/**
* Test PUT /coupons/bulk.
* @since 2.7.0
*/
public function test_edit_coupon_bulk() {
$coupon_1 = WC_Helper_Coupon::create_coupon( 'dummycoupon-1-' . time() );
$test_coupon_data = $this->get_defaults( array( 'description' => rand_str() ) );
$test_coupon_data['coupon']['id'] = $coupon_1->get_id();
$coupons = array( 'coupons' => array( $test_coupon_data['coupon'] ) );
$response = $this->endpoint->bulk( $coupons );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupons', $response );
$this->check_get_coupon_response( $response['coupons'][0], new WC_Coupon( $response['coupons'][0]['code'] ) );
}
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test deleting a single coupon.
* @since 2.7.0
*/
public function test_delete_coupon() {
wp_set_current_user( $this->user );
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
$request = new WP_REST_Request( 'DELETE', '/wc/v1/coupons/' . $coupon->get_id() );
$request->set_param( 'force', true );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
}
/**
* Test deleting a single coupon with an invalid ID.
* @since 2.7.0
*/
public function test_delete_coupon_invalid_id() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'DELETE', '/wc/v1/coupons/0' );
$request->set_param( 'force', true );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test deleting a single coupon without valid permissions.
* @since 2.7.0
*/
public function test_delete_coupon_without_permission() {
wp_set_current_user( 0 );
$coupon = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
$request = new WP_REST_Request( 'DELETE', '/wc/v1/coupons/' . $coupon->get_id() );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test batch operations on coupons.
* @since 2.7.0
*/
public function test_batch_coupon() {
wp_set_current_user( $this->user );
$coupon_1 = WC_Helper_Coupon::create_coupon( 'dummycoupon-1' );
$coupon_2 = WC_Helper_Coupon::create_coupon( 'dummycoupon-2' );
$coupon_3 = WC_Helper_Coupon::create_coupon( 'dummycoupon-3' );
$coupon_4 = WC_Helper_Coupon::create_coupon( 'dummycoupon-4' );
$request = new WP_REST_Request( 'POST', '/wc/v1/coupons/batch' );
$request->set_body_params( array(
'update' => array(
array(
'id' => $coupon_1->get_id(),
'amount' => '5.15',
),
),
'delete' => array(
$coupon_2->get_id(),
$coupon_3->get_id(),
),
'create' => array(
array(
'code' => 'new-coupon',
'amount' => '11.00',
),
),
) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( '5.15', $data['update'][0]['amount'] );
$this->assertEquals( '11.00', $data['create'][0]['amount'] );
$this->assertEquals( 'new-coupon', $data['create'][0]['code'] );
$this->assertEquals( $coupon_2->get_id(), $data['delete'][0]['id'] );
$this->assertEquals( $coupon_3->get_id(), $data['delete'][1]['id'] );
$request = new WP_REST_Request( 'GET', '/wc/v1/coupons' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 3, count( $data ) );
}
/**
* Test coupon schema.
* @since 2.7.0
*/
public function test_coupon_schema() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'OPTIONS', '/wc/v1/coupons' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 23, count( $properties ) );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'code', $properties );
$this->assertArrayHasKey( 'date_created', $properties );
$this->assertArrayHasKey( 'date_modified', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'discount_type', $properties );
$this->assertArrayHasKey( 'amount', $properties );
$this->assertArrayHasKey( 'expiry_date', $properties );
$this->assertArrayHasKey( 'usage_count', $properties );
$this->assertArrayHasKey( 'individual_use', $properties );
$this->assertArrayHasKey( 'product_ids', $properties );
$this->assertArrayHasKey( 'exclude_product_ids', $properties );
$this->assertArrayHasKey( 'usage_limit', $properties );
$this->assertArrayHasKey( 'usage_limit_per_user', $properties );
$this->assertArrayHasKey( 'limit_usage_to_x_items', $properties );
$this->assertArrayHasKey( 'free_shipping', $properties );
$this->assertArrayHasKey( 'product_categories', $properties );
$this->assertArrayHasKey( 'excluded_product_categories', $properties );
$this->assertArrayHasKey( 'exclude_sale_items', $properties );
$this->assertArrayHasKey( 'minimum_amount', $properties );
$this->assertArrayHasKey( 'maximum_amount', $properties );
$this->assertArrayHasKey( 'email_restrictions', $properties );
$this->assertArrayHasKey( 'used_by', $properties );
}
}

View File

@ -0,0 +1,321 @@
<?php
/**
* Legacy Coupon API Tests
* @package WooCommerce\Tests\API
* @since 2.7.0
*/
class WC_Tests_API_Legacy_Coupons extends WC_API_Unit_Test_Case {
/** @var WC_API_Coupons instance */
protected $endpoint;
/**
* Setup test coupon data.
* @see WC_API_UnitTestCase::setup()
* @since 2.7.0
*/
public function setUp() {
parent::setUp();
$this->endpoint = WC()->api->WC_API_Coupons;
$this->coupon = WC_Helper_Coupon::create_coupon();
}
/**
* Ensure valid coupon data response.
* @since 2.7.0
* @param array $response
* @param WC_Coupon $coupon
*/
protected function check_get_coupon_response( $response, $coupon ) {
$this->assertEquals( (int) $coupon->get_id(), $response['id'] );
$this->assertEquals( $coupon->get_code(), $response['code'] );
$this->assertEquals( $coupon->get_discount_type(), $response['type'] );
$this->assertEquals( $coupon->get_amount(), $response['amount'] );
$this->assertEquals( $coupon->get_individual_use(), $response['individual_use'] );
$this->assertEquals( $coupon->get_product_ids(), $response['product_ids'] );
$this->assertEquals( $coupon->get_excluded_product_ids(), $response['exclude_product_ids'] );
$this->assertEquals( (int) $coupon->get_usage_limit(), $response['usage_limit'] );
$this->assertEquals( (int) $coupon->get_usage_limit_per_user(), $response['usage_limit_per_user'] );
$this->assertEquals( (int) $coupon->get_limit_usage_to_x_items(), $response['limit_usage_to_x_items'] );
$this->assertEquals( (int) $coupon->get_usage_count(), $response['usage_count'] );
$this->assertEquals( $coupon->get_expiry_date(), $response['expiry_date'] );
$this->assertEquals( $coupon->get_free_shipping(), $response['enable_free_shipping'] );
$this->assertEquals( $coupon->get_product_categories(), $response['product_category_ids'] );
$this->assertEquals( $coupon->get_excluded_product_categories(), $response['exclude_product_category_ids'] );
$this->assertEquals( $coupon->get_exclude_sale_items(), $response['exclude_sale_items'] );
$this->assertEquals( wc_format_decimal( $coupon->get_minimum_amount(), 2 ), $response['minimum_amount'] );
$this->assertEquals( wc_format_decimal( $coupon->get_maximum_amount(), 2 ), $response['maximum_amount'] );
$this->assertEquals( $coupon->get_email_restrictions(), $response['customer_emails'] );
$this->assertEquals( $coupon->get_description(), $response['description'] );
$this->assertArrayHasKey( 'created_at', $response );
$this->assertArrayHasKey( 'updated_at', $response );
}
/**
* Get default arguments for creating/editing a coupon.
* @since 2.7.0
* @param array $args
* @return array
*/
protected function get_defaults( $args = array() ) {
$defaults = array(
'code' => 'api-dummycoupon',
'description' => 'Test API Coupon',
'amount' => '5',
'type' => 'percent',
);
return array( 'coupon' => wp_parse_args( $args, $defaults ) );
}
/**
* Clears out the post title from our post data before inserting the coupon into the database.
* @since 2.7.0
* @param array $data
* @return array
*/
public function clear_code_from_post_data( $data ) {
$data['post_title'] = '';
return $data;
}
/**
* Test route registration.
* @since 2.7.0
*/
public function test_register_routes() {
$routes = $this->endpoint->register_routes( array() );
$this->assertArrayHasKey( '/coupons', $routes );
$this->assertArrayHasKey( '/coupons/count', $routes );
$this->assertArrayHasKey( '/coupons/(?P<id>\d+)', $routes );
$this->assertArrayHasKey( '/coupons/code/(?P<code>\w[\w\s\-]*)', $routes );
$this->assertArrayHasKey( '/coupons/bulk', $routes );
}
/**
* Test GET /coupons/{id}.
* @since 2.7.0
*/
public function test_get_coupon() {
// invalid ID
$response = $this->endpoint->get_coupon( 0 );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_id', 404, $response );
// valid request
$response = $this->endpoint->get_coupon( $this->coupon->get_id() );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupon', $response );
$this->check_get_coupon_response( $response['coupon'], $this->coupon );
}
/**
* Test GET /coupons/{id} without valid permissions.
* @since 2.7.0
*/
public function test_get_coupon_without_permission() {
$this->disable_capability( 'read_private_shop_coupons' );
$response = $this->endpoint->get_coupon( $this->coupon->get_id() );
$this->assertHasAPIError( 'woocommerce_api_user_cannot_read_coupon', 401, $response );
}
/**
* Test GET /coupons/code/{code}.
* @since 2.7.0
*/
public function test_get_get_coupon_by_code() {
// invalid ID
$response = $this->endpoint->get_coupon_by_code( 'bogus' );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_code', 404, $response );
// valid request
$response = $this->endpoint->get_coupon_by_code( 'dummycoupon' );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupon', $response );
$this->check_get_coupon_response( $response['coupon'], $this->coupon );
}
/**
* Test GET /coupons/code/{code} without valid permissions.
* @since 2.7.0
*/
public function test_get_coupon_by_code_without_permission() {
$this->disable_capability( 'read_private_shop_coupons' );
$response = $this->endpoint->get_coupon( $this->coupon->get_id() );
$this->assertHasAPIError( 'woocommerce_api_user_cannot_read_coupon', 401, $response );
}
/**
* Test GET /coupons.
* @since 2.7.0
*/
public function test_get_coupons() {
$response = $this->endpoint->get_coupons();
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupons', $response );
$this->assertCount( 1, $response['coupons'] );
$this->check_get_coupon_response( $response['coupons'][0], $this->coupon );
}
/**
* Test GET /coupons without valid permissions.
* @since 2.7.0
*/
public function test_get_coupons_without_permission() {
$this->disable_capability( 'read_private_shop_coupons' );
$response = $this->endpoint->get_coupons();
$this->assertArrayHasKey( 'coupons', $response );
$this->assertEmpty( $response['coupons'] );
}
/**
* Test GET /coupons/count.
* @since 2.7.0
*/
public function test_get_coupons_count() {
$response = $this->endpoint->get_coupons_count();
$this->assertArrayHasKey( 'count', $response );
$this->assertEquals( 1, $response['count'] );
}
/**
* Test GET /coupons/count without valid permissions.
* @since 2.7.0
*/
public function test_get_coupons_count_without_permission() {
$this->disable_capability( 'read_private_shop_coupons' );
$response = $this->endpoint->get_coupons_count();
$this->assertHasAPIError( 'woocommerce_api_user_cannot_read_coupons_count', 401, $response );
}
/**
* Test POST /coupons.
* @since 2.7.0
*/
public function test_create_coupon() {
$response = $this->endpoint->create_coupon( $this->get_defaults() );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupon', $response );
$this->check_get_coupon_response( $response['coupon'], new WC_Coupon( $response['coupon']['code'] ) );
}
/**
* Test POST /coupons without valid permissions.
* @since 2.7.0
*/
public function test_create_coupon_without_permission() {
$this->disable_capability( 'publish_shop_coupons' );
$response = $this->endpoint->create_coupon( $this->get_defaults() );
$this->assertHasAPIError( 'woocommerce_api_user_cannot_create_coupon', 401, $response );
}
/**
* Test an empty coupon code for POST /coupons.
* @since 2.7.0
*/
public function test_create_coupon_empty_code() {
$response = $this->endpoint->create_coupon( $this->get_defaults( array( 'code' => null ) ) );
$this->assertHasAPIError( 'woocommerce_api_missing_coupon_code', 400, $response );
}
/**
* Test an empty or invalid discount type for POST /coupons.
* @since 2.7.0
*/
public function test_create_coupon_invalid_discount_type() {
// empty
$response = $this->endpoint->create_coupon( $this->get_defaults( array( 'type' => null ) ) );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_type', 400, $response );
// invalid
$response = $this->endpoint->create_coupon( $this->get_defaults( array( 'type' => 'bogus' ) ) );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_type', 400, $response );
}
/**
* Test wp_insert_post() failure for POST /coupons.
* @since 2.7.0
*/
public function test_create_coupon_insert_post_failure() {
add_filter( 'wp_insert_post_empty_content', '__return_true' );
add_filter( 'wp_insert_post_data', array( $this, 'clear_code_from_post_data' ) );
$response = $this->endpoint->create_coupon( $this->get_defaults( array( 'description' => null, 'code' => '' ) ) );
$this->assertHasAPIError( 'woocommerce_api_cannot_create_coupon', 400, $response );
}
/**
* Test PUT /coupons/{id}.
* @since 2.7.0
*/
public function test_edit_coupon() {
// invalid ID
$response = $this->endpoint->edit_coupon( 0, $this->get_defaults() );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_id', 404, $response );
$args = array(
'description' => rand_str(),
'code' => rand_str(),
);
// valid request
$response = $this->endpoint->edit_coupon( $this->coupon->get_id(), $this->get_defaults( $args ) );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupon', $response );
$this->check_get_coupon_response( $response['coupon'], new WC_Coupon( $response['coupon']['code'] ) );
}
/**
* Test PUT /coupons/{id} without valid permissions.
*
* @since 2.2
*/
public function test_edit_coupon_without_permission() {
$this->disable_capability( 'edit_published_shop_coupons' );
$response = $this->endpoint->edit_coupon( $this->coupon->get_id(), $this->get_defaults() );
$this->assertHasAPIError( 'woocommerce_api_user_cannot_edit_coupon', 401, $response );
}
/**
* Test DELETE /coupons/{id}.
*
* @since 2.2
*/
public function test_delete_coupon() {
$response = $this->endpoint->delete_coupon( 0 );
$this->assertHasAPIError( 'woocommerce_api_invalid_coupon_id', 404, $response );
$response = $this->endpoint->delete_coupon( $this->coupon->get_id() );
$this->assertArrayHasKey( 'message', $response );
$this->assertEquals( 'Deleted coupon', $response['message'] );
}
/**
* Test POST /coupons/bulk.
* @since 2.7.0
*/
public function test_create_coupon_bulk() {
$test_coupon_data = $this->get_defaults();
$test_coupon_data_2 = $this->get_defaults( array( 'code' => time() ) );
$coupons = array( 'coupons' => array( $test_coupon_data['coupon'], $test_coupon_data_2['coupon'] ) );
$response = $this->endpoint->bulk( $coupons );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupons', $response );
$this->assertCount( 2, $response['coupons'] );
$this->check_get_coupon_response( $response['coupons'][0], new WC_Coupon( $response['coupons'][0]['code'] ) );
$this->check_get_coupon_response( $response['coupons'][1], new WC_Coupon( $response['coupons'][1]['code'] ) );
}
/**
* Test PUT /coupons/bulk.
* @since 2.7.0
*/
public function test_edit_coupon_bulk() {
$coupon_1 = WC_Helper_Coupon::create_coupon( 'dummycoupon-1-' . time() );
$test_coupon_data = $this->get_defaults( array( 'description' => rand_str() ) );
$test_coupon_data['coupon']['id'] = $coupon_1->get_id();
$coupons = array( 'coupons' => array( $test_coupon_data['coupon'] ) );
$response = $this->endpoint->bulk( $coupons );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupons', $response );
$this->check_get_coupon_response( $response['coupons'][0], new WC_Coupon( $response['coupons'][0]['code'] ) );
}
}