Progress adding unit tests from core + wc-admin
This commit is contained in:
parent
de1c378054
commit
95b72e56f5
|
@ -221,7 +221,7 @@ abstract class AbstractObjectsController extends AbstractPostsController {
|
|||
$object = $this->get_object( (int) $request['id'] );
|
||||
|
||||
if ( ! $object || 0 === $object->get_id() ) {
|
||||
return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$object = $this->save_object( $request, false );
|
||||
|
@ -272,6 +272,7 @@ abstract class AbstractObjectsController extends AbstractPostsController {
|
|||
$args['post_parent__in'] = $request['parent'];
|
||||
$args['post_parent__not_in'] = $request['parent_exclude'];
|
||||
$args['s'] = $request['search'];
|
||||
$args['fields'] = 'ids';
|
||||
|
||||
if ( 'date' === $args['orderby'] ) {
|
||||
$args['orderby'] = 'date ID';
|
||||
|
@ -348,7 +349,7 @@ abstract class AbstractObjectsController extends AbstractPostsController {
|
|||
continue;
|
||||
}
|
||||
|
||||
$data = $this->prepare_object_for_response( $object, $request );
|
||||
$data = $this->prepare_object_for_response( $object, $request );
|
||||
$objects[] = $this->prepare_response_for_collection( $data );
|
||||
}
|
||||
|
||||
|
@ -407,17 +408,7 @@ abstract class AbstractObjectsController extends AbstractPostsController {
|
|||
return new \WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$supports_trash = EMPTY_TRASH_DAYS > 0 && is_callable( array( $object, 'get_status' ) );
|
||||
|
||||
/**
|
||||
* Filter whether an object is trashable.
|
||||
*
|
||||
* Return false to disable trash support for the object.
|
||||
*
|
||||
* @param boolean $supports_trash Whether the object type support trashing.
|
||||
* @param WC_Data $object The object being considered for trashing support.
|
||||
*/
|
||||
$supports_trash = apply_filters( "woocommerce_rest_{$this->post_type}_object_trashable", $supports_trash, $object );
|
||||
$supports_trash = $this->supports_trash( $object );
|
||||
|
||||
if ( ! wc_rest_check_post_permissions( $this->post_type, 'delete', $object->get_id() ) ) {
|
||||
/* translators: %s: post type */
|
||||
|
@ -425,7 +416,7 @@ abstract class AbstractObjectsController extends AbstractPostsController {
|
|||
}
|
||||
|
||||
$request->set_param( 'context', 'edit' );
|
||||
$response = $this->prepare_object_for_response( $object, $request );
|
||||
$previous = $this->prepare_object_for_response( $object, $request );
|
||||
|
||||
// If we're forcing, then delete permanently.
|
||||
if ( $force ) {
|
||||
|
@ -439,14 +430,12 @@ abstract class AbstractObjectsController extends AbstractPostsController {
|
|||
}
|
||||
|
||||
// Otherwise, only trash if we haven't already.
|
||||
if ( is_callable( array( $object, 'get_status' ) ) ) {
|
||||
if ( 'trash' === $object->get_status() ) {
|
||||
/* translators: %s: post type */
|
||||
return new \WP_Error( 'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 410 ) );
|
||||
}
|
||||
|
||||
if ( is_callable( array( $object, 'get_status' ) ) && 'trash' === $object->get_status() ) {
|
||||
/* translators: %s: post type */
|
||||
return new \WP_Error( 'woocommerce_rest_already_trashed', sprintf( __( 'The %s has already been deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 410 ) );
|
||||
} else {
|
||||
$object->delete();
|
||||
$result = 'trash' === $object->get_status();
|
||||
$result = is_callable( array( $object, 'get_status' ) ) ? 'trash' === $object->get_status() : true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -455,6 +444,14 @@ abstract class AbstractObjectsController extends AbstractPostsController {
|
|||
return new \WP_Error( 'woocommerce_rest_cannot_delete', sprintf( __( 'The %s cannot be deleted.', 'woocommerce' ), $this->post_type ), array( 'status' => 500 ) );
|
||||
}
|
||||
|
||||
$response = new \WP_REST_Response();
|
||||
$response->set_data(
|
||||
array(
|
||||
'deleted' => true,
|
||||
'previous' => $previous->get_data(),
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* Fires after a single object is deleted or trashed via the REST API.
|
||||
*
|
||||
|
@ -467,10 +464,30 @@ abstract class AbstractObjectsController extends AbstractPostsController {
|
|||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can this object be trashed?
|
||||
*
|
||||
* @oaram object $object Object to check.
|
||||
* @return boolean
|
||||
*/
|
||||
protected function supports_trash( $object ) {
|
||||
$supports_trash = EMPTY_TRASH_DAYS > 0;
|
||||
|
||||
/**
|
||||
* Filter whether an object is trashable.
|
||||
*
|
||||
* Return false to disable trash support for the object.
|
||||
*
|
||||
* @param boolean $supports_trash Whether the object type support trashing.
|
||||
* @param WC_Data $object The object being considered for trashing support.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_{$this->post_type}_object_trashable", $supports_trash, $object );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param WC_Data $object Object data.
|
||||
* @param WC_Data $object Object data.
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
* @return array Links for the given post.
|
||||
*/
|
||||
|
|
|
@ -14,7 +14,7 @@ defined( 'ABSPATH' ) || exit;
|
|||
/**
|
||||
* REST API Coupons controller class.
|
||||
*/
|
||||
class Coupons extends AbstractPostsController {
|
||||
class Coupons extends AbstractObjectsController {
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
|
@ -30,13 +30,6 @@ class Coupons extends AbstractPostsController {
|
|||
*/
|
||||
protected $post_type = 'shop_coupon';
|
||||
|
||||
/**
|
||||
* Coupons actions.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_filter( "woocommerce_rest_{$this->post_type}_query", array( $this, 'query_args' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the routes for coupons.
|
||||
*/
|
||||
|
@ -205,7 +198,7 @@ class Coupons extends AbstractPostsController {
|
|||
* Prepare a single coupon output for response.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param WC_Data $object Object data.
|
||||
* @param WC_Data $object Object data.
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
|
@ -250,9 +243,6 @@ class Coupons extends AbstractPostsController {
|
|||
$args['s'] = false;
|
||||
}
|
||||
|
||||
// Get only ids.
|
||||
$args['fields'] = 'ids';
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
|
@ -260,7 +250,7 @@ class Coupons extends AbstractPostsController {
|
|||
* Prepare a single coupon for create or update.
|
||||
*
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
* @param bool $creating If is creating a new object.
|
||||
* @param bool $creating If is creating a new object.
|
||||
* @return \WP_Error|WC_Data
|
||||
*/
|
||||
protected function prepare_object_for_database( $request, $creating = false ) {
|
||||
|
@ -549,101 +539,6 @@ class Coupons extends AbstractPostsController {
|
|||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query args.
|
||||
*
|
||||
* @param array $args Query args.
|
||||
* @param \WP_REST_Request $request Request data.
|
||||
* @return array
|
||||
*/
|
||||
public function query_args( $args, $request ) {
|
||||
if ( ! empty( $request['code'] ) ) {
|
||||
$id = wc_get_coupon_id_by_code( $request['code'] );
|
||||
$args['post__in'] = array( $id );
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single coupon output for response.
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response $data
|
||||
*/
|
||||
public function prepare_item_for_response( $post, $request ) {
|
||||
$coupon = new \WC_Coupon( (int) $post->ID );
|
||||
$_data = $coupon->get_data();
|
||||
|
||||
$format_decimal = array( 'amount', 'minimum_amount', 'maximum_amount' );
|
||||
$format_date = array( 'date_created', 'date_modified' );
|
||||
$format_date_utc = array( 'date_expires' );
|
||||
$format_null = array( 'usage_limit', 'usage_limit_per_user' );
|
||||
|
||||
// Format decimal values.
|
||||
foreach ( $format_decimal as $key ) {
|
||||
$_data[ $key ] = wc_format_decimal( $_data[ $key ], 2 );
|
||||
}
|
||||
|
||||
// Format date values.
|
||||
foreach ( $format_date as $key ) {
|
||||
$_data[ $key ] = $_data[ $key ] ? wc_rest_prepare_date_response( $_data[ $key ], false ) : null;
|
||||
}
|
||||
foreach ( $format_date_utc as $key ) {
|
||||
$_data[ $key ] = $_data[ $key ] ? wc_rest_prepare_date_response( $_data[ $key ] ) : null;
|
||||
}
|
||||
|
||||
// Format null values.
|
||||
foreach ( $format_null as $key ) {
|
||||
$_data[ $key ] = $_data[ $key ] ? $_data[ $key ] : null;
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'id' => $_data['id'],
|
||||
'code' => $_data['code'],
|
||||
'date_created' => $_data['date_created'],
|
||||
'date_modified' => $_data['date_modified'],
|
||||
'discount_type' => $_data['discount_type'],
|
||||
'description' => $_data['description'],
|
||||
'amount' => $_data['amount'],
|
||||
'expiry_date' => $_data['date_expires'],
|
||||
'usage_count' => $_data['usage_count'],
|
||||
'individual_use' => $_data['individual_use'],
|
||||
'product_ids' => $_data['product_ids'],
|
||||
'exclude_product_ids' => $_data['excluded_product_ids'],
|
||||
'usage_limit' => $_data['usage_limit'],
|
||||
'usage_limit_per_user' => $_data['usage_limit_per_user'],
|
||||
'limit_usage_to_x_items' => $_data['limit_usage_to_x_items'],
|
||||
'free_shipping' => $_data['free_shipping'],
|
||||
'product_categories' => $_data['product_categories'],
|
||||
'excluded_product_categories' => $_data['excluded_product_categories'],
|
||||
'exclude_sale_items' => $_data['exclude_sale_items'],
|
||||
'minimum_amount' => $_data['minimum_amount'],
|
||||
'maximum_amount' => $_data['maximum_amount'],
|
||||
'email_restrictions' => $_data['email_restrictions'],
|
||||
'used_by' => $_data['used_by'],
|
||||
);
|
||||
|
||||
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
|
||||
$data = $this->add_additional_fields_to_object( $data, $request );
|
||||
$data = $this->filter_response_by_context( $data, $context );
|
||||
$response = rest_ensure_response( $data );
|
||||
$response->add_links( $this->prepare_links( $post, $request ) );
|
||||
|
||||
/**
|
||||
* Filter the data for a response.
|
||||
*
|
||||
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
|
||||
* prepared for the response.
|
||||
*
|
||||
* @param WP_REST_Response $response The response object.
|
||||
* @param WP_Post $post Post object.
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
*/
|
||||
return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Only return writable props from schema.
|
||||
*
|
||||
|
@ -654,179 +549,6 @@ class Coupons extends AbstractPostsController {
|
|||
return empty( $schema['readonly'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a single coupon for create or update.
|
||||
*
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
* @return \WP_Error|stdClass $data Post object.
|
||||
*/
|
||||
protected function prepare_item_for_database( $request ) {
|
||||
$id = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
|
||||
$coupon = new \WC_Coupon( $id );
|
||||
$schema = $this->get_item_schema();
|
||||
$data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
|
||||
|
||||
// Update to schema to make compatible with CRUD schema.
|
||||
if ( $request['exclude_product_ids'] ) {
|
||||
$request['excluded_product_ids'] = $request['exclude_product_ids'];
|
||||
}
|
||||
if ( $request['expiry_date'] ) {
|
||||
$request['date_expires'] = $request['expiry_date'];
|
||||
}
|
||||
|
||||
// Validate required POST fields.
|
||||
if ( 'POST' === $request->get_method() && 0 === $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 ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Handle all writable props.
|
||||
foreach ( $data_keys as $key ) {
|
||||
$value = $request[ $key ];
|
||||
|
||||
if ( ! is_null( $value ) ) {
|
||||
switch ( $key ) {
|
||||
case 'code':
|
||||
$coupon_code = wc_format_coupon_code( $value );
|
||||
$id = $coupon->get_id() ? $coupon->get_id() : 0;
|
||||
$id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id );
|
||||
|
||||
if ( $id_from_code ) {
|
||||
return new \WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
$coupon->set_code( $coupon_code );
|
||||
break;
|
||||
case 'description':
|
||||
$coupon->set_description( wp_filter_post_kses( $value ) );
|
||||
break;
|
||||
case 'expiry_date':
|
||||
$coupon->set_date_expires( $value );
|
||||
break;
|
||||
default:
|
||||
if ( is_callable( array( $coupon, "set_{$key}" ) ) ) {
|
||||
$coupon->{"set_{$key}"}( $value );
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the query_vars used in `get_items` for the constructed query.
|
||||
*
|
||||
* The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
|
||||
* prepared for insertion.
|
||||
*
|
||||
* @param WC_Coupon $coupon The coupon object.
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
*/
|
||||
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'] ) ) {
|
||||
/* translators: %s: post type */
|
||||
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 WP_Post $post 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a coupon to the database.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param WP_REST_Request $request Full details about the request.
|
||||
* @return WP_Error|int
|
||||
*/
|
||||
protected function save_coupon( $request ) {
|
||||
try {
|
||||
$coupon = $this->prepare_item_for_database( $request );
|
||||
|
||||
if ( is_wp_error( $coupon ) ) {
|
||||
return $coupon;
|
||||
}
|
||||
|
||||
$coupon->save();
|
||||
return $coupon->get_id();
|
||||
} catch ( WC_Data_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
|
||||
} catch ( WC_REST_Exception $e ) {
|
||||
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ) {
|
||||
try {
|
||||
$post_id = (int) $request['id'];
|
||||
|
||||
if ( empty( $post_id ) || get_post_type( $post_id ) !== $this->post_type ) {
|
||||
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 );
|
||||
|
||||
/**
|
||||
* Fires after a single item is created or updated via the REST API.
|
||||
*
|
||||
* @param WP_Post $post 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 );
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
return new \WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a collection of posts and add the code search option to \WP_Query.
|
||||
*
|
||||
|
@ -834,9 +556,9 @@ class Coupons extends AbstractPostsController {
|
|||
* @return \WP_Error|WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
add_filter( 'posts_where', array( __CLASS__, 'add_wp_query_search_code_filter' ), 10, 2 );
|
||||
add_filter( 'posts_where', array( $this, 'add_wp_query_search_code_filter' ), 10, 2 );
|
||||
$response = parent::get_items( $request );
|
||||
remove_filter( 'posts_where', array( __CLASS__, 'add_wp_query_search_code_filter' ), 10 );
|
||||
remove_filter( 'posts_where', array( $this, 'add_wp_query_search_code_filter' ), 10 );
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
@ -847,7 +569,7 @@ class Coupons extends AbstractPostsController {
|
|||
* @param object $wp_query \WP_Query object.
|
||||
* @return string
|
||||
*/
|
||||
public static function add_wp_query_search_code_filter( $where, $wp_query ) {
|
||||
public function add_wp_query_search_code_filter( $where, $wp_query ) {
|
||||
global $wpdb;
|
||||
|
||||
$search = $wp_query->get( 'search' );
|
||||
|
|
|
@ -291,7 +291,7 @@ class Orders extends AbstractObjectsController {
|
|||
* Prepare a single order output for response.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param WC_Data $object Object data.
|
||||
* @param \WC_Data $object Object data.
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
|
@ -321,7 +321,7 @@ class Orders extends AbstractObjectsController {
|
|||
/**
|
||||
* Prepare links for the request.
|
||||
*
|
||||
* @param WC_Data $object Object data.
|
||||
* @param WC_Data $object Object data.
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
* @return array Links for the given post.
|
||||
*/
|
||||
|
@ -381,6 +381,47 @@ class Orders extends AbstractObjectsController {
|
|||
// Put the statuses back for further processing (next/prev links, etc).
|
||||
$request['status'] = $statuses;
|
||||
|
||||
// Customer.
|
||||
if ( isset( $request['customer'] ) ) {
|
||||
if ( ! empty( $args['meta_query'] ) ) {
|
||||
$args['meta_query'] = array(); // WPCS: slow query ok.
|
||||
}
|
||||
|
||||
$args['meta_query'][] = array(
|
||||
'key' => '_customer_user',
|
||||
'value' => $request['customer'],
|
||||
'type' => 'NUMERIC',
|
||||
);
|
||||
}
|
||||
|
||||
// Search by product.
|
||||
if ( ! empty( $request['product'] ) ) {
|
||||
$order_ids = $wpdb->get_col(
|
||||
$wpdb->prepare(
|
||||
"SELECT order_id
|
||||
FROM {$wpdb->prefix}woocommerce_order_items
|
||||
WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
|
||||
AND order_item_type = 'line_item'",
|
||||
$request['product']
|
||||
)
|
||||
);
|
||||
|
||||
// Force WP_Query return empty if don't found any order.
|
||||
$order_ids = ! empty( $order_ids ) ? $order_ids : array( 0 );
|
||||
|
||||
$args['post__in'] = $order_ids;
|
||||
}
|
||||
|
||||
// Search.
|
||||
if ( ! empty( $args['s'] ) ) {
|
||||
$order_ids = wc_order_search( $args['s'] );
|
||||
|
||||
if ( ! empty( $order_ids ) ) {
|
||||
unset( $args['s'] );
|
||||
$args['post__in'] = array_merge( $order_ids, array( 0 ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Search by partial order number.
|
||||
if ( ! empty( $request['number'] ) ) {
|
||||
$partial_number = trim( $request['number'] );
|
||||
|
@ -421,7 +462,7 @@ class Orders extends AbstractObjectsController {
|
|||
*
|
||||
* @throws WC_REST_Exception When fails to set any item.
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
* @param bool $creating If is creating a new object.
|
||||
* @param bool $creating If is creating a new object.
|
||||
* @return \WP_Error|WC_Data
|
||||
*/
|
||||
protected function prepare_object_for_database( $request, $creating = false ) {
|
||||
|
@ -490,12 +531,11 @@ class Orders extends AbstractObjectsController {
|
|||
|
||||
/**
|
||||
* Save an object data.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @throws WC_REST_Exception But all errors are validated before returning any data.
|
||||
* *
|
||||
* @param \WP_REST_Request $request Full details about the request.
|
||||
* @param bool $creating If is creating a new object.
|
||||
* @param bool $creating If is creating a new object.
|
||||
* @return WC_Data|\WP_Error
|
||||
* @throws \WC_REST_Exception But all errors are validated before returning any data.
|
||||
*/
|
||||
protected function save_object( $request, $creating = false ) {
|
||||
try {
|
||||
|
@ -574,9 +614,9 @@ class Orders extends AbstractObjectsController {
|
|||
/**
|
||||
* Gets the product ID from the SKU or posted ID.
|
||||
*
|
||||
* @throws WC_REST_Exception When SKU or ID is not valid.
|
||||
* @param array $posted Request data.
|
||||
* @return int
|
||||
* @throws \WC_REST_Exception When SKU or ID is not valid.
|
||||
*/
|
||||
protected function get_product_id( $posted ) {
|
||||
if ( ! empty( $posted['sku'] ) ) {
|
||||
|
@ -1711,7 +1751,7 @@ class Orders extends AbstractObjectsController {
|
|||
*
|
||||
* @throws WC_REST_Exception When fails to set any item.
|
||||
* @param \WP_REST_Request $request Request object.
|
||||
* @param WC_Order $order Order data.
|
||||
* @param WC_Order $order Order data.
|
||||
* @return bool
|
||||
*/
|
||||
protected function calculate_coupons( $request, $order ) {
|
||||
|
|
|
@ -2,6 +2,18 @@
|
|||
/**
|
||||
* REST API Endpoint Test base class.
|
||||
*
|
||||
* This class can be extended to add test coverage to REST API endpoints.
|
||||
*
|
||||
* For each endpoint, please test:
|
||||
* - Create
|
||||
* - Read
|
||||
* - Update
|
||||
* - Delete
|
||||
* - How the API responds to logged out/unauthorised users
|
||||
* - Schema
|
||||
* - Routes
|
||||
* - Collection params/queries
|
||||
*
|
||||
* @package WooCommerce/RestApi/Tests
|
||||
*/
|
||||
|
||||
|
@ -21,7 +33,7 @@ abstract class AbstractRestApiTest extends WC_REST_Unit_Test_Case {
|
|||
/**
|
||||
* The endpoint schema.
|
||||
*
|
||||
* @var array
|
||||
* @var array Keys are property names, values are supported context.
|
||||
*/
|
||||
protected $properties = [];
|
||||
|
||||
|
@ -43,6 +55,8 @@ abstract class AbstractRestApiTest extends WC_REST_Unit_Test_Case {
|
|||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
|
||||
wp_set_current_user( $this->user );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -63,32 +77,71 @@ abstract class AbstractRestApiTest extends WC_REST_Unit_Test_Case {
|
|||
* @return void
|
||||
*/
|
||||
public function test_schema_properties() {
|
||||
$this->user_request();
|
||||
|
||||
$request = new \WP_REST_Request( 'OPTIONS', $this->routes[0] );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( count( $this->properties ), count( $properties ) );
|
||||
$this->assertEquals( count( array_keys( $this->properties ) ), count( $properties ), print_r( array_diff( array_keys( $properties ), array_keys( $this->properties ) ), true ) );
|
||||
|
||||
foreach ( $this->properties as $property ) {
|
||||
foreach ( array_keys( $this->properties ) as $property ) {
|
||||
$this->assertArrayHasKey( $property, $properties );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set current user to an authenticated user.
|
||||
* Classes should test creation using this method.
|
||||
* If read-only, test to confirm this.
|
||||
*/
|
||||
protected function user_request() {
|
||||
wp_set_current_user( $this->user );
|
||||
abstract public function test_create();
|
||||
|
||||
/**
|
||||
* Classes should test get/read using this method.
|
||||
*/
|
||||
abstract public function test_read();
|
||||
|
||||
/**
|
||||
* Classes should test updates using this method.
|
||||
* If read-only, test to confirm this.
|
||||
*/
|
||||
abstract public function test_update();
|
||||
|
||||
/**
|
||||
* Classes should test delete using this method.
|
||||
* If read-only, test to confirm this.
|
||||
*/
|
||||
abstract public function test_delete();
|
||||
|
||||
/**
|
||||
* Tests delete when there is no user logged in.
|
||||
*/
|
||||
public function test_guest_create() {
|
||||
wp_set_current_user( 0 );
|
||||
$this->assertEquals( 0, get_current_user_id() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set current user to an unauthenticated user.
|
||||
* Tests delete when there is no user logged in.
|
||||
*/
|
||||
protected function guest_request() {
|
||||
public function test_guest_read() {
|
||||
wp_set_current_user( 0 );
|
||||
$this->assertEquals( 0, get_current_user_id() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests delete when there is no user logged in.
|
||||
*/
|
||||
public function test_guest_update() {
|
||||
wp_set_current_user( 0 );
|
||||
$this->assertEquals( 0, get_current_user_id() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests delete when there is no user logged in.
|
||||
*/
|
||||
public function test_guest_delete() {
|
||||
wp_set_current_user( 0 );
|
||||
$this->assertEquals( 0, get_current_user_id() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -96,21 +149,81 @@ abstract class AbstractRestApiTest extends WC_REST_Unit_Test_Case {
|
|||
*
|
||||
* @param string $endpoint Endpoint to hit.
|
||||
* @param string $type Type of request e.g GET or POST.
|
||||
* @param array $params Request body.
|
||||
* @param boolean $authenticated Do request as user or guest.
|
||||
* @param array $params Request body or query.
|
||||
* @return object
|
||||
*/
|
||||
protected function do_request( $endpoint, $type = 'GET', $params = array(), $authenticated = true ) {
|
||||
$authenticated ? $this->user_request() : $this->guest_request();
|
||||
|
||||
protected function do_request( $endpoint, $type = 'GET', $params = [] ) {
|
||||
$request = new \WP_REST_Request( $type, $endpoint );
|
||||
$request->set_body_params( $params );
|
||||
'GET' === $type ? $request->set_query_params( $params ) : $request->set_body_params( $params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
return (object) array(
|
||||
'status' => $response->get_status(),
|
||||
'data' => $response->get_data(),
|
||||
'data' => json_decode( wp_json_encode( $response->get_data() ), true ),
|
||||
'raw' => $response->get_data(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the request/response matched the data we sent.
|
||||
*
|
||||
* @param array $response Array of response data from do_request above.
|
||||
* @param int $status_code Expected status code.
|
||||
* @param array $data Array of expected data.
|
||||
*/
|
||||
protected function assertExpectedResponse( $response, $status_code = 200, $data = array() ) {
|
||||
$this->assertObjectHasAttribute( 'status', $response );
|
||||
$this->assertObjectHasAttribute( 'data', $response );
|
||||
$this->assertEquals( $status_code, $response->status, print_r( $response->data, true ) );
|
||||
|
||||
if ( $data ) {
|
||||
foreach ( $data as $key => $value ) {
|
||||
if ( ! isset( $response->data[ $key ] ) ) {
|
||||
continue;
|
||||
}
|
||||
switch ( $key ) {
|
||||
case 'meta_data':
|
||||
$this->assertMetaData( $value, $response->data[ $key ] );
|
||||
break;
|
||||
default:
|
||||
if ( is_array( $value ) ) {
|
||||
$this->assertArraySubset( $value, $response->data[ $key ] );
|
||||
} else {
|
||||
$this->assertEquals( $value, $response->data[ $key ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test meta data in a response matches what we expect.
|
||||
*
|
||||
* @param array $expected_meta_data Array of data.
|
||||
* @param array $actual_meta_data Array of data.
|
||||
*/
|
||||
protected function assertMetaData( $expected_meta_data, $actual_meta_data ) {
|
||||
$this->assertTrue( is_array( $actual_meta_data ) );
|
||||
$this->assertEquals( count( $expected_meta_data ), count( $actual_meta_data ) );
|
||||
|
||||
foreach ( $actual_meta_data as $key => $meta ) {
|
||||
$this->assertArrayHasKey( 'id', $meta );
|
||||
$this->assertArrayHasKey( 'key', $meta );
|
||||
$this->assertArrayHasKey( 'value', $meta );
|
||||
$this->assertEquals( $expected_meta_data[ $key ]['key'], $meta['key'] );
|
||||
$this->assertEquals( $expected_meta_data[ $key ]['value'], $meta['value'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return array of properties for a given context.
|
||||
*
|
||||
* @param string $context Context to use.
|
||||
* @return array
|
||||
*/
|
||||
protected function get_properties( $context = 'edit' ) {
|
||||
return array_keys( array_filter( $this->properties, function( $contexts ) use( $context ) {
|
||||
return in_array( $context, $contexts );
|
||||
} ) );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
/**
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Product Controller "products attributes terms" REST API Test
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
class WC_Tests_API_Products_Attributes_Terms_Controller extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc-blocks/v1';
|
||||
|
||||
/**
|
||||
* Setup test products data. Called before every test.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
|
||||
$this->contributor = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'contributor',
|
||||
)
|
||||
);
|
||||
|
||||
// Create 2 product attributes with terms.
|
||||
$this->attr_color = WC_Helper_Product::create_attribute( 'color', array( 'red', 'yellow', 'blue' ) );
|
||||
$this->attr_size = WC_Helper_Product::create_attribute( 'size', array( 'small', 'medium', 'large', 'xlarge' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting attribute terms.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_terms() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_color['attribute_id'] . '/terms' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_terms = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 3, count( $response_terms ) );
|
||||
$term = $response_terms[0];
|
||||
$this->assertArrayHasKey( 'attribute', $term );
|
||||
$attribute = $term['attribute'];
|
||||
$this->assertArrayHasKey( 'id', $attribute );
|
||||
$this->assertArrayHasKey( 'name', $attribute );
|
||||
$this->assertArrayHasKey( 'slug', $attribute );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting invalid attribute terms.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_invalid_attribute_terms() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/99999/terms' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test un-authorized getting attribute terms.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_unauthed_attribute_terms() {
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] . '/terms' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting attribute terms as contributor.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_attribute_terms_contributor() {
|
||||
wp_set_current_user( $this->contributor );
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] . '/terms' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_terms = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 4, count( $response_terms ) );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
/**
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Product Controller "products attributes" REST API Test
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
class WC_Tests_API_Products_Attributes_Controller extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc-blocks/v1';
|
||||
|
||||
/**
|
||||
* Setup test products data. Called before every test.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
|
||||
$this->contributor = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'contributor',
|
||||
)
|
||||
);
|
||||
|
||||
// Create 2 product attributes with terms.
|
||||
$this->attr_color = WC_Helper_Product::create_attribute( 'color', array( 'red', 'yellow', 'blue' ) );
|
||||
$this->attr_size = WC_Helper_Product::create_attribute( 'size', array( 'small', 'medium', 'large', 'xlarge' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting attributes.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_attributes() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_attributes = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $response_attributes ) );
|
||||
$attribute = $response_attributes[0];
|
||||
$this->assertArrayHasKey( 'id', $attribute );
|
||||
$this->assertArrayHasKey( 'name', $attribute );
|
||||
$this->assertArrayHasKey( 'slug', $attribute );
|
||||
$this->assertArrayHasKey( 'count', $attribute );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting invalid attribute.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_invalid_attribute() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/11111' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test un-authorized getting attribute.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_unauthed_attribute() {
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting attribute as contributor.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_attribute_contributor() {
|
||||
wp_set_current_user( $this->contributor );
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_size['attribute_id'] );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$attribute = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $this->attr_size['attribute_id'], $attribute['id'] );
|
||||
$this->assertEquals( $this->attr_size['attribute_name'], $attribute['name'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
/**
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Product Categories Controller REST API Test
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
class WC_Tests_API_Products_Categories_Controller extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc-blocks/v1';
|
||||
|
||||
/**
|
||||
* Setup test products data. Called before every test.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
|
||||
$this->contributor = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'contributor',
|
||||
)
|
||||
);
|
||||
|
||||
// Create 3 product categories.
|
||||
$parent = wp_insert_term( 'Parent Category', 'product_cat' );
|
||||
$child = wp_insert_term(
|
||||
'Child Category',
|
||||
'product_cat',
|
||||
array( 'parent' => $parent['term_id'] )
|
||||
);
|
||||
$single = wp_insert_term( 'Standalone Category', 'product_cat' );
|
||||
$this->categories = array(
|
||||
'parent' => $parent,
|
||||
'child' => $child,
|
||||
'single' => $single,
|
||||
);
|
||||
|
||||
// Create two products for the parent category.
|
||||
$this->products = array();
|
||||
$this->products[0] = WC_Helper_Product::create_simple_product( false );
|
||||
$this->products[0]->set_category_ids( array( $parent['term_id'] ) );
|
||||
$this->products[0]->save();
|
||||
|
||||
$this->products[3] = WC_Helper_Product::create_simple_product( false );
|
||||
$this->products[3]->set_category_ids( array( $parent['term_id'], $single['term_id'] ) );
|
||||
$this->products[3]->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting product categories.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_product_categories() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$categories = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 4, count( $categories ) ); // Three created and `uncategorized`.
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting invalid product category.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_invalid_product_category() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories/007' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test un-authorized getting product category.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_unauthed_product_category() {
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories/' . $this->categories['parent']['term_id'] );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting category as contributor.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_attribute_terms_contributor() {
|
||||
wp_set_current_user( $this->contributor );
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/categories/' . $this->categories['parent']['term_id'] );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$category = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $category['name'], 'Parent Category' );
|
||||
$this->assertEquals( $category['parent'], 0 );
|
||||
$this->assertEquals( $category['count'], 2 );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,305 @@
|
|||
<?php
|
||||
/**
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Blocks Product Controller REST API Test
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
class WC_Tests_API_Products_Controller extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc-blocks/v1';
|
||||
|
||||
/**
|
||||
* Setup test products data. Called before every test.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'author',
|
||||
)
|
||||
);
|
||||
$this->contributor = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'contributor',
|
||||
)
|
||||
);
|
||||
$this->subscriber = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'subscriber',
|
||||
)
|
||||
);
|
||||
|
||||
// Create 3 product categories.
|
||||
$parent = wp_insert_term( 'Parent Category', 'product_cat' );
|
||||
$child = wp_insert_term(
|
||||
'Child Category',
|
||||
'product_cat',
|
||||
array( 'parent' => $parent['term_id'] )
|
||||
);
|
||||
$single = wp_insert_term( 'Standalone Category', 'product_cat' );
|
||||
$this->categories = array(
|
||||
'parent' => $parent,
|
||||
'child' => $child,
|
||||
'single' => $single,
|
||||
);
|
||||
|
||||
// Create two products, one with 'parent', and one with 'single'.
|
||||
$this->products = array();
|
||||
$this->products[0] = WC_Helper_Product::create_simple_product( false );
|
||||
$this->products[0]->set_category_ids( array( $parent['term_id'] ) );
|
||||
$this->products[0]->save();
|
||||
|
||||
$this->products[1] = WC_Helper_Product::create_simple_product( false );
|
||||
$this->products[1]->set_category_ids( array( $single['term_id'] ) );
|
||||
$this->products[1]->save();
|
||||
|
||||
$this->products[2] = WC_Helper_Product::create_simple_product( false );
|
||||
$this->products[2]->set_category_ids( array( $child['term_id'], $single['term_id'] ) );
|
||||
$this->products[2]->save();
|
||||
|
||||
$this->products[3] = WC_Helper_Product::create_simple_product( false );
|
||||
$this->products[3]->set_category_ids( array( $parent['term_id'], $single['term_id'] ) );
|
||||
$this->products[3]->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( '/wc-blocks/v1/products', $routes );
|
||||
$this->assertArrayHasKey( '/wc-blocks/v1/products/(?P<id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_products() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Product::create_external_product();
|
||||
sleep( 1 ); // So both products have different timestamps.
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 6, count( $products ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products as an contributor.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_products_as_contributor() {
|
||||
wp_set_current_user( $this->contributor );
|
||||
WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products as an subscriber.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_products_as_subscriber() {
|
||||
wp_set_current_user( $this->subscriber );
|
||||
WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc-blocks/v1/products' ) );
|
||||
$this->assertEquals( 403, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products with custom ordering.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_products_order_by_price() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Product::create_external_product();
|
||||
sleep( 1 ); // So both products have different timestamps.
|
||||
$product = WC_Helper_Product::create_simple_product( false ); // Prevent saving, since we save here.
|
||||
// Customize the price, otherwise both are 10.
|
||||
$product->set_props(
|
||||
array(
|
||||
'regular_price' => 15,
|
||||
'price' => 15,
|
||||
)
|
||||
);
|
||||
$product->save();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' );
|
||||
$request->set_param( 'orderby', 'price' );
|
||||
$request->set_param( 'order', 'asc' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 6, count( $products ) );
|
||||
|
||||
$this->assertEquals( 'Dummy Product', $products[1]['name'] );
|
||||
$this->assertEquals( '10', $products[1]['price'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test product_visibility queries.
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_product_visibility() {
|
||||
wp_set_current_user( $this->user );
|
||||
$visible_product = WC_Helper_Product::create_simple_product();
|
||||
$visible_product->set_name( 'Visible Product' );
|
||||
$visible_product->set_catalog_visibility( 'visible' );
|
||||
$visible_product->save();
|
||||
|
||||
$catalog_product = WC_Helper_Product::create_simple_product();
|
||||
$catalog_product->set_name( 'Catalog Product' );
|
||||
$catalog_product->set_catalog_visibility( 'catalog' );
|
||||
$catalog_product->save();
|
||||
|
||||
$search_product = WC_Helper_Product::create_simple_product();
|
||||
$search_product->set_name( 'Search Product' );
|
||||
$search_product->set_catalog_visibility( 'search' );
|
||||
$search_product->save();
|
||||
|
||||
$hidden_product = WC_Helper_Product::create_simple_product();
|
||||
$hidden_product->set_name( 'Hidden Product' );
|
||||
$hidden_product->set_catalog_visibility( 'hidden' );
|
||||
$hidden_product->save();
|
||||
|
||||
$query_params = array(
|
||||
'catalog_visibility' => 'visible',
|
||||
);
|
||||
$request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 5, count( $products ) );
|
||||
$this->assertEquals( 'Visible Product', $products[0]['name'] );
|
||||
|
||||
$query_params = array(
|
||||
'catalog_visibility' => 'catalog',
|
||||
'orderby' => 'id',
|
||||
'order' => 'asc',
|
||||
);
|
||||
$request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 6, count( $products ) );
|
||||
$this->assertEquals( 'Dummy Product', $products[0]['name'] );
|
||||
|
||||
$query_params = array(
|
||||
'catalog_visibility' => 'search',
|
||||
'orderby' => 'id',
|
||||
'order' => 'asc',
|
||||
);
|
||||
$request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 6, count( $products ) );
|
||||
$this->assertEquals( 'Dummy Product', $products[0]['name'] );
|
||||
|
||||
$query_params = array(
|
||||
'catalog_visibility' => 'hidden',
|
||||
);
|
||||
$request = new WP_REST_REQUEST( 'GET', '/wc-blocks/v1/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $products ) );
|
||||
$this->assertEquals( 'Hidden Product', $products[0]['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test product category intersection: Any product in either Single or Child (3).
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_products_in_any_categories_child() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$cats = $this->categories['child']['term_id'] . ',' . $this->categories['single']['term_id'];
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' );
|
||||
$request->set_param( 'category', $cats );
|
||||
$request->set_param( 'category_operator', 'in' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 3, count( $response_products ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test product category intersection: Any product in both Single and Child (1).
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_products_in_all_categories_child() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$cats = $this->categories['child']['term_id'] . ',' . $this->categories['single']['term_id'];
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' );
|
||||
$request->set_param( 'category', $cats );
|
||||
$request->set_param( 'category_operator', 'and' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $response_products ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test product category intersection: Any product in both Single and Parent (1).
|
||||
*
|
||||
* @since 3.6.0
|
||||
*/
|
||||
public function test_get_products_in_all_categories_parent() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$cats = $this->categories['parent']['term_id'] . ',' . $this->categories['single']['term_id'];
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc-blocks/v1/products' );
|
||||
$request->set_param( 'category', $cats );
|
||||
$request->set_param( 'category_operator', 'and' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $response_products ) );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,471 @@
|
|||
<?php
|
||||
/**
|
||||
* Coupon API Tests
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
class WC_Tests_API_Coupons_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
protected $endpoint;
|
||||
|
||||
/**
|
||||
* Setup test coupon data.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Coupons_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/coupons/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting coupons.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$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' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) );
|
||||
$coupons = $response->get_data();
|
||||
|
||||
$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, false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $post_1->post_date_gmt ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $post_1->post_modified_gmt, false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $post_1->post_modified_gmt ),
|
||||
'discount_type' => 'fixed_cart',
|
||||
'description' => 'This is a dummy coupon',
|
||||
'date_expires' => '',
|
||||
'date_expires_gmt' => '',
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => '',
|
||||
'usage_limit_per_user' => '',
|
||||
'limit_usage_to_x_items' => null,
|
||||
'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(),
|
||||
'meta_data' => array(),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/coupons/' . $coupon_1->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/coupons' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$coupons
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting coupons without valid permissions.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_coupons_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single coupon.
|
||||
* @since 3.0.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/v2/coupons/' . $coupon->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$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, false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $post->post_date_gmt ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt, false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $post->post_modified_gmt ),
|
||||
'discount_type' => 'fixed_cart',
|
||||
'description' => 'This is a dummy coupon',
|
||||
'date_expires' => null,
|
||||
'date_expires_gmt' => null,
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => null,
|
||||
'usage_limit_per_user' => null,
|
||||
'limit_usage_to_x_items' => null,
|
||||
'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(),
|
||||
'meta_data' => array(),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single coupon with an invalid ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single coupon without valid permissions.
|
||||
* @since 3.0.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/v2/coupons/' . $coupon->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single coupon.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/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();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'code' => 'test',
|
||||
'amount' => '5.00',
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'discount_type' => 'fixed_product',
|
||||
'description' => 'Test',
|
||||
'date_expires' => null,
|
||||
'date_expires_gmt' => null,
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => 10,
|
||||
'usage_limit_per_user' => null,
|
||||
'limit_usage_to_x_items' => null,
|
||||
'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(),
|
||||
'meta_data' => array(),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single coupon with invalid fields.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_coupon_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test no code...
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single coupon without valid permissions.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
// test no code...
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/coupons' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'code' => 'fail',
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single coupon.
|
||||
* @since 3.0.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() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/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'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( '10.00', $data['amount'] );
|
||||
$this->assertEquals( 'New description', $data['description'] );
|
||||
$this->assertEquals( 'fixed_cart', $data['discount_type'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single coupon with an invalid ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'code' => 'tester',
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single coupon without valid permissions.
|
||||
* @since 3.0.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() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single coupon.
|
||||
* @since 3.0.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/v2/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 3.0.0
|
||||
*/
|
||||
public function test_delete_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/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 3.0.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/v2/coupons/' . $coupon->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch operations on coupons.
|
||||
* @since 3.0.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/v2/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/v2/coupons' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test coupon schema.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_coupon_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/coupons' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 27, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'code', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'discount_type', $properties );
|
||||
$this->assertArrayHasKey( 'amount', $properties );
|
||||
$this->assertArrayHasKey( 'date_expires', $properties );
|
||||
$this->assertArrayHasKey( 'date_expires_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'usage_count', $properties );
|
||||
$this->assertArrayHasKey( 'individual_use', $properties );
|
||||
$this->assertArrayHasKey( 'product_ids', $properties );
|
||||
$this->assertArrayHasKey( 'excluded_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 );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,566 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the Customers REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
class Customers_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Customers_Controller();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( '/wc/v2/customers', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/customers/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/customers/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting customers.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_customers() {
|
||||
wp_set_current_user( 1 );
|
||||
|
||||
$customer_1 = WC_Helper_Customer::create_customer();
|
||||
WC_Helper_Customer::create_customer( 'test2', 'test2', 'test2@woo.local' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/customers' );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'orderby' => 'id',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$customers = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $customers ) );
|
||||
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $customer_1->get_id(),
|
||||
'date_created' => wc_rest_prepare_date_response( $customer_1->get_date_created(), false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $customer_1->get_date_created() ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $customer_1->get_date_modified(), false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $customer_1->get_date_modified() ),
|
||||
'email' => 'test@woo.local',
|
||||
'first_name' => 'Justin',
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'testcustomer',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $customer_1->get_avatar_url(),
|
||||
'meta_data' => array(),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/customers/' . $customer_1->get_id() . '' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/customers' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$customers
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting customers without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_customers_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a new customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
|
||||
// Test just the basics first..
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'username' => 'create_customer_test',
|
||||
'password' => 'test123',
|
||||
'email' => 'create_customer_test@woo.local',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'create_customer_test@woo.local',
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'create_customer_test',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => '',
|
||||
'postcode' => '',
|
||||
'country' => '',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => '',
|
||||
'postcode' => '',
|
||||
'country' => '',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
),
|
||||
$data
|
||||
);
|
||||
|
||||
// Test extra data
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'username' => 'create_customer_test2',
|
||||
'password' => 'test123',
|
||||
'email' => 'create_customer_test2@woo.local',
|
||||
'first_name' => 'Test',
|
||||
'last_name' => 'McTestFace',
|
||||
'billing' => array(
|
||||
'country' => 'US',
|
||||
'state' => 'WA',
|
||||
),
|
||||
'shipping' => array(
|
||||
'state' => 'CA',
|
||||
'country' => 'US',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'create_customer_test2@woo.local',
|
||||
'first_name' => 'Test',
|
||||
'last_name' => 'McTestFace',
|
||||
'role' => 'customer',
|
||||
'username' => 'create_customer_test2',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => 'WA',
|
||||
'postcode' => '',
|
||||
'country' => 'US',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => 'CA',
|
||||
'postcode' => '',
|
||||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
),
|
||||
$data
|
||||
);
|
||||
|
||||
// Test without required field
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'username' => 'create_customer_test3',
|
||||
'first_name' => 'Test',
|
||||
'last_name' => 'McTestFace',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating customers without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'username' => 'create_customer_test_without_permission',
|
||||
'password' => 'test123',
|
||||
'email' => 'create_customer_test_without_permission@woo.local',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'get_customer_test', 'test123', 'get_customer_test@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'get_customer_test@woo.local',
|
||||
'first_name' => 'Justin',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'get_customer_test',
|
||||
'orders_count' => 0,
|
||||
'total_spent' => '0.00',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single customer without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'get_customer_test_without_permission', 'test123', 'get_customer_test_without_permission@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single customer with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'update_customer_test', 'test123', 'update_customer_test@woo.local' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'update_customer_test', $data['username'] );
|
||||
$this->assertEquals( 'update_customer_test@woo.local', $data['email'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'email' => 'updated_email@woo.local',
|
||||
'first_name' => 'UpdatedTest',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'updated_email@woo.local', $data['email'] );
|
||||
$this->assertEquals( 'UpdatedTest', $data['first_name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a customer without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'update_customer_test_without_permission', 'test123', 'update_customer_test_without_permission@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/' . $customer->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a customer with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/customers/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test deleting a customer.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'delete_customer_test', 'test123', 'delete_customer_test@woo.local' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a customer with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a customer without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'delete_customer_test_without_permission', 'test123', 'delete_customer_test_without_permission@woo.local' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/customers/' . $customer->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test customer batch endpoint.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_batch_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
|
||||
$customer_1 = WC_Helper_Customer::create_customer( 'test_batch_customer', 'test123', 'test_batch_customer@woo.local' );
|
||||
$customer_2 = WC_Helper_Customer::create_customer( 'test_batch_customer2', 'test123', 'test_batch_customer2@woo.local' );
|
||||
$customer_3 = WC_Helper_Customer::create_customer( 'test_batch_customer3', 'test123', 'test_batch_customer3@woo.local' );
|
||||
$customer_4 = WC_Helper_Customer::create_customer( 'test_batch_customer4', 'test123', 'test_batch_customer4@woo.local' );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/customers/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $customer_1->get_id(),
|
||||
'last_name' => 'McTest',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$customer_2->get_id(),
|
||||
$customer_3->get_id(),
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'username' => 'newuser',
|
||||
'password' => 'test123',
|
||||
'email' => 'newuser@woo.local',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'McTest', $data['update'][0]['last_name'] );
|
||||
$this->assertEquals( 'newuser', $data['create'][0]['username'] );
|
||||
$this->assertEmpty( $data['create'][0]['last_name'] );
|
||||
$this->assertEquals( $customer_2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $customer_3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test customer schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_customer_schema() {
|
||||
wp_set_current_user( 1 );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 18, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'email', $properties );
|
||||
$this->assertArrayHasKey( 'first_name', $properties );
|
||||
$this->assertArrayHasKey( 'last_name', $properties );
|
||||
$this->assertArrayHasKey( 'role', $properties );
|
||||
$this->assertArrayHasKey( 'username', $properties );
|
||||
$this->assertArrayHasKey( 'password', $properties );
|
||||
$this->assertArrayHasKey( 'orders_count', $properties );
|
||||
$this->assertArrayHasKey( 'total_spent', $properties );
|
||||
$this->assertArrayHasKey( 'avatar_url', $properties );
|
||||
$this->assertArrayHasKey( 'billing', $properties );
|
||||
$this->assertArrayHasKey( 'first_name', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'last_name', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'company', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_1', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_2', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'city', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'state', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'postcode', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'country', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'email', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'phone', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'shipping', $properties );
|
||||
$this->assertArrayHasKey( 'first_name', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'last_name', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'company', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_1', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_2', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'city', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'state', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'postcode', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'country', $properties['shipping']['properties'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,635 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the orders REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
class WC_Tests_API_Orders_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Array of order to track
|
||||
* @var array
|
||||
*/
|
||||
protected $orders = array();
|
||||
|
||||
/**
|
||||
* Setup our test server.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Orders_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/orders', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/orders/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/orders/(?P<id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all orders.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_items() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Create 10 orders.
|
||||
for ( $i = 0; $i < 10; $i++ ) {
|
||||
$this->orders[] = WC_Helper_Order::create_order( $this->user );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) );
|
||||
$orders = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 10, count( $orders ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure orders cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_items_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$this->orders[] = WC_Helper_Order::create_order();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single order.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_item() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$order->add_meta_data( 'key', 'value' );
|
||||
$order->add_meta_data( 'key2', 'value2' );
|
||||
$order->save();
|
||||
$this->orders[] = $order;
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $order->get_id(), $data['id'] );
|
||||
|
||||
// Test meta data is set.
|
||||
$this->assertEquals( 'key', $data['meta_data'][0]->key );
|
||||
$this->assertEquals( 'value', $data['meta_data'][0]->value );
|
||||
$this->assertEquals( 'key2', $data['meta_data'][1]->key );
|
||||
$this->assertEquals( 'value2', $data['meta_data'][1]->value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single order without the correct permissions.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_item_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$this->orders[] = $order;
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting an order with an invalid ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_item_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/99999999' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting an order with an invalid ID.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_item_refund_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$refund = wc_create_refund(
|
||||
array(
|
||||
'order_id' => $order->get_id(),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $refund->get_id() ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating an order.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => 'Direct Bank Transfer',
|
||||
'set_paid' => true,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => '10',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$order = wc_get_order( $data['id'] );
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals( $order->get_payment_method(), $data['payment_method'] );
|
||||
$this->assertEquals( $order->get_payment_method_title(), $data['payment_method_title'] );
|
||||
$this->assertEquals( $order->get_billing_first_name(), $data['billing']['first_name'] );
|
||||
$this->assertEquals( $order->get_billing_last_name(), $data['billing']['last_name'] );
|
||||
$this->assertEquals( '', $data['billing']['company'] );
|
||||
$this->assertEquals( $order->get_billing_address_1(), $data['billing']['address_1'] );
|
||||
$this->assertEquals( $order->get_billing_address_2(), $data['billing']['address_2'] );
|
||||
$this->assertEquals( $order->get_billing_city(), $data['billing']['city'] );
|
||||
$this->assertEquals( $order->get_billing_state(), $data['billing']['state'] );
|
||||
$this->assertEquals( $order->get_billing_postcode(), $data['billing']['postcode'] );
|
||||
$this->assertEquals( $order->get_billing_country(), $data['billing']['country'] );
|
||||
$this->assertEquals( $order->get_billing_email(), $data['billing']['email'] );
|
||||
$this->assertEquals( $order->get_billing_phone(), $data['billing']['phone'] );
|
||||
$this->assertEquals( $order->get_shipping_first_name(), $data['shipping']['first_name'] );
|
||||
$this->assertEquals( $order->get_shipping_last_name(), $data['shipping']['last_name'] );
|
||||
$this->assertEquals( '', $data['shipping']['company'] );
|
||||
$this->assertEquals( $order->get_shipping_address_1(), $data['shipping']['address_1'] );
|
||||
$this->assertEquals( $order->get_shipping_address_2(), $data['shipping']['address_2'] );
|
||||
$this->assertEquals( $order->get_shipping_city(), $data['shipping']['city'] );
|
||||
$this->assertEquals( $order->get_shipping_state(), $data['shipping']['state'] );
|
||||
$this->assertEquals( $order->get_shipping_postcode(), $data['shipping']['postcode'] );
|
||||
$this->assertEquals( $order->get_shipping_country(), $data['shipping']['country'] );
|
||||
$this->assertEquals( 1, count( $data['line_items'] ) );
|
||||
$this->assertEquals( 1, count( $data['shipping_lines'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the sanitization of the payment_method_title field through the API.
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public function test_create_update_order_payment_method_title_sanitize() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// Test when creating order.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/orders' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => '<h1>Sanitize this <script>alert(1);</script></h1>',
|
||||
'set_paid' => true,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => '10',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$order = wc_get_order( $data['id'] );
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals( $order->get_payment_method(), $data['payment_method'] );
|
||||
$this->assertEquals( $order->get_payment_method_title(), 'Sanitize this' );
|
||||
|
||||
// Test when updating order.
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $data['id'] );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => '<h1>Sanitize this too <script>alert(1);</script></h1>',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$order = wc_get_order( $data['id'] );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $order->get_payment_method(), $data['payment_method'] );
|
||||
$this->assertEquals( $order->get_payment_method_title(), 'Sanitize this too' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating an order without required fields.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_order_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// non-existent customer
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => 'Direct Bank Transfer',
|
||||
'set_paid' => true,
|
||||
'customer_id' => 99999,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => 10,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
'first_name' => 'Fish',
|
||||
'last_name' => 'Face',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'test-update', $data['payment_method'] );
|
||||
$this->assertEquals( 'Fish', $data['billing']['first_name'] );
|
||||
$this->assertEquals( 'Face', $data['billing']['last_name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and removing items.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_order_remove_items() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$fee = new WC_Order_Item_Fee();
|
||||
$fee->set_props(
|
||||
array(
|
||||
'name' => 'Some Fee',
|
||||
'tax_status' => 'taxable',
|
||||
'total' => '100',
|
||||
'tax_class' => '',
|
||||
)
|
||||
);
|
||||
$order->add_item( $fee );
|
||||
$order->save();
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$fee_data = current( $order->get_items( 'fee' ) );
|
||||
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'fee_lines' => array(
|
||||
array(
|
||||
'id' => $fee_data->get_id(),
|
||||
'name' => null,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertTrue( empty( $data['fee_lines'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and adding a coupon.
|
||||
*
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public function test_update_order_add_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$order_item = current( $order->get_items() );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' );
|
||||
$coupon->set_amount( 5 );
|
||||
$coupon->save();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'coupon_lines' => array(
|
||||
array(
|
||||
'code' => 'fake-coupon',
|
||||
'discount_total' => '5',
|
||||
'discount_tax' => '0',
|
||||
),
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'id' => $order_item->get_id(),
|
||||
'product_id' => $order_item->get_product_id(),
|
||||
'total' => '35.00',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $data['coupon_lines'] );
|
||||
$this->assertEquals( '45.00', $data['total'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and removing a coupon.
|
||||
*
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public function test_update_order_remove_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$order_item = current( $order->get_items() );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' );
|
||||
$coupon->set_amount( 5 );
|
||||
$coupon->save();
|
||||
|
||||
$order->apply_coupon( $coupon );
|
||||
$order->save();
|
||||
|
||||
// Check that the coupon is applied.
|
||||
$this->assertEquals( '45.00', $order->get_total() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$coupon_data = current( $order->get_items( 'coupon' ) );
|
||||
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'coupon_lines' => array(
|
||||
array(
|
||||
'id' => $coupon_data->get_id(),
|
||||
'code' => null,
|
||||
),
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'id' => $order_item->get_id(),
|
||||
'product_id' => $order_item->get_product_id(),
|
||||
'total' => '40.00',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertTrue( empty( $data['coupon_lines'] ) );
|
||||
$this->assertEquals( '50.00', $data['total'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order without the correct permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_order_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
'first_name' => 'Fish',
|
||||
'last_name' => 'Face',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that updating an order with an invalid id fails.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_order_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/999999' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
'first_name' => 'Fish',
|
||||
'last_name' => 'Face',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting an order.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( null, get_post( $order->get_id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting an order without permission/creds.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_order_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting an order with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_order_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/9999999' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing product reviews.
|
||||
*/
|
||||
public function test_orders_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$order1 = WC_Helper_Order::create_order();
|
||||
$order2 = WC_Helper_Order::create_order();
|
||||
$order3 = WC_Helper_Order::create_order();
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $order1->get_id(),
|
||||
'payment_method' => 'updated',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$order2->get_id(),
|
||||
$order3->get_id(),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'updated', $data['update'][0]['payment_method'] );
|
||||
$this->assertEquals( $order2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $order3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/orders' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 1, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the order schema.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_order_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/orders/' . $order->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 42, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,337 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the Payment Gateways REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
class Payment_Gateways_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Payment_Gateways_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/payment_gateways', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/payment_gateways/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all payment gateways.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_payment_gateways() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) );
|
||||
$gateways = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'cheque',
|
||||
'title' => 'Check payments',
|
||||
'description' => 'Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.',
|
||||
'order' => '',
|
||||
'enabled' => false,
|
||||
'method_title' => 'Check payments',
|
||||
'method_description' => 'Take payments in person via checks. This offline gateway can also be useful to test purchases.',
|
||||
'settings' => array_diff_key(
|
||||
$this->get_settings( 'WC_Gateway_Cheque' ),
|
||||
array(
|
||||
'enabled' => false,
|
||||
'description' => false,
|
||||
)
|
||||
),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/payment_gateways/cheque' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/payment_gateways' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$gateways
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure payment gateways cannot viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_payment_gateways_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single payment gateway.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_payment_gateway() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => 'paypal',
|
||||
'title' => 'PayPal',
|
||||
'description' => "Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.",
|
||||
'order' => '',
|
||||
'enabled' => false,
|
||||
'method_title' => 'PayPal',
|
||||
'method_description' => 'PayPal Standard redirects customers to PayPal to enter their payment information.',
|
||||
'settings' => array_diff_key(
|
||||
$this->get_settings( 'WC_Gateway_Paypal' ),
|
||||
array(
|
||||
'enabled' => false,
|
||||
'description' => false,
|
||||
)
|
||||
),
|
||||
),
|
||||
$paypal
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a payment gateway without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_payment_gateway_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a payment gateway with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_payment_gateway_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/totally_fake_method' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single payment gateway.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_payment_gateway() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Test defaults
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'admin@example.org', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test updating single setting
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'email' => 'woo@woo.local',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test updating multiple settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'testmode' => 'yes',
|
||||
'title' => 'PayPal - New Title',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// Test other parameters, and recheck settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertFalse( $paypal['enabled'] );
|
||||
$this->assertEquals( 2, $paypal['order'] );
|
||||
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test bogus
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'paymentaction' => 'afasfasf',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'paymentaction' => 'authorization',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
$this->assertEquals( 'authorization', $paypal['settings']['paymentaction']['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a payment gateway without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_payment_gateway_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'testmode' => 'yes',
|
||||
'title' => 'PayPal - New Title',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a payment gateway with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_payment_gateway_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/totally_fake_method' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'enabled' => true,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the payment gateway schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_payment_gateway_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/payment_gateways' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 8, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'title', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'order', $properties );
|
||||
$this->assertArrayHasKey( 'enabled', $properties );
|
||||
$this->assertArrayHasKey( 'method_title', $properties );
|
||||
$this->assertArrayHasKey( 'method_description', $properties );
|
||||
$this->assertArrayHasKey( 'settings', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a particular gateway's settings so we can correctly test API output.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @param string $gateway_class Name of WC_Payment_Gateway class.
|
||||
*/
|
||||
private function get_settings( $gateway_class ) {
|
||||
$gateway = new $gateway_class();
|
||||
$settings = array();
|
||||
$gateway->init_form_fields();
|
||||
foreach ( $gateway->form_fields as $id => $field ) {
|
||||
// Make sure we at least have a title and type
|
||||
if ( empty( $field['title'] ) || empty( $field['type'] ) ) {
|
||||
continue;
|
||||
}
|
||||
// Ignore 'title' settings/fields -- they are UI only
|
||||
if ( 'title' === $field['type'] ) {
|
||||
continue;
|
||||
}
|
||||
$data = array(
|
||||
'id' => $id,
|
||||
'label' => empty( $field['label'] ) ? $field['title'] : $field['label'],
|
||||
'description' => empty( $field['description'] ) ? '' : $field['description'],
|
||||
'type' => $field['type'],
|
||||
'value' => $gateway->settings[ $id ],
|
||||
'default' => empty( $field['default'] ) ? '' : $field['default'],
|
||||
'tip' => empty( $field['description'] ) ? '' : $field['description'],
|
||||
'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'],
|
||||
);
|
||||
if ( ! empty( $field['options'] ) ) {
|
||||
$data['options'] = $field['options'];
|
||||
}
|
||||
$settings[ $id ] = $data;
|
||||
}
|
||||
return $settings;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,468 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the product reviews REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class WC_Tests_API_Product_Reviews_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/products/reviews', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/reviews/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/reviews/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all product reviews.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_reviews() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
// Create 10 products reviews for the product
|
||||
for ( $i = 0; $i < 10; $i++ ) {
|
||||
$review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) );
|
||||
$product_reviews = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 10, count( $product_reviews ) );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $review_id,
|
||||
'date_created' => $product_reviews[0]['date_created'],
|
||||
'date_created_gmt' => $product_reviews[0]['date_created_gmt'],
|
||||
'product_id' => $product->get_id(),
|
||||
'status' => 'approved',
|
||||
'reviewer' => 'admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'review' => "<p>Review content here</p>\n",
|
||||
'rating' => 0,
|
||||
'verified' => false,
|
||||
'reviewer_avatar_urls' => $product_reviews[0]['reviewer_avatar_urls'],
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/products/reviews/' . $review_id ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/products/reviews' ),
|
||||
),
|
||||
),
|
||||
'up' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/products/' . $product->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$product_reviews
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure product reviews cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_reviews_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure an error is returned when an invalid product is loaded.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_reviews_invalid_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/0/reviews' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $product_review_id,
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'product_id' => $product->get_id(),
|
||||
'status' => 'approved',
|
||||
'reviewer' => 'admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'review' => "<p>Review content here</p>\n",
|
||||
'rating' => 0,
|
||||
'verified' => false,
|
||||
'reviewer_avatar_urls' => $data['reviewer_avatar_urls'],
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single product review without the correct permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_review_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a product review with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating a product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'rating' => '5',
|
||||
'product_id' => $product->get_id(),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'product_id' => $product->get_id(),
|
||||
'status' => 'approved',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'review' => 'Hello world.',
|
||||
'rating' => 5,
|
||||
'verified' => false,
|
||||
'reviewer_avatar_urls' => $data['reviewer_avatar_urls'],
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating a product review without required fields.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_product_review_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// missing review
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// Missing reviewer.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// missing reviewer_email
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating a product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( "<p>Review content here</p>\n", $data['review'] );
|
||||
$this->assertEquals( 'admin', $data['reviewer'] );
|
||||
$this->assertEquals( 'woo@woo.local', $data['reviewer_email'] );
|
||||
$this->assertEquals( 0, $data['rating'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world - updated.',
|
||||
'reviewer' => 'Justin',
|
||||
'reviewer_email' => 'woo2@woo.local',
|
||||
'rating' => 3,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'Hello world - updated.', $data['review'] );
|
||||
$this->assertEquals( 'Justin', $data['reviewer'] );
|
||||
$this->assertEquals( 'woo2@woo.local', $data['reviewer_email'] );
|
||||
$this->assertEquals( 3, $data['rating'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating a product review without the correct permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product_review_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.dev',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that updating a product review with an invalid id fails.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.dev',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a product review.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a product review without permission/creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a product review with an invalid id.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing product reviews.
|
||||
*/
|
||||
public function test_product_reviews_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$review_1_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$review_2_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$review_3_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$review_4_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $review_1_id,
|
||||
'review' => 'Updated review.',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$review_2_id,
|
||||
$review_3_id,
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'review' => 'New review.',
|
||||
'reviewer' => 'Justin',
|
||||
'reviewer_email' => 'woo3@woo.local',
|
||||
'product_id' => $product->get_id(),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'Updated review.', $data['update'][0]['review'] );
|
||||
$this->assertEquals( 'New review.', $data['create'][0]['review'] );
|
||||
$this->assertEquals( $review_2_id, $data['delete'][0]['previous']['id'] );
|
||||
$this->assertEquals( $review_3_id, $data['delete'][1]['previous']['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/products/reviews' );
|
||||
$request->set_param( 'product', $product->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the product review schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_product_review_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/products/reviews' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 11, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'product_id', $properties );
|
||||
$this->assertArrayHasKey( 'status', $properties );
|
||||
$this->assertArrayHasKey( 'reviewer', $properties );
|
||||
$this->assertArrayHasKey( 'reviewer_email', $properties );
|
||||
$this->assertArrayHasKey( 'review', $properties );
|
||||
$this->assertArrayHasKey( 'rating', $properties );
|
||||
$this->assertArrayHasKey( 'verified', $properties );
|
||||
|
||||
if ( get_option( 'show_avatars' ) ) {
|
||||
$this->assertArrayHasKey( 'reviewer_avatar_urls', $properties );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,473 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for Variations API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
class Product_Variations_API_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Product_Variations_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting variations.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_variations() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $variations ) );
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE LARGE', $variations[0]['sku'] );
|
||||
$this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting variations without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_variations_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $variation_id, $variation['id'] );
|
||||
$this->assertEquals( 'size', $variation['attributes'][0]['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 1, count( $variations ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single variation with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_variation_with_invalid_id() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test editing a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variation['sku'] );
|
||||
$this->assertEquals( 10, $variation['regular_price'] );
|
||||
$this->assertEmpty( $variation['sale_price'] );
|
||||
$this->assertEquals( 'small', $variation['attributes'][0]['option'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU',
|
||||
'sale_price' => '8',
|
||||
'description' => 'O_O',
|
||||
'image' => array(
|
||||
'position' => 0,
|
||||
'src' => 'http://cldup.com/Dr1Bczxq4q.png',
|
||||
'alt' => 'test upload image',
|
||||
),
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertTrue( isset( $variation['description'] ), print_r( $variation, true ) );
|
||||
$this->assertContains( 'O_O', $variation['description'], print_r( $variation, true ) );
|
||||
$this->assertEquals( '8', $variation['price'], print_r( $variation, true ) );
|
||||
$this->assertEquals( '8', $variation['sale_price'], print_r( $variation, true ) );
|
||||
$this->assertEquals( '10', $variation['regular_price'], print_r( $variation, true ) );
|
||||
$this->assertEquals( 'FIXED-SKU', $variation['sku'], print_r( $variation, true ) );
|
||||
$this->assertEquals( 'medium', $variation['attributes'][0]['option'], print_r( $variation, true ) );
|
||||
$this->assertContains( 'Dr1Bczxq4q', $variation['image']['src'], print_r( $variation, true ) );
|
||||
$this->assertContains( 'test upload image', $variation['image']['alt'], print_r( $variation, true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single variation with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_variation_with_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single variation.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 2, count( $variations ) );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
'regular_price' => '12',
|
||||
'description' => 'A medium size.',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertContains( 'A medium size.', $variation['description'] );
|
||||
$this->assertEquals( '12', $variation['price'] );
|
||||
$this->assertEquals( '12', $variation['regular_price'] );
|
||||
$this->assertTrue( $variation['purchasable'] );
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $variation['sku'] );
|
||||
$this->assertEquals( 'medium', $variation['attributes'][0]['option'] );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 3, count( $variations ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single variation without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
'regular_price' => '12',
|
||||
'description' => 'A medium size.',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing product variations.
|
||||
*/
|
||||
public function test_product_variations_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $children[0],
|
||||
'description' => 'Updated description.',
|
||||
'image' => array(
|
||||
'position' => 0,
|
||||
'src' => 'http://cldup.com/Dr1Bczxq4q.png',
|
||||
'alt' => 'test upload image',
|
||||
),
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$children[1],
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
'regular_price' => '12',
|
||||
'description' => 'A medium size.',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertContains( 'Updated description.', $data['update'][0]['description'] );
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $data['create'][0]['sku'] );
|
||||
$this->assertEquals( 'medium', $data['create'][0]['attributes'][0]['option'] );
|
||||
$this->assertEquals( $children[1], $data['delete'][0]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 2, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test variation schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_variation_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() . '/variations' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 37, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'permalink', $properties );
|
||||
$this->assertArrayHasKey( 'sku', $properties );
|
||||
$this->assertArrayHasKey( 'price', $properties );
|
||||
$this->assertArrayHasKey( 'regular_price', $properties );
|
||||
$this->assertArrayHasKey( 'sale_price', $properties );
|
||||
$this->assertArrayHasKey( 'date_on_sale_from', $properties );
|
||||
$this->assertArrayHasKey( 'date_on_sale_to', $properties );
|
||||
$this->assertArrayHasKey( 'on_sale', $properties );
|
||||
$this->assertArrayHasKey( 'visible', $properties );
|
||||
$this->assertArrayHasKey( 'purchasable', $properties );
|
||||
$this->assertArrayHasKey( 'virtual', $properties );
|
||||
$this->assertArrayHasKey( 'downloadable', $properties );
|
||||
$this->assertArrayHasKey( 'downloads', $properties );
|
||||
$this->assertArrayHasKey( 'download_limit', $properties );
|
||||
$this->assertArrayHasKey( 'download_expiry', $properties );
|
||||
$this->assertArrayHasKey( 'tax_status', $properties );
|
||||
$this->assertArrayHasKey( 'tax_class', $properties );
|
||||
$this->assertArrayHasKey( 'manage_stock', $properties );
|
||||
$this->assertArrayHasKey( 'stock_quantity', $properties );
|
||||
$this->assertArrayHasKey( 'in_stock', $properties );
|
||||
$this->assertArrayHasKey( 'backorders', $properties );
|
||||
$this->assertArrayHasKey( 'backorders_allowed', $properties );
|
||||
$this->assertArrayHasKey( 'backordered', $properties );
|
||||
$this->assertArrayHasKey( 'weight', $properties );
|
||||
$this->assertArrayHasKey( 'dimensions', $properties );
|
||||
$this->assertArrayHasKey( 'shipping_class', $properties );
|
||||
$this->assertArrayHasKey( 'shipping_class_id', $properties );
|
||||
$this->assertArrayHasKey( 'image', $properties );
|
||||
$this->assertArrayHasKey( 'attributes', $properties );
|
||||
$this->assertArrayHasKey( 'menu_order', $properties );
|
||||
$this->assertArrayHasKey( 'meta_data', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a variation stock.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_variation_manage_stock() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$product->set_manage_stock( false );
|
||||
$product->save();
|
||||
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
// Set stock to true.
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => true,
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( true, $variation['manage_stock'] );
|
||||
|
||||
// Set stock to false.
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => false,
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( false, $variation['manage_stock'] );
|
||||
|
||||
// Set stock to false but parent is managing stock.
|
||||
$product->set_manage_stock( true );
|
||||
$product->save();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => false,
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'parent', $variation['manage_stock'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,535 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for Products API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Products_API_V2 class.
|
||||
*/
|
||||
class Products_API_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Products_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/products', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/products/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_products() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Product::create_external_product();
|
||||
sleep( 1 ); // So both products have different timestamps.
|
||||
WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 2, count( $products ) );
|
||||
$this->assertEquals( 'Dummy Product', $products[0]['name'] );
|
||||
$this->assertEquals( 'DUMMY SKU', $products[0]['sku'] );
|
||||
$this->assertEquals( 'Dummy External Product', $products[1]['name'] );
|
||||
$this->assertEquals( 'DUMMY EXTERNAL SKU', $products[1]['sku'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_products_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single product.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
$simple = WC_Helper_Product::create_external_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $simple->get_id() ) );
|
||||
$product = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $simple->get_id(),
|
||||
'name' => 'Dummy External Product',
|
||||
'type' => 'simple',
|
||||
'status' => 'publish',
|
||||
'sku' => 'DUMMY EXTERNAL SKU',
|
||||
'regular_price' => 10,
|
||||
),
|
||||
$product
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single product.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 0, count( $variations ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single product with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_product_with_invalid_id() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test editing a single product. Tests multiple product types.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test simple products.
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU', $data['sku'] );
|
||||
$this->assertEquals( 10, $data['regular_price'] );
|
||||
$this->assertEmpty( $data['sale_price'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU',
|
||||
'sale_price' => '8',
|
||||
'description' => 'Testing',
|
||||
'images' => array(
|
||||
array(
|
||||
'position' => 0,
|
||||
'src' => 'http://cldup.com/Dr1Bczxq4q.png',
|
||||
'alt' => 'test upload image',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertContains( 'Testing', $data['description'] );
|
||||
$this->assertEquals( '8', $data['price'] );
|
||||
$this->assertEquals( '8', $data['sale_price'] );
|
||||
$this->assertEquals( '10', $data['regular_price'] );
|
||||
$this->assertEquals( 'FIXED-SKU', $data['sku'] );
|
||||
$this->assertContains( 'Dr1Bczxq4q', $data['images'][0]['src'] );
|
||||
$this->assertContains( 'test upload image', $data['images'][0]['alt'] );
|
||||
$product->delete( true );
|
||||
|
||||
// test variable product (variations are tested in product-variations.php).
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
foreach ( array( 'small', 'large' ) as $term_name ) {
|
||||
$this->assertContains( $term_name, $data['attributes'][0]['options'] );
|
||||
}
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'attributes' => array(
|
||||
array(
|
||||
'id' => 0,
|
||||
'name' => 'pa_color',
|
||||
'options' => array(
|
||||
'red',
|
||||
'yellow',
|
||||
),
|
||||
'visible' => false,
|
||||
'variation' => 1,
|
||||
),
|
||||
array(
|
||||
'id' => 0,
|
||||
'name' => 'pa_size',
|
||||
'options' => array(
|
||||
'small',
|
||||
),
|
||||
'visible' => false,
|
||||
'variation' => 1,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( array( 'small' ), $data['attributes'][0]['options'] );
|
||||
$this->assertEquals( array( 'red', 'yellow' ), $data['attributes'][1]['options'] );
|
||||
$product->delete( true );
|
||||
|
||||
// test external product.
|
||||
$product = WC_Helper_Product::create_external_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'Buy external product', $data['button_text'] );
|
||||
$this->assertEquals( 'http://woocommerce.com', $data['external_url'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'button_text' => 'Test API Update',
|
||||
'external_url' => 'http://automattic.com',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'Test API Update', $data['button_text'] );
|
||||
$this->assertEquals( 'http://automattic.com', $data['external_url'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single product with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_product_with_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-INVALID-ID',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single product.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/shipping_classes' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$shipping_class_id = $data['id'];
|
||||
|
||||
// Create simple.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'type' => 'simple',
|
||||
'name' => 'Test Simple Product',
|
||||
'sku' => 'DUMMY SKU SIMPLE API',
|
||||
'regular_price' => '10',
|
||||
'shipping_class' => 'test',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( '10', $data['price'] );
|
||||
$this->assertEquals( '10', $data['regular_price'] );
|
||||
$this->assertTrue( $data['purchasable'] );
|
||||
$this->assertEquals( 'DUMMY SKU SIMPLE API', $data['sku'] );
|
||||
$this->assertEquals( 'Test Simple Product', $data['name'] );
|
||||
$this->assertEquals( 'simple', $data['type'] );
|
||||
$this->assertEquals( $shipping_class_id, $data['shipping_class_id'] );
|
||||
|
||||
// Create external.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'type' => 'external',
|
||||
'name' => 'Test External Product',
|
||||
'sku' => 'DUMMY SKU EXTERNAL API',
|
||||
'regular_price' => '10',
|
||||
'button_text' => 'Test Button',
|
||||
'external_url' => 'https://wordpress.org',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( '10', $data['price'] );
|
||||
$this->assertEquals( '10', $data['regular_price'] );
|
||||
$this->assertFalse( $data['purchasable'] );
|
||||
$this->assertEquals( 'DUMMY SKU EXTERNAL API', $data['sku'] );
|
||||
$this->assertEquals( 'Test External Product', $data['name'] );
|
||||
$this->assertEquals( 'external', $data['type'] );
|
||||
$this->assertEquals( 'Test Button', $data['button_text'] );
|
||||
$this->assertEquals( 'https://wordpress.org', $data['external_url'] );
|
||||
|
||||
// Create variable.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'type' => 'variable',
|
||||
'name' => 'Test Variable Product',
|
||||
'sku' => 'DUMMY SKU VARIABLE API',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'id' => 0,
|
||||
'name' => 'pa_size',
|
||||
'options' => array(
|
||||
'small',
|
||||
'medium',
|
||||
),
|
||||
'visible' => false,
|
||||
'variation' => 1,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE API', $data['sku'] );
|
||||
$this->assertEquals( 'Test Variable Product', $data['name'] );
|
||||
$this->assertEquals( 'variable', $data['type'] );
|
||||
$this->assertEquals( array( 'small', 'medium' ), $data['attributes'][0]['options'] );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
|
||||
$products = $response->get_data();
|
||||
$this->assertEquals( 3, count( $products ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single product without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test Product',
|
||||
'regular_price' => '12',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing products.
|
||||
*/
|
||||
public function test_products_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_2 = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/products/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $product->get_id(),
|
||||
'description' => 'Updated description.',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$product_2->get_id(),
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU BATCH TEST 1',
|
||||
'regular_price' => '10',
|
||||
'name' => 'Test Batch Create 1',
|
||||
'type' => 'external',
|
||||
'button_text' => 'Test Button',
|
||||
),
|
||||
array(
|
||||
'sku' => 'DUMMY SKU BATCH TEST 2',
|
||||
'regular_price' => '20',
|
||||
'name' => 'Test Batch Create 2',
|
||||
'type' => 'simple',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertContains( 'Updated description.', $data['update'][0]['description'] );
|
||||
$this->assertEquals( 'DUMMY SKU BATCH TEST 1', $data['create'][0]['sku'] );
|
||||
$this->assertEquals( 'DUMMY SKU BATCH TEST 2', $data['create'][1]['sku'] );
|
||||
$this->assertEquals( 'Test Button', $data['create'][0]['button_text'] );
|
||||
$this->assertEquals( 'external', $data['create'][0]['type'] );
|
||||
$this->assertEquals( 'simple', $data['create'][1]['type'] );
|
||||
$this->assertEquals( $product_2->get_id(), $data['delete'][0]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure you can filter products post statuses by both
|
||||
* the status query arg and WP_Query.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_products_filter_post_status() {
|
||||
wp_set_current_user( $this->user );
|
||||
for ( $i = 0; $i < 8; $i++ ) {
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
if ( 0 === $i % 2 ) {
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $product->get_id(),
|
||||
'post_status' => 'draft',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Test filtering with status=publish.
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_param( 'status', 'publish' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 4, count( $products ) );
|
||||
foreach ( $products as $product ) {
|
||||
$this->assertEquals( 'publish', $product['status'] );
|
||||
}
|
||||
|
||||
// Test filtering with status=draft.
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_param( 'status', 'draft' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 4, count( $products ) );
|
||||
foreach ( $products as $product ) {
|
||||
$this->assertEquals( 'draft', $product['status'] );
|
||||
}
|
||||
|
||||
// Test filtering with no filters - which should return 'any' (all 8).
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 8, count( $products ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test product schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_product_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 65, count( $properties ) );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,892 @@
|
|||
<?php
|
||||
/**
|
||||
* Settings API Tests.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
class Settings_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Setting_Options_Controller();
|
||||
WC_Helper_Settings::register();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/settings', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/settings/(?P<group_id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/settings/(?P<group_id>[\w-]+)/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all groups.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_groups() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'test',
|
||||
'label' => 'Test extension',
|
||||
'parent_id' => '',
|
||||
'description' => 'My awesome test settings.',
|
||||
'sub_groups' => array( 'sub-test' ),
|
||||
'_links' => array(
|
||||
'options' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/test' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'sub-test',
|
||||
'label' => 'Sub test',
|
||||
'parent_id' => 'test',
|
||||
'description' => '',
|
||||
'sub_groups' => array(),
|
||||
'_links' => array(
|
||||
'options' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/sub-test' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /settings without valid permissions/creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_groups_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /settings without valid permissions/creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @covers WC_Rest_Settings_Controller::get_items
|
||||
*/
|
||||
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/v2/settings' ) );
|
||||
$this->assertEquals( 500, $response->get_status() );
|
||||
|
||||
WC_Helper_Settings::register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test groups schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_group_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 5, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'parent_id', $properties );
|
||||
$this->assertArrayHasKey( 'label', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'sub_groups', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test settings schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_setting_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/settings/test/woocommerce_shop_page_display' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 9, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'label', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'value', $properties );
|
||||
$this->assertArrayHasKey( 'default', $properties );
|
||||
$this->assertArrayHasKey( 'tip', $properties );
|
||||
$this->assertArrayHasKey( 'placeholder', $properties );
|
||||
$this->assertArrayHasKey( 'type', $properties );
|
||||
$this->assertArrayHasKey( 'options', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single group.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_group() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test route callback receiving an empty group id
|
||||
$result = $this->endpoint->get_group_settings( '' );
|
||||
$this->assertIsWPError( $result );
|
||||
|
||||
// test getting a group that does not exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/not-real' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting the 'invalid' group
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/invalid' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting a valid group with settings attached to it
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 1, count( $data ) );
|
||||
$this->assertEquals( 'woocommerce_shop_page_display', $data[0]['id'] );
|
||||
$this->assertEmpty( $data[0]['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single group without permission.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_group_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/coupon-data' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single setting.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_setting() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test defaults first
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( '', $data['value'] );
|
||||
|
||||
// test updating shop display setting
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$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' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$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' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$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 multiple settings at once.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test defaults first
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( '', $data[0]['value'] );
|
||||
|
||||
// test setting both at once
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => 'woocommerce_shop_page_display',
|
||||
'value' => 'both',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'both', $data['update'][0]['value'] );
|
||||
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) );
|
||||
|
||||
// test updating one, but making sure the other value stays the same
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => 'woocommerce_shop_page_display',
|
||||
'value' => 'subcategories',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'subcategories', $data['update'][0]['value'] );
|
||||
$this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single setting.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_setting() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test getting an invalid setting from a group that does not exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/not-real/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting an invalid setting from a group that does exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/invalid/invalid' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting a valid setting
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 'woocommerce_shop_page_display', $data['id'] );
|
||||
$this->assertEquals( 'Shop page display', $data['label'] );
|
||||
$this->assertEquals( '', $data['default'] );
|
||||
$this->assertEquals( 'select', $data['type'] );
|
||||
$this->assertEquals( '', $data['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single setting without valid user permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_setting_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/test/woocommerce_shop_page_display' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the GET single setting route handler receiving an empty setting ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_setting_invalid_setting_type() {
|
||||
// $controller = $this->getMock( 'WC_Rest_Setting_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) );
|
||||
$controller = $this->getMockBuilder( 'WC_Rest_Setting_Options_Controller' )->setMethods( array( 'get_group_settings', 'is_setting_type_valid' ) )->getMock();
|
||||
|
||||
$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_shop_page_display' );
|
||||
|
||||
$this->assertIsWPError( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single setting without valid user permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_setting_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'subcategories',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test updating multiple settings without valid user permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_settings_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/settings/test/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => 'woocommerce_shop_page_display',
|
||||
'value' => 'subcategories',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a bad setting ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @covers WC_Rest_Setting_Options_Controller::update_item
|
||||
*/
|
||||
public function test_update_setting_bad_setting_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/test/invalid' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'test',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests our classic setting registration to make sure settings added for WP-Admin are available over the API.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_classic_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Make sure the group is properly registered
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertTrue( is_array( $data ) );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'woocommerce_downloads_require_login',
|
||||
'label' => 'Access restriction',
|
||||
'description' => 'Downloads require login',
|
||||
'type' => 'checkbox',
|
||||
'default' => 'no',
|
||||
'tip' => 'This setting does not apply to guest purchases.',
|
||||
'value' => 'no',
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/products/woocommerce_downloads_require_login' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/products' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
|
||||
// test get single
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/products/woocommerce_dimension_unit' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'cm', $data['default'] );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_dimension_unit' ) );
|
||||
$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' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests our email etting registration to make sure settings added for WP-Admin are available over the API.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_email_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order' ) );
|
||||
$settings = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'recipient',
|
||||
'label' => 'Recipient(s)',
|
||||
'description' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
|
||||
'type' => 'text',
|
||||
'default' => '',
|
||||
'tip' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
|
||||
'value' => '',
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/email_new_order/recipient' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/settings/email_new_order' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$settings
|
||||
);
|
||||
|
||||
// test get single
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order/subject' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => 'subject',
|
||||
'label' => 'Subject',
|
||||
'description' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'type' => 'text',
|
||||
'default' => '',
|
||||
'tip' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'value' => '',
|
||||
),
|
||||
$setting
|
||||
);
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_new_order', 'subject' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'This is my subject',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => 'subject',
|
||||
'label' => 'Subject',
|
||||
'description' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'type' => 'text',
|
||||
'default' => '',
|
||||
'tip' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'value' => 'This is my subject',
|
||||
),
|
||||
$setting
|
||||
);
|
||||
|
||||
// test updating another subject and making sure it works with a "similar" id
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEmpty( $setting['value'] );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'This is my new subject',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'This is my new subject', $setting['value'] );
|
||||
|
||||
// make sure the other is what we left it
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/email_new_order/subject' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'This is my subject', $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of checkbox settings.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_validation_checkbox() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test bogus value
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'not_yes_or_no',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// test yes
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'yes',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
// test no
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'no',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of radio settings.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_validation_radio() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// not a valid option
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'billing2',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// valid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'billing',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of multiselect.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_validation_multiselect() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEmpty( $setting['value'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => array( 'AX', 'DZ', 'MMM' ),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( array( 'AX', 'DZ' ), $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of select.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_validation_select() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'kg', $setting['value'] );
|
||||
|
||||
// invalid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'pounds', // invalid, should be lbs
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// valid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v2/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'lbs', // invalid, should be lbs
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'lbs', $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the 'base location' setting is present in the response.
|
||||
* That it is returned as 'select' and not 'single_select_country',
|
||||
* and that both state and country options are returned.
|
||||
*
|
||||
* @since 3.0.7
|
||||
*/
|
||||
public function test_woocommerce_default_country() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_default_country' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'select', $setting['type'] );
|
||||
$this->assertArrayHasKey( 'GB', $setting['options'] );
|
||||
$this->assertArrayHasKey( 'US:OR', $setting['options'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store address setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public function test_woocommerce_store_address() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_address' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $new_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $old_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store address 2 (line 2) setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public function test_woocommerce_store_address_2() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_address_2' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address_2' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $new_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_address_2' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $old_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store city setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public function test_woocommerce_store_city() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_city' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_city' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $new_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_city' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $old_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store postcode setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.1.1
|
||||
*/
|
||||
public function test_woocommerce_store_postcode() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/settings/general/woocommerce_store_postcode' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_postcode' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $new_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/settings/general/woocommerce_store_postcode' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $old_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,143 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the Shipping Methods REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
|
||||
class Shipping_Methods_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Shipping_Methods_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping_methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping_methods/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all shipping methods.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods' ) );
|
||||
$methods = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'free_shipping',
|
||||
'title' => 'Free shipping',
|
||||
'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.',
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping_methods/free_shipping' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping_methods' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$methods
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure shipping methods cannot viewed without valid permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_methods_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single shipping method.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/local_pickup' ) );
|
||||
$method = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => 'local_pickup',
|
||||
'title' => 'Local pickup',
|
||||
'description' => 'Allow customers to pick up orders themselves. By default, when using local pickup store base taxes will apply regardless of customer address.',
|
||||
),
|
||||
$method
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single shipping method without the correct permissions.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_method_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/local_pickup' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a shipping method with an invalid ID.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_method_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping_methods/fake_method' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the shipping method schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_shipping_method_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping_methods' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'title', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,800 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Shipping Zones API Tests
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0.0
|
||||
*/
|
||||
class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
protected $server;
|
||||
|
||||
protected $endpoint;
|
||||
|
||||
protected $user;
|
||||
|
||||
protected $zones;
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Shipping_Zones_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
$this->zones = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a Shipping Zone.
|
||||
*
|
||||
* @param string $name Zone name.
|
||||
* @param int $order Optional. Zone sort order.
|
||||
* @return WC_Shipping_Zone
|
||||
*/
|
||||
protected function create_shipping_zone( $name, $order = 0, $locations = array() ) {
|
||||
$zone = new WC_Shipping_Zone( null );
|
||||
$zone->set_zone_name( $name );
|
||||
$zone->set_zone_order( $order );
|
||||
$zone->set_locations( $locations );
|
||||
$zone->save();
|
||||
|
||||
$this->zones[] = $zone;
|
||||
|
||||
return $zone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d]+)/locations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d]+)/methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d]+)/methods/(?P<instance_id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all Shipping Zones.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_zones() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// "Rest of the World" zone exists by default
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $data[0]['id'],
|
||||
'name' => 'Locations not covered by your other zones',
|
||||
'order' => 0,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
|
||||
// Create a zone and make sure it's in the response
|
||||
$this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 2 );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $data[1]['id'],
|
||||
'name' => 'Zone 1',
|
||||
'order' => 0,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /shipping/zones without valid permissions/creds.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_zones_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /shipping/zones while Shipping is disabled in WooCommerce.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_zones_disabled_shipping() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
add_filter( 'wc_shipping_enabled', '__return_false' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
remove_filter( 'wc_shipping_enabled', '__return_false' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone schema.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_shipping_zone_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping/zones' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertTrue( $properties['id']['readonly'] );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'order', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone create endpoint.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test Zone',
|
||||
'order' => 1,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'name' => 'Test Zone',
|
||||
'order' => 1,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone create endpoint.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_shipping_zone_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test Zone',
|
||||
'order' => 1,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone update endpoint.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $zone->get_id(),
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone update endpoint with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/555555' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint without permissions.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_shipping_zone_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/555555' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single Shipping Zone.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_single_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $zone->get_id(),
|
||||
'name' => 'Test Zone',
|
||||
'order' => 0,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single Shipping Zone with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_single_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting Shipping Zone Locations.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_locations() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Create a zone
|
||||
$zone = $this->create_shipping_zone(
|
||||
'Zone 1',
|
||||
0,
|
||||
array(
|
||||
array(
|
||||
'code' => 'US',
|
||||
'type' => 'country',
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
array(
|
||||
'code' => 'US',
|
||||
'type' => 'country',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting Shipping Zone Locations with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_locations_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/locations' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone Locations update endpoint.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_locations() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' );
|
||||
$request->add_header( 'Content-Type', 'application/json' );
|
||||
$request->set_body(
|
||||
json_encode(
|
||||
array(
|
||||
array(
|
||||
'code' => 'UK',
|
||||
'type' => 'country',
|
||||
),
|
||||
array(
|
||||
'code' => 'US', // test that locations missing "type" treated as country.
|
||||
),
|
||||
array(
|
||||
'code' => 'SW1A0AA',
|
||||
'type' => 'postcode',
|
||||
),
|
||||
array(
|
||||
'type' => 'continent', // test that locations missing "code" aren't saved
|
||||
),
|
||||
)
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
array(
|
||||
'code' => 'UK',
|
||||
'type' => 'country',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'code' => 'US',
|
||||
'type' => 'country',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'code' => 'SW1A0AA',
|
||||
'type' => 'postcode',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating Shipping Zone Locations with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_locations_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/1/locations' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all Shipping Zone Methods and getting a single Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Create a shipping method and make sure it's in the response
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$methods = $zone->get_shipping_methods();
|
||||
$method = $methods[ $instance_id ];
|
||||
|
||||
$settings = array();
|
||||
$method->init_instance_settings();
|
||||
foreach ( $method->get_instance_form_fields() as $id => $field ) {
|
||||
$data = array(
|
||||
'id' => $id,
|
||||
'label' => $field['title'],
|
||||
'description' => ( empty( $field['description'] ) ? '' : $field['description'] ),
|
||||
'type' => $field['type'],
|
||||
'value' => $method->instance_settings[ $id ],
|
||||
'default' => ( empty( $field['default'] ) ? '' : $field['default'] ),
|
||||
'tip' => ( empty( $field['description'] ) ? '' : $field['description'] ),
|
||||
'placeholder' => ( empty( $field['placeholder'] ) ? '' : $field['placeholder'] ),
|
||||
);
|
||||
if ( ! empty( $field['options'] ) ) {
|
||||
$data['options'] = $field['options'];
|
||||
}
|
||||
$settings[ $id ] = $data;
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ) );
|
||||
$data = $response->get_data();
|
||||
$expected = array(
|
||||
'id' => $instance_id,
|
||||
'instance_id' => $instance_id,
|
||||
'title' => $method->instance_settings['title'],
|
||||
'order' => $method->method_order,
|
||||
'enabled' => ( 'yes' === $method->enabled ),
|
||||
'method_id' => $method->id,
|
||||
'method_title' => $method->method_title,
|
||||
'method_description' => $method->method_description,
|
||||
'settings' => $settings,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertContains( $expected, $data );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $expected, $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all Shipping Zone Methods with a bad zone ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_methods_invalid_zone_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single Shipping Zone Method with a bad ID.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_methods_invalid_method_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_update_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$methods = $zone->get_shipping_methods();
|
||||
$method = $methods[ $instance_id ];
|
||||
|
||||
// Test defaults
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'title', $data['settings'] );
|
||||
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
||||
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
||||
$this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '0', $data['settings']['cost']['value'] );
|
||||
|
||||
// Update a single value
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'cost' => 5,
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'title', $data['settings'] );
|
||||
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
||||
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
||||
$this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '5', $data['settings']['cost']['value'] );
|
||||
|
||||
// Test multiple settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'cost' => 10,
|
||||
'tax_status' => 'none',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'title', $data['settings'] );
|
||||
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
||||
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
||||
$this->assertEquals( 'none', $data['settings']['tax_status']['value'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '10', $data['settings']['cost']['value'] );
|
||||
|
||||
// Test bogus
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'cost' => 10,
|
||||
'tax_status' => 'this_is_not_a_valid_option',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// Test other parameters
|
||||
$this->assertTrue( $data['enabled'] );
|
||||
$this->assertEquals( 1, $data['order'] );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertFalse( $data['enabled'] );
|
||||
$this->assertEquals( 2, $data['order'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '10', $data['settings']['cost']['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_create_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertFalse( $data['enabled'] );
|
||||
$this->assertEquals( 2, $data['order'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '0', $data['settings']['cost']['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a Shipping Zone Method.
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_delete_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$methods = $zone->get_shipping_methods();
|
||||
$method = $methods[ $instance_id ];
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,356 @@
|
|||
<?php
|
||||
/**
|
||||
* Class WC_Tests_REST_System_Status file.
|
||||
*
|
||||
* @package WooCommerce/Tests
|
||||
*/
|
||||
|
||||
/**
|
||||
* System Status REST Tests.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.0
|
||||
*/
|
||||
class WC_Tests_REST_System_Status_V2 extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_System_Status_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status/tools', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v2/system_status/tools/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure system status cannot be accessed without valid creds
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure root properties are present.
|
||||
* (environment, theme, database, etc).
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_returns_root_properties() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'environment', $data );
|
||||
$this->assertArrayHasKey( 'database', $data );
|
||||
$this->assertArrayHasKey( 'active_plugins', $data );
|
||||
$this->assertArrayHasKey( 'theme', $data );
|
||||
$this->assertArrayHasKey( 'settings', $data );
|
||||
$this->assertArrayHasKey( 'security', $data );
|
||||
$this->assertArrayHasKey( 'pages', $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure environment response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_environment() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$environment = (array) $data['environment'];
|
||||
|
||||
// Make sure all expected data is present.
|
||||
$this->assertEquals( 32, count( $environment ) );
|
||||
|
||||
// Test some responses to make sure they match up.
|
||||
$this->assertEquals( get_option( 'home' ), $environment['home_url'] );
|
||||
$this->assertEquals( get_option( 'siteurl' ), $environment['site_url'] );
|
||||
$this->assertEquals( WC()->version, $environment['version'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure database response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_database() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$database = (array) $data['database'];
|
||||
|
||||
$this->assertEquals( get_option( 'woocommerce_db_version' ), $database['wc_database_version'] );
|
||||
$this->assertEquals( $wpdb->prefix, $database['database_prefix'] );
|
||||
$this->assertEquals( WC_Geolocation::get_local_database_path(), $database['maxmind_geoip_database'] );
|
||||
$this->assertArrayHasKey( 'woocommerce', $database['database_tables'], print_r( $database, true ) );
|
||||
$this->assertArrayHasKey( $wpdb->prefix . 'woocommerce_payment_tokens', $database['database_tables']['woocommerce'], print_r( $database, true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure active plugins response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_active_plugins() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$actual_plugins = array( 'hello.php' );
|
||||
update_option( 'active_plugins', $actual_plugins );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
update_option( 'active_plugins', array() );
|
||||
|
||||
$data = $response->get_data();
|
||||
$plugins = (array) $data['active_plugins'];
|
||||
|
||||
$this->assertEquals( 1, count( $plugins ) );
|
||||
$this->assertEquals( 'Hello Dolly', $plugins[0]['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure theme response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_theme() {
|
||||
wp_set_current_user( $this->user );
|
||||
$active_theme = wp_get_theme();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$theme = (array) $data['theme'];
|
||||
|
||||
$this->assertEquals( 13, count( $theme ) );
|
||||
$this->assertEquals( $active_theme->Name, $theme['name'] ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure settings response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$term_response = array();
|
||||
$terms = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
|
||||
foreach ( $terms as $term ) {
|
||||
$term_response[ $term->slug ] = strtolower( $term->name );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$settings = (array) $data['settings'];
|
||||
|
||||
$this->assertEquals( 12, count( $settings ) );
|
||||
$this->assertEquals( ( 'yes' === get_option( 'woocommerce_api_enabled' ) ), $settings['api_enabled'] );
|
||||
$this->assertEquals( get_woocommerce_currency(), $settings['currency'] );
|
||||
$this->assertEquals( $term_response, $settings['taxonomies'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure security response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_security() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$settings = (array) $data['security'];
|
||||
|
||||
$this->assertEquals( 2, count( $settings ) );
|
||||
$this->assertEquals( 'https' === substr( wc_get_page_permalink( 'shop' ), 0, 5 ), $settings['secure_connection'] );
|
||||
$this->assertEquals( ! ( defined( 'WP_DEBUG' ) && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG && WP_DEBUG_DISPLAY ) || 0 === intval( ini_get( 'display_errors' ) ), $settings['hide_errors'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure pages response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_info_pages() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$pages = $data['pages'];
|
||||
$this->assertEquals( 5, count( $pages ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test system status schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_system_status_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 9, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'environment', $properties );
|
||||
$this->assertArrayHasKey( 'database', $properties );
|
||||
$this->assertArrayHasKey( 'active_plugins', $properties );
|
||||
$this->assertArrayHasKey( 'theme', $properties );
|
||||
$this->assertArrayHasKey( 'settings', $properties );
|
||||
$this->assertArrayHasKey( 'security', $properties );
|
||||
$this->assertArrayHasKey( 'pages', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure get_items (all tools) response is correct.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_tools() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$tools_controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$raw_tools = $tools_controller->get_tools();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $raw_tools ), count( $data ) );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'regenerate_thumbnails',
|
||||
'name' => 'Regenerate shop thumbnails',
|
||||
'action' => 'Regenerate',
|
||||
'description' => 'This will regenerate all shop thumbnails to match your theme and/or image settings.',
|
||||
'_links' => array(
|
||||
'item' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v2/system_status/tools/regenerate_thumbnails' ),
|
||||
'embeddable' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure system status tools cannot be accessed without valid creds
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_tools_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure we can load a single tool correctly.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_tool() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$tools_controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$raw_tools = $tools_controller->get_tools();
|
||||
$raw_tool = $raw_tools['recount_terms'];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 'recount_terms', $data['id'] );
|
||||
$this->assertEquals( 'Term counts', $data['name'] );
|
||||
$this->assertEquals( 'Recount terms', $data['action'] );
|
||||
$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure a single system status toolscannot be accessed without valid creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_get_system_status_tool_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure we can RUN a tool correctly.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_execute_system_tool() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$tools_controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$raw_tools = $tools_controller->get_tools();
|
||||
$raw_tool = $raw_tools['recount_terms'];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'recount_terms', $data['id'] );
|
||||
$this->assertEquals( 'Term counts', $data['name'] );
|
||||
$this->assertEquals( 'Recount terms', $data['action'] );
|
||||
$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
|
||||
$this->assertTrue( $data['success'] );
|
||||
$this->assertEquals( 1, did_action( 'woocommerce_rest_insert_system_status_tool' ) );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/not_a_real_tool' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure a tool cannot be run without valid creds.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_execute_system_status_tool_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v2/system_status/tools/recount_terms' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test system status schema.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function test_system_status_tool_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/system_status/tools' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 6, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'action', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'success', $properties );
|
||||
$this->assertArrayHasKey( 'message', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,471 @@
|
|||
<?php
|
||||
/**
|
||||
* Coupon API Tests
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
|
||||
|
||||
protected $endpoint;
|
||||
|
||||
/**
|
||||
* Setup test coupon data.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Coupons_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/coupons', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/coupons/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/coupons/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting coupons.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$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' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons' ) );
|
||||
$coupons = $response->get_data();
|
||||
|
||||
$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, false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $post_1->post_date_gmt ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $post_1->post_modified_gmt, false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $post_1->post_modified_gmt ),
|
||||
'discount_type' => 'fixed_cart',
|
||||
'description' => 'This is a dummy coupon',
|
||||
'date_expires' => '',
|
||||
'date_expires_gmt' => '',
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => '',
|
||||
'usage_limit_per_user' => '',
|
||||
'limit_usage_to_x_items' => null,
|
||||
'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(),
|
||||
'meta_data' => array(),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/coupons/' . $coupon_1->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/coupons' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$coupons
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting coupons without valid permissions.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_coupons_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single coupon.
|
||||
* @since 3.5.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/v3/coupons/' . $coupon->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$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, false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $post->post_date_gmt ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt, false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $post->post_modified_gmt ),
|
||||
'discount_type' => 'fixed_cart',
|
||||
'description' => 'This is a dummy coupon',
|
||||
'date_expires' => null,
|
||||
'date_expires_gmt' => null,
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => null,
|
||||
'usage_limit_per_user' => null,
|
||||
'limit_usage_to_x_items' => null,
|
||||
'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(),
|
||||
'meta_data' => array(),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single coupon with an invalid ID.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single coupon without valid permissions.
|
||||
* @since 3.5.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/v3/coupons/' . $coupon->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single coupon.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/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();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'code' => 'test',
|
||||
'amount' => '5.00',
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'discount_type' => 'fixed_product',
|
||||
'description' => 'Test',
|
||||
'date_expires' => null,
|
||||
'date_expires_gmt' => null,
|
||||
'usage_count' => 0,
|
||||
'individual_use' => false,
|
||||
'product_ids' => array(),
|
||||
'excluded_product_ids' => array(),
|
||||
'usage_limit' => 10,
|
||||
'usage_limit_per_user' => null,
|
||||
'limit_usage_to_x_items' => null,
|
||||
'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(),
|
||||
'meta_data' => array(),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single coupon with invalid fields.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_coupon_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test no code...
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/coupons' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single coupon without valid permissions.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_coupon_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
// test no code...
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/coupons' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'code' => 'fail',
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single coupon.
|
||||
* @since 3.5.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() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/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'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/coupons/' . $coupon->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( '10.00', $data['amount'] );
|
||||
$this->assertEquals( 'New description', $data['description'] );
|
||||
$this->assertEquals( 'fixed_cart', $data['discount_type'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single coupon with an invalid ID.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/coupons/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'code' => 'tester',
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single coupon without valid permissions.
|
||||
* @since 3.5.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() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/coupons/' . $coupon->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'amount' => '10.00',
|
||||
'description' => 'New description',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single coupon.
|
||||
* @since 3.5.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/v3/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 3.5.0
|
||||
*/
|
||||
public function test_delete_coupon_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/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 3.5.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/v3/coupons/' . $coupon->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch operations on coupons.
|
||||
* @since 3.5.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/v3/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/v3/coupons' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test coupon schema.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_coupon_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/coupons' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 27, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'code', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'discount_type', $properties );
|
||||
$this->assertArrayHasKey( 'amount', $properties );
|
||||
$this->assertArrayHasKey( 'date_expires', $properties );
|
||||
$this->assertArrayHasKey( 'date_expires_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'usage_count', $properties );
|
||||
$this->assertArrayHasKey( 'individual_use', $properties );
|
||||
$this->assertArrayHasKey( 'product_ids', $properties );
|
||||
$this->assertArrayHasKey( 'excluded_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 );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,634 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the Customers REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for the Customers REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @extends WC_REST_Unit_Test_Case
|
||||
*/
|
||||
class Customers extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Customers_Controller();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( '/wc/v3/customers', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/customers/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/customers/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting customers.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_customers() {
|
||||
wp_set_current_user( 1 );
|
||||
|
||||
$customer_1 = WC_Helper_Customer::create_customer();
|
||||
WC_Helper_Customer::create_customer( 'test2', 'test2', 'test2@woo.local' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/customers' );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'orderby' => 'id',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$customers = $response->get_data();
|
||||
$date_created = get_date_from_gmt( date( 'Y-m-d H:i:s', strtotime( $customer_1->get_date_created() ) ) );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $customers ) );
|
||||
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $customer_1->get_id(),
|
||||
'date_created' => wc_rest_prepare_date_response( $date_created, false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $date_created ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $customer_1->get_date_modified(), false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $customer_1->get_date_modified() ),
|
||||
'email' => 'test@woo.local',
|
||||
'first_name' => 'Justin',
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'testcustomer',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'avatar_url' => $customer_1->get_avatar_url(),
|
||||
'meta_data' => array(),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/customers/' . $customer_1->get_id() . '' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/customers' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$customers
|
||||
);
|
||||
|
||||
update_option( 'timezone_tring', 'America/New York' );
|
||||
$customer_3 = WC_Helper_Customer::create_customer( 'timezonetest', 'timezonetest', 'timezonetest@woo.local' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/customers' );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'orderby' => 'id',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$customers = $response->get_data();
|
||||
$date_created = get_date_from_gmt( date( 'Y-m-d H:i:s', strtotime( $customer_3->get_date_created() ) ) );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $customer_3->get_id(),
|
||||
'date_created' => wc_rest_prepare_date_response( $date_created, false ),
|
||||
'date_created_gmt' => wc_rest_prepare_date_response( $date_created ),
|
||||
'date_modified' => wc_rest_prepare_date_response( $customer_3->get_date_modified(), false ),
|
||||
'date_modified_gmt' => wc_rest_prepare_date_response( $customer_3->get_date_modified() ),
|
||||
'email' => 'timezonetest@woo.local',
|
||||
'first_name' => 'Justin',
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'timezonetest',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'avatar_url' => $customer_3->get_avatar_url(),
|
||||
'meta_data' => array(),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/customers/' . $customer_3->get_id() . '' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/customers' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$customers
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting customers without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_customers_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a new customer.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
|
||||
// Test just the basics first..
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'username' => 'create_customer_test',
|
||||
'password' => 'test123',
|
||||
'email' => 'create_customer_test@woo.local',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'create_customer_test@woo.local',
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'create_customer_test',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => '',
|
||||
'postcode' => '',
|
||||
'country' => '',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => '',
|
||||
'postcode' => '',
|
||||
'country' => '',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
),
|
||||
$data
|
||||
);
|
||||
|
||||
// Test extra data.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'username' => 'create_customer_test2',
|
||||
'password' => 'test123',
|
||||
'email' => 'create_customer_test2@woo.local',
|
||||
'first_name' => 'Test',
|
||||
'last_name' => 'McTestFace',
|
||||
'billing' => array(
|
||||
'country' => 'US',
|
||||
'state' => 'WA',
|
||||
),
|
||||
'shipping' => array(
|
||||
'state' => 'CA',
|
||||
'country' => 'US',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'create_customer_test2@woo.local',
|
||||
'first_name' => 'Test',
|
||||
'last_name' => 'McTestFace',
|
||||
'role' => 'customer',
|
||||
'username' => 'create_customer_test2',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => 'WA',
|
||||
'postcode' => '',
|
||||
'country' => 'US',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '',
|
||||
'address_2' => '',
|
||||
'city' => '',
|
||||
'state' => 'CA',
|
||||
'postcode' => '',
|
||||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
),
|
||||
$data
|
||||
);
|
||||
|
||||
// Test without required field.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'username' => 'create_customer_test3',
|
||||
'first_name' => 'Test',
|
||||
'last_name' => 'McTestFace',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating customers without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'username' => 'create_customer_test_without_permission',
|
||||
'password' => 'test123',
|
||||
'email' => 'create_customer_test_without_permission@woo.local',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single customer.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'get_customer_test', 'test123', 'get_customer_test@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'date_modified' => $data['date_modified'],
|
||||
'date_modified_gmt' => $data['date_modified_gmt'],
|
||||
'email' => 'get_customer_test@woo.local',
|
||||
'first_name' => 'Justin',
|
||||
'billing' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
'email' => '',
|
||||
'phone' => '',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'company' => '',
|
||||
'address_1' => '123 South Street',
|
||||
'address_2' => 'Apt 1',
|
||||
'city' => 'Philadelphia',
|
||||
'state' => 'PA',
|
||||
'postcode' => '19123',
|
||||
'country' => 'US',
|
||||
),
|
||||
'is_paying_customer' => false,
|
||||
'meta_data' => array(),
|
||||
'last_name' => '',
|
||||
'role' => 'customer',
|
||||
'username' => 'get_customer_test',
|
||||
'avatar_url' => $data['avatar_url'],
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single customer without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'get_customer_test_without_permission', 'test123', 'get_customer_test_without_permission@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single customer with an invalid ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a customer.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'update_customer_test', 'test123', 'update_customer_test@woo.local' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'update_customer_test', $data['username'] );
|
||||
$this->assertEquals( 'update_customer_test@woo.local', $data['email'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/customers/' . $customer->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'email' => 'updated_email@woo.local',
|
||||
'first_name' => 'UpdatedTest',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'updated_email@woo.local', $data['email'] );
|
||||
$this->assertEquals( 'UpdatedTest', $data['first_name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a customer without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'update_customer_test_without_permission', 'test123', 'update_customer_test_without_permission@woo.local' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a customer with an invalid ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test deleting a customer.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'delete_customer_test', 'test123', 'delete_customer_test@woo.local' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/' . $customer->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a customer with an invalid ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_customer_invalid_id() {
|
||||
wp_set_current_user( 1 );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a customer without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_customer_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$customer = WC_Helper_Customer::create_customer( 'delete_customer_test_without_permission', 'test123', 'delete_customer_test_without_permission@woo.local' );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/' . $customer->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test customer batch endpoint.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_batch_customer() {
|
||||
wp_set_current_user( 1 );
|
||||
|
||||
$customer_1 = WC_Helper_Customer::create_customer( 'test_batch_customer', 'test123', 'test_batch_customer@woo.local' );
|
||||
$customer_2 = WC_Helper_Customer::create_customer( 'test_batch_customer2', 'test123', 'test_batch_customer2@woo.local' );
|
||||
$customer_3 = WC_Helper_Customer::create_customer( 'test_batch_customer3', 'test123', 'test_batch_customer3@woo.local' );
|
||||
$customer_4 = WC_Helper_Customer::create_customer( 'test_batch_customer4', 'test123', 'test_batch_customer4@woo.local' );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/customers/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $customer_1->get_id(),
|
||||
'last_name' => 'McTest',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$customer_2->get_id(),
|
||||
$customer_3->get_id(),
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'username' => 'newuser',
|
||||
'password' => 'test123',
|
||||
'email' => 'newuser@woo.local',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'McTest', $data['update'][0]['last_name'] );
|
||||
$this->assertEquals( 'newuser', $data['create'][0]['username'] );
|
||||
$this->assertEmpty( $data['create'][0]['last_name'] );
|
||||
$this->assertEquals( $customer_2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $customer_3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test customer schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_customer_schema() {
|
||||
wp_set_current_user( 1 );
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 16, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'email', $properties );
|
||||
$this->assertArrayHasKey( 'first_name', $properties );
|
||||
$this->assertArrayHasKey( 'last_name', $properties );
|
||||
$this->assertArrayHasKey( 'role', $properties );
|
||||
$this->assertArrayHasKey( 'username', $properties );
|
||||
$this->assertArrayHasKey( 'password', $properties );
|
||||
$this->assertArrayHasKey( 'avatar_url', $properties );
|
||||
$this->assertArrayHasKey( 'billing', $properties );
|
||||
$this->assertArrayHasKey( 'first_name', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'last_name', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'company', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_1', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_2', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'city', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'state', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'postcode', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'country', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'email', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'phone', $properties['billing']['properties'] );
|
||||
$this->assertArrayHasKey( 'shipping', $properties );
|
||||
$this->assertArrayHasKey( 'first_name', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'last_name', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'company', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_1', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'address_2', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'city', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'state', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'postcode', $properties['shipping']['properties'] );
|
||||
$this->assertArrayHasKey( 'country', $properties['shipping']['properties'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,251 @@
|
|||
<?php
|
||||
/**
|
||||
* File for the WC_Tests_API_Functions class.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* REST API Functions.
|
||||
* @since 2.6.0
|
||||
*/
|
||||
class WC_Tests_API_Functions extends WC_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* @var string path to the WP upload dir.
|
||||
*/
|
||||
private $upload_dir_path;
|
||||
|
||||
/**
|
||||
* @var string WP upload dir URL.
|
||||
*/
|
||||
private $upload_dir_url;
|
||||
|
||||
/**
|
||||
* @var string Name of the file used in wc_rest_upload_image_from_url() tests.
|
||||
*/
|
||||
private $file_name;
|
||||
|
||||
/**
|
||||
* Run setup code for unit tests.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Callback used by WP_HTTP_TestCase to decide whether to perform HTTP requests or to provide a mocked response.
|
||||
$this->http_responder = array( $this, 'mock_http_responses' );
|
||||
|
||||
$upload_dir_info = wp_upload_dir();
|
||||
$this->upload_dir_path = $upload_dir_info['path'];
|
||||
$this->upload_dir_url = $upload_dir_info['url'];
|
||||
$this->file_name = 'Dr1Bczxq4q.png';
|
||||
}
|
||||
|
||||
/**
|
||||
* Run tear down code for unit tests.
|
||||
*/
|
||||
public function tearDown() {
|
||||
parent::tearDown();
|
||||
|
||||
// remove files created in the wc_rest_upload_image_from_url() tests.
|
||||
$file_path = $this->upload_dir_path . '/' . $this->file_name;
|
||||
|
||||
if ( file_exists( $file_path ) ) {
|
||||
unlink( $file_path );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rest_prepare_date_response().
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function test_wc_rest_prepare_date_response() {
|
||||
$this->assertEquals( '2016-06-06T06:06:06', wc_rest_prepare_date_response( '2016-06-06 06:06:06' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rest_upload_image_from_url() should return error when unable to download image.
|
||||
*/
|
||||
public function test_wc_rest_upload_image_from_url_should_return_error_when_unable_to_download_image() {
|
||||
$expected_error_message = 'Error getting remote image http://somedomain.com/nonexistent-image.png. Error: Not found.';
|
||||
$result = wc_rest_upload_image_from_url( 'http://somedomain.com/nonexistent-image.png' );
|
||||
|
||||
$this->assertIsWPError( $result );
|
||||
$this->assertEquals( $expected_error_message, $result->get_error_message() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rest_upload_image_from_url() should return error when invalid image is passed.
|
||||
*
|
||||
* @requires PHP 5.4
|
||||
*/
|
||||
public function test_wc_rest_upload_image_from_url_should_return_error_when_invalid_image_is_passed() {
|
||||
// empty file.
|
||||
$expected_error_message = 'Invalid image: File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.';
|
||||
$result = wc_rest_upload_image_from_url( 'http://somedomain.com/invalid-image-1.png' );
|
||||
|
||||
$this->assertIsWPError( $result );
|
||||
$this->assertEquals( $expected_error_message, $result->get_error_message() );
|
||||
|
||||
// unsupported mime type.
|
||||
$expected_error_message = 'Invalid image: Sorry, this file type is not permitted for security reasons.';
|
||||
$result = wc_rest_upload_image_from_url( 'http://somedomain.com/invalid-image-2.png' );
|
||||
|
||||
$this->assertIsWPError( $result );
|
||||
$this->assertEquals( $expected_error_message, $result->get_error_message() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rest_upload_image_from_url() should download image and return an array containing
|
||||
* information about it.
|
||||
*
|
||||
* @requires PHP 5.4
|
||||
*/
|
||||
public function test_wc_rest_upload_image_from_url_should_download_image_and_return_array() {
|
||||
$expected_result = array(
|
||||
'file' => $this->upload_dir_path . '/' . $this->file_name,
|
||||
'url' => $this->upload_dir_url . '/' . $this->file_name,
|
||||
'type' => 'image/png',
|
||||
);
|
||||
$result = wc_rest_upload_image_from_url( 'http://somedomain.com/' . $this->file_name );
|
||||
|
||||
$this->assertEquals( $expected_result, $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rest_set_uploaded_image_as_attachment().
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function test_wc_rest_set_uploaded_image_as_attachment() {
|
||||
$this->assertInternalType(
|
||||
'int',
|
||||
wc_rest_set_uploaded_image_as_attachment(
|
||||
array(
|
||||
'file' => '',
|
||||
'url' => '',
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rest_validate_reports_request_arg().
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function test_wc_rest_validate_reports_request_arg() {
|
||||
$request = new WP_REST_Request(
|
||||
'GET',
|
||||
'/wc/v3/foo',
|
||||
array(
|
||||
'args' => array(
|
||||
'date' => array(
|
||||
'type' => 'string',
|
||||
'format' => 'date',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// Success.
|
||||
$this->assertTrue( wc_rest_validate_reports_request_arg( '2016-06-06', $request, 'date' ) );
|
||||
|
||||
// Error.
|
||||
$error = wc_rest_validate_reports_request_arg( 'foo', $request, 'date' );
|
||||
$this->assertEquals( 'The date you provided is invalid.', $error->get_error_message() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rest_urlencode_rfc3986().
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function test_wc_rest_urlencode_rfc3986() {
|
||||
$this->assertEquals( 'https%3A%2F%2Fwoocommerce.com%2F', wc_rest_urlencode_rfc3986( 'https://woocommerce.com/' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rest_check_post_permissions().
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function test_wc_rest_check_post_permissions() {
|
||||
$this->assertFalse( wc_rest_check_post_permissions( 'shop_order' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rest_check_user_permissions().
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function test_wc_rest_check_user_permissions() {
|
||||
$this->assertFalse( wc_rest_check_user_permissions() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rest_check_product_term_permissions().
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function test_wc_rest_check_product_term_permissions() {
|
||||
$this->assertFalse( wc_rest_check_product_term_permissions( 'product_cat' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test wc_rest_check_manager_permissions().
|
||||
*
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public function test_wc_rest_check_manager_permissions() {
|
||||
$this->assertFalse( wc_rest_check_manager_permissions( 'reports' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to define mocked HTTP responses using WP_HTTP_TestCase.
|
||||
* Thanks to WP_HTTP_TestCase, it is not necessary to perform a regular request
|
||||
* to an external server which would significantly slow down the tests.
|
||||
*
|
||||
* This function is called by WP_HTTP_TestCase::http_request_listner().
|
||||
*
|
||||
* @param array $request Request arguments.
|
||||
* @param string $url URL of the request.
|
||||
*
|
||||
* @return array|false mocked response or false to let WP perform a regular request.
|
||||
*/
|
||||
protected function mock_http_responses( $request, $url ) {
|
||||
$mocked_response = false;
|
||||
|
||||
if ( 'http://somedomain.com/nonexistent-image.png' === $url ) {
|
||||
$mocked_response = array(
|
||||
'response' => array(
|
||||
'code' => 404,
|
||||
'message' => 'Not found.',
|
||||
),
|
||||
);
|
||||
} elseif ( 'http://somedomain.com/invalid-image-1.png' === $url ) {
|
||||
// empty image.
|
||||
$mocked_response = array(
|
||||
'response' => array( 'code' => 200 ),
|
||||
);
|
||||
} elseif ( 'http://somedomain.com/invalid-image-2.png' === $url ) {
|
||||
// image with an unsupported mime type.
|
||||
// we need to manually copy the file as we are mocking the request. without this an empty file is created.
|
||||
copy( WC_Unit_Tests_Bootstrap::instance()->tests_dir . '/data/file.txt', $request['filename'] );
|
||||
|
||||
$mocked_response = array(
|
||||
'response' => array( 'code' => 200 ),
|
||||
);
|
||||
} elseif ( 'http://somedomain.com/' . $this->file_name === $url ) {
|
||||
// we need to manually copy the file as we are mocking the request. without this an empty file is created.
|
||||
copy( WC_Unit_Tests_Bootstrap::instance()->tests_dir . '/data/Dr1Bczxq4q.png', $request['filename'] );
|
||||
|
||||
$mocked_response = array(
|
||||
'response' => array( 'code' => 200 ),
|
||||
);
|
||||
}
|
||||
|
||||
return $mocked_response;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,665 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the orders REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Orders
|
||||
*/
|
||||
class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Array of order to track
|
||||
* @var array
|
||||
*/
|
||||
protected $orders = array();
|
||||
|
||||
/**
|
||||
* Setup our test server.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Orders_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/orders', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/orders/batch', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/orders/(?P<id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all orders.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_items() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Create 10 orders.
|
||||
for ( $i = 0; $i < 10; $i++ ) {
|
||||
$this->orders[] = WC_Helper_Order::create_order( $this->user );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders' ) );
|
||||
$orders = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 10, count( $orders ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure orders cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_items_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$this->orders[] = WC_Helper_Order::create_order();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single order.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_item() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$order->add_meta_data( 'key', 'value' );
|
||||
$order->add_meta_data( 'key2', 'value2' );
|
||||
$order->save();
|
||||
$this->orders[] = $order;
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $order->get_id(), $data['id'] );
|
||||
|
||||
// Test meta data is set.
|
||||
$this->assertEquals( 'key', $data['meta_data'][0]->key );
|
||||
$this->assertEquals( 'value', $data['meta_data'][0]->value );
|
||||
$this->assertEquals( 'key2', $data['meta_data'][1]->key );
|
||||
$this->assertEquals( 'value2', $data['meta_data'][1]->value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single order without the correct permissions.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_item_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$this->orders[] = $order;
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting an order with an invalid ID.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_item_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/99999999' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting an order with an invalid ID.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_item_refund_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$refund = wc_create_refund(
|
||||
array(
|
||||
'order_id' => $order->get_id(),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $refund->get_id() ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating an order.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/orders' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => 'Direct Bank Transfer',
|
||||
'set_paid' => true,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => '10',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$order = wc_get_order( $data['id'] );
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals( $order->get_payment_method(), $data['payment_method'] );
|
||||
$this->assertEquals( $order->get_payment_method_title(), $data['payment_method_title'] );
|
||||
$this->assertEquals( $order->get_billing_first_name(), $data['billing']['first_name'] );
|
||||
$this->assertEquals( $order->get_billing_last_name(), $data['billing']['last_name'] );
|
||||
$this->assertEquals( '', $data['billing']['company'] );
|
||||
$this->assertEquals( $order->get_billing_address_1(), $data['billing']['address_1'] );
|
||||
$this->assertEquals( $order->get_billing_address_2(), $data['billing']['address_2'] );
|
||||
$this->assertEquals( $order->get_billing_city(), $data['billing']['city'] );
|
||||
$this->assertEquals( $order->get_billing_state(), $data['billing']['state'] );
|
||||
$this->assertEquals( $order->get_billing_postcode(), $data['billing']['postcode'] );
|
||||
$this->assertEquals( $order->get_billing_country(), $data['billing']['country'] );
|
||||
$this->assertEquals( $order->get_billing_email(), $data['billing']['email'] );
|
||||
$this->assertEquals( $order->get_billing_phone(), $data['billing']['phone'] );
|
||||
$this->assertEquals( $order->get_shipping_first_name(), $data['shipping']['first_name'] );
|
||||
$this->assertEquals( $order->get_shipping_last_name(), $data['shipping']['last_name'] );
|
||||
$this->assertEquals( '', $data['shipping']['company'] );
|
||||
$this->assertEquals( $order->get_shipping_address_1(), $data['shipping']['address_1'] );
|
||||
$this->assertEquals( $order->get_shipping_address_2(), $data['shipping']['address_2'] );
|
||||
$this->assertEquals( $order->get_shipping_city(), $data['shipping']['city'] );
|
||||
$this->assertEquals( $order->get_shipping_state(), $data['shipping']['state'] );
|
||||
$this->assertEquals( $order->get_shipping_postcode(), $data['shipping']['postcode'] );
|
||||
$this->assertEquals( $order->get_shipping_country(), $data['shipping']['country'] );
|
||||
$this->assertEquals( 1, count( $data['line_items'] ) );
|
||||
$this->assertEquals( 1, count( $data['shipping_lines'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the sanitization of the payment_method_title field through the API.
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public function test_create_update_order_payment_method_title_sanitize() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// Test when creating order.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/orders' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => '<h1>Sanitize this <script>alert(1);</script></h1>',
|
||||
'set_paid' => true,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => '10',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$order = wc_get_order( $data['id'] );
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals( $order->get_payment_method(), $data['payment_method'] );
|
||||
$this->assertEquals( $order->get_payment_method_title(), 'Sanitize this' );
|
||||
|
||||
// Test when updating order.
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $data['id'] );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => '<h1>Sanitize this too <script>alert(1);</script></h1>',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$order = wc_get_order( $data['id'] );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $order->get_payment_method(), $data['payment_method'] );
|
||||
$this->assertEquals( $order->get_payment_method_title(), 'Sanitize this too' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating an order without required fields.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_order_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// Non-existent customer.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/orders' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => 'Direct Bank Transfer',
|
||||
'set_paid' => true,
|
||||
'customer_id' => 99999,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => 10,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
'first_name' => 'Fish',
|
||||
'last_name' => 'Face',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'test-update', $data['payment_method'] );
|
||||
$this->assertEquals( 'Fish', $data['billing']['first_name'] );
|
||||
$this->assertEquals( 'Face', $data['billing']['last_name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and removing items.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_remove_items() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$fee = new WC_Order_Item_Fee();
|
||||
$fee->set_props(
|
||||
array(
|
||||
'name' => 'Some Fee',
|
||||
'tax_status' => 'taxable',
|
||||
'total' => '100',
|
||||
'tax_class' => '',
|
||||
)
|
||||
);
|
||||
$order->add_item( $fee );
|
||||
$order->save();
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
|
||||
$fee_data = current( $order->get_items( 'fee' ) );
|
||||
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'fee_lines' => array(
|
||||
array(
|
||||
'id' => $fee_data->get_id(),
|
||||
'name' => null,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertTrue( empty( $data['fee_lines'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and adding a coupon.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_add_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$order_item = current( $order->get_items() );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' );
|
||||
$coupon->set_amount( 5 );
|
||||
$coupon->save();
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'coupon_lines' => array(
|
||||
array(
|
||||
'code' => 'fake-coupon',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $data['coupon_lines'] );
|
||||
$this->assertEquals( '45.00', $data['total'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and removing a coupon.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_remove_coupons() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$order_item = current( $order->get_items() );
|
||||
$coupon = WC_Helper_Coupon::create_coupon( 'fake-coupon' );
|
||||
$coupon->set_amount( 5 );
|
||||
$coupon->save();
|
||||
|
||||
$order->apply_coupon( $coupon );
|
||||
$order->save();
|
||||
|
||||
// Check that the coupon is applied.
|
||||
$this->assertEquals( '45.00', $order->get_total() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
|
||||
$coupon_data = current( $order->get_items( 'coupon' ) );
|
||||
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'coupon_lines' => array(
|
||||
array(
|
||||
'id' => $coupon_data->get_id(),
|
||||
'code' => null,
|
||||
),
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'id' => $order_item->get_id(),
|
||||
'product_id' => $order_item->get_product_id(),
|
||||
'total' => '40.00',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertTrue( empty( $data['coupon_lines'] ) );
|
||||
$this->assertEquals( '50.00', $data['total'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order with an invalid coupon.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_invalid_coupon() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
|
||||
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'coupon_lines' => array(
|
||||
array(
|
||||
'code' => 'NON_EXISTING_COUPON',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
$this->assertEquals( 'woocommerce_rest_invalid_coupon', $data['code'] );
|
||||
$this->assertEquals( 'Coupon "non_existing_coupon" does not exist!', $data['message'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order without the correct permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
'first_name' => 'Fish',
|
||||
'last_name' => 'Face',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that updating an order with an invalid id fails.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/orders/999999' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
'first_name' => 'Fish',
|
||||
'last_name' => 'Face',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting an order.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_order() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/' . $order->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( null, get_post( $order->get_id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting an order without permission/creds.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_order_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/' . $order->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting an order with an invalid id.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_order_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/9999999' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing product reviews.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_orders_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$order1 = WC_Helper_Order::create_order();
|
||||
$order2 = WC_Helper_Order::create_order();
|
||||
$order3 = WC_Helper_Order::create_order();
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/orders/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $order1->get_id(),
|
||||
'payment_method' => 'updated',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$order2->get_id(),
|
||||
$order3->get_id(),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'updated', $data['update'][0]['payment_method'] );
|
||||
$this->assertEquals( $order2->get_id(), $data['delete'][0]['id'] );
|
||||
$this->assertEquals( $order3->get_id(), $data['delete'][1]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/orders' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 1, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the order schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_order_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$order = WC_Helper_Order::create_order();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/orders/' . $order->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 42, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,345 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the Payment Gateways REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class Payment_Gateways extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Payment_Gateways_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/payment_gateways', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/payment_gateways/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all payment gateways.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_payment_gateways() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways' ) );
|
||||
$gateways = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'cheque',
|
||||
'title' => 'Check payments',
|
||||
'description' => 'Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.',
|
||||
'order' => '',
|
||||
'enabled' => false,
|
||||
'method_title' => 'Check payments',
|
||||
'method_description' => 'Take payments in person via checks. This offline gateway can also be useful to test purchases.',
|
||||
'method_supports' => array(
|
||||
'products',
|
||||
),
|
||||
'settings' => array_diff_key(
|
||||
$this->get_settings( 'WC_Gateway_Cheque' ),
|
||||
array(
|
||||
'enabled' => false,
|
||||
'description' => false,
|
||||
)
|
||||
),
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/payment_gateways/cheque' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/payment_gateways' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$gateways
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure payment gateways cannot viewed without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_payment_gateways_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single payment gateway.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_payment_gateway() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => 'paypal',
|
||||
'title' => 'PayPal',
|
||||
'description' => "Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.",
|
||||
'order' => '',
|
||||
'enabled' => false,
|
||||
'method_title' => 'PayPal',
|
||||
'method_description' => 'PayPal Standard redirects customers to PayPal to enter their payment information.',
|
||||
'method_supports' => array(
|
||||
'products',
|
||||
'refunds',
|
||||
),
|
||||
'settings' => array_diff_key(
|
||||
$this->get_settings( 'WC_Gateway_Paypal' ),
|
||||
array(
|
||||
'enabled' => false,
|
||||
'description' => false,
|
||||
)
|
||||
),
|
||||
),
|
||||
$paypal
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a payment gateway without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_payment_gateway_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a payment gateway with an invalid id.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_payment_gateway_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/totally_fake_method' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single payment gateway.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_payment_gateway() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Test defaults
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'admin@example.org', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test updating single setting
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'email' => 'woo@woo.local',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test updating multiple settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'testmode' => 'yes',
|
||||
'title' => 'PayPal - New Title',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// Test other parameters, and recheck settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
|
||||
$this->assertFalse( $paypal['enabled'] );
|
||||
$this->assertEquals( 2, $paypal['order'] );
|
||||
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] );
|
||||
$this->assertEquals( 'woo@woo.local', $paypal['settings']['email']['value'] );
|
||||
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] );
|
||||
|
||||
// test bogus
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'paymentaction' => 'afasfasf',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'paymentaction' => 'authorization',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$paypal = $response->get_data();
|
||||
$this->assertEquals( 'authorization', $paypal['settings']['paymentaction']['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a payment gateway without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_payment_gateway_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'testmode' => 'yes',
|
||||
'title' => 'PayPal - New Title',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a payment gateway with an invalid id.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_payment_gateway_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/totally_fake_method' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'enabled' => true,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the payment gateway schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_payment_gateway_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/payment_gateways' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 9, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'title', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'order', $properties );
|
||||
$this->assertArrayHasKey( 'enabled', $properties );
|
||||
$this->assertArrayHasKey( 'method_title', $properties );
|
||||
$this->assertArrayHasKey( 'method_description', $properties );
|
||||
$this->assertArrayHasKey( 'method_supports', $properties );
|
||||
$this->assertArrayHasKey( 'settings', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a particular gateway's settings so we can correctly test API output.
|
||||
*
|
||||
* @since 3.5.0
|
||||
* @param string $gateway_class Name of WC_Payment_Gateway class.
|
||||
*/
|
||||
private function get_settings( $gateway_class ) {
|
||||
$gateway = new $gateway_class();
|
||||
$settings = array();
|
||||
$gateway->init_form_fields();
|
||||
foreach ( $gateway->form_fields as $id => $field ) {
|
||||
// Make sure we at least have a title and type
|
||||
if ( empty( $field['title'] ) || empty( $field['type'] ) ) {
|
||||
continue;
|
||||
}
|
||||
// Ignore 'enabled' and 'description', to be in line with \WC_REST_Payment_Gateways_Controller::get_settings.
|
||||
if ( in_array( $id, array( 'enabled', 'description' ), true ) ) {
|
||||
continue;
|
||||
}
|
||||
$data = array(
|
||||
'id' => $id,
|
||||
'label' => empty( $field['label'] ) ? $field['title'] : $field['label'],
|
||||
'description' => empty( $field['description'] ) ? '' : $field['description'],
|
||||
'type' => $field['type'],
|
||||
'value' => $gateway->settings[ $id ],
|
||||
'default' => empty( $field['default'] ) ? '' : $field['default'],
|
||||
'tip' => empty( $field['description'] ) ? '' : $field['description'],
|
||||
'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'],
|
||||
);
|
||||
if ( ! empty( $field['options'] ) ) {
|
||||
$data['options'] = $field['options'];
|
||||
}
|
||||
$settings[ $id ] = $data;
|
||||
}
|
||||
return $settings;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,470 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the product reviews REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/products/reviews', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/reviews/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/reviews/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all product reviews.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_reviews() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
// Create 10 products reviews for the product
|
||||
for ( $i = 0; $i < 10; $i++ ) {
|
||||
$review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) );
|
||||
$product_reviews = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 10, count( $product_reviews ) );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $review_id,
|
||||
'date_created' => $product_reviews[0]['date_created'],
|
||||
'date_created_gmt' => $product_reviews[0]['date_created_gmt'],
|
||||
'product_id' => $product->get_id(),
|
||||
'status' => 'approved',
|
||||
'reviewer' => 'admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'review' => "<p>Review content here</p>\n",
|
||||
'rating' => 0,
|
||||
'verified' => false,
|
||||
'reviewer_avatar_urls' => $product_reviews[0]['reviewer_avatar_urls'],
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/products/reviews/' . $review_id ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/products/reviews' ),
|
||||
),
|
||||
),
|
||||
'up' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/products/' . $product->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$product_reviews
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure product reviews cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_reviews_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure an error is returned when an invalid product is loaded.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_reviews_invalid_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/0/reviews' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single product review.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $product_review_id,
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'product_id' => $product->get_id(),
|
||||
'status' => 'approved',
|
||||
'reviewer' => 'admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'review' => "<p>Review content here</p>\n",
|
||||
'rating' => 0,
|
||||
'verified' => false,
|
||||
'reviewer_avatar_urls' => $data['reviewer_avatar_urls'],
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single product review without the correct permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_review_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a product review with an invalid ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/0' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating a product review.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'rating' => '5',
|
||||
'product_id' => $product->get_id(),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'date_created' => $data['date_created'],
|
||||
'date_created_gmt' => $data['date_created_gmt'],
|
||||
'product_id' => $product->get_id(),
|
||||
'status' => 'approved',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
'review' => 'Hello world.',
|
||||
'rating' => 5,
|
||||
'verified' => false,
|
||||
'reviewer_avatar_urls' => $data['reviewer_avatar_urls'],
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating a product review without required fields.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_product_review_invalid_fields() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// missing review
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// Missing reviewer.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer_email' => 'woo@woo.local',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// missing reviewer_email
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating a product review.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( "<p>Review content here</p>\n", $data['review'] );
|
||||
$this->assertEquals( 'admin', $data['reviewer'] );
|
||||
$this->assertEquals( 'woo@woo.local', $data['reviewer_email'] );
|
||||
$this->assertEquals( 0, $data['rating'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world - updated.',
|
||||
'reviewer' => 'Justin',
|
||||
'reviewer_email' => 'woo2@woo.local',
|
||||
'rating' => 3,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'Hello world - updated.', $data['review'] );
|
||||
$this->assertEquals( 'Justin', $data['reviewer'] );
|
||||
$this->assertEquals( 'woo2@woo.local', $data['reviewer_email'] );
|
||||
$this->assertEquals( 3, $data['rating'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating a product review without the correct permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product_review_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.dev',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that updating a product review with an invalid id fails.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'review' => 'Hello world.',
|
||||
'reviewer' => 'Admin',
|
||||
'reviewer_email' => 'woo@woo.dev',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a product review.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product_review() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a product review without permission/creds.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a product review with an invalid id.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product_review_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_review_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing product reviews.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_reviews_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$review_1_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$review_2_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$review_3_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
$review_4_id = WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $review_1_id,
|
||||
'review' => 'Updated review.',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$review_2_id,
|
||||
$review_3_id,
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'review' => 'New review.',
|
||||
'reviewer' => 'Justin',
|
||||
'reviewer_email' => 'woo3@woo.local',
|
||||
'product_id' => $product->get_id(),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'Updated review.', $data['update'][0]['review'] );
|
||||
$this->assertEquals( 'New review.', $data['create'][0]['review'] );
|
||||
$this->assertEquals( $review_2_id, $data['delete'][0]['previous']['id'] );
|
||||
$this->assertEquals( $review_3_id, $data['delete'][1]['previous']['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/products/reviews' );
|
||||
$request->set_param( 'product', $product->get_id() );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the product review schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_review_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/products/reviews' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 11, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'product_id', $properties );
|
||||
$this->assertArrayHasKey( 'status', $properties );
|
||||
$this->assertArrayHasKey( 'reviewer', $properties );
|
||||
$this->assertArrayHasKey( 'reviewer_email', $properties );
|
||||
$this->assertArrayHasKey( 'review', $properties );
|
||||
$this->assertArrayHasKey( 'rating', $properties );
|
||||
$this->assertArrayHasKey( 'verified', $properties );
|
||||
|
||||
if ( get_option( 'show_avatars' ) ) {
|
||||
$this->assertArrayHasKey( 'reviewer_avatar_urls', $properties );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,474 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for Variations API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class Product_Variations_API extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Product_Variations_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/products/(?P<product_id>[\d]+)/variations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/(?P<product_id>[\d]+)/variations/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/(?P<product_id>[\d]+)/variations/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting variations.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_variations() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $variations ) );
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE LARGE', $variations[0]['sku'] );
|
||||
$this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting variations without permission.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_variations_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single variation.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $variation_id, $variation['id'] );
|
||||
$this->assertEquals( 'size', $variation['attributes'][0]['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting single variation without permission.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single variation.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 1, count( $variations ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single variation without permission.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single variation with an invalid ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_variation_with_invalid_id() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() . '/variations/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test editing a single variation.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id ) );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variation['sku'] );
|
||||
$this->assertEquals( 10, $variation['regular_price'] );
|
||||
$this->assertEmpty( $variation['sale_price'] );
|
||||
$this->assertEquals( 'small', $variation['attributes'][0]['option'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-\'SKU',
|
||||
'sale_price' => '8',
|
||||
'description' => 'O_O',
|
||||
'image' => array(
|
||||
'position' => 0,
|
||||
'src' => 'http://cldup.com/Dr1Bczxq4q.png',
|
||||
'alt' => 'test upload image',
|
||||
),
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertTrue( isset( $variation['description'] ), print_r( $variation, true ) );
|
||||
$this->assertContains( 'O_O', $variation['description'], print_r( $variation, true ) );
|
||||
$this->assertEquals( '8', $variation['price'], print_r( $variation, true ) );
|
||||
$this->assertEquals( '8', $variation['sale_price'], print_r( $variation, true ) );
|
||||
$this->assertEquals( '10', $variation['regular_price'], print_r( $variation, true ) );
|
||||
$this->assertEquals( 'FIXED-\'SKU', $variation['sku'], print_r( $variation, true ) );
|
||||
$this->assertEquals( 'medium', $variation['attributes'][0]['option'], print_r( $variation, true ) );
|
||||
$this->assertContains( 'Dr1Bczxq4q', $variation['image']['src'], print_r( $variation, true ) );
|
||||
$this->assertContains( 'test upload image', $variation['image']['alt'], print_r( $variation, true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single variation without permission.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single variation with an invalid ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_variation_with_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single variation.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_variation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 2, count( $variations ) );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/' . $product->get_id() . '/variations' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
'regular_price' => '12',
|
||||
'description' => 'A medium size.',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertContains( 'A medium size.', $variation['description'] );
|
||||
$this->assertEquals( '12', $variation['price'] );
|
||||
$this->assertEquals( '12', $variation['regular_price'] );
|
||||
$this->assertTrue( $variation['purchasable'] );
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $variation['sku'] );
|
||||
$this->assertEquals( 'medium', $variation['attributes'][0]['option'] );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 3, count( $variations ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single variation without permission.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_variation_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/' . $product->get_id() . '/variations' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
'regular_price' => '12',
|
||||
'description' => 'A medium size.',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing product variations.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_variations_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$children = $product->get_children();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/' . $product->get_id() . '/variations/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $children[0],
|
||||
'description' => 'Updated description.',
|
||||
'image' => array(
|
||||
'position' => 0,
|
||||
'src' => 'http://cldup.com/Dr1Bczxq4q.png',
|
||||
'alt' => 'test upload image',
|
||||
),
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$children[1],
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU VARIABLE MEDIUM',
|
||||
'regular_price' => '12',
|
||||
'description' => 'A medium size.',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'name' => 'pa_size',
|
||||
'option' => 'medium',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertContains( 'Updated description.', $data['update'][0]['description'] );
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $data['create'][0]['sku'] );
|
||||
$this->assertEquals( 'medium', $data['create'][0]['attributes'][0]['option'] );
|
||||
$this->assertEquals( $children[1], $data['delete'][0]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 2, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test variation schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_variation_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/products/' . $product->get_id() . '/variations' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 37, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_modified', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'permalink', $properties );
|
||||
$this->assertArrayHasKey( 'sku', $properties );
|
||||
$this->assertArrayHasKey( 'price', $properties );
|
||||
$this->assertArrayHasKey( 'regular_price', $properties );
|
||||
$this->assertArrayHasKey( 'sale_price', $properties );
|
||||
$this->assertArrayHasKey( 'date_on_sale_from', $properties );
|
||||
$this->assertArrayHasKey( 'date_on_sale_to', $properties );
|
||||
$this->assertArrayHasKey( 'on_sale', $properties );
|
||||
$this->assertArrayHasKey( 'purchasable', $properties );
|
||||
$this->assertArrayHasKey( 'virtual', $properties );
|
||||
$this->assertArrayHasKey( 'downloadable', $properties );
|
||||
$this->assertArrayHasKey( 'downloads', $properties );
|
||||
$this->assertArrayHasKey( 'download_limit', $properties );
|
||||
$this->assertArrayHasKey( 'download_expiry', $properties );
|
||||
$this->assertArrayHasKey( 'tax_status', $properties );
|
||||
$this->assertArrayHasKey( 'tax_class', $properties );
|
||||
$this->assertArrayHasKey( 'manage_stock', $properties );
|
||||
$this->assertArrayHasKey( 'stock_quantity', $properties );
|
||||
$this->assertArrayHasKey( 'stock_status', $properties );
|
||||
$this->assertArrayHasKey( 'backorders', $properties );
|
||||
$this->assertArrayHasKey( 'backorders_allowed', $properties );
|
||||
$this->assertArrayHasKey( 'backordered', $properties );
|
||||
$this->assertArrayHasKey( 'weight', $properties );
|
||||
$this->assertArrayHasKey( 'dimensions', $properties );
|
||||
$this->assertArrayHasKey( 'shipping_class', $properties );
|
||||
$this->assertArrayHasKey( 'shipping_class_id', $properties );
|
||||
$this->assertArrayHasKey( 'image', $properties );
|
||||
$this->assertArrayHasKey( 'attributes', $properties );
|
||||
$this->assertArrayHasKey( 'menu_order', $properties );
|
||||
$this->assertArrayHasKey( 'meta_data', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a variation stock.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_variation_manage_stock() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$product->set_manage_stock( false );
|
||||
$product->save();
|
||||
|
||||
$children = $product->get_children();
|
||||
$variation_id = $children[0];
|
||||
|
||||
// Set stock to true.
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => true,
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( true, $variation['manage_stock'] );
|
||||
|
||||
// Set stock to false.
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => false,
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( false, $variation['manage_stock'] );
|
||||
|
||||
// Set stock to false but parent is managing stock.
|
||||
$product->set_manage_stock( true );
|
||||
$product->save();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() . '/variations/' . $variation_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'manage_stock' => false,
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$variation = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'parent', $variation['manage_stock'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,807 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for Products API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC_Tests_API_Product class.
|
||||
*/
|
||||
class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Products_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/products', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/(?P<id>[\d]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/products/batch', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_products() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Product::create_external_product();
|
||||
sleep( 1 ); // So both products have different timestamps.
|
||||
WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products' ) );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 2, count( $products ) );
|
||||
$this->assertEquals( 'Dummy Product', $products[0]['name'] );
|
||||
$this->assertEquals( 'DUMMY SKU', $products[0]['sku'] );
|
||||
$this->assertEquals( 'Dummy External Product', $products[1]['name'] );
|
||||
$this->assertEquals( 'DUMMY EXTERNAL SKU', $products[1]['sku'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products without permission.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_products_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single product.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
$simple = WC_Helper_Product::create_external_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $simple->get_id() ) );
|
||||
$product = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $simple->get_id(),
|
||||
'name' => 'Dummy External Product',
|
||||
'type' => 'simple',
|
||||
'status' => 'publish',
|
||||
'sku' => 'DUMMY EXTERNAL SKU',
|
||||
'regular_price' => 10,
|
||||
),
|
||||
$product
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting single product without permission.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single product.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products' ) );
|
||||
$variations = $response->get_data();
|
||||
$this->assertEquals( 0, count( $variations ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single product without permission.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/' . $product->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a single product with an invalid ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_product_with_invalid_id() {
|
||||
wp_set_current_user( 0 );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/products/0' );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test editing a single product. Tests multiple product types.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test simple products.
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
$date_created = date( 'Y-m-d\TH:i:s', current_time( 'timestamp' ) );
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU', $data['sku'] );
|
||||
$this->assertEquals( 10, $data['regular_price'] );
|
||||
$this->assertEmpty( $data['sale_price'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU',
|
||||
'sale_price' => '8',
|
||||
'description' => 'Testing',
|
||||
'date_created' => $date_created,
|
||||
'images' => array(
|
||||
array(
|
||||
'position' => 0,
|
||||
'src' => 'http://cldup.com/Dr1Bczxq4q.png',
|
||||
'alt' => 'test upload image',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertContains( 'Testing', $data['description'] );
|
||||
$this->assertEquals( '8', $data['price'] );
|
||||
$this->assertEquals( '8', $data['sale_price'] );
|
||||
$this->assertEquals( '10', $data['regular_price'] );
|
||||
$this->assertEquals( 'FIXED-SKU', $data['sku'] );
|
||||
$this->assertEquals( $date_created, $data['date_created'] );
|
||||
$this->assertContains( 'Dr1Bczxq4q', $data['images'][0]['src'] );
|
||||
$this->assertContains( 'test upload image', $data['images'][0]['alt'] );
|
||||
$product->delete( true );
|
||||
|
||||
// test variable product (variations are tested in product-variations.php).
|
||||
$product = WC_Helper_Product::create_variation_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
foreach ( array( 'small', 'large' ) as $term_name ) {
|
||||
$this->assertContains( $term_name, $data['attributes'][0]['options'] );
|
||||
}
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'attributes' => array(
|
||||
array(
|
||||
'id' => 0,
|
||||
'name' => 'pa_color',
|
||||
'options' => array(
|
||||
'red',
|
||||
'yellow',
|
||||
),
|
||||
'visible' => false,
|
||||
'variation' => 1,
|
||||
),
|
||||
array(
|
||||
'id' => 0,
|
||||
'name' => 'pa_size',
|
||||
'options' => array(
|
||||
'small',
|
||||
),
|
||||
'visible' => false,
|
||||
'variation' => 1,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( array( 'small' ), $data['attributes'][0]['options'] );
|
||||
|
||||
foreach ( array( 'red', 'yellow' ) as $term_name ) {
|
||||
$this->assertContains( $term_name, $data['attributes'][1]['options'] );
|
||||
}
|
||||
|
||||
$product->delete( true );
|
||||
|
||||
// test external product.
|
||||
$product = WC_Helper_Product::create_external_product();
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'Buy external product', $data['button_text'] );
|
||||
$this->assertEquals( 'http://woocommerce.com', $data['external_url'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'button_text' => 'Test API Update',
|
||||
'external_url' => 'http://automattic.com',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'Test API Update', $data['button_text'] );
|
||||
$this->assertEquals( 'http://automattic.com', $data['external_url'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single product without permission.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-NO-PERMISSION',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single product with an invalid ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_product_with_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v2/products/0' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'sku' => 'FIXED-SKU-INVALID-ID',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single product.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_product() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/shipping_classes' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$shipping_class_id = $data['id'];
|
||||
|
||||
// Create simple.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'type' => 'simple',
|
||||
'name' => 'Test Simple Product',
|
||||
'sku' => 'DUMMY SKU SIMPLE API',
|
||||
'regular_price' => '10',
|
||||
'shipping_class' => 'test',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( '10', $data['price'] );
|
||||
$this->assertEquals( '10', $data['regular_price'] );
|
||||
$this->assertTrue( $data['purchasable'] );
|
||||
$this->assertEquals( 'DUMMY SKU SIMPLE API', $data['sku'] );
|
||||
$this->assertEquals( 'Test Simple Product', $data['name'] );
|
||||
$this->assertEquals( 'simple', $data['type'] );
|
||||
$this->assertEquals( $shipping_class_id, $data['shipping_class_id'] );
|
||||
|
||||
// Create external.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'type' => 'external',
|
||||
'name' => 'Test External Product',
|
||||
'sku' => 'DUMMY SKU EXTERNAL API',
|
||||
'regular_price' => '10',
|
||||
'button_text' => 'Test Button',
|
||||
'external_url' => 'https://wordpress.org',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( '10', $data['price'] );
|
||||
$this->assertEquals( '10', $data['regular_price'] );
|
||||
$this->assertFalse( $data['purchasable'] );
|
||||
$this->assertEquals( 'DUMMY SKU EXTERNAL API', $data['sku'] );
|
||||
$this->assertEquals( 'Test External Product', $data['name'] );
|
||||
$this->assertEquals( 'external', $data['type'] );
|
||||
$this->assertEquals( 'Test Button', $data['button_text'] );
|
||||
$this->assertEquals( 'https://wordpress.org', $data['external_url'] );
|
||||
|
||||
// Create variable.
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'type' => 'variable',
|
||||
'name' => 'Test Variable Product',
|
||||
'sku' => 'DUMMY SKU VARIABLE API',
|
||||
'attributes' => array(
|
||||
array(
|
||||
'id' => 0,
|
||||
'name' => 'pa_size',
|
||||
'options' => array(
|
||||
'small',
|
||||
'medium',
|
||||
),
|
||||
'visible' => false,
|
||||
'variation' => 1,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'DUMMY SKU VARIABLE API', $data['sku'] );
|
||||
$this->assertEquals( 'Test Variable Product', $data['name'] );
|
||||
$this->assertEquals( 'variable', $data['type'] );
|
||||
$this->assertEquals( array( 'small', 'medium' ), $data['attributes'][0]['options'] );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products' ) );
|
||||
$products = $response->get_data();
|
||||
$this->assertEquals( 3, count( $products ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a single product without permission.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_product_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test Product',
|
||||
'regular_price' => '12',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test batch managing products.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_products_batch() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product_2 = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/products/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $product->get_id(),
|
||||
'description' => 'Updated description.',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$product_2->get_id(),
|
||||
),
|
||||
'create' => array(
|
||||
array(
|
||||
'sku' => 'DUMMY SKU BATCH TEST 1',
|
||||
'regular_price' => '10',
|
||||
'name' => 'Test Batch Create 1',
|
||||
'type' => 'external',
|
||||
'button_text' => 'Test Button',
|
||||
),
|
||||
array(
|
||||
'sku' => 'DUMMY SKU BATCH TEST 2',
|
||||
'regular_price' => '20',
|
||||
'name' => 'Test Batch Create 2',
|
||||
'type' => 'simple',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertContains( 'Updated description.', $data['update'][0]['description'] );
|
||||
$this->assertEquals( 'DUMMY SKU BATCH TEST 1', $data['create'][0]['sku'] );
|
||||
$this->assertEquals( 'DUMMY SKU BATCH TEST 2', $data['create'][1]['sku'] );
|
||||
$this->assertEquals( 'Test Button', $data['create'][0]['button_text'] );
|
||||
$this->assertEquals( 'external', $data['create'][0]['type'] );
|
||||
$this->assertEquals( 'simple', $data['create'][1]['type'] );
|
||||
$this->assertEquals( $product_2->get_id(), $data['delete'][0]['id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/products' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure you can filter products post statuses by both
|
||||
* the status query arg and WP_Query.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_products_filter_post_status() {
|
||||
wp_set_current_user( $this->user );
|
||||
for ( $i = 0; $i < 8; $i++ ) {
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
if ( 0 === $i % 2 ) {
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $product->get_id(),
|
||||
'post_status' => 'draft',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Test filtering with status=publish.
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/products' );
|
||||
$request->set_param( 'status', 'publish' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 4, count( $products ) );
|
||||
foreach ( $products as $product ) {
|
||||
$this->assertEquals( 'publish', $product['status'] );
|
||||
}
|
||||
|
||||
// Test filtering with status=draft.
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/products' );
|
||||
$request->set_param( 'status', 'draft' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 4, count( $products ) );
|
||||
foreach ( $products as $product ) {
|
||||
$this->assertEquals( 'draft', $product['status'] );
|
||||
}
|
||||
|
||||
// Test filtering with no filters - which should return 'any' (all 8).
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/products' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 8, count( $products ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test product schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/products/' . $product->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 65, count( $properties ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test product category.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_products_by_category() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Create one product with a category.
|
||||
$category = wp_insert_term( 'Some Category', 'product_cat' );
|
||||
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_category_ids( array( $category['term_id'] ) );
|
||||
$product->save();
|
||||
|
||||
// Create one product without category, i.e. Uncategorized.
|
||||
$product_2 = new WC_Product_Simple();
|
||||
$product_2->save();
|
||||
|
||||
// Test product assigned to a single category.
|
||||
$query_params = array(
|
||||
'category' => (string) $category['term_id'],
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
foreach ( $response_products as $response_product ) {
|
||||
$this->assertEquals( $product->get_id(), $response_product['id'] );
|
||||
$this->assertEquals( $product->get_category_ids(), wp_list_pluck( $response_product['categories'], 'id' ) );
|
||||
}
|
||||
|
||||
// Test product without categories.
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product_2->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_product = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $response_product['categories'], print_r( $response_product, true ) );
|
||||
$this->assertEquals( 'uncategorized', $response_product['categories'][0]['slug'] );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products by product type.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_products_by_type() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$simple = WC_Helper_Product::create_simple_product();
|
||||
$external = WC_Helper_Product::create_external_product();
|
||||
$grouped = WC_Helper_Product::create_grouped_product();
|
||||
$variable = WC_Helper_Product::create_variation_product();
|
||||
|
||||
$product_ids_for_type = array(
|
||||
'simple' => array( $simple->get_id() ),
|
||||
'external' => array( $external->get_id() ),
|
||||
'grouped' => array( $grouped->get_id() ),
|
||||
'variable' => array( $variable->get_id() ),
|
||||
);
|
||||
|
||||
foreach ( $grouped->get_children() as $additional_product ) {
|
||||
$product_ids_for_type['simple'][] = $additional_product;
|
||||
}
|
||||
|
||||
foreach ( $product_ids_for_type as $product_type => $product_ids ) {
|
||||
$query_params = array(
|
||||
'type' => $product_type,
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $product_ids ), count( $response_products ) );
|
||||
foreach ( $response_products as $response_product ) {
|
||||
$this->assertContains( $response_product['id'], $product_ids_for_type[ $product_type ], 'REST API: ' . $product_type . ' not found correctly' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products by featured property.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_featured_products() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Create a featured product.
|
||||
$feat_product = WC_Helper_Product::create_simple_product();
|
||||
$feat_product->set_featured( true );
|
||||
$feat_product->save();
|
||||
|
||||
// Create a non-featured product.
|
||||
$nonfeat_product = WC_Helper_Product::create_simple_product();
|
||||
$nonfeat_product->save();
|
||||
|
||||
$query_params = array(
|
||||
'featured' => 'true',
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
foreach ( $response_products as $response_product ) {
|
||||
$this->assertEquals( $feat_product->get_id(), $response_product['id'], 'REST API: Featured product not found correctly' );
|
||||
}
|
||||
|
||||
$query_params = array(
|
||||
'featured' => 'false',
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
foreach ( $response_products as $response_product ) {
|
||||
$this->assertEquals( $nonfeat_product->get_id(), $response_product['id'], 'REST API: Featured product not found correctly' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products by shipping class property.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_products_by_shipping_class() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$shipping_class_1 = wp_insert_term( 'Bulky', 'product_shipping_class' );
|
||||
|
||||
$product_1 = new WC_Product_Simple();
|
||||
$product_1->set_shipping_class_id( $shipping_class_1['term_id'] );
|
||||
$product_1->save();
|
||||
|
||||
$query_params = array(
|
||||
'shipping_class' => (string) $shipping_class_1['term_id'],
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
foreach ( $response_products as $response_product ) {
|
||||
$this->assertEquals( $product_1->get_id(), $response_product['id'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products by tag.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_products_by_tag() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$test_tag_1 = wp_insert_term( 'Tag 1', 'product_tag' );
|
||||
|
||||
// Product with a tag.
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$product->set_tag_ids( array( $test_tag_1['term_id'] ) );
|
||||
$product->save();
|
||||
|
||||
// Product without a tag.
|
||||
$product_2 = WC_Helper_Product::create_simple_product();
|
||||
|
||||
$query_params = array(
|
||||
'tag' => (string) $test_tag_1['term_id'],
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
foreach ( $response_products as $response_product ) {
|
||||
$this->assertEquals( $product->get_id(), $response_product['id'] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting products by global attribute.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_products_by_attribute() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Variable product with 2 different variations.
|
||||
$variable_product = WC_Helper_Product::create_variation_product();
|
||||
|
||||
// Terms created by variable product.
|
||||
$term_large = get_term_by( 'slug', 'large', 'pa_size' );
|
||||
$term_small = get_term_by( 'slug', 'small', 'pa_size' );
|
||||
|
||||
// Simple product without attribute.
|
||||
$product_1 = WC_Helper_Product::create_simple_product();
|
||||
|
||||
// Simple product with attribute size = large.
|
||||
$product_2 = WC_Helper_Product::create_simple_product();
|
||||
$product_2->set_attributes( array( 'pa_size' => 'large' ) );
|
||||
$product_2->save();
|
||||
|
||||
// Link the product to the term.
|
||||
$wpdb->insert(
|
||||
$wpdb->prefix . 'term_relationships',
|
||||
array(
|
||||
'object_id' => $product_2->get_id(),
|
||||
'term_taxonomy_id' => $term_large->term_id,
|
||||
'term_order' => 0,
|
||||
)
|
||||
);
|
||||
|
||||
// Products with attribute size == large.
|
||||
$expected_product_ids = array(
|
||||
$variable_product->get_id(),
|
||||
$product_2->get_id(),
|
||||
);
|
||||
$query_params = array(
|
||||
'attribute' => 'pa_size',
|
||||
'attribute_term' => (string) $term_large->term_id,
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $expected_product_ids ), count( $response_products ) );
|
||||
foreach ( $response_products as $response_product ) {
|
||||
$this->assertContains( $response_product['id'], $expected_product_ids );
|
||||
}
|
||||
|
||||
// Products with attribute size == small.
|
||||
$expected_product_ids = array(
|
||||
$variable_product->get_id(),
|
||||
);
|
||||
$query_params = array(
|
||||
'attribute' => 'pa_size',
|
||||
'attribute_term' => (string) $term_small->term_id,
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$response_products = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $expected_product_ids ), count( $response_products ) );
|
||||
foreach ( $response_products as $response_product ) {
|
||||
$this->assertContains( $response_product['id'], $expected_product_ids );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the reports coupons totals REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class WC_Tests_API_Reports_Coupons_Totals extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/reports/coupons/totals', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all product reviews.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/coupons/totals' ) );
|
||||
$report = $response->get_data();
|
||||
$types = wc_get_coupon_types();
|
||||
$data = array();
|
||||
|
||||
foreach ( $types as $slug => $name ) {
|
||||
$results = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"
|
||||
SELECT count(meta_id) AS total
|
||||
FROM $wpdb->postmeta
|
||||
WHERE meta_key = 'discount_type'
|
||||
AND meta_value = %s
|
||||
",
|
||||
$slug
|
||||
)
|
||||
);
|
||||
|
||||
$total = isset( $results[0] ) ? (int) $results[0]->total : 0;
|
||||
|
||||
$data[] = array(
|
||||
'slug' => $slug,
|
||||
'name' => $name,
|
||||
'total' => $total,
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $types ), count( $report ) );
|
||||
$this->assertEquals( $data, $report );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure product reviews cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/coupons/totals' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the product review schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_review_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/coupons/totals' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'slug', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'total', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the reports customers totals REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class WC_Tests_API_Reports_Customers_Totals extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/reports/customers/totals', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all product reviews.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/customers/totals' ) );
|
||||
$report = $response->get_data();
|
||||
$users_count = count_users();
|
||||
$total_customers = 0;
|
||||
|
||||
foreach ( $users_count['avail_roles'] as $role => $total ) {
|
||||
if ( in_array( $role, array( 'administrator', 'shop_manager' ), true ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$total_customers += (int) $total;
|
||||
}
|
||||
|
||||
$customers_query = new WP_User_Query(
|
||||
array(
|
||||
'role__not_in' => array( 'administrator', 'shop_manager' ),
|
||||
'number' => 0,
|
||||
'fields' => 'ID',
|
||||
'count_total' => true,
|
||||
'meta_query' => array( // WPCS: slow query ok.
|
||||
array(
|
||||
'key' => 'paying_customer',
|
||||
'value' => 1,
|
||||
'compare' => '=',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$total_paying = (int) $customers_query->get_total();
|
||||
|
||||
$data = array(
|
||||
array(
|
||||
'slug' => 'paying',
|
||||
'name' => __( 'Paying customer', 'woocommerce' ),
|
||||
'total' => $total_paying,
|
||||
),
|
||||
array(
|
||||
'slug' => 'non_paying',
|
||||
'name' => __( 'Non-paying customer', 'woocommerce' ),
|
||||
'total' => $total_customers - $total_paying,
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $report ) );
|
||||
$this->assertEquals( $data, $report );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure product reviews cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/customers/totals' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the product review schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_review_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/customers/totals' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'slug', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'total', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the reports orders totals REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class WC_Tests_API_Reports_Orders_Totals extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/reports/orders/totals', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all product reviews.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/orders/totals' ) );
|
||||
$report = $response->get_data();
|
||||
$totals = wp_count_posts( 'shop_order' );
|
||||
$data = array();
|
||||
|
||||
foreach ( wc_get_order_statuses() as $slug => $name ) {
|
||||
if ( ! isset( $totals->$slug ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data[] = array(
|
||||
'slug' => str_replace( 'wc-', '', $slug ),
|
||||
'name' => $name,
|
||||
'total' => (int) $totals->$slug,
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( wc_get_order_statuses() ), count( $report ) );
|
||||
$this->assertEquals( $data, $report );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure product reviews cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/orders/totals' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the product review schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_review_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/orders/totals' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'slug', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'total', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the reports products totals REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class WC_Tests_API_Reports_Products_Totals extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/reports/products/totals', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all product reviews.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Install::create_terms();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/products/totals' ) );
|
||||
$report = $response->get_data();
|
||||
$types = wc_get_product_types();
|
||||
$terms = get_terms(
|
||||
array(
|
||||
'taxonomy' => 'product_type',
|
||||
'hide_empty' => false,
|
||||
)
|
||||
);
|
||||
$data = array();
|
||||
|
||||
foreach ( $terms as $product_type ) {
|
||||
if ( ! isset( $types[ $product_type->name ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data[] = array(
|
||||
'slug' => $product_type->name,
|
||||
'name' => $types[ $product_type->name ],
|
||||
'total' => (int) $product_type->count,
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $types ), count( $report ) );
|
||||
$this->assertEquals( $data, $report );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure product reviews cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/products/totals' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the product review schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_review_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/products/totals' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'slug', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'total', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the reports reviews totals REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class WC_Tests_API_Reports_Reviews_Totals extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/reports/reviews/totals', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all product reviews.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/reviews/totals' ) );
|
||||
$report = $response->get_data();
|
||||
$data = array();
|
||||
|
||||
$query_data = array(
|
||||
'count' => true,
|
||||
'post_type' => 'product',
|
||||
'meta_key' => 'rating', // WPCS: slow query ok.
|
||||
'meta_value' => '', // WPCS: slow query ok.
|
||||
);
|
||||
|
||||
for ( $i = 1; $i <= 5; $i++ ) {
|
||||
$query_data['meta_value'] = $i;
|
||||
|
||||
$data[] = array(
|
||||
'slug' => 'rated_' . $i . '_out_of_5',
|
||||
/* translators: %s: average rating */
|
||||
'name' => sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $i ),
|
||||
'total' => (int) get_comments( $query_data ),
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), count( $report ) );
|
||||
$this->assertEquals( $data, $report );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure product reviews cannot be viewed without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/reports/reviews/totals' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the product review schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_product_review_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/reports/reviews/totals' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'slug', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'total', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,895 @@
|
|||
<?php
|
||||
/**
|
||||
* Settings API Tests.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class Settings extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Setting_Options_Controller();
|
||||
WC_Helper_Settings::register();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/settings', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/settings/(?P<group_id>[\w-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/settings/(?P<group_id>[\w-]+)/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all groups.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_groups() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'test',
|
||||
'label' => 'Test extension',
|
||||
'parent_id' => '',
|
||||
'description' => 'My awesome test settings.',
|
||||
'sub_groups' => array( 'sub-test' ),
|
||||
'_links' => array(
|
||||
'options' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/settings/test' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'sub-test',
|
||||
'label' => 'Sub test',
|
||||
'parent_id' => 'test',
|
||||
'description' => '',
|
||||
'sub_groups' => array(),
|
||||
'_links' => array(
|
||||
'options' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/settings/sub-test' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /settings without valid permissions/creds.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_groups_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /settings without valid permissions/creds.
|
||||
*
|
||||
* @since 3.5.0
|
||||
* @covers WC_Rest_Settings_Controller::get_items
|
||||
*/
|
||||
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/v3/settings' ) );
|
||||
$this->assertEquals( 500, $response->get_status() );
|
||||
|
||||
WC_Helper_Settings::register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test groups schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_group_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/settings' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 5, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'parent_id', $properties );
|
||||
$this->assertArrayHasKey( 'label', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'sub_groups', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test settings schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_setting_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/settings/test/woocommerce_shop_page_display' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 10, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'label', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'value', $properties );
|
||||
$this->assertArrayHasKey( 'default', $properties );
|
||||
$this->assertArrayHasKey( 'tip', $properties );
|
||||
$this->assertArrayHasKey( 'placeholder', $properties );
|
||||
$this->assertArrayHasKey( 'type', $properties );
|
||||
$this->assertArrayHasKey( 'options', $properties );
|
||||
$this->assertArrayHasKey( 'group_id', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single group.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_group() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test route callback receiving an empty group id
|
||||
$result = $this->endpoint->get_group_settings( '' );
|
||||
$this->assertIsWPError( $result );
|
||||
|
||||
// test getting a group that does not exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/not-real' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting the 'invalid' group
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/invalid' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting a valid group with settings attached to it
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 1, count( $data ) );
|
||||
$this->assertEquals( 'woocommerce_shop_page_display', $data[0]['id'] );
|
||||
$this->assertEmpty( $data[0]['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single group without permission.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_group_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/coupon-data' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single setting.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_setting() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test defaults first
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( '', $data['value'] );
|
||||
|
||||
// test updating shop display setting
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$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' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$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' ) );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$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 multiple settings at once.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test defaults first
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( '', $data[0]['value'] );
|
||||
|
||||
// test setting both at once
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => 'woocommerce_shop_page_display',
|
||||
'value' => 'both',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'both', $data['update'][0]['value'] );
|
||||
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) );
|
||||
|
||||
// test updating one, but making sure the other value stays the same
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => 'woocommerce_shop_page_display',
|
||||
'value' => 'subcategories',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 'subcategories', $data['update'][0]['value'] );
|
||||
$this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single setting.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_setting() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test getting an invalid setting from a group that does not exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/not-real/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting an invalid setting from a group that does exist
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/invalid/invalid' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// test getting a valid setting
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 'woocommerce_shop_page_display', $data['id'] );
|
||||
$this->assertEquals( 'Shop page display', $data['label'] );
|
||||
$this->assertEquals( '', $data['default'] );
|
||||
$this->assertEquals( 'select', $data['type'] );
|
||||
$this->assertEquals( '', $data['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single setting without valid user permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_setting_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the GET single setting route handler receiving an empty setting ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_setting_invalid_setting_type() {
|
||||
// $controller = $this->getMock( 'WC_Rest_Setting_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) );
|
||||
$controller = $this->getMockBuilder( 'WC_Rest_Setting_Options_Controller' )->setMethods( array( 'get_group_settings', 'is_setting_type_valid' ) )->getMock();
|
||||
|
||||
$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_shop_page_display' );
|
||||
|
||||
$this->assertIsWPError( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single setting without valid user permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_setting_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'subcategories',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test updating multiple settings without valid user permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_settings_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => 'woocommerce_shop_page_display',
|
||||
'value' => 'subcategories',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a bad setting ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
* @covers WC_Rest_Setting_Options_Controller::update_item
|
||||
*/
|
||||
public function test_update_setting_bad_setting_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/test/invalid' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'test',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests our classic setting registration to make sure settings added for WP-Admin are available over the API.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_classic_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Make sure the group is properly registered
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/products' ) );
|
||||
$data = $response->get_data();
|
||||
$this->assertTrue( is_array( $data ) );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'woocommerce_downloads_require_login',
|
||||
'label' => 'Access restriction',
|
||||
'description' => 'Downloads require login',
|
||||
'type' => 'checkbox',
|
||||
'default' => 'no',
|
||||
'tip' => 'This setting does not apply to guest purchases.',
|
||||
'value' => 'no',
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/settings/products/woocommerce_downloads_require_login' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/settings/products' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
|
||||
// test get single
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/products/woocommerce_dimension_unit' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'cm', $data['default'] );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_dimension_unit' ) );
|
||||
$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' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests our email etting registration to make sure settings added for WP-Admin are available over the API.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_email_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order' ) );
|
||||
$settings = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'recipient',
|
||||
'label' => 'Recipient(s)',
|
||||
'description' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
|
||||
'type' => 'text',
|
||||
'default' => '',
|
||||
'tip' => 'Enter recipients (comma separated) for this email. Defaults to <code>admin@example.org</code>.',
|
||||
'value' => '',
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/settings/email_new_order/recipient' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/settings/email_new_order' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$settings
|
||||
);
|
||||
|
||||
// test get single
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order/subject' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => 'subject',
|
||||
'label' => 'Subject',
|
||||
'description' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'type' => 'text',
|
||||
'default' => '',
|
||||
'tip' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'value' => '',
|
||||
'group_id' => 'email_new_order',
|
||||
),
|
||||
$setting
|
||||
);
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_new_order', 'subject' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'This is my subject',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => 'subject',
|
||||
'label' => 'Subject',
|
||||
'description' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'type' => 'text',
|
||||
'default' => '',
|
||||
'tip' => 'Available placeholders: <code>{site_title}, {order_date}, {order_number}</code>',
|
||||
'value' => 'This is my subject',
|
||||
'group_id' => 'email_new_order',
|
||||
),
|
||||
$setting
|
||||
);
|
||||
|
||||
// test updating another subject and making sure it works with a "similar" id
|
||||
$request = new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEmpty( $setting['value'] );
|
||||
|
||||
// test update
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_customer_new_account', 'subject' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'This is my new subject',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'This is my new subject', $setting['value'] );
|
||||
|
||||
// make sure the other is what we left it
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order/subject' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'This is my subject', $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of checkbox settings.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_validation_checkbox() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// test bogus value
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'not_yes_or_no',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// test yes
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'yes',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
// test no
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'no',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of radio settings.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_validation_radio() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// not a valid option
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'billing2',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// valid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'billing',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of multiselect.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_validation_multiselect() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEmpty( $setting['value'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => array( 'AX', 'DZ', 'MMM' ),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( array( 'AX', 'DZ' ), $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation of select.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_validation_select() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'kg', $setting['value'] );
|
||||
|
||||
// invalid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'pounds', // invalid, should be lbs
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// valid
|
||||
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => 'lbs', // invalid, should be lbs
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'lbs', $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the 'base location' setting is present in the response.
|
||||
* That it is returned as 'select' and not 'single_select_country',
|
||||
* and that both state and country options are returned.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_woocommerce_default_country() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_default_country' ) );
|
||||
$setting = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'select', $setting['type'] );
|
||||
$this->assertArrayHasKey( 'GB', $setting['options'] );
|
||||
$this->assertArrayHasKey( 'US:OR', $setting['options'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store address setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_woocommerce_store_address() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_address' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $new_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $old_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store address 2 (line 2) setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_woocommerce_store_address_2() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_address_2' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address_2' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $new_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address_2' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $old_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store city setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_woocommerce_store_city() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_city' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_city' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $new_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_city' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $old_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure the store postcode setting can be fetched and updated.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_woocommerce_store_postcode() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_postcode' ) );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( 'text', $setting['type'] );
|
||||
|
||||
// Repalce the old value with something uniquely new
|
||||
$old_value = $setting['value'];
|
||||
$new_value = $old_value . ' ' . rand( 1000, 9999 );
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_postcode' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $new_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $new_value, $setting['value'] );
|
||||
|
||||
// Put the original value back
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_postcode' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'value' => $old_value,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$setting = $response->get_data();
|
||||
$this->assertEquals( $old_value, $setting['value'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,143 @@
|
|||
<?php
|
||||
/**
|
||||
* Tests for the Shipping Methods REST API.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
class Shipping_Methods extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Shipping_Methods_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping_methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping_methods/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all shipping methods.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods' ) );
|
||||
$methods = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'free_shipping',
|
||||
'title' => 'Free shipping',
|
||||
'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.',
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping_methods/free_shipping' ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping_methods' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$methods
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to make sure shipping methods cannot viewed without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_methods_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single shipping method.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/local_pickup' ) );
|
||||
$method = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => 'local_pickup',
|
||||
'title' => 'Local pickup',
|
||||
'description' => 'Allow customers to pick up orders themselves. By default, when using local pickup store base taxes will apply regardless of customer address.',
|
||||
),
|
||||
$method
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a single shipping method without the correct permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_method_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/local_pickup' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests getting a shipping method with an invalid ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_method_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/fake_method' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the shipping method schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_shipping_method_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/shipping_methods' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'title', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,825 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Shipping Zones API Tests
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
|
||||
|
||||
protected $server;
|
||||
|
||||
protected $endpoint;
|
||||
|
||||
protected $user;
|
||||
|
||||
protected $zones;
|
||||
|
||||
/**
|
||||
* Setup our test server, endpoints, and user info.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_Shipping_Zones_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
$this->zones = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to create a Shipping Zone.
|
||||
*
|
||||
* @param string $name Zone name.
|
||||
* @param int $order Optional. Zone sort order.
|
||||
* @return WC_Shipping_Zone
|
||||
*/
|
||||
protected function create_shipping_zone( $name, $order = 0, $locations = array() ) {
|
||||
$zone = new WC_Shipping_Zone( null );
|
||||
$zone->set_zone_name( $name );
|
||||
$zone->set_zone_order( $order );
|
||||
$zone->set_locations( $locations );
|
||||
$zone->save();
|
||||
|
||||
$this->zones[] = $zone;
|
||||
|
||||
return $zone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping/zones', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P<id>[\d-]+)', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P<id>[\d]+)/locations', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P<zone_id>[\d]+)/methods', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/shipping/zones/(?P<zone_id>[\d]+)/methods/(?P<instance_id>[\d]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all Shipping Zones.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_zones() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// "Rest of the World" zone exists by default
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $data[0]['id'],
|
||||
'name' => 'Locations not covered by your other zones',
|
||||
'order' => 0,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[0]['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[0]['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
|
||||
// Create a zone and make sure it's in the response
|
||||
$this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 2 );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => $data[1]['id'],
|
||||
'name' => 'Zone 1',
|
||||
'order' => 0,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[1]['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data[1]['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /shipping/zones without valid permissions/creds.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_zones_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test /shipping/zones while Shipping is disabled in WooCommerce.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_zones_disabled_shipping() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
add_filter( 'wc_shipping_enabled', '__return_false' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
remove_filter( 'wc_shipping_enabled', '__return_false' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_shipping_zone_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/shipping/zones' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertTrue( $properties['id']['readonly'] );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'order', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone create endpoint.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test Zone',
|
||||
'order' => 1,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 201, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $data['id'],
|
||||
'name' => 'Test Zone',
|
||||
'order' => 1,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data['id'] ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $data['id'] . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone create endpoint.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_shipping_zone_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Test Zone',
|
||||
'order' => 1,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone update endpoint.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $zone->get_id(),
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone update endpoint with a bad zone ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/555555' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'name' => 'Zone Test',
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint without permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_shipping_zone_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/' . $zone->get_id() );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone delete endpoint with a bad zone ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/555555' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single Shipping Zone.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_single_shipping_zone() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'id' => $zone->get_id(),
|
||||
'name' => 'Test Zone',
|
||||
'order' => 0,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones' ),
|
||||
),
|
||||
),
|
||||
'describedby' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single Shipping Zone with a bad zone ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_single_shipping_zone_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting Shipping Zone Locations.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_locations() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Create a zone
|
||||
$zone = $this->create_shipping_zone(
|
||||
'Zone 1',
|
||||
0,
|
||||
array(
|
||||
array(
|
||||
'code' => 'US',
|
||||
'type' => 'country',
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
array(
|
||||
'code' => 'US',
|
||||
'type' => 'country',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting Shipping Zone Locations with a bad zone ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_locations_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1/locations' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Shipping Zone Locations update endpoint.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_locations() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Test Zone' );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' );
|
||||
$request->add_header( 'Content-Type', 'application/json' );
|
||||
$request->set_body(
|
||||
json_encode(
|
||||
array(
|
||||
array(
|
||||
'code' => 'UK',
|
||||
'type' => 'country',
|
||||
),
|
||||
array(
|
||||
'code' => 'US', // test that locations missing "type" treated as country.
|
||||
),
|
||||
array(
|
||||
'code' => 'SW1A0AA',
|
||||
'type' => 'postcode',
|
||||
),
|
||||
array(
|
||||
'type' => 'continent', // test that locations missing "code" aren't saved
|
||||
),
|
||||
)
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $data ) );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
array(
|
||||
'code' => 'UK',
|
||||
'type' => 'country',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'code' => 'US',
|
||||
'type' => 'country',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'code' => 'SW1A0AA',
|
||||
'type' => 'postcode',
|
||||
'_links' => array(
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/locations' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating Shipping Zone Locations with a bad zone ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_locations_invalid_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v3/shipping/zones/1/locations' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all Shipping Zone Methods and getting a single Shipping Zone Method.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Create a shipping method and make sure it's in the response
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$methods = $zone->get_shipping_methods();
|
||||
$method = $methods[ $instance_id ];
|
||||
|
||||
$settings = array();
|
||||
$method->init_instance_settings();
|
||||
foreach ( $method->get_instance_form_fields() as $id => $field ) {
|
||||
$data = array(
|
||||
'id' => $id,
|
||||
'label' => $field['title'],
|
||||
'description' => ( empty( $field['description'] ) ? '' : $field['description'] ),
|
||||
'type' => $field['type'],
|
||||
'value' => $method->instance_settings[ $id ],
|
||||
'default' => ( empty( $field['default'] ) ? '' : $field['default'] ),
|
||||
'tip' => ( empty( $field['description'] ) ? '' : $field['description'] ),
|
||||
'placeholder' => ( empty( $field['placeholder'] ) ? '' : $field['placeholder'] ),
|
||||
);
|
||||
if ( ! empty( $field['options'] ) ) {
|
||||
$data['options'] = $field['options'];
|
||||
}
|
||||
$settings[ $id ] = $data;
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods' ) );
|
||||
$data = $response->get_data();
|
||||
$expected = array(
|
||||
'id' => $instance_id,
|
||||
'instance_id' => $instance_id,
|
||||
'title' => $method->instance_settings['title'],
|
||||
'order' => $method->method_order,
|
||||
'enabled' => ( 'yes' === $method->enabled ),
|
||||
'method_id' => $method->id,
|
||||
'method_title' => $method->method_title,
|
||||
'method_description' => $method->method_description,
|
||||
'settings' => $settings,
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ),
|
||||
),
|
||||
),
|
||||
'collection' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods' ),
|
||||
),
|
||||
),
|
||||
'describes' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/shipping/zones/' . $zone->get_id() ),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $data ), 1 );
|
||||
$this->assertContains( $expected, $data );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $expected, $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting all Shipping Zone Methods with a bad zone ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_methods_invalid_zone_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1/methods' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/1/methods/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single Shipping Zone Method with a bad ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_methods_invalid_method_id() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/1' ) );
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a Shipping Zone Method.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_methods() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$methods = $zone->get_shipping_methods();
|
||||
$method = $methods[ $instance_id ];
|
||||
|
||||
// Test defaults
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'title', $data['settings'] );
|
||||
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
||||
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
||||
$this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '0', $data['settings']['cost']['value'] );
|
||||
|
||||
// Update a single value
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'cost' => 5,
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'title', $data['settings'] );
|
||||
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
||||
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
||||
$this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '5', $data['settings']['cost']['value'] );
|
||||
|
||||
// Test multiple settings
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'cost' => 10,
|
||||
'tax_status' => 'none',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'title', $data['settings'] );
|
||||
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
||||
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
||||
$this->assertEquals( 'none', $data['settings']['tax_status']['value'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '10', $data['settings']['cost']['value'] );
|
||||
|
||||
// Test bogus
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'settings' => array(
|
||||
'cost' => 10,
|
||||
'tax_status' => 'this_is_not_a_valid_option',
|
||||
),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 400, $response->get_status() );
|
||||
|
||||
// Test other parameters
|
||||
$this->assertTrue( $data['enabled'] );
|
||||
$this->assertEquals( 1, $data['order'] );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertFalse( $data['enabled'] );
|
||||
$this->assertEquals( 2, $data['order'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '10', $data['settings']['cost']['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a Shipping Zone Method.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$request = new WP_REST_Request( 'POST', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'enabled' => false,
|
||||
'order' => 2,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertFalse( $data['enabled'] );
|
||||
$this->assertEquals( 2, $data['order'] );
|
||||
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
||||
$this->assertEquals( '0', $data['settings']['cost']['value'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test deleting a Shipping Zone Method.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_delete_method() {
|
||||
wp_set_current_user( $this->user );
|
||||
$zone = $this->create_shipping_zone( 'Zone 1' );
|
||||
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
||||
$methods = $zone->get_shipping_methods();
|
||||
$method = $methods[ $instance_id ];
|
||||
$request = new WP_REST_Request( 'DELETE', '/wc/v3/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
||||
$request->set_param( 'force', true );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,467 @@
|
|||
<?php
|
||||
/**
|
||||
* Class WC_Tests_REST_System_Status file.
|
||||
*
|
||||
* @package WooCommerce/Tests
|
||||
*/
|
||||
|
||||
/**
|
||||
* System Status REST Tests.
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_REST_System_Status extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Setup our test server.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->endpoint = new WC_REST_System_Status_Controller();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
|
||||
// Callback used by WP_HTTP_TestCase to decide whether to perform HTTP requests or to provide a mocked response.
|
||||
$this->http_responder = array( $this, 'mock_http_responses' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
$this->assertArrayHasKey( '/wc/v3/system_status', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/system_status/tools', $routes );
|
||||
$this->assertArrayHasKey( '/wc/v3/system_status/tools/(?P<id>[\w-]+)', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure system status cannot be accessed without valid creds
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure root properties are present.
|
||||
* (environment, theme, database, etc).
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_returns_root_properties() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertArrayHasKey( 'environment', $data );
|
||||
$this->assertArrayHasKey( 'database', $data );
|
||||
$this->assertArrayHasKey( 'active_plugins', $data );
|
||||
$this->assertArrayHasKey( 'theme', $data );
|
||||
$this->assertArrayHasKey( 'settings', $data );
|
||||
$this->assertArrayHasKey( 'security', $data );
|
||||
$this->assertArrayHasKey( 'pages', $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure environment response is correct.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_environment() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$environment = (array) $data['environment'];
|
||||
|
||||
// Make sure all expected data is present.
|
||||
$this->assertEquals( 32, count( $environment ) );
|
||||
|
||||
// Test some responses to make sure they match up.
|
||||
$this->assertEquals( get_option( 'home' ), $environment['home_url'] );
|
||||
$this->assertEquals( get_option( 'siteurl' ), $environment['site_url'] );
|
||||
$this->assertEquals( WC()->version, $environment['version'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure database response is correct.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_database() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$database = (array) $data['database'];
|
||||
|
||||
$this->assertEquals( get_option( 'woocommerce_db_version' ), $database['wc_database_version'] );
|
||||
$this->assertEquals( $wpdb->prefix, $database['database_prefix'] );
|
||||
$this->assertEquals( WC_Geolocation::get_local_database_path(), $database['maxmind_geoip_database'] );
|
||||
$this->assertArrayHasKey( 'woocommerce', $database['database_tables'], wc_print_r( $database, true ) );
|
||||
$this->assertArrayHasKey( $wpdb->prefix . 'woocommerce_payment_tokens', $database['database_tables']['woocommerce'], wc_print_r( $database, true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure active plugins response is correct.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_active_plugins() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$actual_plugins = array( 'hello.php' );
|
||||
update_option( 'active_plugins', $actual_plugins );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
update_option( 'active_plugins', array() );
|
||||
|
||||
$data = $response->get_data();
|
||||
$plugins = (array) $data['active_plugins'];
|
||||
|
||||
$this->assertEquals( 1, count( $plugins ) );
|
||||
$this->assertEquals( 'Hello Dolly', $plugins[0]['name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure theme response is correct.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_theme() {
|
||||
wp_set_current_user( $this->user );
|
||||
$active_theme = wp_get_theme();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$theme = (array) $data['theme'];
|
||||
|
||||
$this->assertEquals( 13, count( $theme ) );
|
||||
$this->assertEquals( $active_theme->Name, $theme['name'] ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure settings response is correct.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_settings() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$term_response = array();
|
||||
$terms = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
|
||||
foreach ( $terms as $term ) {
|
||||
$term_response[ $term->slug ] = strtolower( $term->name );
|
||||
}
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$settings = (array) $data['settings'];
|
||||
|
||||
$this->assertEquals( 12, count( $settings ) );
|
||||
$this->assertEquals( ( 'yes' === get_option( 'woocommerce_api_enabled' ) ), $settings['api_enabled'] );
|
||||
$this->assertEquals( get_woocommerce_currency(), $settings['currency'] );
|
||||
$this->assertEquals( $term_response, $settings['taxonomies'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure security response is correct.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_security() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$settings = (array) $data['security'];
|
||||
|
||||
$this->assertEquals( 2, count( $settings ) );
|
||||
$this->assertEquals( 'https' === substr( wc_get_page_permalink( 'shop' ), 0, 5 ), $settings['secure_connection'] );
|
||||
$this->assertEquals( ! ( defined( 'WP_DEBUG' ) && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG && WP_DEBUG_DISPLAY ) || 0 === intval( ini_get( 'display_errors' ) ), $settings['hide_errors'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure pages response is correct.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_info_pages() {
|
||||
wp_set_current_user( $this->user );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status' ) );
|
||||
$data = $response->get_data();
|
||||
$pages = $data['pages'];
|
||||
$this->assertEquals( 5, count( $pages ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test system status schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_system_status_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/system_status' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
$this->assertEquals( 9, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'environment', $properties );
|
||||
$this->assertArrayHasKey( 'database', $properties );
|
||||
$this->assertArrayHasKey( 'active_plugins', $properties );
|
||||
$this->assertArrayHasKey( 'theme', $properties );
|
||||
$this->assertArrayHasKey( 'settings', $properties );
|
||||
$this->assertArrayHasKey( 'security', $properties );
|
||||
$this->assertArrayHasKey( 'pages', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure get_items (all tools) response is correct.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_tools() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$tools_controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$raw_tools = $tools_controller->get_tools();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $raw_tools ), count( $data ) );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'regenerate_thumbnails',
|
||||
'name' => 'Regenerate shop thumbnails',
|
||||
'action' => 'Regenerate',
|
||||
'description' => 'This will regenerate all shop thumbnails to match your theme and/or image settings.',
|
||||
'_links' => array(
|
||||
'item' => array(
|
||||
array(
|
||||
'href' => rest_url( '/wc/v3/system_status/tools/regenerate_thumbnails' ),
|
||||
'embeddable' => 1,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
$data
|
||||
);
|
||||
|
||||
$query_params = array(
|
||||
'_fields' => 'id,name,nonexisting',
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( count( $raw_tools ), count( $data ) );
|
||||
$this->assertContains(
|
||||
array(
|
||||
'id' => 'regenerate_thumbnails',
|
||||
'name' => 'Regenerate shop thumbnails',
|
||||
),
|
||||
$data
|
||||
);
|
||||
foreach ( $data as $item ) {
|
||||
// Fields that are not requested are not returned in response.
|
||||
$this->assertArrayNotHasKey( 'action', $item );
|
||||
$this->assertArrayNotHasKey( 'description', $item );
|
||||
// Links are part of data in collections, so excluded if not explicitly requested.
|
||||
$this->assertArrayNotHasKey( '_links', $item );
|
||||
// Non existing field is ignored.
|
||||
$this->assertArrayNotHasKey( 'nonexisting', $item );
|
||||
}
|
||||
|
||||
// Links are part of data, not links in collections.
|
||||
$links = $response->get_links();
|
||||
$this->assertEquals( 0, count( $links ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure system status tools cannot be accessed without valid creds
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_tools_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure we can load a single tool correctly.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_tool() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$tools_controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$raw_tools = $tools_controller->get_tools();
|
||||
$raw_tool = $raw_tools['recount_terms'];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools/recount_terms' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 'recount_terms', $data['id'] );
|
||||
$this->assertEquals( 'Term counts', $data['name'] );
|
||||
$this->assertEquals( 'Recount terms', $data['action'] );
|
||||
$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
|
||||
|
||||
// Test for _fields query parameter.
|
||||
$query_params = array(
|
||||
'_fields' => 'id,name,nonexisting',
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v3/system_status/tools/recount_terms' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 'recount_terms', $data['id'] );
|
||||
$this->assertEquals( 'Term counts', $data['name'] );
|
||||
$this->assertArrayNotHasKey( 'action', $data );
|
||||
$this->assertArrayNotHasKey( 'description', $data );
|
||||
// Links are part of links, not data in single items.
|
||||
$this->assertArrayNotHasKey( '_links', $data );
|
||||
|
||||
// Links are part of links, not data in single item response.
|
||||
$links = $response->get_links();
|
||||
$this->assertEquals( 1, count( $links ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure a single system status toolscannot be accessed without valid creds.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_system_status_tool_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/system_status/tools/recount_terms' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure we can RUN a tool correctly.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_execute_system_tool() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$tools_controller = new WC_REST_System_Status_Tools_Controller();
|
||||
$raw_tools = $tools_controller->get_tools();
|
||||
$raw_tool = $raw_tools['recount_terms'];
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/recount_terms' ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 'recount_terms', $data['id'] );
|
||||
$this->assertEquals( 'Term counts', $data['name'] );
|
||||
$this->assertEquals( 'Recount terms', $data['action'] );
|
||||
$this->assertEquals( 'This tool will recount product terms - useful when changing your settings in a way which hides products from the catalog.', $data['description'] );
|
||||
$this->assertTrue( $data['success'] );
|
||||
$this->assertEquals( 1, did_action( 'woocommerce_rest_insert_system_status_tool' ) );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/not_a_real_tool' ) );
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
|
||||
// Test _fields for execute system tool request.
|
||||
$query_params = array(
|
||||
'_fields' => 'id,success,nonexisting',
|
||||
);
|
||||
$request = new WP_REST_Request( 'PUT', '/wc/v3/system_status/tools/recount_terms' );
|
||||
$request->set_query_params( $query_params );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'recount_terms', $data['id'] );
|
||||
$this->assertTrue( $data['success'] );
|
||||
|
||||
// Fields that are not requested are not returned in response.
|
||||
$this->assertArrayNotHasKey( 'action', $data );
|
||||
$this->assertArrayNotHasKey( 'name', $data );
|
||||
$this->assertArrayNotHasKey( 'description', $data );
|
||||
// Links are part of links, not data in single item response.
|
||||
$this->assertArrayNotHasKey( '_links', $data );
|
||||
// Non existing field is ignored.
|
||||
$this->assertArrayNotHasKey( 'nonexisting', $data );
|
||||
|
||||
// Links are part of links, not data in single item response.
|
||||
$links = $response->get_links();
|
||||
$this->assertEquals( 1, count( $links ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to make sure a tool cannot be run without valid creds.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_execute_system_status_tool_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', '/wc/v3/system_status/tools/recount_terms' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test system status schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_system_status_tool_schema() {
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/system_status/tools' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 6, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'action', $properties );
|
||||
$this->assertArrayHasKey( 'description', $properties );
|
||||
$this->assertArrayHasKey( 'success', $properties );
|
||||
$this->assertArrayHasKey( 'message', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a mocked response for external requests performed by WC_REST_System_Status_Controller.
|
||||
* This way it is not necessary to perform a regular request to an external server which would
|
||||
* significantly slow down the tests.
|
||||
*
|
||||
* This function is called by WP_HTTP_TestCase::http_request_listner().
|
||||
*
|
||||
* @param array $request Request arguments.
|
||||
* @param string $url URL of the request.
|
||||
*
|
||||
* @return array|false mocked response or false to let WP perform a regular request.
|
||||
*/
|
||||
protected function mock_http_responses( $request, $url ) {
|
||||
$mocked_response = false;
|
||||
|
||||
if ( in_array( $url, array( 'https://www.paypal.com/cgi-bin/webscr', 'https://woocommerce.com/wc-api/product-key-api?request=ping&network=0' ), true ) ) {
|
||||
$mocked_response = array(
|
||||
'response' => array( 'code' => 200 ),
|
||||
);
|
||||
} elseif ( 'https://api.wordpress.org/themes/info/1.0/' === $url ) {
|
||||
$mocked_response = array(
|
||||
'body' => 'O:8:"stdClass":12:{s:4:"name";s:7:"Default";s:4:"slug";s:7:"default";s:7:"version";s:5:"1.7.2";s:11:"preview_url";s:29:"https://wp-themes.com/default";s:6:"author";s:15:"wordpressdotorg";s:14:"screenshot_url";s:61:"//ts.w.org/wp-content/themes/default/screenshot.png?ver=1.7.2";s:6:"rating";d:100;s:11:"num_ratings";s:1:"3";s:10:"downloaded";i:296618;s:12:"last_updated";s:10:"2010-06-14";s:8:"homepage";s:37:"https://wordpress.org/themes/default/";s:13:"download_link";s:55:"https://downloads.wordpress.org/theme/default.1.7.2.zip";}',
|
||||
'response' => array( 'code' => 200 ),
|
||||
);
|
||||
}
|
||||
|
||||
return $mocked_response;
|
||||
}
|
||||
}
|
|
@ -31,140 +31,232 @@ class Coupons extends AbstractRestApiTest {
|
|||
/**
|
||||
* The endpoint schema.
|
||||
*
|
||||
* @var array
|
||||
* @var array Keys are property names, values are supported context.
|
||||
*/
|
||||
protected $properties = [
|
||||
'id',
|
||||
'code',
|
||||
'amount',
|
||||
'date_created',
|
||||
'date_created_gmt',
|
||||
'date_modified',
|
||||
'date_modified_gmt',
|
||||
'discount_type',
|
||||
'description',
|
||||
'date_expires',
|
||||
'date_expires_gmt',
|
||||
'usage_count',
|
||||
'individual_use',
|
||||
'product_ids',
|
||||
'excluded_product_ids',
|
||||
'usage_limit',
|
||||
'usage_limit_per_user',
|
||||
'limit_usage_to_x_items',
|
||||
'free_shipping',
|
||||
'product_categories',
|
||||
'excluded_product_categories',
|
||||
'exclude_sale_items',
|
||||
'minimum_amount',
|
||||
'maximum_amount',
|
||||
'email_restrictions',
|
||||
'used_by',
|
||||
'meta_data',
|
||||
'id' => array( 'view', 'edit' ),
|
||||
'code' => array( 'view', 'edit' ),
|
||||
'amount' => array( 'view', 'edit' ),
|
||||
'date_created' => array( 'view', 'edit' ),
|
||||
'date_created_gmt' => array( 'view', 'edit' ),
|
||||
'date_modified' => array( 'view', 'edit' ),
|
||||
'date_modified_gmt' => array( 'view', 'edit' ),
|
||||
'discount_type' => array( 'view', 'edit' ),
|
||||
'description' => array( 'view', 'edit' ),
|
||||
'date_expires' => array( 'view', 'edit' ),
|
||||
'date_expires_gmt' => array( 'view', 'edit' ),
|
||||
'usage_count' => array( 'view', 'edit' ),
|
||||
'individual_use' => array( 'view', 'edit' ),
|
||||
'product_ids' => array( 'view', 'edit' ),
|
||||
'excluded_product_ids' => array( 'view', 'edit' ),
|
||||
'usage_limit' => array( 'view', 'edit' ),
|
||||
'usage_limit_per_user' => array( 'view', 'edit' ),
|
||||
'limit_usage_to_x_items' => array( 'view', 'edit' ),
|
||||
'free_shipping' => array( 'view', 'edit' ),
|
||||
'product_categories' => array( 'view', 'edit' ),
|
||||
'excluded_product_categories' => array( 'view', 'edit' ),
|
||||
'exclude_sale_items' => array( 'view', 'edit' ),
|
||||
'minimum_amount' => array( 'view', 'edit' ),
|
||||
'maximum_amount' => array( 'view', 'edit' ),
|
||||
'email_restrictions' => array( 'view', 'edit' ),
|
||||
'used_by' => array( 'view', 'edit' ),
|
||||
'meta_data' => array( 'view', 'edit' ),
|
||||
];
|
||||
|
||||
/**
|
||||
* Test delete.
|
||||
*
|
||||
* @return void
|
||||
* Test create.
|
||||
*/
|
||||
public function test_delete() {
|
||||
$coupon = \WC_Helper_Coupon::create_coupon( 'testcoupon-1' );
|
||||
$result = $this->do_request(
|
||||
'/wc/v4/coupons/' . $coupon->get_id(),
|
||||
'DELETE',
|
||||
[ 'force' => false ]
|
||||
);
|
||||
$this->assertEquals( 200, $result->status );
|
||||
$this->assertEquals( 'trash', get_post_status( $coupon->get_id() ) );
|
||||
public function test_create() {
|
||||
$valid_data = [
|
||||
'code' => 'test-coupon',
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
'description' => 'Test description.',
|
||||
'date_expires' => date( 'Y-m-d\T00:00:00', strtotime( '+1 day' ) ),
|
||||
'individual_use' => true,
|
||||
'product_ids' => [ 1, 2, 3 ],
|
||||
'excluded_product_ids' => [ 3, 4, 5 ],
|
||||
'usage_limit' => 10,
|
||||
'usage_limit_per_user' => 10,
|
||||
'limit_usage_to_x_items' => 10,
|
||||
'free_shipping' => false,
|
||||
'product_categories' => [ 1, 2, 3 ],
|
||||
'excluded_product_categories' => [ 3, 4, 5 ],
|
||||
'exclude_sale_items' => true,
|
||||
'minimum_amount' => '100',
|
||||
'maximum_amount' => '200',
|
||||
'email_restrictions' => [ 'test@test.com' ],
|
||||
'meta_data' => [
|
||||
[
|
||||
'key' => 'test_key',
|
||||
'value' => 'test_value',
|
||||
]
|
||||
]
|
||||
];
|
||||
$response = $this->do_request( '/wc/v4/coupons', 'POST', $valid_data );
|
||||
$this->assertExpectedResponse( $response, 201, $valid_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test force delete.
|
||||
*
|
||||
* @return void
|
||||
* Test read.
|
||||
*/
|
||||
public function test_force_delete() {
|
||||
$coupon = \WC_Helper_Coupon::create_coupon( 'testcoupon-1' );
|
||||
$result = $this->do_request(
|
||||
public function test_read() {
|
||||
$coupon1 = \WC_Helper_Coupon::create_coupon( 'testcoupon-1' );
|
||||
$coupon2 = \WC_Helper_Coupon::create_coupon( 'testcoupon-2' );
|
||||
$coupon3 = \WC_Helper_Coupon::create_coupon( 'anothertestcoupon-3' );
|
||||
$coupon4 = \WC_Helper_Coupon::create_coupon( 'anothertestcoupon-4' );
|
||||
|
||||
// Collection.
|
||||
$response = $this->do_request( '/wc/v4/coupons', 'GET' );
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
$this->assertEquals( 4, count( $response->data ) );
|
||||
|
||||
// Collection args.
|
||||
$response = $this->do_request( '/wc/v4/coupons', 'GET', [ 'code' => 'testcoupon-1' ] );
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
$this->assertEquals( 1, count( $response->data ) );
|
||||
|
||||
$response = $this->do_request( '/wc/v4/coupons', 'GET', [ 'search' => 'anothertestcoupon' ] );
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
$this->assertEquals( 2, count( $response->data ) );
|
||||
|
||||
// Single.
|
||||
$response = $this->do_request( '/wc/v4/coupons/' . $coupon1->get_id(), 'GET' );
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
|
||||
foreach ( $this->get_properties( 'view' ) as $property ) {
|
||||
$this->assertArrayHasKey( $property, $response->data );
|
||||
}
|
||||
|
||||
// Invalid.
|
||||
$response = $this->do_request( '/wc/v4/coupons/0', 'GET' );
|
||||
$this->assertExpectedResponse( $response, 404 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update.
|
||||
*/
|
||||
public function test_update() {
|
||||
// Invalid.
|
||||
$response = $this->do_request( '/wc/v4/coupons/0', 'POST', [ 'code' => 'test' ] );
|
||||
$this->assertExpectedResponse( $response, 404 );
|
||||
|
||||
// Update existing.
|
||||
$coupon = \WC_Helper_Coupon::create_coupon( 'testcoupon-1' );
|
||||
$response = $this->do_request(
|
||||
'/wc/v4/coupons/' . $coupon->get_id(),
|
||||
'DELETE',
|
||||
[ 'force' => true ]
|
||||
'POST',
|
||||
[
|
||||
'code' => 'new-code',
|
||||
'description' => 'new description',
|
||||
]
|
||||
);
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
|
||||
foreach ( $this->get_properties( 'view' ) as $property ) {
|
||||
$this->assertArrayHasKey( $property, $response->data );
|
||||
}
|
||||
|
||||
$this->assertEquals( $coupon->get_id(), $response->data['id'] );
|
||||
$this->assertEquals( 'new-code', $response->data['code'] );
|
||||
$this->assertEquals( 'new description', $response->data['description'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete.
|
||||
*/
|
||||
public function test_delete() {
|
||||
// Invalid.
|
||||
$result = $this->do_request( '/wc/v4/coupons/0', 'DELETE', [ 'force' => false ] );
|
||||
$this->assertEquals( 404, $result->status );
|
||||
|
||||
// Trash.
|
||||
$coupon = \WC_Helper_Coupon::create_coupon( 'testcoupon-1' );
|
||||
|
||||
$result = $this->do_request( '/wc/v4/coupons/' . $coupon->get_id(), 'DELETE', [ 'force' => false ] );
|
||||
$this->assertEquals( 200, $result->status );
|
||||
$this->assertEquals( 'trash', get_post_status( $coupon->get_id() ) );
|
||||
|
||||
// Force.
|
||||
$coupon = \WC_Helper_Coupon::create_coupon( 'testcoupon-2' );
|
||||
|
||||
$result = $this->do_request( '/wc/v4/coupons/' . $coupon->get_id(), 'DELETE', [ 'force' => true ] );
|
||||
$this->assertEquals( 200, $result->status );
|
||||
$this->assertEquals( false, get_post( $coupon->get_id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete.
|
||||
*
|
||||
* @return void
|
||||
* Test read.
|
||||
*/
|
||||
public function test_delete_without_permission() {
|
||||
$coupon = \WC_Helper_Coupon::create_coupon( 'testcoupon-1' );
|
||||
$result = $this->do_request(
|
||||
public function test_guest_create() {
|
||||
parent::test_guest_create();
|
||||
|
||||
$valid_data = [
|
||||
'code' => 'test-coupon',
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
'description' => 'Test description.',
|
||||
'date_expires' => date( 'Y-m-d\T00:00:00', strtotime( '+1 day' ) ),
|
||||
'individual_use' => true,
|
||||
'product_ids' => [ 1, 2, 3 ],
|
||||
'excluded_product_ids' => [ 3, 4, 5 ],
|
||||
'usage_limit' => 10,
|
||||
'usage_limit_per_user' => 10,
|
||||
'limit_usage_to_x_items' => 10,
|
||||
'free_shipping' => false,
|
||||
'product_categories' => [ 1, 2, 3 ],
|
||||
'excluded_product_categories' => [ 3, 4, 5 ],
|
||||
'exclude_sale_items' => true,
|
||||
'minimum_amount' => '100',
|
||||
'maximum_amount' => '200',
|
||||
'email_restrictions' => [ 'test@test.com' ],
|
||||
'meta_data' => [
|
||||
[
|
||||
'key' => 'test_key',
|
||||
'value' => 'test_value',
|
||||
]
|
||||
]
|
||||
];
|
||||
$response = $this->do_request( '/wc/v4/coupons', 'POST', $valid_data );
|
||||
$this->assertExpectedResponse( $response, 401 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test read.
|
||||
*/
|
||||
public function test_guest_read() {
|
||||
parent::test_guest_read();
|
||||
|
||||
$response = $this->do_request( '/wc/v4/coupons', 'GET' );
|
||||
$this->assertExpectedResponse( $response, 401 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update.
|
||||
*/
|
||||
public function test_guest_update() {
|
||||
parent::test_guest_update();
|
||||
|
||||
$coupon = \WC_Helper_Coupon::create_coupon( 'testcoupon-1' );
|
||||
$response = $this->do_request(
|
||||
'/wc/v4/coupons/' . $coupon->get_id(),
|
||||
'DELETE',
|
||||
[ 'force' => false ],
|
||||
false
|
||||
);
|
||||
$this->assertEquals( 401, $result->status );
|
||||
$this->assertNotEquals( 'trash', get_post_status( $coupon->get_id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function test_delete_non_existing() {
|
||||
$result = $this->do_request(
|
||||
'/wc/v4/coupons/0',
|
||||
'DELETE',
|
||||
[ 'force' => false ]
|
||||
);
|
||||
$this->assertEquals( 404, $result->status );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creation.
|
||||
*/
|
||||
public function test_create() {
|
||||
$result = $this->do_request(
|
||||
'/wc/v4/coupons',
|
||||
'POST',
|
||||
[
|
||||
'code' => 'test',
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
'description' => 'Test',
|
||||
'usage_limit' => 10,
|
||||
'code' => 'new-code',
|
||||
'description' => 'new description',
|
||||
]
|
||||
);
|
||||
$this->assertEquals( 201, $result->status );
|
||||
$this->assertEquals( 'test', $result->data['code'] );
|
||||
$this->assertEquals( '5.00', $result->data['amount'] );
|
||||
$this->assertEquals( 'fixed_product', $result->data['discount_type'] );
|
||||
$this->assertEquals( 'Test', $result->data['description'] );
|
||||
$this->assertEquals( 10, $result->data['usage_limit'] );
|
||||
$this->assertExpectedResponse( $response, 401 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creation.
|
||||
* Test delete.
|
||||
*/
|
||||
public function test_create_without_permission() {
|
||||
$result = $this->do_request(
|
||||
'/wc/v4/coupons',
|
||||
'POST',
|
||||
[
|
||||
'code' => 'test',
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fixed_product',
|
||||
'description' => 'Test',
|
||||
'usage_limit' => 10,
|
||||
],
|
||||
false
|
||||
);
|
||||
public function test_guest_delete() {
|
||||
parent::test_guest_delete();
|
||||
|
||||
$coupon = \WC_Helper_Coupon::create_coupon( 'testcoupon-1' );
|
||||
$result = $this->do_request( '/wc/v4/coupons/' . $coupon->get_id(), 'DELETE', [ 'force' => false ] );
|
||||
$this->assertEquals( 401, $result->status );
|
||||
}
|
||||
|
||||
|
@ -179,8 +271,6 @@ class Coupons extends AbstractRestApiTest {
|
|||
'code' => 'test',
|
||||
'amount' => '5.00',
|
||||
'discount_type' => 'fake',
|
||||
'description' => 'Test',
|
||||
'usage_limit' => 10,
|
||||
]
|
||||
);
|
||||
|
||||
|
@ -223,12 +313,10 @@ class Coupons extends AbstractRestApiTest {
|
|||
$this->assertEquals( '5.15', $result->data['update'][0]['amount'] );
|
||||
$this->assertEquals( '11.00', $result->data['create'][0]['amount'] );
|
||||
$this->assertEquals( 'new-coupon', $result->data['create'][0]['code'] );
|
||||
$this->assertEquals( $coupon_2->get_id(), $result->data['delete'][0]['id'] );
|
||||
$this->assertEquals( $coupon_3->get_id(), $result->data['delete'][1]['id'] );
|
||||
$this->assertEquals( $coupon_2->get_id(), $result->data['delete'][0]['previous']['id'] );
|
||||
$this->assertEquals( $coupon_3->get_id(), $result->data['delete'][1]['previous']['id'] );
|
||||
|
||||
$result = $this->do_request(
|
||||
'/wc/v4/coupons'
|
||||
);
|
||||
$result = $this->do_request( '/wc/v4/coupons' );
|
||||
$this->assertEquals( 3, count( $result->data ) );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
/**
|
||||
* Onboarding Levels REST API Test
|
||||
*
|
||||
* @package WooCommerce Admin\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC Tests API Onboarding Levels
|
||||
*/
|
||||
class WC_Tests_API_Onboarding_Levels extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc-admin/v1/onboarding/levels';
|
||||
|
||||
/**
|
||||
* Setup test data. Called before every test.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that levels are returned by the endpoint.
|
||||
*/
|
||||
public function test_get_level_items() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'account', $data[0]['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertCount( 2, $properties );
|
||||
$this->assert_item_schema( $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the item schema is correct.
|
||||
*
|
||||
* @param array $schema Item to check schema.
|
||||
*/
|
||||
public function assert_item_schema( $schema ) {
|
||||
$this->assertArrayHasKey( 'id', $schema );
|
||||
$this->assertArrayHasKey( 'tasks', $schema );
|
||||
|
||||
$task_properties = $schema['tasks']['items']['properties'];
|
||||
$this->assertCount( 5, $task_properties );
|
||||
$this->assertArrayHasKey( 'id', $task_properties );
|
||||
$this->assertArrayHasKey( 'label', $task_properties );
|
||||
$this->assertArrayHasKey( 'description', $task_properties );
|
||||
$this->assertArrayHasKey( 'illustration', $task_properties );
|
||||
$this->assertArrayHasKey( 'status', $task_properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that levels response changes based on applied filters.
|
||||
*/
|
||||
public function test_filter_levels() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
add_filter(
|
||||
'woocommerce_onboarding_levels',
|
||||
function( $levels ) {
|
||||
$levels['test_level'] = array(
|
||||
'id' => 'test_level',
|
||||
'tasks' => array(),
|
||||
);
|
||||
return $levels;
|
||||
}
|
||||
);
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'test_level', end( $data )['id'] );
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
<?php
|
||||
/**
|
||||
* Onboarding Profile REST API Test
|
||||
*
|
||||
* @package WooCommerce Admin\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC Tests API Onboarding Profile
|
||||
*/
|
||||
class WC_Tests_API_Onboarding_Profiles extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc-admin/v1/onboarding/profile';
|
||||
|
||||
/**
|
||||
* Setup test data. Called before every test.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that profile data is returned by the endpoint.
|
||||
*/
|
||||
public function test_get_profile_items() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$properties = WC_Admin_REST_Onboarding_Profile_Controller::get_profile_properties();
|
||||
foreach ( $properties as $key => $property ) {
|
||||
$this->assertArrayHasKey( $key, $properties );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that profile date is updated by the endpoint.
|
||||
*/
|
||||
public function test_update_profile_items() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Test updating 2 fields separately.
|
||||
$request = new WP_REST_Request( 'POST', $this->endpoint );
|
||||
$request->set_headers( array( 'content-type' => 'application/json' ) );
|
||||
$request->set_body( wp_json_encode( array( 'industry' => 'health-beauty' ) ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'success', $data['status'] );
|
||||
|
||||
// Test that the update works.
|
||||
$request = new WP_REST_Request( 'POST', $this->endpoint );
|
||||
$request->set_headers( array( 'content-type' => 'application/json' ) );
|
||||
$request->set_body( wp_json_encode( array( 'theme' => 'Storefront' ) ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'success', $data['status'] );
|
||||
|
||||
// Make sure the original field value wasn't overwritten.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'health-beauty', $data['industry'][0] );
|
||||
$this->assertEquals( 'storefront', $data['theme'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertCount( 10, $properties );
|
||||
$this->assertArrayHasKey( 'completed', $properties );
|
||||
$this->assertArrayHasKey( 'skipped', $properties );
|
||||
$this->assertArrayHasKey( 'account_type', $properties );
|
||||
$this->assertArrayHasKey( 'industry', $properties );
|
||||
$this->assertArrayHasKey( 'product_types', $properties );
|
||||
$this->assertArrayHasKey( 'product_count', $properties );
|
||||
$this->assertArrayHasKey( 'selling_venues', $properties );
|
||||
$this->assertArrayHasKey( 'other_platform', $properties );
|
||||
$this->assertArrayHasKey( 'theme', $properties );
|
||||
$this->assertArrayHasKey( 'items_purchased', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that profiles response changes based on applied filters.
|
||||
*/
|
||||
public function test_profile_extensibility() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
add_filter(
|
||||
'woocommerce_onboarding_profile_properties',
|
||||
function( $properties ) {
|
||||
$properties['test_profile_datum'] = array(
|
||||
'type' => 'array',
|
||||
'description' => __( 'Test onboarding profile extensibility.', 'woocommerce-admin' ),
|
||||
'context' => array( 'view' ),
|
||||
'readonly' => true,
|
||||
);
|
||||
return $properties;
|
||||
}
|
||||
);
|
||||
|
||||
// Test that the update works.
|
||||
$request = new WP_REST_Request( 'POST', $this->endpoint );
|
||||
$request->set_headers( array( 'content-type' => 'application/json' ) );
|
||||
$request->set_body( wp_json_encode( array( 'test_profile_datum' => 'woo' ) ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'success', $data['status'] );
|
||||
|
||||
// Test that the new field is retrieved.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'woo', $data['test_profile_datum'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,657 @@
|
|||
<?php
|
||||
/**
|
||||
* Orders REST API tests.
|
||||
*
|
||||
* @package WooCommerce/RestApi/Tests
|
||||
*/
|
||||
|
||||
namespace WooCommerce\RestApi\Tests\Version4;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
use \WooCommerce\RestApi\Tests\AbstractRestApiTest;
|
||||
|
||||
/**
|
||||
* Abstract Rest API Test Class
|
||||
*
|
||||
* @extends AbstractRestApiTest
|
||||
*/
|
||||
class Orders extends AbstractRestApiTest {
|
||||
/**
|
||||
* Routes that this endpoint creates.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $routes = [
|
||||
'/wc/v4/orders',
|
||||
'/wc/v4/orders/(?P<id>[\d]+)',
|
||||
'/wc/v4/orders/batch',
|
||||
];
|
||||
|
||||
/**
|
||||
* The endpoint schema.
|
||||
*
|
||||
* @var array Keys are property names, values are supported context.
|
||||
*/
|
||||
protected $properties = [
|
||||
'id' => array( 'view', 'edit' ),
|
||||
'parent_id' => array( 'view', 'edit' ),
|
||||
'number' => array( 'view', 'edit' ),
|
||||
'order_key' => array( 'view', 'edit' ),
|
||||
'created_via' => array( 'view', 'edit' ),
|
||||
'version' => array( 'view', 'edit' ),
|
||||
'status' => array( 'view', 'edit' ),
|
||||
'currency' => array( 'view', 'edit' ),
|
||||
'currency_symbol' => array( 'view', 'edit' ),
|
||||
'date_created' => array( 'view', 'edit' ),
|
||||
'date_created_gmt' => array( 'view', 'edit' ),
|
||||
'date_modified' => array( 'view', 'edit' ),
|
||||
'date_modified_gmt' => array( 'view', 'edit' ),
|
||||
'discount_total' => array( 'view', 'edit' ),
|
||||
'discount_tax' => array( 'view', 'edit' ),
|
||||
'shipping_total' => array( 'view', 'edit' ),
|
||||
'shipping_tax' => array( 'view', 'edit' ),
|
||||
'cart_tax' => array( 'view', 'edit' ),
|
||||
'total' => array( 'view', 'edit' ),
|
||||
'total_tax' => array( 'view', 'edit' ),
|
||||
'prices_include_tax' => array( 'view', 'edit' ),
|
||||
'customer_id' => array( 'view', 'edit' ),
|
||||
'customer_ip_address' => array( 'view', 'edit' ),
|
||||
'customer_user_agent' => array( 'view', 'edit' ),
|
||||
'customer_note' => array( 'view', 'edit' ),
|
||||
'billing' => array( 'view', 'edit' ),
|
||||
'shipping' => array( 'view', 'edit' ),
|
||||
'payment_method' => array( 'view', 'edit' ),
|
||||
'payment_method_title' => array( 'view', 'edit' ),
|
||||
'transaction_id' => array( 'view', 'edit' ),
|
||||
'date_paid' => array( 'view', 'edit' ),
|
||||
'date_paid_gmt' => array( 'view', 'edit' ),
|
||||
'date_completed' => array( 'view', 'edit' ),
|
||||
'date_completed_gmt' => array( 'view', 'edit' ),
|
||||
'cart_hash' => array( 'view', 'edit' ),
|
||||
'meta_data' => array( 'view', 'edit' ),
|
||||
'line_items' => array( 'view', 'edit' ),
|
||||
'tax_lines' => array( 'view', 'edit' ),
|
||||
'shipping_lines' => array( 'view', 'edit' ),
|
||||
'fee_lines' => array( 'view', 'edit' ),
|
||||
'coupon_lines' => array( 'view', 'edit' ),
|
||||
'refunds' => array( 'view', 'edit' ),
|
||||
'set_paid' => array( 'edit' ),
|
||||
];
|
||||
|
||||
/**
|
||||
* Test create.
|
||||
*/
|
||||
public function test_create() {
|
||||
$product = \WC_Helper_Product::create_simple_product();
|
||||
$data = [
|
||||
'currency' => 'ZAR',
|
||||
'customer_id' => 1,
|
||||
'customer_note' => 'I am a note',
|
||||
'transaction_id' => 'test',
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => 'Direct Bank Transfer',
|
||||
'set_paid' => true,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'company' => '',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'company' => '',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => '10',
|
||||
),
|
||||
),
|
||||
];
|
||||
$response = $this->do_request( '/wc/v4/orders', 'POST', $data );
|
||||
$this->assertExpectedResponse( $response, 201, $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the sanitization of the payment_method_title field through the API.
|
||||
*
|
||||
* @since 3.5.2
|
||||
*/
|
||||
public function test_create_update_order_payment_method_title_sanitize() {
|
||||
$product = \WC_Helper_Product::create_simple_product();
|
||||
$data = [
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => '<h1>Sanitize this <script>alert(1);</script></h1>',
|
||||
'set_paid' => true,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => '10',
|
||||
),
|
||||
),
|
||||
];
|
||||
$response = $this->do_request( '/wc/v4/orders', 'POST', $data );
|
||||
$order = wc_get_order( $response->data['id'] );
|
||||
$this->assertExpectedResponse( $response, 201 );
|
||||
$this->assertEquals( $order->get_payment_method(), $response->data['payment_method'] );
|
||||
$this->assertEquals( $order->get_payment_method_title(), 'Sanitize this' );
|
||||
|
||||
// Test when updating order.
|
||||
$response = $this->do_request(
|
||||
'/wc/v4/orders/' . $response->data['id'],
|
||||
'POST', [
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => '<h1>Sanitize this too <script>alert(1);</script></h1>',
|
||||
]
|
||||
);
|
||||
$order = wc_get_order( $response->data['id'] );
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
$this->assertEquals( $order->get_payment_method(), $response->data['payment_method'] );
|
||||
$this->assertEquals( $order->get_payment_method_title(), 'Sanitize this too' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating an order without required fields.
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_create_order_invalid_fields() {
|
||||
$product = \WC_Helper_Product::create_simple_product();
|
||||
$data = [
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => 'Direct Bank Transfer',
|
||||
'set_paid' => true,
|
||||
'customer_id' => 99999,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => 10,
|
||||
),
|
||||
),
|
||||
];
|
||||
$response = $this->do_request( '/wc/v4/orders', 'POST', $data );
|
||||
$this->assertExpectedResponse( $response, 400 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test read.
|
||||
*/
|
||||
public function test_read() {
|
||||
$product = \WC_Helper_Product::create_simple_product();
|
||||
$product->set_regular_price( 10.95 );
|
||||
$product->set_sale_price( false );
|
||||
$product->save();
|
||||
$customer = \WC_Helper_Customer::create_customer();
|
||||
$orders = [];
|
||||
$orders[] = \WC_Helper_Order::create_order( $customer->get_id(), $product );
|
||||
|
||||
// Create orders.
|
||||
for ( $i = 0; $i < 9; $i++ ) {
|
||||
$orders[] = \WC_Helper_Order::create_order( $this->user );
|
||||
}
|
||||
|
||||
$orders[5]->set_status( 'on-hold' );
|
||||
$orders[5]->save();
|
||||
$orders[6]->set_status( 'on-hold' );
|
||||
$orders[6]->save();
|
||||
$orders[0]->calculate_totals();
|
||||
$orders[0]->save();
|
||||
|
||||
// Collection.
|
||||
$response = $this->do_request( '/wc/v4/orders', 'GET' );
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
$this->assertEquals( 10, count( $response->data ) );
|
||||
|
||||
// Collection args.
|
||||
$response = $this->do_request( '/wc/v4/orders', 'GET', [ 'status' => 'on-hold' ] );
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
$this->assertEquals( 2, count( $response->data ) );
|
||||
|
||||
$response = $this->do_request( '/wc/v4/orders', 'GET', [ 'number' => (string) $orders[0]->get_id() ] );
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
$this->assertEquals( 1, count( $response->data ) );
|
||||
|
||||
$response = $this->do_request( '/wc/v4/orders', 'GET', [ 'customer' => $customer->get_id() ] );
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
$this->assertEquals( 1, count( $response->data ) );
|
||||
|
||||
$response = $this->do_request( '/wc/v4/orders', 'GET', [ 'product' => $product->get_id() ] );
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
$this->assertEquals( 1, count( $response->data ) );
|
||||
|
||||
// Single collection args.
|
||||
$response = $this->do_request( '/wc/v4/orders/' . $orders[0]->get_id(), 'GET', [ 'dp' => 0 ] );
|
||||
$this->assertEquals( '54', $response->data['total'] );
|
||||
$response = $this->do_request( '/wc/v4/orders/' . $orders[0]->get_id(), 'GET', [ 'dp' => 2 ] );
|
||||
$this->assertEquals( '53.80', $response->data['total'] );
|
||||
|
||||
// Single.
|
||||
$response = $this->do_request( '/wc/v4/orders/' . $orders[0]->get_id(), 'GET' );
|
||||
$this->assertExpectedResponse( $response, 200 );
|
||||
|
||||
foreach ( $this->get_properties( 'view' ) as $property ) {
|
||||
$this->assertArrayHasKey( $property, $response->data );
|
||||
}
|
||||
|
||||
// Invalid.
|
||||
$response = $this->do_request( '/wc/v4/orders/0', 'GET' );
|
||||
$this->assertExpectedResponse( $response, 404 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update.
|
||||
*/
|
||||
public function test_update() {
|
||||
// Invalid.
|
||||
$response = $this->do_request( '/wc/v4/orders/0', 'POST', [ 'payment_method' => 'test' ] );
|
||||
$this->assertExpectedResponse( $response, 404 );
|
||||
|
||||
// Update existing.
|
||||
$order = \WC_Helper_Order::create_order();
|
||||
$data = [
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
'first_name' => 'Fish',
|
||||
'last_name' => 'Face',
|
||||
),
|
||||
];
|
||||
$response = $this->do_request(
|
||||
'/wc/v4/orders/' . $order->get_id(),
|
||||
'POST',
|
||||
$data
|
||||
);
|
||||
$this->assertExpectedResponse( $response, 200, $data );
|
||||
|
||||
foreach ( $this->get_properties( 'view' ) as $property ) {
|
||||
$this->assertArrayHasKey( $property, $response->data );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete.
|
||||
*/
|
||||
public function test_delete() {
|
||||
// Invalid.
|
||||
$result = $this->do_request( '/wc/v4/orders/0', 'DELETE', [ 'force' => false ] );
|
||||
$this->assertEquals( 404, $result->status );
|
||||
|
||||
// Trash.
|
||||
$order = \WC_Helper_Order::create_order();
|
||||
$result = $this->do_request( '/wc/v4/orders/' . $order->get_id(), 'DELETE', [ 'force' => false ] );
|
||||
$this->assertEquals( 200, $result->status );
|
||||
$this->assertEquals( 'trash', get_post_status( $order->get_id() ) );
|
||||
|
||||
// Force.
|
||||
$order = \WC_Helper_Order::create_order();
|
||||
$result = $this->do_request( '/wc/v4/orders/' . $order->get_id(), 'DELETE', [ 'force' => true ] );
|
||||
$this->assertEquals( 200, $result->status );
|
||||
$this->assertEquals( false, get_post( $order->get_id() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test read.
|
||||
*/
|
||||
public function test_guest_create() {
|
||||
parent::test_guest_create();
|
||||
|
||||
$product = \WC_Helper_Product::create_simple_product();
|
||||
$data = [
|
||||
'currency' => 'ZAR',
|
||||
'customer_id' => 1,
|
||||
'customer_note' => 'I am a note',
|
||||
'transaction_id' => 'test',
|
||||
'payment_method' => 'bacs',
|
||||
'payment_method_title' => 'Direct Bank Transfer',
|
||||
'set_paid' => true,
|
||||
'billing' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'company' => '',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
'email' => 'john.doe@example.com',
|
||||
'phone' => '(555) 555-5555',
|
||||
),
|
||||
'shipping' => array(
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
'company' => '',
|
||||
'address_1' => '969 Market',
|
||||
'address_2' => '',
|
||||
'city' => 'San Francisco',
|
||||
'state' => 'CA',
|
||||
'postcode' => '94103',
|
||||
'country' => 'US',
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'product_id' => $product->get_id(),
|
||||
'quantity' => 2,
|
||||
),
|
||||
),
|
||||
'shipping_lines' => array(
|
||||
array(
|
||||
'method_id' => 'flat_rate',
|
||||
'method_title' => 'Flat rate',
|
||||
'total' => '10',
|
||||
),
|
||||
),
|
||||
];
|
||||
$response = $this->do_request( '/wc/v4/orders', 'POST', $data );
|
||||
$this->assertExpectedResponse( $response, 401, $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test read.
|
||||
*/
|
||||
public function test_guest_read() {
|
||||
parent::test_guest_read();
|
||||
|
||||
$response = $this->do_request( '/wc/v4/orders', 'GET' );
|
||||
$this->assertExpectedResponse( $response, 401 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update.
|
||||
*/
|
||||
public function test_guest_update() {
|
||||
parent::test_guest_update();
|
||||
|
||||
$order = \WC_Helper_Order::create_order();
|
||||
$data = [
|
||||
'payment_method' => 'test-update',
|
||||
'billing' => array(
|
||||
'first_name' => 'Fish',
|
||||
'last_name' => 'Face',
|
||||
),
|
||||
];
|
||||
$response = $this->do_request(
|
||||
'/wc/v4/orders/' . $order->get_id(),
|
||||
'POST',
|
||||
$data
|
||||
);
|
||||
$this->assertExpectedResponse( $response, 401 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test delete.
|
||||
*/
|
||||
public function test_guest_delete() {
|
||||
parent::test_guest_delete();
|
||||
|
||||
$order = \WC_Helper_Order::create_order();
|
||||
$response = $this->do_request( '/wc/v4/orders/' . $order->get_id(), 'DELETE', [ 'force' => true ] );
|
||||
$this->assertEquals( 401, $response->status );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validation.
|
||||
*/
|
||||
public function test_enums() {
|
||||
$order = \WC_Helper_Order::create_order();
|
||||
|
||||
$response = $this->do_request(
|
||||
'/wc/v4/orders/' . $order->get_id(),
|
||||
'POST',
|
||||
[
|
||||
'status' => 'invalid',
|
||||
]
|
||||
);
|
||||
$this->assertEquals( 400, $response->status );
|
||||
$this->assertEquals( 'Invalid parameter(s): status', $response->data['message'] );
|
||||
|
||||
$response = $this->do_request(
|
||||
'/wc/v4/orders/' . $order->get_id(),
|
||||
'POST',
|
||||
[
|
||||
'currency' => 'invalid',
|
||||
]
|
||||
);
|
||||
$this->assertEquals( 400, $response->status );
|
||||
$this->assertEquals( 'Invalid parameter(s): currency', $response->data['message'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a batch update.
|
||||
*/
|
||||
public function test_batch() {
|
||||
$order1 = \WC_Helper_Order::create_order();
|
||||
$order2 = \WC_Helper_Order::create_order();
|
||||
$order3 = \WC_Helper_Order::create_order();
|
||||
|
||||
$result = $this->do_request(
|
||||
'/wc/v4/orders/batch',
|
||||
'POST',
|
||||
array(
|
||||
'update' => array(
|
||||
array(
|
||||
'id' => $order1->get_id(),
|
||||
'payment_method' => 'updated',
|
||||
),
|
||||
),
|
||||
'delete' => array(
|
||||
$order2->get_id(),
|
||||
$order3->get_id(),
|
||||
),
|
||||
)
|
||||
);
|
||||
$this->assertEquals( 'updated', $result->data['update'][0]['payment_method'] );
|
||||
$this->assertEquals( $order2->get_id(), $result->data['delete'][0]['previous']['id'] );
|
||||
$this->assertEquals( $order3->get_id(), $result->data['delete'][1]['previous']['id'] );
|
||||
|
||||
$result = $this->do_request( '/wc/v4/orders' );
|
||||
$this->assertEquals( 1, count( $result->data ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and removing items.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_remove_items() {
|
||||
$order = \WC_Helper_Order::create_order();
|
||||
$fee = new \WC_Order_Item_Fee();
|
||||
$fee->set_props(
|
||||
array(
|
||||
'name' => 'Some Fee',
|
||||
'tax_status' => 'taxable',
|
||||
'total' => '100',
|
||||
'tax_class' => '',
|
||||
)
|
||||
);
|
||||
$order->add_item( $fee );
|
||||
$order->save();
|
||||
|
||||
$fee_data = current( $order->get_items( 'fee' ) );
|
||||
$response = $this->do_request(
|
||||
'/wc/v3/orders/' . $order->get_id(),
|
||||
'PUT',
|
||||
[
|
||||
'fee_lines' => array(
|
||||
array(
|
||||
'id' => $fee_data->get_id(),
|
||||
'name' => null,
|
||||
),
|
||||
),
|
||||
]
|
||||
);
|
||||
$this->assertEquals( 200, $response->status );
|
||||
$this->assertTrue( empty( $response->data['fee_lines'] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and adding a coupon.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_add_coupons() {
|
||||
$order = \WC_Helper_Order::create_order();
|
||||
$order_item = current( $order->get_items() );
|
||||
$coupon = \WC_Helper_Coupon::create_coupon( 'fake-coupon' );
|
||||
$coupon->set_amount( 5 );
|
||||
$coupon->save();
|
||||
|
||||
$response = $this->do_request(
|
||||
'/wc/v3/orders/' . $order->get_id(),
|
||||
'PUT',
|
||||
[
|
||||
'coupon_lines' => array(
|
||||
array(
|
||||
'code' => 'fake-coupon',
|
||||
),
|
||||
),
|
||||
]
|
||||
);
|
||||
$this->assertEquals( 200, $response->status );
|
||||
$this->assertCount( 1, $response->data['coupon_lines'] );
|
||||
$this->assertEquals( '45.00', $response->data['total'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order and removing a coupon.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_order_remove_coupons() {
|
||||
$order = \WC_Helper_Order::create_order();
|
||||
$order_item = current( $order->get_items() );
|
||||
$coupon = \WC_Helper_Coupon::create_coupon( 'fake-coupon' );
|
||||
$coupon->set_amount( 5 );
|
||||
$coupon->save();
|
||||
$order->apply_coupon( $coupon );
|
||||
$order->save();
|
||||
|
||||
// Check that the coupon is applied.
|
||||
$this->assertEquals( '45.00', $order->get_total() );
|
||||
|
||||
$coupon_data = current( $order->get_items( 'coupon' ) );
|
||||
$response = $this->do_request(
|
||||
'/wc/v3/orders/' . $order->get_id(),
|
||||
'PUT',
|
||||
[
|
||||
'coupon_lines' => array(
|
||||
array(
|
||||
'id' => $coupon_data->get_id(),
|
||||
'code' => null,
|
||||
),
|
||||
),
|
||||
'line_items' => array(
|
||||
array(
|
||||
'id' => $order_item->get_id(),
|
||||
'product_id' => $order_item->get_product_id(),
|
||||
'total' => '40.00',
|
||||
),
|
||||
),
|
||||
]
|
||||
);
|
||||
$this->assertEquals( 200, $response->status );
|
||||
$this->assertTrue( empty( $response->data['coupon_lines'] ) );
|
||||
$this->assertEquals( '50.00', $response->data['total'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests updating an order with an invalid coupon.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_invalid_coupon() {
|
||||
$order = \WC_Helper_Order::create_order();
|
||||
$response = $this->do_request(
|
||||
'/wc/v3/orders/' . $order->get_id(),
|
||||
'PUT',
|
||||
[
|
||||
'coupon_lines' => array(
|
||||
array(
|
||||
'code' => 'NON_EXISTING_COUPON',
|
||||
),
|
||||
),
|
||||
]
|
||||
);
|
||||
$this->assertEquals( 400, $response->status );
|
||||
$this->assertEquals( 'woocommerce_rest_invalid_coupon', $response->data['code'] );
|
||||
$this->assertEquals( 'Coupon "non_existing_coupon" does not exist!', $response->data['message'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,182 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Categories REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Reports_Categories
|
||||
*/
|
||||
class WC_Tests_API_Reports_Categories extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/categories';
|
||||
|
||||
/**
|
||||
* Setup test reports categories data.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$uncategorized_term = get_term_by( 'slug', 'uncategorized', 'product_cat' );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
|
||||
$category_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( $uncategorized_term->term_id, $category_report['category_id'] );
|
||||
$this->assertEquals( 4, $category_report['items_sold'] );
|
||||
$this->assertEquals( 1, $category_report['orders_count'] );
|
||||
$this->assertEquals( 1, $category_report['products_count'] );
|
||||
$this->assertArrayHasKey( '_links', $category_report );
|
||||
$this->assertArrayHasKey( 'category', $category_report['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports with the `categories` param.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_categories_param() {
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product 2' );
|
||||
$product->set_regular_price( 100 );
|
||||
$second_category_id = wp_create_category( 'Second Category' );
|
||||
$product->set_category_ids( array( $second_category_id ) );
|
||||
$product->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$uncategorized_term = get_term_by( 'slug', 'uncategorized', 'product_cat' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'categories' => $uncategorized_term->term_id . ',' . $second_category_id,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
$category_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( $second_category_id, $category_report['category_id'] );
|
||||
$this->assertEquals( 0, $category_report['items_sold'] );
|
||||
$this->assertEquals( 0, $category_report['orders_count'] );
|
||||
$this->assertEquals( 0, $category_report['products_count'] );
|
||||
$this->assertArrayHasKey( '_links', $category_report );
|
||||
$this->assertArrayHasKey( 'category', $category_report['_links'] );
|
||||
|
||||
$category_report = next( $reports );
|
||||
|
||||
$this->assertEquals( $uncategorized_term->term_id, $category_report['category_id'] );
|
||||
$this->assertEquals( 4, $category_report['items_sold'] );
|
||||
$this->assertEquals( 1, $category_report['orders_count'] );
|
||||
$this->assertEquals( 1, $category_report['products_count'] );
|
||||
$this->assertArrayHasKey( '_links', $category_report );
|
||||
$this->assertArrayHasKey( 'category', $category_report['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 6, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'category_id', $properties );
|
||||
$this->assertArrayHasKey( 'items_sold', $properties );
|
||||
$this->assertArrayHasKey( 'net_revenue', $properties );
|
||||
$this->assertArrayHasKey( 'orders_count', $properties );
|
||||
$this->assertArrayHasKey( 'products_count', $properties );
|
||||
$this->assertArrayHasKey( 'extended_info', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,180 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Coupons Stats REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Reports_Coupons_Stats
|
||||
*/
|
||||
class WC_Tests_API_Reports_Coupons_Stats extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/coupons/stats';
|
||||
|
||||
/**
|
||||
* Setup test reports products stats data.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Populate all of the data.
|
||||
// Simple product.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
// Coupons.
|
||||
$coupon_1_amount = 1; // by default in create_coupon.
|
||||
$coupon_1 = WC_Helper_Coupon::create_coupon( 'coupon_1' );
|
||||
|
||||
$coupon_2_amount = 2;
|
||||
$coupon_2 = WC_Helper_Coupon::create_coupon( 'coupon_2' );
|
||||
$coupon_2->set_amount( $coupon_2_amount );
|
||||
$coupon_2->save();
|
||||
|
||||
// Order without coupon.
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
$time = time();
|
||||
|
||||
// Order with 1 coupon.
|
||||
$order_1c = WC_Helper_Order::create_order( 1, $product );
|
||||
$order_1c->set_status( 'completed' );
|
||||
$order_1c->apply_coupon( $coupon_1 );
|
||||
$order_1c->calculate_totals();
|
||||
$order_1c->set_date_created( $time );
|
||||
$order_1c->save();
|
||||
|
||||
// Order with 2 coupons.
|
||||
$order_2c = WC_Helper_Order::create_order( 1, $product );
|
||||
$order_2c->set_status( 'completed' );
|
||||
$order_2c->apply_coupon( $coupon_1 );
|
||||
$order_2c->apply_coupon( $coupon_2 );
|
||||
$order_2c->calculate_totals();
|
||||
$order_2c->set_date_created( $time );
|
||||
$order_2c->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'before' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'after' => date( 'Y-m-d 00:00:00', $time ),
|
||||
'interval' => 'day',
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$expected_reports = array(
|
||||
'totals' => array(
|
||||
'amount' => 4.0,
|
||||
'coupons_count' => 2,
|
||||
'orders_count' => 2,
|
||||
'segments' => array(),
|
||||
),
|
||||
'intervals' => array(
|
||||
array(
|
||||
'interval' => date( 'Y-m-d', $time ),
|
||||
'date_start' => date( 'Y-m-d 00:00:00', $time ),
|
||||
'date_start_gmt' => date( 'Y-m-d 00:00:00', $time ),
|
||||
'date_end' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'date_end_gmt' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'subtotals' => (object) array(
|
||||
'amount' => 4.0,
|
||||
'coupons_count' => 2,
|
||||
'orders_count' => 2,
|
||||
'segments' => array(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $expected_reports, $reports );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 2, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'totals', $properties );
|
||||
$this->assertArrayHasKey( 'intervals', $properties );
|
||||
|
||||
$totals = $properties['totals']['properties'];
|
||||
$this->assertEquals( 4, count( $totals ) );
|
||||
$this->assertArrayHasKey( 'amount', $totals );
|
||||
$this->assertArrayHasKey( 'coupons_count', $totals );
|
||||
$this->assertArrayHasKey( 'orders_count', $totals );
|
||||
$this->assertArrayHasKey( 'segments', $totals );
|
||||
|
||||
$intervals = $properties['intervals']['items']['properties'];
|
||||
$this->assertEquals( 6, count( $intervals ) );
|
||||
$this->assertArrayHasKey( 'interval', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'subtotals', $intervals );
|
||||
|
||||
$subtotals = $properties['intervals']['items']['properties']['subtotals']['properties'];
|
||||
$this->assertEquals( 4, count( $subtotals ) );
|
||||
$this->assertArrayHasKey( 'amount', $subtotals );
|
||||
$this->assertArrayHasKey( 'coupons_count', $subtotals );
|
||||
$this->assertArrayHasKey( 'orders_count', $subtotals );
|
||||
$this->assertArrayHasKey( 'segments', $subtotals );
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,188 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Coupons REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Reports_Coupons
|
||||
*/
|
||||
class WC_Tests_API_Reports_Coupons extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/coupons';
|
||||
|
||||
/**
|
||||
* Setup test reports products data.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting basic reports.
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Simple product.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
// Coupons.
|
||||
$coupon_1_amount = 1; // by default in create_coupon.
|
||||
$coupon_1 = WC_Helper_Coupon::create_coupon( 'coupon_1' );
|
||||
|
||||
$coupon_2_amount = 2;
|
||||
$coupon_2 = WC_Helper_Coupon::create_coupon( 'coupon_2' );
|
||||
$coupon_2->set_amount( $coupon_2_amount );
|
||||
$coupon_2->save();
|
||||
|
||||
// Order without coupon.
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
// Order with 1 coupon.
|
||||
$order_1c = WC_Helper_Order::create_order( 1, $product );
|
||||
$order_1c->set_status( 'completed' );
|
||||
$order_1c->apply_coupon( $coupon_1 );
|
||||
$order_1c->calculate_totals();
|
||||
$order_1c->save();
|
||||
|
||||
// Order with 2 coupons.
|
||||
$order_2c = WC_Helper_Order::create_order( 1, $product );
|
||||
$order_2c->set_status( 'completed' );
|
||||
$order_2c->apply_coupon( $coupon_1 );
|
||||
$order_2c->apply_coupon( $coupon_2 );
|
||||
$order_2c->calculate_totals();
|
||||
$order_2c->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$coupon_reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $coupon_reports ) );
|
||||
|
||||
$this->assertEquals( $coupon_2->get_id(), $coupon_reports[0]['coupon_id'] );
|
||||
$this->assertEquals( 1 * $coupon_2_amount, $coupon_reports[0]['amount'] );
|
||||
$this->assertEquals( 1, $coupon_reports[0]['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $coupon_reports[0] );
|
||||
$this->assertArrayHasKey( 'coupon', $coupon_reports[0]['_links'] );
|
||||
|
||||
$this->assertEquals( $coupon_1->get_id(), $coupon_reports[1]['coupon_id'] );
|
||||
$this->assertEquals( 2 * $coupon_1_amount, $coupon_reports[1]['amount'] );
|
||||
$this->assertEquals( 2, $coupon_reports[1]['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $coupon_reports[1] );
|
||||
$this->assertArrayHasKey( 'coupon', $coupon_reports[1]['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting basic reports with the `coupons` param.
|
||||
*/
|
||||
public function test_get_reports_coupons_param() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Simple product.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
// Coupons.
|
||||
$coupon_1_amount = 1; // by default in create_coupon.
|
||||
$coupon_1 = WC_Helper_Coupon::create_coupon( 'coupon_1' );
|
||||
|
||||
$coupon_2_amount = 2;
|
||||
$coupon_2 = WC_Helper_Coupon::create_coupon( 'coupon_2' );
|
||||
$coupon_2->set_amount( $coupon_2_amount );
|
||||
$coupon_2->save();
|
||||
|
||||
// Order with 1 coupon.
|
||||
$order_1c = WC_Helper_Order::create_order( 1, $product );
|
||||
$order_1c->set_status( 'completed' );
|
||||
$order_1c->apply_coupon( $coupon_1 );
|
||||
$order_1c->calculate_totals();
|
||||
$order_1c->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'coupons' => $coupon_1->get_id() . ',' . $coupon_2->get_id(),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$coupon_reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $coupon_reports ) );
|
||||
|
||||
$this->assertEquals( $coupon_2->get_id(), $coupon_reports[0]['coupon_id'] );
|
||||
$this->assertEquals( 0, $coupon_reports[0]['amount'] );
|
||||
$this->assertEquals( 0, $coupon_reports[0]['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $coupon_reports[0] );
|
||||
$this->assertArrayHasKey( 'coupon', $coupon_reports[0]['_links'] );
|
||||
|
||||
$this->assertEquals( $coupon_1->get_id(), $coupon_reports[1]['coupon_id'] );
|
||||
$this->assertEquals( $coupon_1_amount, $coupon_reports[1]['amount'] );
|
||||
$this->assertEquals( 1, $coupon_reports[1]['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $coupon_reports[1] );
|
||||
$this->assertArrayHasKey( 'coupon', $coupon_reports[1]['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 4, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'coupon_id', $properties );
|
||||
$this->assertArrayHasKey( 'amount', $properties );
|
||||
$this->assertArrayHasKey( 'orders_count', $properties );
|
||||
$this->assertArrayHasKey( 'extended_info', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,172 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Customers Stats REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reports Customers Stats REST API Test Class
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_API_Reports_Customers_Stats extends WC_REST_Unit_Test_Case {
|
||||
/**
|
||||
* Endpoint.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/customers/stats';
|
||||
|
||||
/**
|
||||
* Setup test reports products data.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertCount( 1, $properties );
|
||||
$this->assertArrayHasKey( 'totals', $properties );
|
||||
$this->assertCount( 4, $properties['totals']['properties'] );
|
||||
$this->assertArrayHasKey( 'customers_count', $properties['totals']['properties'] );
|
||||
$this->assertArrayHasKey( 'avg_orders_count', $properties['totals']['properties'] );
|
||||
$this->assertArrayHasKey( 'avg_total_spend', $properties['totals']['properties'] );
|
||||
$this->assertArrayHasKey( 'avg_avg_order_value', $properties['totals']['properties'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
$test_customers = array();
|
||||
|
||||
// Create 10 test customers.
|
||||
for ( $i = 1; $i <= 10; $i++ ) {
|
||||
$test_customers[] = WC_Helper_Customer::create_customer( "customer{$i}", 'password', "customer{$i}@example.com" );
|
||||
}
|
||||
// One differing name.
|
||||
$test_customers[2]->set_first_name( 'Jeff' );
|
||||
$test_customers[2]->save();
|
||||
|
||||
// Create a test product for use in an order.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
// Place some test orders.
|
||||
$order = WC_Helper_Order::create_order( $test_customers[0]->get_id(), $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 );
|
||||
$order->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( $test_customers[0]->get_id(), $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 234 );
|
||||
$order->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( $test_customers[1]->get_id(), $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 55 );
|
||||
$order->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( $test_customers[2]->get_id(), $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 9.12 );
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$headers = $response->get_headers();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 10, $reports['totals']->customers_count );
|
||||
$this->assertEquals( 1.333, round( $reports['totals']->avg_orders_count, 3 ) );
|
||||
$this->assertEquals( 132.707, round( $reports['totals']->avg_total_spend, 3 ) );
|
||||
$this->assertEquals( 77.04, $reports['totals']->avg_avg_order_value );
|
||||
|
||||
// Test name parameter (case with no matches).
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'search' => 'Nota Customername',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 0, $reports['totals']->customers_count );
|
||||
$this->assertEquals( 0, $reports['totals']->avg_orders_count );
|
||||
$this->assertEquals( 0, $reports['totals']->avg_total_spend );
|
||||
$this->assertEquals( 0, $reports['totals']->avg_avg_order_value );
|
||||
|
||||
// Test name and last_order parameters.
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'search' => 'Jeff',
|
||||
'last_order_after' => date( 'Y-m-d' ) . 'T00:00:00Z',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, $reports['totals']->customers_count );
|
||||
$this->assertEquals( 1, $reports['totals']->avg_orders_count );
|
||||
$this->assertEquals( 9.12, $reports['totals']->avg_total_spend );
|
||||
$this->assertEquals( 9.12, $reports['totals']->avg_avg_order_value );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,405 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Customers REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reports Customers REST API Test Class
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case {
|
||||
/**
|
||||
* Endpoint.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/customers';
|
||||
|
||||
/**
|
||||
* Setup test reports products data.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the report item schema is correct.
|
||||
*
|
||||
* @param array $schema Item to check schema.
|
||||
*/
|
||||
public function assert_report_item_schema( $schema ) {
|
||||
$this->assertArrayHasKey( 'id', $schema );
|
||||
$this->assertArrayHasKey( 'user_id', $schema );
|
||||
$this->assertArrayHasKey( 'name', $schema );
|
||||
$this->assertArrayHasKey( 'username', $schema );
|
||||
$this->assertArrayHasKey( 'country', $schema );
|
||||
$this->assertArrayHasKey( 'city', $schema );
|
||||
$this->assertArrayHasKey( 'postcode', $schema );
|
||||
$this->assertArrayHasKey( 'date_registered', $schema );
|
||||
$this->assertArrayHasKey( 'date_registered_gmt', $schema );
|
||||
$this->assertArrayHasKey( 'date_last_active', $schema );
|
||||
$this->assertArrayHasKey( 'date_last_active_gmt', $schema );
|
||||
$this->assertArrayHasKey( 'orders_count', $schema );
|
||||
$this->assertArrayHasKey( 'total_spend', $schema );
|
||||
$this->assertArrayHasKey( 'avg_order_value', $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertCount( 14, $properties );
|
||||
$this->assert_report_item_schema( $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test calling update_registered_customer() with a bad user id.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_update_registered_customer_with_bad_user_id() {
|
||||
$result = WC_Admin_Reports_Customers_Data_Store::update_registered_customer( 2 );
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creation of various user roles
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_user_creation() {
|
||||
wp_set_current_user( $this->user );
|
||||
$admin_id = wp_insert_user(
|
||||
array(
|
||||
'user_login' => 'testadmin',
|
||||
'user_pass' => null,
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
|
||||
// Admin user without orders should not be shown.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( array( 'per_page' => 10 ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$headers = $response->get_headers();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 0, $reports );
|
||||
|
||||
// Creating an order with admin should return the admin.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( $admin_id, $product );
|
||||
$order->set_status( 'processing' );
|
||||
$order->set_total( 100 );
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( array( 'per_page' => 10 ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$headers = $response->get_headers();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
$this->assertEquals( $admin_id, $reports[0]['user_id'] );
|
||||
|
||||
// Creating a customer should show up regardless of orders.
|
||||
$customer = WC_Helper_Customer::create_customer( 'customer', 'password', 'customer@example.com' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'per_page' => 10,
|
||||
'order' => 'asc',
|
||||
'orderby' => 'username',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$headers = $response->get_headers();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 2, $reports );
|
||||
$this->assertEquals( $customer->get_id(), $reports[0]['user_id'] );
|
||||
$this->assertEquals( $admin_id, $reports[1]['user_id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
global $wpdb;
|
||||
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
$test_customers = array();
|
||||
|
||||
$customer_names = array( 'Alice', 'Betty', 'Catherine', 'Dan', 'Eric', 'Fred', 'Greg', 'Henry', 'Ivan', 'Justin' );
|
||||
|
||||
// Create 10 test customers.
|
||||
for ( $i = 1; $i <= 10; $i++ ) {
|
||||
$name = $customer_names[ $i - 1 ];
|
||||
$email = 'customer+' . strtolower( $name ) . '@example.com';
|
||||
$customer = WC_Helper_Customer::create_customer( "customer{$i}", 'password', $email );
|
||||
$customer->set_first_name( $name );
|
||||
$customer->save();
|
||||
$test_customers[] = $customer;
|
||||
}
|
||||
|
||||
// Create a test product for use in an order.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
// Place an order for the first test customer.
|
||||
$order = WC_Helper_Order::create_order( $test_customers[0]->get_id(), $product );
|
||||
$order->set_status( 'processing' );
|
||||
$order->set_total( 100 );
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'per_page' => 5,
|
||||
'order' => 'asc',
|
||||
'orderby' => 'username',
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$headers = $response->get_headers();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 5, $reports );
|
||||
$this->assertArrayHasKey( 'X-WP-Total', $headers );
|
||||
$this->assertEquals( 10, $headers['X-WP-Total'] );
|
||||
$this->assertArrayHasKey( 'X-WP-TotalPages', $headers );
|
||||
$this->assertEquals( 2, $headers['X-WP-TotalPages'] );
|
||||
$this->assertEquals( $test_customers[0]->get_id(), $reports[0]['user_id'] );
|
||||
$this->assertEquals( 1, $reports[0]['orders_count'] );
|
||||
$this->assertEquals( 100, $reports[0]['total_spend'] );
|
||||
$this->assert_report_item_schema( $reports[0] );
|
||||
|
||||
// Test name parameter (case with no matches).
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'search' => 'Nota Customername',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 0, $reports );
|
||||
|
||||
// Test name parameter (partial match).
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'search' => 're',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 2, $reports );
|
||||
|
||||
// Test email search.
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'search' => 'customer+justin',
|
||||
'searchby' => 'email',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
|
||||
// Test username search.
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'search' => 'customer1',
|
||||
'searchby' => 'username',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
// customer1 and customer10.
|
||||
$this->assertCount( 2, $reports );
|
||||
|
||||
// Test name and last_order parameters.
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'search' => 'Alice',
|
||||
'last_order_after' => date( 'Y-m-d' ) . 'T00:00:00Z',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
|
||||
$this->assertEquals( $test_customers[0]->get_id(), $reports[0]['user_id'] );
|
||||
$this->assertEquals( 1, $reports[0]['orders_count'] );
|
||||
$this->assertEquals( 100, $reports[0]['total_spend'] );
|
||||
|
||||
$customer_id = $wpdb->get_col(
|
||||
$wpdb->prepare(
|
||||
"SELECT customer_id FROM {$wpdb->prefix}wc_customer_lookup WHERE user_id = %d",
|
||||
$reports[0]['user_id']
|
||||
)
|
||||
);
|
||||
|
||||
// Test customers param.
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'customers' => $customer_id,
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
$this->assertEquals( $test_customers[0]->get_id(), $reports[0]['user_id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test customer first and last name.
|
||||
*/
|
||||
public function test_customer_name() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$customer = wp_insert_user(
|
||||
array(
|
||||
'user_login' => 'daenerys',
|
||||
'user_pass' => null,
|
||||
'role' => 'customer',
|
||||
)
|
||||
);
|
||||
|
||||
// Test shipping name and empty billing name.
|
||||
$order = WC_Helper_Order::create_order( $customer );
|
||||
$order->set_billing_first_name( '' );
|
||||
$order->set_billing_last_name( '' );
|
||||
$order->set_shipping_first_name( 'Daenerys' );
|
||||
$order->set_shipping_last_name( 'Targaryen' );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 );
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$headers = $response->get_headers();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
$this->assertEquals( 'Daenerys Targaryen', $reports[0]['name'] );
|
||||
|
||||
// Test billing name.
|
||||
$order->set_billing_first_name( 'Jon' );
|
||||
$order->set_billing_last_name( 'Snow' );
|
||||
$order->save();
|
||||
do_action( 'woocommerce_update_customer', $customer );
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( array( 'orderby' => 'username' ) ); // Cache busting.
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$headers = $response->get_headers();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
$this->assertEquals( 'Jon Snow', $reports[0]['name'] );
|
||||
|
||||
// Test user profile name.
|
||||
wp_update_user(
|
||||
array(
|
||||
'ID' => $customer,
|
||||
'first_name' => 'Tyrion',
|
||||
'last_name' => 'Lanister',
|
||||
)
|
||||
);
|
||||
do_action( 'woocommerce_update_customer', $customer );
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( array( 'orderby' => 'name' ) ); // Cache busting.
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$headers = $response->get_headers();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
$this->assertEquals( 'Tyrion Lanister', $reports[0]['name'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,365 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Downloads Stats REST API Test
|
||||
*
|
||||
* @package WooCommerce Admin\Tests\API.
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC_Tests_API_Reports_Downloads_Stats
|
||||
*/
|
||||
class WC_Tests_API_Reports_Downloads_Stats extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/downloads/stats';
|
||||
|
||||
/**
|
||||
* Setup test reports downloads data.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting report.
|
||||
*/
|
||||
public function test_get_report() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$prod_download = new WC_Product_Download();
|
||||
$prod_download->set_file( plugin_dir_url( __FILE__ ) . '/assets/images/help.png' );
|
||||
$prod_download->set_id( 1 );
|
||||
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_downloadable( 'yes' );
|
||||
$product->set_downloads( array( $prod_download ) );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 );
|
||||
$order->save();
|
||||
|
||||
$download = new WC_Customer_Download();
|
||||
$download->set_user_id( $this->user );
|
||||
$download->set_order_id( $order->get_id() );
|
||||
$download->set_product_id( $product->get_id() );
|
||||
$download->set_download_id( $prod_download->get_id() );
|
||||
$download->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '1.2.3.4' );
|
||||
$id = $object->save();
|
||||
|
||||
$time = time();
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'before' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'after' => date( 'Y-m-d H:00:00', $time - ( 7 * DAY_IN_SECONDS ) ),
|
||||
'interval' => 'day',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$totals = array(
|
||||
'download_count' => 1,
|
||||
);
|
||||
$this->assertEquals( $totals, $reports['totals'] );
|
||||
|
||||
$today_interval = array(
|
||||
'interval' => date( 'Y-m-d', $time ),
|
||||
'date_start' => date( 'Y-m-d 00:00:00', $time ),
|
||||
'date_start_gmt' => date( 'Y-m-d 00:00:00', $time ),
|
||||
'date_end' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'date_end_gmt' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'subtotals' => (object) array(
|
||||
'download_count' => 1,
|
||||
),
|
||||
);
|
||||
$this->assertEquals( $today_interval, $reports['intervals'][0] );
|
||||
|
||||
$this->assertEquals( 8, count( $reports['intervals'] ) );
|
||||
$this->assertEquals( 0, $reports['intervals'][1]['subtotals']->download_count );
|
||||
|
||||
// Test sorting by download_count.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'before' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'after' => date( 'Y-m-d H:00:00', $time - ( 7 * DAY_IN_SECONDS ) ),
|
||||
'interval' => 'day',
|
||||
'orderby' => 'download_count',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting report with user filter.
|
||||
*/
|
||||
public function test_get_report_with_user_filter() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
$time = time();
|
||||
|
||||
// First set of data.
|
||||
$prod_download = new WC_Product_Download();
|
||||
$prod_download->set_file( plugin_dir_url( __FILE__ ) . '/assets/images/help.png' );
|
||||
$prod_download->set_id( 1 );
|
||||
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_downloadable( 'yes' );
|
||||
$product->set_downloads( array( $prod_download ) );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 25 );
|
||||
$order->save();
|
||||
$order_1 = $order->get_id();
|
||||
|
||||
$download = new WC_Customer_Download();
|
||||
$download->set_user_id( 1 );
|
||||
$download->set_order_id( $order->get_id() );
|
||||
$download->set_product_id( $product->get_id() );
|
||||
$download->set_download_id( $prod_download->get_id() );
|
||||
$download->save();
|
||||
|
||||
for ( $i = 1; $i < 3; $i++ ) {
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( 1 );
|
||||
$object->set_user_ip_address( '1.2.3.4' );
|
||||
$id = $object->save();
|
||||
}
|
||||
|
||||
$order = WC_Helper_Order::create_order( 2, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 10 );
|
||||
$order->save();
|
||||
|
||||
$download = new WC_Customer_Download();
|
||||
$download->set_user_id( 2 );
|
||||
$download->set_order_id( $order->get_id() );
|
||||
$download->set_product_id( $product->get_id() );
|
||||
$download->set_download_id( $prod_download->get_id() );
|
||||
$download->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( 2 );
|
||||
$object->set_user_ip_address( '5.4.3.2.1' );
|
||||
$object->save();
|
||||
|
||||
$customer_id = $wpdb->get_col(
|
||||
$wpdb->prepare(
|
||||
"SELECT customer_id FROM {$wpdb->prefix}wc_customer_lookup WHERE user_id = %d",
|
||||
1
|
||||
)
|
||||
);
|
||||
|
||||
// Test includes filtering.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'customer_includes' => $customer_id,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, $reports['totals']['download_count'] );
|
||||
|
||||
// Test excludes filtering.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'customer_excludes' => $customer_id,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, $reports['totals']['download_count'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting report ordering.
|
||||
*/
|
||||
public function test_get_report_orderby() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$prod_download = new WC_Product_Download();
|
||||
$prod_download->set_file( plugin_dir_url( __FILE__ ) . '/assets/images/help.png' );
|
||||
$prod_download->set_id( 1 );
|
||||
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_downloadable( 'yes' );
|
||||
$product->set_downloads( array( $prod_download ) );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 );
|
||||
$order->save();
|
||||
|
||||
$download = new WC_Customer_Download();
|
||||
$download->set_user_id( $this->user );
|
||||
$download->set_order_id( $order->get_id() );
|
||||
$download->set_product_id( $product->get_id() );
|
||||
$download->set_download_id( $prod_download->get_id() );
|
||||
$download->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '1.2.3.4' );
|
||||
$object->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '1.2.3.4' );
|
||||
$object->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '1.2.3.4' );
|
||||
$object->save();
|
||||
|
||||
$three_days_from_now = current_time( 'timestamp', true ) - ( 3 * DAY_IN_SECONDS );
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '1.2.3.4' );
|
||||
$object->set_timestamp( $three_days_from_now );
|
||||
$object->save();
|
||||
|
||||
$time = time();
|
||||
$seven_days_ago = $time - ( 7 * DAY_IN_SECONDS );
|
||||
|
||||
// Test sorting by download_count.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'before' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'after' => date( 'Y-m-d H:00:00', $seven_days_ago ),
|
||||
'interval' => 'day',
|
||||
'orderby' => 'download_count',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, $reports['intervals'][0]['subtotals']->download_count );
|
||||
$this->assertEquals( date( 'Y-m-d', $time ), $reports['intervals'][0]['interval'] );
|
||||
|
||||
$this->assertEquals( 1, $reports['intervals'][1]['subtotals']->download_count );
|
||||
$this->assertEquals( date( 'Y-m-d', $three_days_from_now ), $reports['intervals'][1]['interval'] );
|
||||
|
||||
// Test sorting by date.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'before' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'after' => date( 'Y-m-d H:00:00', $seven_days_ago ),
|
||||
'interval' => 'day',
|
||||
'orderby' => 'date',
|
||||
'order' => 'asc',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 0, $reports['intervals'][0]['subtotals']->download_count );
|
||||
$this->assertEquals( date( 'Y-m-d', $seven_days_ago ), $reports['intervals'][0]['interval'] );
|
||||
|
||||
$this->assertEquals( 3, $reports['intervals'][7]['subtotals']->download_count );
|
||||
$this->assertEquals( date( 'Y-m-d', $time ), $reports['intervals'][7]['interval'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 2, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'totals', $properties );
|
||||
$this->assertArrayHasKey( 'intervals', $properties );
|
||||
|
||||
$totals = $properties['totals']['properties'];
|
||||
$this->assertEquals( 1, count( $totals ) );
|
||||
$this->assertArrayHasKey( 'download_count', $totals );
|
||||
|
||||
$intervals = $properties['intervals']['items']['properties'];
|
||||
$this->assertEquals( 6, count( $intervals ) );
|
||||
$this->assertArrayHasKey( 'interval', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'subtotals', $intervals );
|
||||
|
||||
$subtotals = $properties['intervals']['items']['properties']['subtotals']['properties'];
|
||||
$this->assertEquals( 1, count( $subtotals ) );
|
||||
$this->assertArrayHasKey( 'download_count', $subtotals );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,414 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Downloads REST API Test
|
||||
*
|
||||
* @package WooCommerce Admin\Tests\API.
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC_Tests_API_Reports_Downloads
|
||||
*/
|
||||
class WC_Tests_API_Reports_Downloads extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/downloads';
|
||||
|
||||
/**
|
||||
* Setup test reports downloads data.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting report.
|
||||
*/
|
||||
public function test_get_report() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$prod_download = new WC_Product_Download();
|
||||
$prod_download->set_file( plugin_dir_url( __FILE__ ) . '/assets/images/help.png' );
|
||||
$prod_download->set_id( 1 );
|
||||
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_downloadable( 'yes' );
|
||||
$product->set_downloads( array( $prod_download ) );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 );
|
||||
$order->save();
|
||||
|
||||
$download = new WC_Customer_Download();
|
||||
$download->set_user_id( $this->user );
|
||||
$download->set_order_id( $order->get_id() );
|
||||
$download->set_product_id( $product->get_id() );
|
||||
$download->set_download_id( $prod_download->get_id() );
|
||||
$download->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '1.2.3.4' );
|
||||
$id = $object->save();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
|
||||
$download_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 1, $download_report['download_id'] );
|
||||
$this->assertEquals( $product->get_id(), $download_report['product_id'] );
|
||||
$this->assertEquals( $order->get_id(), $download_report['order_id'] );
|
||||
$this->assertEquals( $order->get_order_number(), $download_report['order_number'] );
|
||||
$this->assertEquals( $this->user, $download_report['user_id'] );
|
||||
$this->assertEquals( '1.2.3.4', $download_report['ip_address'] );
|
||||
$this->assertEquals( 'help.png', $download_report['file_name'] );
|
||||
$this->assertEquals( plugin_dir_url( __FILE__ ) . '/assets/images/help.png', $download_report['file_path'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Does some test setup so we can filter with different options in later tests.
|
||||
*/
|
||||
public function filter_setup() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
$time = time();
|
||||
|
||||
// First set of data.
|
||||
$prod_download = new WC_Product_Download();
|
||||
$prod_download->set_file( plugin_dir_url( __FILE__ ) . '/assets/images/help.png' );
|
||||
$prod_download->set_id( 1 );
|
||||
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_downloadable( 'yes' );
|
||||
$product->set_downloads( array( $prod_download ) );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
$product_1 = $product->get_id();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 25 );
|
||||
$order->save();
|
||||
$order_1 = $order->get_id();
|
||||
|
||||
$download = new WC_Customer_Download();
|
||||
$download->set_user_id( 1 );
|
||||
$download->set_order_id( $order->get_id() );
|
||||
$download->set_product_id( $product->get_id() );
|
||||
$download->set_download_id( $prod_download->get_id() );
|
||||
$download->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( 1 );
|
||||
$object->set_user_ip_address( '1.2.3.4' );
|
||||
$id = $object->save();
|
||||
|
||||
// Second set of data.
|
||||
$prod_download = new WC_Product_Download();
|
||||
$prod_download->set_file( plugin_dir_url( __FILE__ ) . '/assets/images/test.png' );
|
||||
$prod_download->set_id( 2 );
|
||||
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product 2' );
|
||||
$product->set_downloadable( 'yes' );
|
||||
$product->set_downloads( array( $prod_download ) );
|
||||
$product->set_regular_price( 10 );
|
||||
$product->save();
|
||||
$product_2 = $product->get_id();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 2, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 10 );
|
||||
$order->save();
|
||||
$order_2 = $order->get_id();
|
||||
|
||||
$download = new WC_Customer_Download();
|
||||
$download->set_user_id( 2 );
|
||||
$download->set_order_id( $order->get_id() );
|
||||
$download->set_product_id( $product->get_id() );
|
||||
$download->set_download_id( $prod_download->get_id() );
|
||||
$download->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( 2 );
|
||||
$object->set_user_ip_address( '5.4.3.2.1' );
|
||||
$object->set_timestamp( date( 'Y-m-d H:00:00', $time - ( 2 * DAY_IN_SECONDS ) ) );
|
||||
$id = $object->save();
|
||||
|
||||
return array(
|
||||
'time' => $time,
|
||||
'product_1' => $product_1,
|
||||
'product_2' => $product_2,
|
||||
'order_1' => $order_1,
|
||||
'order_2' => $order_2,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting report with date filter.
|
||||
*/
|
||||
public function test_get_report_with_date_filter() {
|
||||
$test_info = $this->filter_setup();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
// Test date filtering.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'before' => date( 'Y-m-d H:00:00', $test_info['time'] + DAY_IN_SECONDS ),
|
||||
'after' => date( 'Y-m-d H:00:00', $test_info['time'] - DAY_IN_SECONDS ),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$download_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
$this->assertEquals( 'help.png', $download_report['file_name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting report with product filter.
|
||||
*/
|
||||
public function test_get_report_with_product_filter() {
|
||||
$test_info = $this->filter_setup();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
// Test includes filtering.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'product_includes' => $test_info['product_1'],
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$download_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
$this->assertEquals( 'help.png', $download_report['file_name'] );
|
||||
|
||||
// Test excludes filtering.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'product_excludes' => $test_info['product_1'],
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$download_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
$this->assertEquals( 'test.png', $download_report['file_name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting report with order filter.
|
||||
*/
|
||||
public function test_get_report_with_order_filter() {
|
||||
$test_info = $this->filter_setup();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
// Test includes filtering.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'order_includes' => $test_info['order_1'],
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$download_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
$this->assertEquals( 'help.png', $download_report['file_name'] );
|
||||
|
||||
// Test excludes filtering.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'order_excludes' => $test_info['order_1'],
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$download_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
$this->assertEquals( 'test.png', $download_report['file_name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting report with user filter.
|
||||
*/
|
||||
public function test_get_report_with_user_filter() {
|
||||
$test_info = $this->filter_setup();
|
||||
global $wpdb;
|
||||
|
||||
$customer_id = $wpdb->get_col(
|
||||
$wpdb->prepare(
|
||||
"SELECT customer_id FROM {$wpdb->prefix}wc_customer_lookup WHERE user_id = %d",
|
||||
1
|
||||
)
|
||||
);
|
||||
|
||||
// Test includes filtering.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'customer_includes' => $customer_id,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$download_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
$this->assertEquals( 'help.png', $download_report['file_name'] );
|
||||
$this->assertEquals( 1, $download_report['user_id'] );
|
||||
|
||||
// Test excludes filtering.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'customer_excludes' => $customer_id,
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$download_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
$this->assertEquals( 'test.png', $download_report['file_name'] );
|
||||
$this->assertEquals( 2, $download_report['user_id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting report with ip address filter.
|
||||
*/
|
||||
public function test_get_report_with_ip_address_filter() {
|
||||
$test_info = $this->filter_setup();
|
||||
|
||||
// Test includes filtering.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'ip_address_includes' => '1.2.3.4',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$download_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
$this->assertEquals( 'help.png', $download_report['file_name'] );
|
||||
|
||||
// Test excludes filtering.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'ip_address_excludes' => '1.2.3.4',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$download_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
$this->assertEquals( 'test.png', $download_report['file_name'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 12, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'product_id', $properties );
|
||||
$this->assertArrayHasKey( 'date', $properties );
|
||||
$this->assertArrayHasKey( 'date_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'download_id', $properties );
|
||||
$this->assertArrayHasKey( 'file_name', $properties );
|
||||
$this->assertArrayHasKey( 'file_path', $properties );
|
||||
$this->assertArrayHasKey( 'order_id', $properties );
|
||||
$this->assertArrayHasKey( 'order_number', $properties );
|
||||
$this->assertArrayHasKey( 'user_id', $properties );
|
||||
$this->assertArrayHasKey( 'username', $properties );
|
||||
$this->assertArrayHasKey( 'ip_address', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,397 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Import REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reports Import REST API Test Class
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
class WC_Tests_API_Reports_Import extends WC_REST_Unit_Test_Case {
|
||||
/**
|
||||
* Endpoint.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/import';
|
||||
|
||||
/**
|
||||
* Setup test reports products data.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
|
||||
$this->customer = $this->factory->user->create(
|
||||
array(
|
||||
'first_name' => 'Steve',
|
||||
'last_name' => 'User',
|
||||
'role' => 'customer',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the report item schema is correct.
|
||||
*
|
||||
* @param array $schema Item to check schema.
|
||||
*/
|
||||
public function assert_report_item_schema( $schema ) {
|
||||
$this->assertArrayHasKey( 'status', $schema );
|
||||
$this->assertArrayHasKey( 'message', $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertCount( 2, $properties );
|
||||
$this->assert_report_item_schema( $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'POST', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the import paramaters.
|
||||
*/
|
||||
public function test_import_params() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order_1 = WC_Helper_Order::create_order( 1, $product );
|
||||
$order_1->set_status( 'completed' );
|
||||
$order_1->set_date_created( time() - ( 3 * DAY_IN_SECONDS ) );
|
||||
$order_1->save();
|
||||
$order_2 = WC_Helper_Order::create_order( 1, $product );
|
||||
$order_2->set_total( 100 );
|
||||
$order_2->set_status( 'completed' );
|
||||
$order_2->save();
|
||||
|
||||
// Delete order stats so we can test import API.
|
||||
$wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type = 'scheduled-action'" );
|
||||
$wpdb->query( "DELETE FROM {$wpdb->prefix}wc_order_stats" );
|
||||
|
||||
// Use the days param to only process orders in the last day.
|
||||
$request = new WP_REST_Request( 'POST', $this->endpoint );
|
||||
$request->set_query_params( array( 'days' => '1' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$report = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'success', $report['status'] );
|
||||
|
||||
// Run pending thrice to process batch and order.
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v4/reports/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 2, $reports );
|
||||
$this->assertEquals( $this->customer, $reports[0]['user_id'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v4/reports/orders' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
$this->assertEquals( $order_2->get_id(), $reports[0]['order_id'] );
|
||||
|
||||
// Use the skip existing params to skip processing customers/orders.
|
||||
// Compare against order status to make sure previously imported order was skipped.
|
||||
$order_2->set_status( 'processing' );
|
||||
$order_2->save();
|
||||
|
||||
// Compare against name to make sure previously imported customer was skipped.
|
||||
wp_update_user(
|
||||
array(
|
||||
'ID' => $this->customer,
|
||||
'first_name' => 'Changed',
|
||||
)
|
||||
);
|
||||
|
||||
// Delete scheduled actions to avoid default order processing.
|
||||
$wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type = 'scheduled-action'" );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', $this->endpoint );
|
||||
$request->set_query_params( array( 'skip_existing' => '1' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$report = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'success', $report['status'] );
|
||||
|
||||
// Run pending thrice to process batch and order.
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v4/reports/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 2, $reports );
|
||||
$this->assertEquals( 'Steve User', $reports[0]['name'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v4/reports/orders' );
|
||||
$request->set_query_params( array( 'per_page' => 5 ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 2, $reports );
|
||||
$this->assertEquals( 'completed', $reports[0]['status'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test cancelling import actions.
|
||||
*/
|
||||
public function test_cancel_import() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_date_created( time() - ( 3 * DAY_IN_SECONDS ) );
|
||||
$order->save();
|
||||
|
||||
// Verify there are actions to cancel.
|
||||
$pending_actions = WC_Helper_Queue::get_all_pending();
|
||||
$this->assertCount( 1, $pending_actions );
|
||||
|
||||
// Cancel outstanding actions.
|
||||
$request = new WP_REST_Request( 'POST', $this->endpoint . '/cancel' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$report = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'success', $report['status'] );
|
||||
|
||||
// Verify there are no pending actions.
|
||||
$pending_actions = WC_Helper_Queue::get_all_pending();
|
||||
$this->assertCount( 0, $pending_actions );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test import deletion.
|
||||
*/
|
||||
public function test_delete_stats() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
for ( $i = 0; $i < 25; $i++ ) {
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->save();
|
||||
}
|
||||
|
||||
// Check that stats exist before deleting.
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v4/reports/orders' );
|
||||
$request->set_query_params( array( 'per_page' => 25 ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 25, $reports );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v4/reports/customers' );
|
||||
$request->set_query_params( array( 'per_page' => 25 ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
|
||||
// Delete all stats.
|
||||
$request = new WP_REST_Request( 'POST', $this->endpoint . '/delete' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$report = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'success', $report['status'] );
|
||||
|
||||
// Run pending three times to process batches and dependent actions.
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
// Check that stats have been deleted.
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v4/reports/orders' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 0, $reports );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v4/reports/customers' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 0, $reports );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test import status and totals query.
|
||||
*/
|
||||
public function test_import_status() {
|
||||
// Delete any pending actions that weren't fully run.
|
||||
WC_Admin_Reports_Sync::clear_queued_actions();
|
||||
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
// Create 5 completed orders.
|
||||
for ( $i = 0; $i < 5; $i++ ) {
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_date_created( time() - ( ( $i + 1 ) * DAY_IN_SECONDS ) );
|
||||
$order->save();
|
||||
}
|
||||
|
||||
// Trash one test order - excludes it from totals.
|
||||
$order->set_status( 'trash' );
|
||||
$order->save();
|
||||
|
||||
// Create 1 draft order - to be excluded from totals.
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_date_created( time() - ( 5 * DAY_IN_SECONDS ) );
|
||||
$order->save();
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $order->get_id(),
|
||||
'post_status' => 'auto-draft',
|
||||
)
|
||||
);
|
||||
|
||||
// Test totals and total params.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/totals' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$report = $response->get_data();
|
||||
$user_query = new WP_User_Query(
|
||||
array(
|
||||
'fields' => 'ID',
|
||||
'number' => 1,
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 4, $report['orders'] );
|
||||
$this->assertEquals( $user_query->get_total(), $report['customers'] );
|
||||
|
||||
// Test totals with days param.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/totals' );
|
||||
$request->set_query_params( array( 'days' => 2 ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$report = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, $report['orders'] );
|
||||
|
||||
// Test import status.
|
||||
$request = new WP_REST_Request( 'POST', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$report = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'success', $report['status'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/status' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$report = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( true, $report['is_importing'] );
|
||||
$this->assertEquals( 0, $report['customers_count'] );
|
||||
$this->assertEquals( 3, $report['customers_total'] );
|
||||
$this->assertEquals( 0, $report['orders_count'] );
|
||||
$this->assertEquals( 4, $report['orders_total'] );
|
||||
|
||||
// Run pending thrice to process batch and order.
|
||||
WC_Helper_Queue::process_pending();
|
||||
WC_Helper_Queue::process_pending();
|
||||
WC_Helper_Queue::process_pending();
|
||||
|
||||
// Test import status after processing.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/status' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$report = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( false, $report['is_importing'] );
|
||||
$this->assertEquals( 1, $report['customers_count'] );
|
||||
$this->assertEquals( 3, $report['customers_total'] );
|
||||
$this->assertEquals( 4, $report['orders_count'] );
|
||||
$this->assertEquals( 4, $report['orders_total'] );
|
||||
|
||||
// Test totals with skip existing param.
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/totals' );
|
||||
$request->set_query_params( array( 'skip_existing' => 1 ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$report = $response->get_data();
|
||||
|
||||
// @todo The following line should be uncommented when https://github.com/woocommerce/woocommerce-admin/issues/2195 is resolved.
|
||||
// $this->assertEquals( 0, $report['customers'] );
|
||||
$this->assertEquals( 0, $report['orders'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,925 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Interval Test
|
||||
*
|
||||
* @package WC Admin
|
||||
* @since x.x.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_Reports_Interval_Stats
|
||||
*/
|
||||
class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Local timezone used throughout the tests.
|
||||
*
|
||||
* @var DateTimeZone
|
||||
*/
|
||||
protected static $local_tz;
|
||||
|
||||
/**
|
||||
* Set current local timezone.
|
||||
*/
|
||||
public static function setUpBeforeClass() {
|
||||
self::$local_tz = new DateTimeZone( wc_timezone_string() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test quarter function.
|
||||
*/
|
||||
public function test_quarter() {
|
||||
$datetime = new DateTime( '2017-12-31T00:00:00', self::$local_tz );
|
||||
$this->assertEquals( 4, WC_Admin_Reports_Interval::quarter( $datetime ) );
|
||||
|
||||
$datetime = new DateTime( '2018-01-01T00:00:00', self::$local_tz );
|
||||
$this->assertEquals( 1, WC_Admin_Reports_Interval::quarter( $datetime ) );
|
||||
|
||||
$datetime = new DateTime( '2018-03-31T23:59:59', self::$local_tz );
|
||||
$this->assertEquals( 1, WC_Admin_Reports_Interval::quarter( $datetime ) );
|
||||
|
||||
$datetime = new DateTime( '2018-04-01T00:00:00', self::$local_tz );
|
||||
$this->assertEquals( 2, WC_Admin_Reports_Interval::quarter( $datetime ) );
|
||||
|
||||
$datetime = new DateTime( '2018-06-30T23:59:59', self::$local_tz );
|
||||
$this->assertEquals( 2, WC_Admin_Reports_Interval::quarter( $datetime ) );
|
||||
|
||||
$datetime = new DateTime( '2018-07-01T00:00:00', self::$local_tz );
|
||||
$this->assertEquals( 3, WC_Admin_Reports_Interval::quarter( $datetime ) );
|
||||
|
||||
$datetime = new DateTime( '2018-09-30T23:59:59', self::$local_tz );
|
||||
$this->assertEquals( 3, WC_Admin_Reports_Interval::quarter( $datetime ) );
|
||||
|
||||
$datetime = new DateTime( '2018-10-01T00:00:00', self::$local_tz );
|
||||
$this->assertEquals( 4, WC_Admin_Reports_Interval::quarter( $datetime ) );
|
||||
|
||||
$datetime = new DateTime( '2018-12-31T23:59:59', self::$local_tz );
|
||||
$this->assertEquals( 4, WC_Admin_Reports_Interval::quarter( $datetime ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test simple week number function.
|
||||
*/
|
||||
public function test_simple_week_number() {
|
||||
$expected_week_no = array(
|
||||
'2010-12-24' => array(
|
||||
1 => 52,
|
||||
2 => 52,
|
||||
3 => 52,
|
||||
4 => 52,
|
||||
5 => 52,
|
||||
6 => 52,
|
||||
7 => 52,
|
||||
),
|
||||
'2010-12-25' => array(
|
||||
1 => 52,
|
||||
2 => 52,
|
||||
3 => 52,
|
||||
4 => 52,
|
||||
5 => 52,
|
||||
6 => 53,
|
||||
7 => 52,
|
||||
),
|
||||
'2010-12-26' => array(
|
||||
1 => 52,
|
||||
2 => 52,
|
||||
3 => 52,
|
||||
4 => 52,
|
||||
5 => 52,
|
||||
6 => 53,
|
||||
7 => 53,
|
||||
),
|
||||
'2010-12-27' => array(
|
||||
1 => 53,
|
||||
2 => 52,
|
||||
3 => 52,
|
||||
4 => 52,
|
||||
5 => 52,
|
||||
6 => 53,
|
||||
7 => 53,
|
||||
),
|
||||
'2010-12-28' => array(
|
||||
1 => 53,
|
||||
2 => 53,
|
||||
3 => 52,
|
||||
4 => 52,
|
||||
5 => 52,
|
||||
6 => 53,
|
||||
7 => 53,
|
||||
),
|
||||
'2010-12-29' => array(
|
||||
1 => 53,
|
||||
2 => 53,
|
||||
3 => 53,
|
||||
4 => 52,
|
||||
5 => 52,
|
||||
6 => 53,
|
||||
7 => 53,
|
||||
),
|
||||
'2010-12-30' => array(
|
||||
1 => 53,
|
||||
2 => 53,
|
||||
3 => 53,
|
||||
4 => 53,
|
||||
5 => 52,
|
||||
6 => 53,
|
||||
7 => 53,
|
||||
),
|
||||
'2010-12-31' => array(
|
||||
1 => 53,
|
||||
2 => 53,
|
||||
3 => 53,
|
||||
4 => 53,
|
||||
5 => 53,
|
||||
6 => 53,
|
||||
7 => 53,
|
||||
),
|
||||
'2011-01-01' => array(
|
||||
1 => 1,
|
||||
2 => 1,
|
||||
3 => 1,
|
||||
4 => 1,
|
||||
5 => 1,
|
||||
6 => 1,
|
||||
7 => 1,
|
||||
),
|
||||
'2011-01-02' => array(
|
||||
1 => 1,
|
||||
2 => 1,
|
||||
3 => 1,
|
||||
4 => 1,
|
||||
5 => 1,
|
||||
6 => 1,
|
||||
7 => 2,
|
||||
),
|
||||
'2011-01-03' => array(
|
||||
1 => 2,
|
||||
2 => 1,
|
||||
3 => 1,
|
||||
4 => 1,
|
||||
5 => 1,
|
||||
6 => 1,
|
||||
7 => 2,
|
||||
),
|
||||
'2011-01-04' => array(
|
||||
1 => 2,
|
||||
2 => 2,
|
||||
3 => 1,
|
||||
4 => 1,
|
||||
5 => 1,
|
||||
6 => 1,
|
||||
7 => 2,
|
||||
),
|
||||
'2011-01-05' => array(
|
||||
1 => 2,
|
||||
2 => 2,
|
||||
3 => 2,
|
||||
4 => 1,
|
||||
5 => 1,
|
||||
6 => 1,
|
||||
7 => 2,
|
||||
),
|
||||
'2011-01-06' => array(
|
||||
1 => 2,
|
||||
2 => 2,
|
||||
3 => 2,
|
||||
4 => 2,
|
||||
5 => 1,
|
||||
6 => 1,
|
||||
7 => 2,
|
||||
),
|
||||
'2011-01-07' => array(
|
||||
1 => 2,
|
||||
2 => 2,
|
||||
3 => 2,
|
||||
4 => 2,
|
||||
5 => 2,
|
||||
6 => 1,
|
||||
7 => 2,
|
||||
),
|
||||
'2011-01-08' => array(
|
||||
1 => 2,
|
||||
2 => 2,
|
||||
3 => 2,
|
||||
4 => 2,
|
||||
5 => 2,
|
||||
6 => 2,
|
||||
7 => 2,
|
||||
),
|
||||
'2011-01-09' => array(
|
||||
1 => 2,
|
||||
2 => 2,
|
||||
3 => 2,
|
||||
4 => 2,
|
||||
5 => 2,
|
||||
6 => 2,
|
||||
7 => 3,
|
||||
),
|
||||
'2011-01-10' => array(
|
||||
1 => 3,
|
||||
2 => 2,
|
||||
3 => 2,
|
||||
4 => 2,
|
||||
5 => 2,
|
||||
6 => 2,
|
||||
7 => 3,
|
||||
),
|
||||
'2011-12-26' => array(
|
||||
1 => 53,
|
||||
2 => 52,
|
||||
3 => 52,
|
||||
4 => 52,
|
||||
5 => 52,
|
||||
6 => 52,
|
||||
7 => 53,
|
||||
),
|
||||
'2011-12-27' => array(
|
||||
1 => 53,
|
||||
2 => 53,
|
||||
3 => 52,
|
||||
4 => 52,
|
||||
5 => 52,
|
||||
6 => 52,
|
||||
7 => 53,
|
||||
),
|
||||
'2011-12-28' => array(
|
||||
1 => 53,
|
||||
2 => 53,
|
||||
3 => 53,
|
||||
4 => 52,
|
||||
5 => 52,
|
||||
6 => 52,
|
||||
7 => 53,
|
||||
),
|
||||
'2011-12-29' => array(
|
||||
1 => 53,
|
||||
2 => 53,
|
||||
3 => 53,
|
||||
4 => 53,
|
||||
5 => 52,
|
||||
6 => 52,
|
||||
7 => 53,
|
||||
),
|
||||
'2011-12-30' => array(
|
||||
1 => 53,
|
||||
2 => 53,
|
||||
3 => 53,
|
||||
4 => 53,
|
||||
5 => 53,
|
||||
6 => 52,
|
||||
7 => 53,
|
||||
),
|
||||
'2011-12-31' => array(
|
||||
1 => 53,
|
||||
2 => 53,
|
||||
3 => 53,
|
||||
4 => 53,
|
||||
5 => 53,
|
||||
6 => 53,
|
||||
7 => 53,
|
||||
),
|
||||
'2012-01-01' => array(
|
||||
1 => 1,
|
||||
2 => 1,
|
||||
3 => 1,
|
||||
4 => 1,
|
||||
5 => 1,
|
||||
6 => 1,
|
||||
7 => 1,
|
||||
),
|
||||
'2012-01-02' => array(
|
||||
1 => 2,
|
||||
2 => 1,
|
||||
3 => 1,
|
||||
4 => 1,
|
||||
5 => 1,
|
||||
6 => 1,
|
||||
7 => 1,
|
||||
),
|
||||
'2012-01-03' => array(
|
||||
1 => 2,
|
||||
2 => 2,
|
||||
3 => 1,
|
||||
4 => 1,
|
||||
5 => 1,
|
||||
6 => 1,
|
||||
7 => 1,
|
||||
),
|
||||
'2012-01-04' => array(
|
||||
1 => 2,
|
||||
2 => 2,
|
||||
3 => 2,
|
||||
4 => 1,
|
||||
5 => 1,
|
||||
6 => 1,
|
||||
7 => 1,
|
||||
),
|
||||
'2012-01-05' => array(
|
||||
1 => 2,
|
||||
2 => 2,
|
||||
3 => 2,
|
||||
4 => 2,
|
||||
5 => 1,
|
||||
6 => 1,
|
||||
7 => 1,
|
||||
),
|
||||
'2012-01-06' => array(
|
||||
1 => 2,
|
||||
2 => 2,
|
||||
3 => 2,
|
||||
4 => 2,
|
||||
5 => 2,
|
||||
6 => 1,
|
||||
7 => 1,
|
||||
),
|
||||
);
|
||||
|
||||
foreach ( $expected_week_no as $date => $week_numbers ) {
|
||||
for ( $first_day_of_week = 1; $first_day_of_week <= 7; $first_day_of_week++ ) {
|
||||
$datetime = new DateTime( $date, self::$local_tz );
|
||||
$this->assertEquals( $expected_week_no[ $date ][ $first_day_of_week ], WC_Admin_Reports_Interval::simple_week_number( $datetime, $first_day_of_week ), "First day of week: $first_day_of_week; Date: $date" );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing ISO week number function.
|
||||
*/
|
||||
public function test_ISO_week_no() {
|
||||
$expected_week_no = array(
|
||||
'2010-12-24' => 51,
|
||||
'2010-12-25' => 51,
|
||||
'2010-12-26' => 51,
|
||||
'2010-12-27' => 52,
|
||||
'2010-12-28' => 52,
|
||||
'2010-12-29' => 52,
|
||||
'2010-12-30' => 52,
|
||||
'2010-12-31' => 52,
|
||||
'2011-01-01' => 52,
|
||||
'2011-01-02' => 52,
|
||||
'2011-01-03' => 1,
|
||||
'2011-01-04' => 1,
|
||||
'2011-01-05' => 1,
|
||||
'2011-01-06' => 1,
|
||||
'2011-01-07' => 1,
|
||||
'2011-01-08' => 1,
|
||||
'2011-01-09' => 1,
|
||||
'2011-01-10' => 2,
|
||||
'2011-12-26' => 52,
|
||||
'2011-12-27' => 52,
|
||||
'2011-12-28' => 52,
|
||||
'2011-12-29' => 52,
|
||||
'2011-12-30' => 52,
|
||||
'2011-12-31' => 52,
|
||||
'2012-01-01' => 52,
|
||||
'2012-01-02' => 1,
|
||||
'2012-01-03' => 1,
|
||||
'2012-01-04' => 1,
|
||||
'2012-01-05' => 1,
|
||||
'2012-01-06' => 1,
|
||||
);
|
||||
foreach ( $expected_week_no as $date => $week_numbers ) {
|
||||
$datetime = new DateTime( $date, self::$local_tz );
|
||||
$this->assertEquals( $expected_week_no[ $date ], WC_Admin_Reports_Interval::week_number( $datetime, 1 ), "ISO week number for date: $date" );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function counting intervals between two datetimes.
|
||||
*/
|
||||
public function test_intervals_between() {
|
||||
// Please note that all intervals are inclusive on both sides.
|
||||
$test_settings = array(
|
||||
// 0 interval length, should just return 1.
|
||||
array(
|
||||
'start' => '2017-12-24T11:00:00',
|
||||
'end' => '2017-12-24T11:00:00',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 1,
|
||||
'day' => 1,
|
||||
'week' => 1,
|
||||
'month' => 1,
|
||||
'quarter' => 1,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// <1 hour interval length -> should return 1 for all
|
||||
array(
|
||||
'start' => '2017-12-24T11:00:00',
|
||||
'end' => '2017-12-24T11:40:00',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 1,
|
||||
'day' => 1,
|
||||
'week' => 1,
|
||||
'month' => 1,
|
||||
'quarter' => 1,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// 1.66 hour interval length -> 2 hours, 1 for the rest
|
||||
array(
|
||||
'start' => '2017-12-24T11:00:00',
|
||||
'end' => '2017-12-24T12:40:00',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 2,
|
||||
'day' => 1,
|
||||
'week' => 1,
|
||||
'month' => 1,
|
||||
'quarter' => 1,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// 8.0186111 hours interval length -> 10 hours, 2 days, 1 for the rest
|
||||
array(
|
||||
'start' => '2019-01-16T16:59:00',
|
||||
'end' => '2019-01-17T01:00:07',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 10,
|
||||
'day' => 2,
|
||||
'week' => 1,
|
||||
'month' => 1,
|
||||
'quarter' => 1,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// 23:59:59 h:m:s interval -> 24 hours, 2 days, 1 for the rest
|
||||
array(
|
||||
'start' => '2017-12-24T11:00:00',
|
||||
'end' => '2017-12-25T10:59:59',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 24,
|
||||
'day' => 2,
|
||||
'week' => 2,
|
||||
'month' => 1,
|
||||
'quarter' => 1,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// 24 hour inclusive interval -> 25 hours, 2 days, 1 for the rest
|
||||
array(
|
||||
'start' => '2017-12-24T11:00:00',
|
||||
'end' => '2017-12-25T11:00:00',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 25,
|
||||
'day' => 2,
|
||||
'week' => 2,
|
||||
'month' => 1,
|
||||
'quarter' => 1,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// 1 month interval spanning 1 month -> 720 hours, 30 days, 5 iso weeks, 1 months
|
||||
array(
|
||||
'start' => '2017-11-01T00:00:00',
|
||||
'end' => '2017-11-30T23:59:59',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 720,
|
||||
'day' => 30,
|
||||
'week' => 5,
|
||||
'month' => 1,
|
||||
'quarter' => 1,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// 1 month interval spanning 2 months, but 1 quarter and 1 year -> 721 hours, 31 days, 5 iso weeks, 2 months
|
||||
array(
|
||||
'start' => '2017-11-24T11:00:00',
|
||||
'end' => '2017-12-24T11:00:00',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 30 * 24 + 1, // 30 full days + 1 second from next hour
|
||||
'day' => 31,
|
||||
'week' => 5,
|
||||
'month' => 2,
|
||||
'quarter' => 1,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// 1 month + 14 hour interval spanning 2 months in 1 quarter -> 735 hours, 32 days, 6 iso weeks, 2 months
|
||||
array(
|
||||
'start' => '2017-11-24T11:00:00',
|
||||
'end' => '2017-12-25T01:00:00',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 30 * 24 + 13 + 2, // 30 full days + 13 full hours on Nov 24 + 2 hours on Dec 25
|
||||
'day' => 32,
|
||||
'week' => 6,
|
||||
'month' => 2,
|
||||
'quarter' => 1,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// 1 month interval spanning 2 months and 2 quarters, 1 year -> 720 hours, 31 days, 6 iso weeks, 2 months, 2 q, 1 y
|
||||
array(
|
||||
'start' => '2017-09-24T11:00:00',
|
||||
'end' => '2017-10-24T10:59:59',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 30 * 24,
|
||||
'day' => 31, // Sept has 30 days, but need to include interval for half of Sept 24 and interval for half of Oct 24.
|
||||
'week' => 6,
|
||||
'month' => 2,
|
||||
'quarter' => 2,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// 1 month interval spanning 2 months and 2 quarters, 2 years -> 744 hours, 32 days, 6 iso weeks, 2 months, 2 quarters, 2 years
|
||||
array(
|
||||
'start' => '2017-12-24T11:00:00',
|
||||
'end' => '2018-01-24T10:59:59',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 31 * 24,
|
||||
'day' => 32, // Dec has 31 days, plus 1 interval for half day at the end.
|
||||
'week' => 6,
|
||||
'month' => 2,
|
||||
'quarter' => 2,
|
||||
'year' => 2,
|
||||
),
|
||||
),
|
||||
// 3 months interval spanning 1 quarter, 1 year -> 2208 hours, 92 days, 14 iso weeks, 3 months, 1 quarters, 1 years
|
||||
array(
|
||||
'start' => '2017-10-01T00:00:00',
|
||||
'end' => '2017-12-31T23:59:59',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 92 * 24, // 92 days
|
||||
'day' => 92,
|
||||
'week' => 14,
|
||||
'month' => 3,
|
||||
'quarter' => 1,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// 3 months + 1 day interval spanning 2 quarters, 1 year -> 2208 hours, 92 days, 14 iso weeks, 3 months, 2 quarters, 1 years
|
||||
array(
|
||||
'start' => '2017-09-30T00:00:00',
|
||||
'end' => '2017-12-30T23:59:59',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 92 * 24, // 92 days
|
||||
'day' => 92,
|
||||
'week' => 14,
|
||||
'month' => 4,
|
||||
'quarter' => 2,
|
||||
'year' => 1,
|
||||
),
|
||||
),
|
||||
// 3 months + 1 day interval spanning 2 quarters, 2 years -> 2232 hours, 93 days, 14 iso weeks, 3 months, 2 quarters, 2 years
|
||||
array(
|
||||
'start' => '2017-10-31T00:00:00',
|
||||
'end' => '2018-01-31T23:59:59',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'hour' => 93 * 24, // 93 days
|
||||
'day' => 93, // Jan 31d + Dec 31d + Nov 30d + Oct 1d = 93d.
|
||||
'week' => 14,
|
||||
'month' => 4,
|
||||
'quarter' => 2,
|
||||
'year' => 2,
|
||||
),
|
||||
),
|
||||
// 9 months + 1 day interval spanning 2 quarters, 2 years.
|
||||
array(
|
||||
'start' => '2017-04-01T00:00:00',
|
||||
'end' => '2018-01-01T00:00:00',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'month' => 9 + 1,
|
||||
'quarter' => 3 + 1,
|
||||
'year' => 2,
|
||||
),
|
||||
),
|
||||
// 9 months + 1 day interval spanning 2 quarters, 2 years.
|
||||
array(
|
||||
'start' => '2015-04-01T00:00:00',
|
||||
'end' => '2018-01-01T00:00:00',
|
||||
'week_start' => 1,
|
||||
'intervals' => array(
|
||||
'day' => 1007, // This includes leap year in 2016.
|
||||
'month' => 9 + 12 + 12 + 1, // Rest of 2015 + 2016 + 2017 + 1 second in 2018.
|
||||
'quarter' => 3 + 4 + 4 + 1, // Rest of 2015 + 2016 + 2017 + 1 second in 2018.
|
||||
'year' => 4,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
foreach ( $test_settings as $setting ) {
|
||||
update_option( 'start_of_week', $setting['week_start'] );
|
||||
foreach ( $setting['intervals'] as $interval => $exp_value ) {
|
||||
$start_datetime = new DateTime( $setting['start'], self::$local_tz );
|
||||
$end_datetime = new DateTime( $setting['end'], self::$local_tz );
|
||||
$this->assertEquals( $exp_value, WC_Admin_Reports_Interval::intervals_between( $start_datetime, $end_datetime, $interval ), "First Day of Week: {$setting['week_start']}; Start: {$setting['start']}; End: {$setting['end']}; Interval: {$interval}" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function that returns beginning of next hour.
|
||||
*/
|
||||
public function test_next_hour_start() {
|
||||
$settings = array(
|
||||
'2017-12-30T00:00:00' => array(
|
||||
0 => '2017-12-30T01:00:00',
|
||||
1 => '2017-12-29T23:59:59',
|
||||
),
|
||||
'2017-12-30T10:00:00' => array(
|
||||
0 => '2017-12-30T11:00:00',
|
||||
1 => '2017-12-30T09:59:59',
|
||||
),
|
||||
);
|
||||
foreach ( $settings as $datetime_s => $setting ) {
|
||||
$datetime = new DateTime( $datetime_s, self::$local_tz );
|
||||
foreach ( $setting as $reversed => $exp_value ) {
|
||||
$result_dt = WC_Admin_Reports_Interval::next_hour_start( $datetime, $reversed );
|
||||
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function that returns beginning of next day.
|
||||
*/
|
||||
public function test_next_day_start() {
|
||||
$settings = array(
|
||||
'2017-12-30T00:00:00' => array(
|
||||
0 => '2017-12-31T00:00:00',
|
||||
1 => '2017-12-29T23:59:59',
|
||||
),
|
||||
'2017-12-30T10:00:00' => array(
|
||||
0 => '2017-12-31T00:00:00',
|
||||
1 => '2017-12-29T23:59:59',
|
||||
),
|
||||
);
|
||||
foreach ( $settings as $datetime_s => $setting ) {
|
||||
$datetime = new DateTime( $datetime_s, self::$local_tz );
|
||||
foreach ( $setting as $reversed => $exp_value ) {
|
||||
$result_dt = WC_Admin_Reports_Interval::next_day_start( $datetime, $reversed );
|
||||
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function that returns beginning of next week, for weeks starting on Monday.
|
||||
*/
|
||||
public function test_next_week_start_ISO_week() {
|
||||
update_option( 'start_of_week', 1 );
|
||||
$settings = array(
|
||||
'2017-12-30T00:00:00' => array(
|
||||
0 => '2018-01-01T00:00:00',
|
||||
1 => '2017-12-24T23:59:59',
|
||||
),
|
||||
'2017-12-30T10:00:00' => array(
|
||||
0 => '2018-01-01T00:00:00',
|
||||
1 => '2017-12-24T23:59:59',
|
||||
),
|
||||
'2010-12-25T10:00:00' => array(
|
||||
0 => '2010-12-27T00:00:00',
|
||||
1 => '2010-12-19T23:59:59',
|
||||
),
|
||||
'2010-12-26T10:00:00' => array(
|
||||
0 => '2010-12-27T00:00:00',
|
||||
1 => '2010-12-19T23:59:59',
|
||||
),
|
||||
'2010-12-27T00:00:00' => array(
|
||||
0 => '2011-01-03T00:00:00',
|
||||
1 => '2010-12-26T23:59:59',
|
||||
),
|
||||
'2010-12-31T00:00:00' => array(
|
||||
0 => '2011-01-03T00:00:00',
|
||||
1 => '2010-12-26T23:59:59',
|
||||
),
|
||||
'2011-01-01T00:00:00' => array(
|
||||
0 => '2011-01-03T00:00:00',
|
||||
1 => '2010-12-26T23:59:59',
|
||||
),
|
||||
'2011-01-03T00:00:00' => array(
|
||||
0 => '2011-01-10T00:00:00',
|
||||
1 => '2011-01-02T23:59:59',
|
||||
),
|
||||
);
|
||||
foreach ( $settings as $datetime_s => $setting ) {
|
||||
$datetime = new DateTime( $datetime_s, self::$local_tz );
|
||||
foreach ( $setting as $reversed => $exp_value ) {
|
||||
$result_dt = WC_Admin_Reports_Interval::next_week_start( $datetime, $reversed );
|
||||
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function that returns beginning of next week, for weeks starting on Sunday.
|
||||
*/
|
||||
public function test_next_week_start_Sunday_based_week() {
|
||||
update_option( 'start_of_week', 7 );
|
||||
$settings = array(
|
||||
'2010-12-25T10:00:00' => array(
|
||||
0 => '2010-12-26T00:00:00',
|
||||
1 => '2010-12-18T23:59:59',
|
||||
),
|
||||
'2010-12-26T10:00:00' => array(
|
||||
0 => '2011-01-01T00:00:00',
|
||||
1 => '2010-12-25T23:59:59',
|
||||
),
|
||||
'2011-01-01T00:00:00' => array(
|
||||
0 => '2011-01-02T00:00:00',
|
||||
1 => '2010-12-31T23:59:59',
|
||||
),
|
||||
'2011-01-02T00:00:00' => array(
|
||||
0 => '2011-01-09T00:00:00',
|
||||
1 => '2011-01-01T23:59:59',
|
||||
),
|
||||
);
|
||||
foreach ( $settings as $datetime_s => $setting ) {
|
||||
$datetime = new DateTime( $datetime_s, self::$local_tz );
|
||||
foreach ( $setting as $reversed => $exp_value ) {
|
||||
$result_dt = WC_Admin_Reports_Interval::next_week_start( $datetime, $reversed );
|
||||
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function that returns beginning of next month.
|
||||
*/
|
||||
public function test_next_month_start() {
|
||||
$settings = array(
|
||||
'2017-12-30T00:00:00' => array(
|
||||
0 => '2018-01-01T00:00:00',
|
||||
1 => '2017-11-30T23:59:59',
|
||||
),
|
||||
// Leap year reversed test.
|
||||
'2016-03-05T10:00:00' => array(
|
||||
0 => '2016-04-01T00:00:00',
|
||||
1 => '2016-02-29T23:59:59',
|
||||
),
|
||||
);
|
||||
foreach ( $settings as $datetime_s => $setting ) {
|
||||
$datetime = new DateTime( $datetime_s, self::$local_tz );
|
||||
foreach ( $setting as $reversed => $exp_value ) {
|
||||
$result_dt = WC_Admin_Reports_Interval::next_month_start( $datetime, $reversed );
|
||||
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function that returns beginning of next quarter.
|
||||
*/
|
||||
public function test_next_quarter_start() {
|
||||
$settings = array(
|
||||
'2017-12-31T00:00:00' => array(
|
||||
0 => '2018-01-01T00:00:00',
|
||||
1 => '2017-09-30T23:59:59',
|
||||
),
|
||||
'2018-01-01T10:00:00' => array(
|
||||
0 => '2018-04-01T00:00:00',
|
||||
1 => '2017-12-31T23:59:59',
|
||||
),
|
||||
'2018-02-14T10:00:00' => array(
|
||||
0 => '2018-04-01T00:00:00',
|
||||
1 => '2017-12-31T23:59:59',
|
||||
),
|
||||
'2018-04-14T10:00:00' => array(
|
||||
0 => '2018-07-01T00:00:00',
|
||||
1 => '2018-03-31T23:59:59',
|
||||
),
|
||||
'2018-07-14T10:00:00' => array(
|
||||
0 => '2018-10-01T00:00:00',
|
||||
1 => '2018-06-30T23:59:59',
|
||||
),
|
||||
);
|
||||
foreach ( $settings as $datetime_s => $setting ) {
|
||||
$datetime = new DateTime( $datetime_s, self::$local_tz );
|
||||
foreach ( $setting as $reversed => $exp_value ) {
|
||||
$result_dt = WC_Admin_Reports_Interval::next_quarter_start( $datetime, $reversed );
|
||||
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function that returns beginning of next year.
|
||||
*/
|
||||
public function test_next_year_start() {
|
||||
$settings = array(
|
||||
'2017-12-31T23:59:59' => array(
|
||||
0 => '2018-01-01T00:00:00',
|
||||
1 => '2016-12-31T23:59:59',
|
||||
),
|
||||
'2017-01-01T00:00:00' => array(
|
||||
0 => '2018-01-01T00:00:00',
|
||||
1 => '2016-12-31T23:59:59',
|
||||
),
|
||||
'2017-04-23T14:53:00' => array(
|
||||
0 => '2018-01-01T00:00:00',
|
||||
1 => '2016-12-31T23:59:59',
|
||||
),
|
||||
);
|
||||
foreach ( $settings as $datetime_s => $setting ) {
|
||||
$datetime = new DateTime( $datetime_s, self::$local_tz );
|
||||
foreach ( $setting as $reversed => $exp_value ) {
|
||||
$result_dt = WC_Admin_Reports_Interval::next_year_start( $datetime, $reversed );
|
||||
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function that normalizes *_between query parameters to *_min & *_max.
|
||||
*/
|
||||
public function test_normalize_between_params() {
|
||||
$request = array(
|
||||
'a_between' => 'malformed', // won't be normalized (not an array).
|
||||
'b_between' => array( 1, 5 ), // results in min=1, max=5.
|
||||
'c_between' => array( 4, 2 ), // results in min=2, max=4.
|
||||
'd_between' => array( 7 ), // won't be normalized (only 1 item).
|
||||
'f_between' => array( 10, 12 ), // not in params, skipped.
|
||||
);
|
||||
$params = array( 'a', 'b', 'c', 'd' );
|
||||
$result = WC_Admin_Reports_Interval::normalize_between_params( $request, $params, false );
|
||||
$expected = array(
|
||||
'b_min' => 1,
|
||||
'b_max' => 5,
|
||||
'c_min' => 2,
|
||||
'c_max' => 4,
|
||||
);
|
||||
|
||||
$this->assertEquals( $result, $expected );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function that normalizes *_between query parameters for dates to *_after & *_before.
|
||||
*/
|
||||
public function test_normalize_between_date_params() {
|
||||
$request = array(
|
||||
'a_between' => 'malformed', // won't be normalized (not an array).
|
||||
'b_between' => array( 1, 5 ), // results in after=1, before=5.
|
||||
'c_between' => array( 4, 2 ), // results in after=2, before=4.
|
||||
'd_between' => array( 7 ), // won't be normalized (only 1 item).
|
||||
'f_between' => array( 10, 12 ), // not in params, skipped.
|
||||
);
|
||||
$params = array( 'a', 'b', 'c', 'd' );
|
||||
$result = WC_Admin_Reports_Interval::normalize_between_params( $request, $params, true );
|
||||
$expected = array(
|
||||
'b_after' => 1,
|
||||
'b_before' => 5,
|
||||
'c_after' => 2,
|
||||
'c_before' => 4,
|
||||
);
|
||||
|
||||
$this->assertEquals( $result, $expected );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function that validates *_between query parameters for numeric values.
|
||||
*/
|
||||
public function test_rest_validate_between_numeric_arg() {
|
||||
$this->assertIsWPError(
|
||||
WC_Admin_Reports_Interval::rest_validate_between_numeric_arg( 'not array', null, 'param' ),
|
||||
'param is not a numerically indexed array.'
|
||||
);
|
||||
|
||||
$this->assertIsWPError(
|
||||
WC_Admin_Reports_Interval::rest_validate_between_numeric_arg( array( 1 ), null, 'param' ),
|
||||
'param must contain 2 numbers.'
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
WC_Admin_Reports_Interval::rest_validate_between_numeric_arg( array( 1, 2 ), null, 'param' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test function that validates *_between query parameters for date values.
|
||||
*/
|
||||
public function rest_validate_between_date_arg() {
|
||||
$this->assertIsWPError(
|
||||
WC_Admin_Reports_Interval::rest_validate_between_date_arg( 'not array', null, 'param' ),
|
||||
'param is not a numerically indexed array.'
|
||||
);
|
||||
|
||||
$this->assertIsWPError(
|
||||
WC_Admin_Reports_Interval::rest_validate_between_date_arg( array( '2019-01-01T00:00:00' ), null, 'param' ),
|
||||
'param must contain 2 valid dates.'
|
||||
);
|
||||
|
||||
$this->assertIsWPError(
|
||||
WC_Admin_Reports_Interval::rest_validate_between_date_arg( array( 'not a valid date' ), null, 'param' ),
|
||||
'param must contain 2 valid dates.'
|
||||
);
|
||||
|
||||
$this->assertTrue(
|
||||
WC_Admin_Reports_Interval::rest_validate_between_date_arg( array( '2019-01-01T00:00:00', '2019-01-15T00:00:00' ), null, 'param' )
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Orders Stats REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC_Tests_API_Reports_Orders_Stats
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Reports_Orders_Stats
|
||||
*/
|
||||
class WC_Tests_API_Reports_Orders_Stats extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/orders/stats';
|
||||
|
||||
/**
|
||||
* Setup test reports orders data.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// @todo Update after report interface is done.
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) ); // totals and intervals.
|
||||
// @todo Update results after implement report interface.
|
||||
// $this->assertEquals( array(), $reports ); // @codingStandardsIgnoreLine.
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 2, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'totals', $properties );
|
||||
$this->assertArrayHasKey( 'intervals', $properties );
|
||||
|
||||
$totals = $properties['totals']['properties'];
|
||||
$this->assertEquals( 11, count( $totals ) );
|
||||
$this->assertArrayHasKey( 'net_revenue', $totals );
|
||||
$this->assertArrayHasKey( 'avg_order_value', $totals );
|
||||
$this->assertArrayHasKey( 'orders_count', $totals );
|
||||
$this->assertArrayHasKey( 'avg_items_per_order', $totals );
|
||||
$this->assertArrayHasKey( 'num_items_sold', $totals );
|
||||
$this->assertArrayHasKey( 'coupons', $totals );
|
||||
$this->assertArrayHasKey( 'coupons_count', $totals );
|
||||
$this->assertArrayHasKey( 'num_returning_customers', $totals );
|
||||
$this->assertArrayHasKey( 'num_new_customers', $totals );
|
||||
$this->assertArrayHasKey( 'products', $totals );
|
||||
$this->assertArrayHasKey( 'segments', $totals );
|
||||
|
||||
$intervals = $properties['intervals']['items']['properties'];
|
||||
$this->assertEquals( 6, count( $intervals ) );
|
||||
$this->assertArrayHasKey( 'interval', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'subtotals', $intervals );
|
||||
|
||||
$subtotals = $properties['intervals']['items']['properties']['subtotals']['properties'];
|
||||
$this->assertEquals( 10, count( $subtotals ) );
|
||||
$this->assertArrayHasKey( 'net_revenue', $subtotals );
|
||||
$this->assertArrayHasKey( 'avg_order_value', $subtotals );
|
||||
$this->assertArrayHasKey( 'orders_count', $subtotals );
|
||||
$this->assertArrayHasKey( 'avg_items_per_order', $subtotals );
|
||||
$this->assertArrayHasKey( 'num_items_sold', $subtotals );
|
||||
$this->assertArrayHasKey( 'coupons', $subtotals );
|
||||
$this->assertArrayHasKey( 'coupons_count', $subtotals );
|
||||
$this->assertArrayHasKey( 'num_returning_customers', $subtotals );
|
||||
$this->assertArrayHasKey( 'num_new_customers', $subtotals );
|
||||
$this->assertArrayHasKey( 'segments', $subtotals );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Orders REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reports Orders REST API Test Class
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_API_Reports_Orders extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/orders';
|
||||
|
||||
/**
|
||||
* Setup test reports orders data.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$expected_customer_id = WC_Admin_Reports_Customers_Data_Store::get_customer_id_by_user_id( 1 );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
|
||||
$order_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( $order->get_id(), $order_report['order_id'] );
|
||||
$this->assertEquals( $order->get_order_number(), $order_report['order_number'] );
|
||||
$this->assertEquals( date( 'Y-m-d H:i:s', $order->get_date_created()->getTimestamp() ), $order_report['date_created'] );
|
||||
$this->assertEquals( $expected_customer_id, $order_report['customer_id'] );
|
||||
$this->assertEquals( 4, $order_report['num_items_sold'] );
|
||||
$this->assertEquals( 90.0, $order_report['net_total'] ); // 25 x 4 - 10 (shipping)
|
||||
$this->assertEquals( 'new', $order_report['customer_type'] );
|
||||
$this->assertArrayHasKey( '_links', $order_report );
|
||||
$this->assertArrayHasKey( 'order', $order_report['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 9, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'order_id', $properties );
|
||||
$this->assertArrayHasKey( 'order_number', $properties );
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'status', $properties );
|
||||
$this->assertArrayHasKey( 'customer_id', $properties );
|
||||
$this->assertArrayHasKey( 'net_total', $properties );
|
||||
$this->assertArrayHasKey( 'num_items_sold', $properties );
|
||||
$this->assertArrayHasKey( 'customer_type', $properties );
|
||||
$this->assertArrayHasKey( 'extended_info', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,183 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Performance indicators REST API Tests
|
||||
*
|
||||
* @package WooCommerce Admin\Tests\API.
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC_Tests_API_Reports_Performance_Indicators
|
||||
*/
|
||||
class WC_Tests_API_Reports_Performance_Indicators extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/performance-indicators';
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
$this->assertArrayHasKey( $this->endpoint . '/allowed', $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting indicators.
|
||||
*/
|
||||
public function test_get_indicators() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data. We'll create an order and a download.
|
||||
$prod_download = new WC_Product_Download();
|
||||
$prod_download->set_file( plugin_dir_url( __FILE__ ) . '/assets/images/help.png' );
|
||||
$prod_download->set_id( 1 );
|
||||
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_downloadable( 'yes' );
|
||||
$product->set_downloads( array( $prod_download ) );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 25 );
|
||||
$order->save();
|
||||
|
||||
$download = new WC_Customer_Download();
|
||||
$download->set_user_id( $this->user );
|
||||
$download->set_order_id( $order->get_id() );
|
||||
$download->set_product_id( $product->get_id() );
|
||||
$download->set_download_id( $prod_download->get_id() );
|
||||
$download->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '1.2.3.4' );
|
||||
$object->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '1.2.3.4' );
|
||||
$object->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$time = time();
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'before' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'after' => date( 'Y-m-d H:00:00', $time - ( 7 * DAY_IN_SECONDS ) ),
|
||||
'stats' => 'orders/orders_count,downloads/download_count,test/bogus_stat',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
$this->assertEquals( 'orders/orders_count', $reports[0]['stat'] );
|
||||
$this->assertEquals( 'Amount of orders', $reports[0]['label'] );
|
||||
$this->assertEquals( 1, $reports[0]['value'] );
|
||||
$this->assertEquals( 'orders_count', $reports[0]['chart'] );
|
||||
$this->assertEquals( '/analytics/orders', $response->data[0]['_links']['report'][0]['href'] );
|
||||
|
||||
$this->assertEquals( 'downloads/download_count', $reports[1]['stat'] );
|
||||
$this->assertEquals( 'Number of downloads', $reports[1]['label'] );
|
||||
$this->assertEquals( 2, $reports[1]['value'] );
|
||||
$this->assertEquals( 'download_count', $reports[1]['chart'] );
|
||||
$this->assertEquals( '/analytics/downloads', $response->data[1]['_links']['report'][0]['href'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting indicators with an empty request.
|
||||
*/
|
||||
public function test_get_indicators_empty_request() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
$time = time();
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'before' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'after' => date( 'Y-m-d H:00:00', $time - ( 7 * DAY_IN_SECONDS ) ),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 500, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting without valid permissions.
|
||||
*/
|
||||
public function test_get_indicators_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test schema.
|
||||
*/
|
||||
public function test_indicators_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 5, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'stat', $properties );
|
||||
$this->assertArrayHasKey( 'chart', $properties );
|
||||
$this->assertArrayHasKey( 'label', $properties );
|
||||
$this->assertArrayHasKey( 'format', $properties );
|
||||
$this->assertArrayHasKey( 'value', $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test schema for /allowed indicators endpoint.
|
||||
*/
|
||||
public function test_indicators_schema_allowed() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint . '/allowed' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 3, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'stat', $properties );
|
||||
$this->assertArrayHasKey( 'chart', $properties );
|
||||
$this->assertArrayHasKey( 'label', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,171 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Products Stats REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Reports_Products_Stats
|
||||
*/
|
||||
class WC_Tests_API_Reports_Products_Stats extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/products/stats';
|
||||
|
||||
/**
|
||||
* Setup test reports products stats data.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$time = time();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_shipping_total( 10 );
|
||||
$order->set_discount_total( 20 );
|
||||
$order->set_discount_tax( 0 );
|
||||
$order->set_cart_tax( 5 );
|
||||
$order->set_shipping_tax( 2 );
|
||||
$order->set_total( 97 ); // $25x4 products + $10 shipping - $20 discount + $7 tax.
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'before' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'after' => date( 'Y-m-d 00:00:00', $time ),
|
||||
'interval' => 'day',
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$expected_reports = array(
|
||||
'totals' => array(
|
||||
'items_sold' => 4,
|
||||
'net_revenue' => 100.0,
|
||||
'orders_count' => 1,
|
||||
'products_count' => 1,
|
||||
'variations_count' => 1,
|
||||
'segments' => array(),
|
||||
),
|
||||
'intervals' => array(
|
||||
array(
|
||||
'interval' => date( 'Y-m-d', $time ),
|
||||
'date_start' => date( 'Y-m-d 00:00:00', $time ),
|
||||
'date_start_gmt' => date( 'Y-m-d 00:00:00', $time ),
|
||||
'date_end' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'date_end_gmt' => date( 'Y-m-d 23:59:59', $time ),
|
||||
'subtotals' => (object) array(
|
||||
'items_sold' => 4,
|
||||
'net_revenue' => 100.0,
|
||||
'orders_count' => 1,
|
||||
'products_count' => 1,
|
||||
'variations_count' => 1,
|
||||
'segments' => array(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( $expected_reports, $reports );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 2, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'totals', $properties );
|
||||
$this->assertArrayHasKey( 'intervals', $properties );
|
||||
|
||||
$totals = $properties['totals']['properties'];
|
||||
$this->assertEquals( 4, count( $totals ) );
|
||||
$this->assertArrayHasKey( 'net_revenue', $totals );
|
||||
$this->assertArrayHasKey( 'items_sold', $totals );
|
||||
$this->assertArrayHasKey( 'orders_count', $totals );
|
||||
$this->assertArrayHasKey( 'segments', $totals );
|
||||
|
||||
$intervals = $properties['intervals']['items']['properties'];
|
||||
$this->assertEquals( 6, count( $intervals ) );
|
||||
$this->assertArrayHasKey( 'interval', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'subtotals', $intervals );
|
||||
|
||||
$subtotals = $properties['intervals']['items']['properties']['subtotals']['properties'];
|
||||
$this->assertEquals( 4, count( $subtotals ) );
|
||||
$this->assertArrayHasKey( 'net_revenue', $subtotals );
|
||||
$this->assertArrayHasKey( 'items_sold', $subtotals );
|
||||
$this->assertArrayHasKey( 'orders_count', $subtotals );
|
||||
$this->assertArrayHasKey( 'segments', $subtotals );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,174 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Products REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reports Products REST API Test Class
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
class WC_Tests_API_Reports_Products extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/products';
|
||||
|
||||
/**
|
||||
* Setup test reports products data.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
|
||||
$product_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( $product->get_id(), $product_report['product_id'] );
|
||||
$this->assertEquals( 4, $product_report['items_sold'] );
|
||||
$this->assertEquals( 1, $product_report['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $product_report );
|
||||
$this->assertArrayHasKey( 'product', $product_report['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports with the `products` param.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_products_param() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$product_2 = new WC_Product_Simple();
|
||||
$product_2->set_name( 'Test Product 2' );
|
||||
$product_2->set_regular_price( 25 );
|
||||
$product_2->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'products' => $product->get_id() . ',' . $product_2->get_id(),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
$product_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( $product->get_id(), $product_report['product_id'] );
|
||||
$this->assertEquals( 4, $product_report['items_sold'] );
|
||||
$this->assertEquals( 1, $product_report['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $product_report );
|
||||
$this->assertArrayHasKey( 'product', $product_report['_links'] );
|
||||
|
||||
$product_report = next( $reports );
|
||||
|
||||
$this->assertEquals( $product_2->get_id(), $product_report['product_id'] );
|
||||
$this->assertEquals( null, $product_report['items_sold'] );
|
||||
$this->assertEquals( null, $product_report['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $product_report );
|
||||
$this->assertArrayHasKey( 'product', $product_report['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 5, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'product_id', $properties );
|
||||
$this->assertArrayHasKey( 'items_sold', $properties );
|
||||
$this->assertArrayHasKey( 'net_revenue', $properties );
|
||||
$this->assertArrayHasKey( 'orders_count', $properties );
|
||||
$this->assertArrayHasKey( 'extended_info', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Revenue Stats REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Reports_Revenue_Stats
|
||||
*/
|
||||
class WC_Tests_API_Reports_Revenue_Stats extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/revenue/stats';
|
||||
|
||||
/**
|
||||
* Orders
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $orders = array();
|
||||
|
||||
/**
|
||||
* Setup test reports revenue data.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
// @todo update after report interface is done.
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $data ) ); // @todo Update results after implement report interface.
|
||||
// $this->assertEquals( array(), $reports ); // @todo Update results after implement report interface.
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 2, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'totals', $properties );
|
||||
$this->assertArrayHasKey( 'intervals', $properties );
|
||||
|
||||
$totals = $properties['totals']['properties'];
|
||||
$this->assertEquals( 11, count( $totals ) );
|
||||
$this->assertArrayHasKey( 'gross_revenue', $totals );
|
||||
$this->assertArrayHasKey( 'net_revenue', $totals );
|
||||
$this->assertArrayHasKey( 'coupons', $totals );
|
||||
$this->assertArrayHasKey( 'coupons_count', $totals );
|
||||
$this->assertArrayHasKey( 'shipping', $totals );
|
||||
$this->assertArrayHasKey( 'taxes', $totals );
|
||||
$this->assertArrayHasKey( 'refunds', $totals );
|
||||
$this->assertArrayHasKey( 'orders_count', $totals );
|
||||
$this->assertArrayHasKey( 'num_items_sold', $totals );
|
||||
$this->assertArrayHasKey( 'products', $totals );
|
||||
$this->assertArrayHasKey( 'segments', $totals );
|
||||
|
||||
$intervals = $properties['intervals']['items']['properties'];
|
||||
$this->assertEquals( 6, count( $intervals ) );
|
||||
$this->assertArrayHasKey( 'interval', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'subtotals', $intervals );
|
||||
|
||||
$subtotals = $properties['intervals']['items']['properties']['subtotals']['properties'];
|
||||
$this->assertEquals( 10, count( $subtotals ) );
|
||||
$this->assertArrayHasKey( 'gross_revenue', $subtotals );
|
||||
$this->assertArrayHasKey( 'net_revenue', $subtotals );
|
||||
$this->assertArrayHasKey( 'coupons', $subtotals );
|
||||
$this->assertArrayHasKey( 'coupons_count', $subtotals );
|
||||
$this->assertArrayHasKey( 'shipping', $subtotals );
|
||||
$this->assertArrayHasKey( 'taxes', $subtotals );
|
||||
$this->assertArrayHasKey( 'refunds', $subtotals );
|
||||
$this->assertArrayHasKey( 'orders_count', $subtotals );
|
||||
$this->assertArrayHasKey( 'num_items_sold', $subtotals );
|
||||
$this->assertArrayHasKey( 'segments', $subtotals );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Stock Stats REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Reports_Stock_Stats
|
||||
*/
|
||||
class WC_Tests_API_Reports_Stock_Stats extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/stock/stats';
|
||||
|
||||
/**
|
||||
* Setup test reports stock data.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
$number_of_low_stock = 3;
|
||||
for ( $i = 1; $i <= $number_of_low_stock; $i++ ) {
|
||||
$low_stock = new WC_Product_Simple();
|
||||
$low_stock->set_name( "Test low stock {$i}" );
|
||||
$low_stock->set_regular_price( 5 );
|
||||
$low_stock->set_manage_stock( true );
|
||||
$low_stock->set_stock_quantity( 1 );
|
||||
$low_stock->set_stock_status( 'instock' );
|
||||
$low_stock->save();
|
||||
}
|
||||
|
||||
$number_of_out_of_stock = 6;
|
||||
for ( $i = 1; $i <= $number_of_out_of_stock; $i++ ) {
|
||||
$out_of_stock = new WC_Product_Simple();
|
||||
$out_of_stock->set_name( "Test out of stock {$i}" );
|
||||
$out_of_stock->set_regular_price( 5 );
|
||||
$out_of_stock->set_stock_status( 'outofstock' );
|
||||
$out_of_stock->save();
|
||||
}
|
||||
|
||||
$number_of_in_stock = 10;
|
||||
for ( $i = 1; $i <= $number_of_in_stock; $i++ ) {
|
||||
$in_stock = new WC_Product_Simple();
|
||||
$in_stock->set_name( "Test in stock {$i}" );
|
||||
$in_stock->set_regular_price( 25 );
|
||||
$in_stock->save();
|
||||
}
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertArrayHasKey( 'totals', $reports );
|
||||
$this->assertEquals( 19, $reports['totals']['products'] );
|
||||
$this->assertEquals( 6, $reports['totals']['outofstock'] );
|
||||
$this->assertEquals( 0, $reports['totals']['onbackorder'] );
|
||||
$this->assertEquals( 3, $reports['totals']['lowstock'] );
|
||||
$this->assertEquals( 13, $reports['totals']['instock'] );
|
||||
|
||||
// Test backorder and cache update.
|
||||
$backorder_stock = new WC_Product_Simple();
|
||||
$backorder_stock->set_name( 'Test backorder' );
|
||||
$backorder_stock->set_regular_price( 5 );
|
||||
$backorder_stock->set_stock_status( 'onbackorder' );
|
||||
$backorder_stock->save();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 20, $reports['totals']['products'] );
|
||||
$this->assertEquals( 6, $reports['totals']['outofstock'] );
|
||||
$this->assertEquals( 1, $reports['totals']['onbackorder'] );
|
||||
$this->assertEquals( 3, $reports['totals']['lowstock'] );
|
||||
$this->assertEquals( 13, $reports['totals']['instock'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertCount( 1, $properties );
|
||||
$this->assertArrayHasKey( 'totals', $properties );
|
||||
$this->assertCount( 5, $properties['totals']['properties'] );
|
||||
$this->assertArrayHasKey( 'products', $properties['totals']['properties'] );
|
||||
$this->assertArrayHasKey( 'outofstock', $properties['totals']['properties'] );
|
||||
$this->assertArrayHasKey( 'onbackorder', $properties['totals']['properties'] );
|
||||
$this->assertArrayHasKey( 'lowstock', $properties['totals']['properties'] );
|
||||
$this->assertArrayHasKey( 'instock', $properties['totals']['properties'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Stock REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Reports_Stock
|
||||
*/
|
||||
class WC_Tests_API_Reports_Stock extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/stock';
|
||||
|
||||
/**
|
||||
* Setup test reports stock data.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$low_stock = new WC_Product_Simple();
|
||||
$low_stock->set_name( 'Test low stock' );
|
||||
$low_stock->set_regular_price( 5 );
|
||||
$low_stock->set_manage_stock( true );
|
||||
$low_stock->set_stock_quantity( 1 );
|
||||
$low_stock->set_stock_status( 'instock' );
|
||||
$low_stock->save();
|
||||
|
||||
$out_of_stock = new WC_Product_Simple();
|
||||
$out_of_stock->set_name( 'Test out of stock' );
|
||||
$out_of_stock->set_regular_price( 5 );
|
||||
$out_of_stock->set_stock_status( 'outofstock' );
|
||||
$out_of_stock->save();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_param( 'include', implode( ',', array( $low_stock->get_id(), $out_of_stock->get_id() ) ) );
|
||||
$request->set_param( 'orderby', 'id' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
$this->assertEquals( $low_stock->get_id(), $reports[0]['id'] );
|
||||
$this->assertEquals( 'instock', $reports[0]['stock_status'] );
|
||||
$this->assertEquals( 1, $reports[0]['stock_quantity'] );
|
||||
$this->assertArrayHasKey( '_links', $reports[0] );
|
||||
$this->assertArrayHasKey( 'product', $reports[0]['_links'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_param( 'include', implode( ',', array( $low_stock->get_id(), $out_of_stock->get_id() ) ) );
|
||||
$request->set_param( 'type', 'lowstock' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 7, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'parent_id', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'sku', $properties );
|
||||
$this->assertArrayHasKey( 'stock_status', $properties );
|
||||
$this->assertArrayHasKey( 'stock_quantity', $properties );
|
||||
$this->assertArrayHasKey( 'manage_stock', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,172 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Taxes Stats REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC_Tests_API_Reports_Taxes_Stats
|
||||
*/
|
||||
class WC_Tests_API_Reports_Taxes_Stats extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/taxes/stats';
|
||||
|
||||
/**
|
||||
* Setup test reports taxes data.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$tax = WC_Tax::_insert_tax_rate(
|
||||
array(
|
||||
'tax_rate_country' => 'US',
|
||||
'tax_rate_state' => '',
|
||||
'tax_rate' => '7',
|
||||
'tax_rate_name' => 'TestTax',
|
||||
'tax_rate_priority' => '1',
|
||||
'tax_rate_compound' => '0',
|
||||
'tax_rate_shipping' => '1',
|
||||
'tax_rate_order' => '1',
|
||||
'tax_rate_class' => '',
|
||||
)
|
||||
);
|
||||
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->set_tax_class( 'TestTax' );
|
||||
$product->save();
|
||||
|
||||
update_option( 'woocommerce_calc_taxes', 'yes' );
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->calculate_taxes();
|
||||
$order->save();
|
||||
|
||||
// Add refunds to line items.
|
||||
foreach ( $order->get_items() as $item_id => $item ) {
|
||||
$refund = array(
|
||||
'amount' => 1,
|
||||
'reason' => 'Testing line item refund',
|
||||
'order_id' => $order->get_id(),
|
||||
'line_items' => array(
|
||||
$item_id => array(
|
||||
'qty' => 1,
|
||||
'refund_total' => 1,
|
||||
'refund_tax' => array( $tax => 1 ),
|
||||
),
|
||||
),
|
||||
);
|
||||
$wc_refund = wc_create_refund( $refund );
|
||||
}
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
$tax_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 1, $tax_report['tax_codes'] );
|
||||
$this->assertEquals( 6.7, $tax_report['total_tax'] ); // 110 * 0.07 (tax rate) - 1 (refund)
|
||||
$this->assertEquals( 6, $tax_report['order_tax'] ); // 100 * 0.07 (tax rate) - 1 (refund)
|
||||
$this->assertEquals( 0.7, $tax_report['shipping_tax'] );
|
||||
$this->assertEquals( 1, $tax_report['orders_count'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$totals = $properties['totals']['properties'];
|
||||
|
||||
$this->assertEquals( 6, count( $totals ) );
|
||||
$this->assertArrayHasKey( 'order_tax', $totals );
|
||||
$this->assertArrayHasKey( 'orders_count', $totals );
|
||||
$this->assertArrayHasKey( 'shipping_tax', $totals );
|
||||
$this->assertArrayHasKey( 'tax_codes', $totals );
|
||||
$this->assertArrayHasKey( 'total_tax', $totals );
|
||||
$this->assertArrayHasKey( 'segments', $totals );
|
||||
|
||||
$intervals = $properties['intervals']['items']['properties'];
|
||||
$this->assertEquals( 6, count( $intervals ) );
|
||||
$this->assertArrayHasKey( 'interval', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start', $intervals );
|
||||
$this->assertArrayHasKey( 'date_start_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end', $intervals );
|
||||
$this->assertArrayHasKey( 'date_end_gmt', $intervals );
|
||||
$this->assertArrayHasKey( 'subtotals', $intervals );
|
||||
|
||||
$subtotals = $properties['intervals']['items']['properties']['subtotals']['properties'];
|
||||
$this->assertEquals( 6, count( $subtotals ) );
|
||||
$this->assertArrayHasKey( 'order_tax', $subtotals );
|
||||
$this->assertArrayHasKey( 'orders_count', $subtotals );
|
||||
$this->assertArrayHasKey( 'shipping_tax', $subtotals );
|
||||
$this->assertArrayHasKey( 'tax_codes', $subtotals );
|
||||
$this->assertArrayHasKey( 'total_tax', $subtotals );
|
||||
$this->assertArrayHasKey( 'segments', $subtotals );
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,363 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Taxes REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC_Tests_API_Reports_Taxes
|
||||
*/
|
||||
class WC_Tests_API_Reports_Taxes extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/taxes';
|
||||
|
||||
/**
|
||||
* Setup test reports taxes data.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$wpdb->insert(
|
||||
$wpdb->prefix . 'woocommerce_tax_rates',
|
||||
array(
|
||||
'tax_rate_id' => 1,
|
||||
'tax_rate' => '7',
|
||||
'tax_rate_country' => 'US',
|
||||
'tax_rate_state' => 'GA',
|
||||
'tax_rate_name' => 'TestTax',
|
||||
'tax_rate_priority' => 1,
|
||||
'tax_rate_order' => 1,
|
||||
)
|
||||
);
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
// @todo Remove this once order data is synced to wc_order_tax_lookup
|
||||
$wpdb->insert(
|
||||
$wpdb->prefix . 'wc_order_tax_lookup',
|
||||
array(
|
||||
'order_id' => $order->get_id(),
|
||||
'tax_rate_id' => 1,
|
||||
'date_created' => date( 'Y-m-d H:i:s' ),
|
||||
'shipping_tax' => 2,
|
||||
'order_tax' => 5,
|
||||
'total_tax' => 7,
|
||||
)
|
||||
);
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
|
||||
$tax_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 1, $tax_report['tax_rate_id'] );
|
||||
$this->assertEquals( 'TestTax', $tax_report['name'] );
|
||||
$this->assertEquals( 7, $tax_report['tax_rate'] );
|
||||
$this->assertEquals( 'US', $tax_report['country'] );
|
||||
$this->assertEquals( 'GA', $tax_report['state'] );
|
||||
$this->assertEquals( 7, $tax_report['total_tax'] );
|
||||
$this->assertEquals( 5, $tax_report['order_tax'] );
|
||||
$this->assertEquals( 2, $tax_report['shipping_tax'] );
|
||||
$this->assertEquals( 1, $tax_report['orders_count'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports with the `taxes` report.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_taxes_param() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$wpdb->insert(
|
||||
$wpdb->prefix . 'woocommerce_tax_rates',
|
||||
array(
|
||||
'tax_rate_id' => 1,
|
||||
'tax_rate' => '7',
|
||||
'tax_rate_country' => 'US',
|
||||
'tax_rate_state' => 'GA',
|
||||
'tax_rate_name' => 'TestTax',
|
||||
'tax_rate_priority' => 1,
|
||||
'tax_rate_order' => 1,
|
||||
)
|
||||
);
|
||||
|
||||
$wpdb->insert(
|
||||
$wpdb->prefix . 'woocommerce_tax_rates',
|
||||
array(
|
||||
'tax_rate_id' => 2,
|
||||
'tax_rate' => '8',
|
||||
'tax_rate_country' => 'CA',
|
||||
'tax_rate_state' => 'ON',
|
||||
'tax_rate_name' => 'TestTax 2',
|
||||
'tax_rate_priority' => 1,
|
||||
'tax_rate_order' => 1,
|
||||
)
|
||||
);
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
// @todo Remove this once order data is synced to wc_order_tax_lookup
|
||||
$wpdb->insert(
|
||||
$wpdb->prefix . 'wc_order_tax_lookup',
|
||||
array(
|
||||
'order_id' => $order->get_id(),
|
||||
'tax_rate_id' => 1,
|
||||
'date_created' => date( 'Y-m-d H:i:s' ),
|
||||
'shipping_tax' => 2,
|
||||
'order_tax' => 5,
|
||||
'total_tax' => 7,
|
||||
)
|
||||
);
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'taxes' => '1,2',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
$tax_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( 2, $tax_report['tax_rate_id'] );
|
||||
$this->assertEquals( 'TestTax 2', $tax_report['name'] );
|
||||
$this->assertEquals( 8, $tax_report['tax_rate'] );
|
||||
$this->assertEquals( 'CA', $tax_report['country'] );
|
||||
$this->assertEquals( 'ON', $tax_report['state'] );
|
||||
$this->assertEquals( 0, $tax_report['total_tax'] );
|
||||
$this->assertEquals( 0, $tax_report['order_tax'] );
|
||||
$this->assertEquals( 0, $tax_report['shipping_tax'] );
|
||||
$this->assertEquals( 0, $tax_report['orders_count'] );
|
||||
|
||||
$tax_report = next( $reports );
|
||||
|
||||
$this->assertEquals( 1, $tax_report['tax_rate_id'] );
|
||||
$this->assertEquals( 'TestTax', $tax_report['name'] );
|
||||
$this->assertEquals( 7, $tax_report['tax_rate'] );
|
||||
$this->assertEquals( 'US', $tax_report['country'] );
|
||||
$this->assertEquals( 'GA', $tax_report['state'] );
|
||||
$this->assertEquals( 7, $tax_report['total_tax'] );
|
||||
$this->assertEquals( 5, $tax_report['order_tax'] );
|
||||
$this->assertEquals( 2, $tax_report['shipping_tax'] );
|
||||
$this->assertEquals( 1, $tax_report['orders_count'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports with param `orderby=rate`.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_orderby_tax_rate() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
$wpdb->insert(
|
||||
$wpdb->prefix . 'woocommerce_tax_rates',
|
||||
array(
|
||||
'tax_rate_id' => 1,
|
||||
'tax_rate' => '7',
|
||||
'tax_rate_country' => 'US',
|
||||
'tax_rate_state' => 'GA',
|
||||
'tax_rate_name' => 'TestTax',
|
||||
'tax_rate_priority' => 1,
|
||||
'tax_rate_order' => 1,
|
||||
)
|
||||
);
|
||||
|
||||
$wpdb->insert(
|
||||
$wpdb->prefix . 'woocommerce_tax_rates',
|
||||
array(
|
||||
'tax_rate_id' => 2,
|
||||
'tax_rate' => '10',
|
||||
'tax_rate_country' => 'CA',
|
||||
'tax_rate_state' => 'ON',
|
||||
'tax_rate_name' => 'TestTax 2',
|
||||
'tax_rate_priority' => 1,
|
||||
'tax_rate_order' => 1,
|
||||
)
|
||||
);
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'order' => 'asc',
|
||||
'orderby' => 'rate',
|
||||
'taxes' => '1,2',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
$this->assertEquals( 1, $reports[0]['tax_rate_id'] );
|
||||
$this->assertEquals( 7, $reports[0]['tax_rate'] );
|
||||
|
||||
$this->assertEquals( 2, $reports[1]['tax_rate_id'] );
|
||||
$this->assertEquals( 10, $reports[1]['tax_rate'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports with param `orderby=tax_code`.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_orderby_tax_code() {
|
||||
global $wpdb;
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
$wpdb->insert(
|
||||
$wpdb->prefix . 'woocommerce_tax_rates',
|
||||
array(
|
||||
'tax_rate_id' => 1,
|
||||
'tax_rate' => '7',
|
||||
'tax_rate_country' => 'US',
|
||||
'tax_rate_state' => 'GA',
|
||||
'tax_rate_name' => 'TestTax',
|
||||
'tax_rate_priority' => 1,
|
||||
'tax_rate_order' => 1,
|
||||
)
|
||||
);
|
||||
|
||||
$wpdb->insert(
|
||||
$wpdb->prefix . 'woocommerce_tax_rates',
|
||||
array(
|
||||
'tax_rate_id' => 2,
|
||||
'tax_rate' => '10',
|
||||
'tax_rate_country' => 'CA',
|
||||
'tax_rate_state' => 'ON',
|
||||
'tax_rate_name' => 'TestTax 2',
|
||||
'tax_rate_priority' => 1,
|
||||
'tax_rate_order' => 1,
|
||||
)
|
||||
);
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'order' => 'asc',
|
||||
'orderby' => 'tax_code',
|
||||
'taxes' => '1,2',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
$this->assertEquals( 2, $reports[0]['tax_rate_id'] );
|
||||
|
||||
$this->assertEquals( 1, $reports[1]['tax_rate_id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 10, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'tax_rate_id', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'tax_rate', $properties );
|
||||
$this->assertArrayHasKey( 'country', $properties );
|
||||
$this->assertArrayHasKey( 'state', $properties );
|
||||
$this->assertArrayHasKey( 'priority', $properties );
|
||||
$this->assertArrayHasKey( 'total_tax', $properties );
|
||||
$this->assertArrayHasKey( 'order_tax', $properties );
|
||||
$this->assertArrayHasKey( 'shipping_tax', $properties );
|
||||
$this->assertArrayHasKey( 'orders_count', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,183 @@
|
|||
<?php
|
||||
/**
|
||||
* Reports Products REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
* @since 3.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Reports_Variations
|
||||
*/
|
||||
class WC_Tests_API_Reports_Variations extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/reports/variations';
|
||||
|
||||
/**
|
||||
* Setup test reports products data.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$variation = new WC_Product_Variation();
|
||||
$variation->set_name( 'Test Variation' );
|
||||
$variation->set_regular_price( 25 );
|
||||
$variation->set_attributes( array( 'color' => 'green' ) );
|
||||
$variation->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $variation );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $reports ) );
|
||||
|
||||
$variation_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( $variation->get_id(), $variation_report['variation_id'] );
|
||||
$this->assertEquals( 4, $variation_report['items_sold'] );
|
||||
$this->assertEquals( 1, $variation_report['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $variation_report );
|
||||
$this->assertArrayHasKey( 'extended_info', $variation_report );
|
||||
$this->assertArrayHasKey( 'product', $variation_report['_links'] );
|
||||
$this->assertArrayHasKey( 'variation', $variation_report['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports with the `variations` param.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_variations_param() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
// Populate all of the data.
|
||||
$variation = new WC_Product_Variation();
|
||||
$variation->set_name( 'Test Variation' );
|
||||
$variation->set_regular_price( 25 );
|
||||
$variation->set_attributes( array( 'color' => 'green' ) );
|
||||
$variation->save();
|
||||
|
||||
$variation_2 = new WC_Product_Variation();
|
||||
$variation_2->set_name( 'Test Variation 2' );
|
||||
$variation_2->set_regular_price( 100 );
|
||||
$variation_2->set_attributes( array( 'color' => 'red' ) );
|
||||
$variation_2->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $variation );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 ); // $25 x 4.
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'product_includes' => $variation->get_parent_id(),
|
||||
'products' => $variation->get_parent_id(),
|
||||
'variations' => $variation->get_id() . ',' . $variation_2->get_id(),
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $reports ) );
|
||||
|
||||
$variation_report = reset( $reports );
|
||||
|
||||
$this->assertEquals( $variation->get_id(), $variation_report['variation_id'] );
|
||||
$this->assertEquals( 4, $variation_report['items_sold'] );
|
||||
$this->assertEquals( 1, $variation_report['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $variation_report );
|
||||
$this->assertArrayHasKey( 'extended_info', $variation_report );
|
||||
$this->assertArrayHasKey( 'product', $variation_report['_links'] );
|
||||
$this->assertArrayHasKey( 'variation', $variation_report['_links'] );
|
||||
|
||||
$variation_report = next( $reports );
|
||||
|
||||
$this->assertEquals( $variation_2->get_id(), $variation_report['variation_id'] );
|
||||
$this->assertEquals( 0, $variation_report['items_sold'] );
|
||||
$this->assertEquals( 0, $variation_report['orders_count'] );
|
||||
$this->assertArrayHasKey( '_links', $variation_report );
|
||||
$this->assertArrayHasKey( 'extended_info', $variation_report );
|
||||
$this->assertArrayHasKey( 'product', $variation_report['_links'] );
|
||||
$this->assertArrayHasKey( 'variation', $variation_report['_links'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting reports without valid permissions.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_reports_without_permission() {
|
||||
wp_set_current_user( 0 );
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_reports_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 6, count( $properties ) );
|
||||
$this->assertArrayHasKey( 'product_id', $properties );
|
||||
$this->assertArrayHasKey( 'variation_id', $properties );
|
||||
$this->assertArrayHasKey( 'items_sold', $properties );
|
||||
$this->assertArrayHasKey( 'net_revenue', $properties );
|
||||
$this->assertArrayHasKey( 'orders_count', $properties );
|
||||
$this->assertArrayHasKey( 'extended_info', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,313 @@
|
|||
<?php
|
||||
/**
|
||||
* Admin notes REST API Test
|
||||
*
|
||||
* @package WooCommerce\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Class WC_Tests_API_Admin_Notes
|
||||
*/
|
||||
class WC_Tests_API_Admin_Notes extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/admin/notes';
|
||||
|
||||
/**
|
||||
* Setup test admin notes data. Called before every test.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
|
||||
WC_Helper_Admin_Notes::reset_notes_dbs();
|
||||
WC_Helper_Admin_Notes::add_notes_for_tests();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test route registration.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_register_routes() {
|
||||
$routes = $this->server->get_routes();
|
||||
|
||||
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single note.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_note() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint . '/1' ) );
|
||||
$note = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
|
||||
$this->assertEquals( 1, $note['id'] );
|
||||
$this->assertEquals( 'PHPUNIT_TEST_NOTE_NAME', $note['name'] );
|
||||
$this->assertEquals( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL, $note['type'] );
|
||||
$this->assertArrayHasKey( 'locale', $note );
|
||||
$this->assertEquals( 'PHPUNIT_TEST_NOTE_1_TITLE', $note['title'] );
|
||||
|
||||
$this->assertEquals( 'PHPUNIT_TEST_NOTE_1_CONTENT', $note['content'] );
|
||||
$this->assertEquals( 'info', $note['icon'] );
|
||||
$this->assertArrayHasKey( 'content_data', $note );
|
||||
$this->assertEquals( 1.23, $note['content_data']->amount );
|
||||
$this->assertEquals( WC_Admin_Note::E_WC_ADMIN_NOTE_UNACTIONED, $note['status'] );
|
||||
$this->assertEquals( 'PHPUNIT_TEST', $note['source'] );
|
||||
|
||||
$this->assertArrayHasKey( 'date_created', $note );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $note );
|
||||
$this->assertArrayHasKey( 'date_reminder', $note );
|
||||
$this->assertArrayHasKey( 'date_reminder_gmt', $note );
|
||||
$this->assertArrayHasKey( 'actions', $note );
|
||||
|
||||
$this->assertEquals( 'PHPUNIT_TEST_NOTE_1_ACTION_1_SLUG', $note['actions'][0]->name );
|
||||
$this->assertEquals( 'http://example.org/wp-admin/admin.php?s=PHPUNIT_TEST_NOTE_1_ACTION_1_URL', $note['actions'][0]->url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a 404 from invalid ID.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_invalid_note() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint . '/999' ) );
|
||||
$note = $response->get_data();
|
||||
|
||||
$this->assertEquals( 404, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting a single note without permission. It should fail.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_note_without_permission() {
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint . '/1' ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single note.
|
||||
*/
|
||||
public function test_update_note() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint . '/1' ) );
|
||||
$note = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'unactioned', $note['status'] );
|
||||
|
||||
$request = new WP_REST_Request( 'PUT', $this->endpoint . '/1' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'status' => 'actioned',
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$note = $response->get_data();
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'actioned', $note['status'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test updating a single note without permission. It should fail.
|
||||
*/
|
||||
public function test_update_note_without_permission() {
|
||||
$request = new WP_REST_Request( 'PUT', $this->endpoint . '/1' );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'status' => 'actioned',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting lots of notes.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_notes() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$notes = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 3, count( $notes ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting notes of a certain type.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_warning_notes() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( array( 'type' => 'warning' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$notes = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $notes ) );
|
||||
$this->assertEquals( $notes[0]['title'], 'PHPUNIT_TEST_NOTE_2_TITLE' );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test getting notes of a certain status.
|
||||
*/
|
||||
public function test_get_actioned_notes() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( array( 'status' => 'actioned' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$notes = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $notes ) );
|
||||
$this->assertEquals( $notes[0]['title'], 'PHPUNIT_TEST_NOTE_2_TITLE' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( array( 'status' => 'invalid' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$notes = $response->get_data();
|
||||
|
||||
// get_notes returns all results since 'status' is not one of actioned or unactioned.
|
||||
$this->assertEquals( 3, count( $notes ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test note "unsnoozing".
|
||||
*/
|
||||
public function test_note_unsnoozing() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( array( 'status' => 'snoozed' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$notes = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 1, count( $notes ) );
|
||||
$this->assertEquals( $notes[0]['title'], 'PHPUNIT_TEST_NOTE_3_TITLE' );
|
||||
|
||||
// The test snoozed note's reminder date is an hour ago.
|
||||
WC_Admin_Notes::unsnooze_notes();
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$notes = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEmpty( $notes );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test ordering of notes.
|
||||
*/
|
||||
public function test_order_notes() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'orderby' => 'title',
|
||||
'order' => 'asc',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$notes = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 3, count( $notes ) );
|
||||
$this->assertEquals( $notes[0]['title'], 'PHPUNIT_TEST_NOTE_1_TITLE' );
|
||||
$this->assertEquals( $notes[1]['title'], 'PHPUNIT_TEST_NOTE_2_TITLE' );
|
||||
$this->assertEquals( $notes[2]['title'], 'PHPUNIT_TEST_NOTE_3_TITLE' );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'orderby' => 'status',
|
||||
'order' => 'desc',
|
||||
)
|
||||
);
|
||||
$response = $this->server->dispatch( $request );
|
||||
$notes = $response->get_data();
|
||||
|
||||
$this->assertEquals( 3, count( $notes ) );
|
||||
$this->assertEquals( $notes[0]['status'], WC_Admin_Note::E_WC_ADMIN_NOTE_UNACTIONED );
|
||||
$this->assertEquals( $notes[1]['status'], WC_Admin_Note::E_WC_ADMIN_NOTE_SNOOZED );
|
||||
$this->assertEquals( $notes[2]['status'], WC_Admin_Note::E_WC_ADMIN_NOTE_ACTIONED );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting lots of notes without permission. It should fail.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_notes_without_permission() {
|
||||
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||
$this->assertEquals( 401, $response->get_status() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting the notes schema.
|
||||
*
|
||||
* @since 3.5.0
|
||||
*/
|
||||
public function test_get_notes_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertEquals( 16, count( $properties ) );
|
||||
|
||||
$this->assertArrayHasKey( 'id', $properties );
|
||||
$this->assertArrayHasKey( 'name', $properties );
|
||||
$this->assertArrayHasKey( 'type', $properties );
|
||||
$this->assertArrayHasKey( 'locale', $properties );
|
||||
$this->assertArrayHasKey( 'title', $properties );
|
||||
|
||||
$this->assertArrayHasKey( 'content', $properties );
|
||||
$this->assertArrayHasKey( 'icon', $properties );
|
||||
$this->assertArrayHasKey( 'content_data', $properties );
|
||||
$this->assertArrayHasKey( 'status', $properties );
|
||||
$this->assertArrayHasKey( 'source', $properties );
|
||||
|
||||
$this->assertArrayHasKey( 'date_created', $properties );
|
||||
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'date_reminder', $properties );
|
||||
$this->assertArrayHasKey( 'date_reminder_gmt', $properties );
|
||||
$this->assertArrayHasKey( 'actions', $properties );
|
||||
$this->assertArrayHasKey( 'is_snoozable', $properties );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
/**
|
||||
* Data REST API Test
|
||||
*
|
||||
* @package WooCommerce Admin\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC Tests API Data
|
||||
*/
|
||||
class WC_Tests_API_Data extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/data';
|
||||
|
||||
/**
|
||||
* Setup test data. Called before every test.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the list of data endpoints includes download-ips.
|
||||
*/
|
||||
public function test_get_items_contains_download_ips() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 4, count( $data ) );
|
||||
$this->assertEquals( 'download-ips', $data[3]['slug'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test download-ips match searching.
|
||||
*/
|
||||
public function test_download_ips() {
|
||||
wp_set_current_user( $this->user );
|
||||
WC_Helper_Reports::reset_stats_dbs();
|
||||
|
||||
$prod_download = new WC_Product_Download();
|
||||
$prod_download->set_file( plugin_dir_url( __FILE__ ) . '/assets/images/help.png' );
|
||||
$prod_download->set_id( 1 );
|
||||
|
||||
$product = new WC_Product_Simple();
|
||||
$product->set_name( 'Test Product' );
|
||||
$product->set_downloadable( 'yes' );
|
||||
$product->set_downloads( array( $prod_download ) );
|
||||
$product->set_regular_price( 25 );
|
||||
$product->save();
|
||||
|
||||
$order = WC_Helper_Order::create_order( 1, $product );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 );
|
||||
$order->save();
|
||||
|
||||
$download = new WC_Customer_Download();
|
||||
$download->set_user_id( $this->user );
|
||||
$download->set_order_id( $order->get_id() );
|
||||
$download->set_product_id( $product->get_id() );
|
||||
$download->set_download_id( $prod_download->get_id() );
|
||||
$download->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '1.2.3.4' );
|
||||
$id = $object->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '54.2.1.3' );
|
||||
$id = $object->save();
|
||||
|
||||
// Save a second log for the same IP -- only one result for this IP should be returned.
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '54.2.1.3' );
|
||||
$id = $object->save();
|
||||
|
||||
$object = new WC_Customer_Download_Log();
|
||||
$object->set_permission_id( $download->get_id() );
|
||||
$object->set_user_id( $this->user );
|
||||
$object->set_user_ip_address( '54.5.1.7' );
|
||||
$id = $object->save();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/download-ips' );
|
||||
$request->set_query_params(
|
||||
array(
|
||||
'match' => '54',
|
||||
)
|
||||
);
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$addresses = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 2, count( $addresses ) );
|
||||
|
||||
$this->assertEquals( '54.2.1.3', $addresses[0]['user_ip_address'] );
|
||||
$this->assertEquals( '54.5.1.7', $addresses[1]['user_ip_address'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
/**
|
||||
* Leaderboards REST API Test
|
||||
*
|
||||
* @package WooCommerce Admin\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC Tests API Leaderboards
|
||||
*/
|
||||
class WC_Tests_API_Leaderboards extends WC_REST_Unit_Test_Case {
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/leaderboards';
|
||||
|
||||
/**
|
||||
* Setup test data. Called before every test.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that leaderboards are returned by the endpoint.
|
||||
*/
|
||||
public function test_get_leaderboards() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'customers', $data[0]['id'] );
|
||||
$this->assertEquals( 'coupons', $data[1]['id'] );
|
||||
$this->assertEquals( 'categories', $data[2]['id'] );
|
||||
$this->assertEquals( 'products', $data[3]['id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test reports schema.
|
||||
*/
|
||||
public function test_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertCount( 4, $properties );
|
||||
$this->assert_item_schema( $properties );
|
||||
|
||||
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint . '/allowed' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$this->assertCount( 3, $properties );
|
||||
$this->assert_allowed_item_schema( $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the item schema is correct.
|
||||
*
|
||||
* @param array $schema Item to check schema.
|
||||
*/
|
||||
public function assert_item_schema( $schema ) {
|
||||
$this->assertArrayHasKey( 'id', $schema );
|
||||
$this->assertArrayHasKey( 'label', $schema );
|
||||
$this->assertArrayHasKey( 'headers', $schema );
|
||||
$this->assertArrayHasKey( 'rows', $schema );
|
||||
|
||||
$header_properties = $schema['headers']['items']['properties'];
|
||||
$this->assertCount( 1, $header_properties );
|
||||
$this->assertArrayHasKey( 'label', $header_properties );
|
||||
|
||||
$row_properties = $schema['rows']['items']['properties'];
|
||||
$this->assertCount( 2, $row_properties );
|
||||
$this->assertArrayHasKey( 'display', $row_properties );
|
||||
$this->assertArrayHasKey( 'value', $row_properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the allowed item schema is correct.
|
||||
*
|
||||
* @param array $schema Item to check schema.
|
||||
*/
|
||||
public function assert_allowed_item_schema( $schema ) {
|
||||
$this->assertArrayHasKey( 'id', $schema );
|
||||
$this->assertArrayHasKey( 'label', $schema );
|
||||
$this->assertArrayHasKey( 'headers', $schema );
|
||||
|
||||
$header_properties = $schema['headers']['items']['properties'];
|
||||
$this->assertCount( 1, $header_properties );
|
||||
$this->assertArrayHasKey( 'label', $header_properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that leaderboards response changes based on applied filters.
|
||||
*/
|
||||
public function test_filter_leaderboards() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
add_filter(
|
||||
'woocommerce_leaderboards',
|
||||
function( $leaderboards, $per_page, $after, $before, $persisted_query ) {
|
||||
$leaderboards[] = array(
|
||||
'id' => 'top_widgets',
|
||||
'label' => 'Top Widgets',
|
||||
'headers' => array(
|
||||
array(
|
||||
'label' => 'Widget Link',
|
||||
),
|
||||
),
|
||||
'rows' => array(
|
||||
array(
|
||||
'display' => wc_admin_url( 'test/path', $persisted_query ),
|
||||
'value' => null,
|
||||
),
|
||||
),
|
||||
);
|
||||
return $leaderboards;
|
||||
},
|
||||
10,
|
||||
5
|
||||
);
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( array( 'persisted_query' => '{ "persisted_param": 1 }' ) );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$widgets_leaderboard = end( $data );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'top_widgets', $widgets_leaderboard['id'] );
|
||||
$this->assertEquals( admin_url( 'admin.php?page=wc-admin#test/path?persisted_param=1' ), $widgets_leaderboard['rows'][0]['display'] );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint . '/allowed' );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$widgets_leaderboard = end( $data );
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertEquals( 'top_widgets', $widgets_leaderboard['id'] );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
/**
|
||||
* Product Reviews REST API Test
|
||||
*
|
||||
* @package WooCommerce Admin\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC Tests API Product Reviews
|
||||
*/
|
||||
class WC_Tests_API_Product_Reviews extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/products/reviews';
|
||||
|
||||
/**
|
||||
* Setup test data. Called before every test.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test product reviews shows product field as embeddable.
|
||||
*/
|
||||
public function test_product_review_embed() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
WC_Helper_Product::create_product_review( $product->get_id() );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wc/v4/products/reviews' );
|
||||
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
$this->assertTrue( $data[0]['_links']['up'][0]['embeddable'] );
|
||||
|
||||
$product->delete( true );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
/**
|
||||
* Products REST API Test
|
||||
*
|
||||
* @package WooCommerce Admin\Tests\API
|
||||
*/
|
||||
|
||||
/**
|
||||
* WC Tests API Products
|
||||
*/
|
||||
class WC_Tests_API_Products extends WC_REST_Unit_Test_Case {
|
||||
|
||||
/**
|
||||
* Endpoints.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $endpoint = '/wc/v4/products';
|
||||
|
||||
/**
|
||||
* Setup test data. Called before every test.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->user = $this->factory->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test product schema contains embed fields.
|
||||
*/
|
||||
public function test_product_schema() {
|
||||
wp_set_current_user( $this->user );
|
||||
$product = WC_Helper_Product::create_simple_product();
|
||||
$request = new WP_REST_Request( 'OPTIONS', '/wc/v4/products/' . $product->get_id() );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$properties = $data['schema']['properties'];
|
||||
|
||||
$properties_to_embed = array(
|
||||
'id',
|
||||
'name',
|
||||
'slug',
|
||||
'permalink',
|
||||
'images',
|
||||
'description',
|
||||
'short_description',
|
||||
);
|
||||
|
||||
foreach ( $properties as $property_key => $property ) {
|
||||
if ( in_array( $property_key, $properties_to_embed, true ) ) {
|
||||
$this->assertEquals( array( 'view', 'edit', 'embed' ), $property['context'] );
|
||||
}
|
||||
}
|
||||
|
||||
$product->delete( true );
|
||||
}
|
||||
}
|
|
@ -43,6 +43,7 @@ require $tests_dir . '/includes/bootstrap.php';
|
|||
require $wc_tests_dir . '/bootstrap.php';
|
||||
|
||||
// Framework.
|
||||
require_once __DIR__ . '/AbstractRestApiTest.php';
|
||||
require_once $wc_tests_dir . '/framework/class-wc-unit-test-factory.php';
|
||||
require_once $wc_tests_dir . '/framework/class-wc-mock-session-handler.php';
|
||||
require_once $wc_tests_dir . '/framework/class-wc-mock-wc-data.php';
|
||||
|
|
Loading…
Reference in New Issue