Refund unit tests

This commit is contained in:
Mike Jolley 2016-06-22 16:31:05 +01:00
parent 9da9d3bf69
commit 46439ddba5
6 changed files with 238 additions and 44 deletions

View File

@ -175,6 +175,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
// Map standard post data // Map standard post data
$this->set_id( $order_id ); $this->set_id( $order_id );
$this->set_parent_id( $post_object->post_parent );
$this->set_date_created( $post_object->post_date ); $this->set_date_created( $post_object->post_date );
$this->set_date_modified( $post_object->post_modified ); $this->set_date_modified( $post_object->post_modified );
$this->set_status( $post_object->post_status ); $this->set_status( $post_object->post_status );

View File

@ -44,11 +44,11 @@ class WC_Order_Refund extends WC_Abstract_Order {
} }
/** /**
* Read from the database. * Read from the database.
* @since 2.7.0 * @since 2.7.0
* @param int $id ID of object to read. * @param int $id ID of object to read.
*/ */
public function read( $id ) { public function read( $id ) {
parent::read( $id ); parent::read( $id );
// Read additonal order data // Read additonal order data
@ -153,39 +153,39 @@ class WC_Order_Refund extends WC_Abstract_Order {
} }
/** /**
* Magic __get method for backwards compatibility. * Magic __get method for backwards compatibility.
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public function __get( $key ) { public function __get( $key ) {
_doing_it_wrong( $key, 'Refund properties should not be accessed directly.', '2.7' ); _doing_it_wrong( $key, 'Refund properties should not be accessed directly.', '2.7' );
/** /**
* Maps legacy vars to new getters. * Maps legacy vars to new getters.
*/ */
if ( 'reason' === $key ) { if ( 'reason' === $key ) {
return $this->get_refund_reason(); return $this->get_refund_reason();
} elseif ( 'refund_amount' === $key ) { } elseif ( 'refund_amount' === $key ) {
return $this->get_refund_amount(); return $this->get_refund_amount();
} }
return parent::__get( $key ); return parent::__get( $key );
} }
/** /**
* Gets an refund from the database. * Gets an refund from the database.
* @deprecated 2.7 * @deprecated 2.7
* @param int $id (default: 0). * @param int $id (default: 0).
* @return bool * @return bool
*/ */
public function get_refund( $id = 0 ) { public function get_refund( $id = 0 ) {
_deprecated_function( 'get_refund', '2.7', 'read' ); _deprecated_function( 'get_refund', '2.7', 'read' );
if ( ! $id ) { if ( ! $id ) {
return false; return false;
} }
if ( $result = get_post( $id ) ) { if ( $result = get_post( $id ) ) {
$this->populate( $result ); $this->populate( $result );
return true; return true;
} }
return false; return false;
} }
} }

View File

@ -1195,7 +1195,7 @@ class WC_Order extends WC_Abstract_Order {
*/ */
/** /**
* Adds a note (comment) to the order. * Adds a note (comment) to the order. Order must exist.
* *
* @param string $note Note to add. * @param string $note Note to add.
* @param int $is_customer_note (default: 0) Is this a note for the customer? * @param int $is_customer_note (default: 0) Is this a note for the customer?
@ -1203,6 +1203,10 @@ class WC_Order extends WC_Abstract_Order {
* @return int Comment ID. * @return int Comment ID.
*/ */
public function add_order_note( $note, $is_customer_note = 0, $added_by_user = false ) { public function add_order_note( $note, $is_customer_note = 0, $added_by_user = false ) {
if ( ! $this->get_id() ) {
return 0;
}
if ( is_user_logged_in() && current_user_can( 'edit_shop_order', $this->get_id() ) && $added_by_user ) { if ( is_user_logged_in() && current_user_can( 'edit_shop_order', $this->get_id() ) && $added_by_user ) {
$user = get_user_by( 'id', get_current_user_id() ); $user = get_user_by( 'id', get_current_user_id() );
$comment_author = $user->display_name; $comment_author = $user->display_name;
@ -1278,13 +1282,11 @@ class WC_Order extends WC_Abstract_Order {
* @return array of WC_Order_Refund objects * @return array of WC_Order_Refund objects
*/ */
public function get_refunds() { public function get_refunds() {
if ( empty( $this->refunds ) || ! is_array( $this->refunds ) ) { $this->refunds = wc_get_orders( array(
$this->refunds = wc_get_orders( array( 'type' => 'shop_order_refund',
'type' => 'shop_order_refund', 'parent' => $this->get_id(),
'parent' => $this->get_id(), 'limit' => -1,
'limit' => -1, ) );
) );
}
return $this->refunds; return $this->refunds;
} }

View File

@ -849,7 +849,7 @@ function wc_create_refund( $args = array() ) {
$order = wc_get_order( $args['order_id'] ); $order = wc_get_order( $args['order_id'] );
// Refund currency is the same used for the parent order // Refund currency is the same used for the parent order
update_post_meta( $refund_id, '_order_currency', $order->get_order_currency() ); update_post_meta( $refund_id, '_order_currency', $order->get_currency() );
// Negative line items // Negative line items
if ( sizeof( $args['line_items'] ) > 0 ) { if ( sizeof( $args['line_items'] ) > 0 ) {
@ -906,7 +906,7 @@ function wc_create_refund( $args = array() ) {
$refund->calculate_totals( false ); $refund->calculate_totals( false );
// Set total to total refunded which may vary from order items // Set total to total refunded which may vary from order items
$refund->set_total( wc_format_decimal( $args['amount'] ) * -1, 'total' ); $refund->set_total( wc_format_decimal( $args['amount'] ) * -1 );
do_action( 'woocommerce_refund_created', $refund_id, $args ); do_action( 'woocommerce_refund_created', $refund_id, $args );
} }

View File

@ -566,7 +566,8 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
$object = new WC_Order(); $object = new WC_Order();
$this->assertFalse( $object->needs_shipping_address() ); $this->assertFalse( $object->needs_shipping_address() );
// @todo $object = WC_Helper_Order::create_order();
$this->assertTrue( $object->needs_shipping_address() );
} }
/** /**
@ -576,7 +577,8 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
$object = new WC_Order(); $object = new WC_Order();
$this->assertFalse( $object->has_downloadable_item() ); $this->assertFalse( $object->has_downloadable_item() );
// @todo $object = WC_Helper_Order::create_order();
$this->assertFalse( $object->has_downloadable_item() );
} }
/** /**
@ -646,4 +648,150 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
$id = $object->save(); $id = $object->save();
$this->assertEquals( '?view-order=' . $id, $object->get_view_order_url() ); $this->assertEquals( '?view-order=' . $id, $object->get_view_order_url() );
} }
/**
* Test: add_order_note
*/
function test_add_order_note() {
$object = new WC_Order();
$id = $object->save();
$comment_id = $object->add_order_note( "Hello, I am a fish" );
$this->assertTrue( $comment_id > 0 );
$comment = get_comment( $comment_id );
$this->assertEquals( "Hello, I am a fish", $comment->comment_content );
}
/**
* Test: get_customer_order_notes
*/
function test_get_customer_order_notes() {
$object = new WC_Order();
$id = $object->save();
$this->assertCount( 0, $object->get_customer_order_notes() );
$object->add_order_note( "Hello, I am a fish", true );
$object->add_order_note( "Hello, I am a fish", false );
$object->add_order_note( "Hello, I am a fish", true );
$this->assertCount( 2, $object->get_customer_order_notes() );
}
/**
* Test: get_refunds
*/
function test_get_refunds() {
$object = new WC_Order();
$id = $object->save();
$this->assertCount( 0, $object->get_refunds() );
wc_create_refund( array(
'order_id' => $id,
'amount' => '100',
'line_items' => array(),
) );
$this->assertCount( 1, $object->get_refunds() );
}
/**
* Test: get_total_refunded
*/
function test_get_total_refunded() {
$object = new WC_Order();
$object->set_total( 400 );
$id = $object->save();
wc_create_refund( array(
'order_id' => $id,
'amount' => '100',
'line_items' => array(),
) );
wc_create_refund( array(
'order_id' => $id,
'amount' => '100',
'line_items' => array(),
) );
$this->assertEquals( 200, $object->get_total_refunded() );
}
/**
* Test: get_total_tax_refunded
*/
function test_get_total_tax_refunded() {
$object = new WC_Order();
$this->assertEquals( 0, $object->get_total_tax_refunded() );
}
/**
* Test: get_total_shipping_refunded
*/
function test_get_total_shipping_refunded() {
$object = new WC_Order();
$this->assertEquals( 0, $object->get_total_shipping_refunded() );
}
/**
* Test: get_total_shipping_refunded
*/
function test_get_total_qty_refunded() {
$object = new WC_Order();
$this->assertEquals( 0, $object->get_total_shipping_refunded() );
}
/**
* Test: get_qty_refunded_for_item
*/
function test_get_qty_refunded_for_item() {
$object = new WC_Order();
$this->assertEquals( 0, $object->get_qty_refunded_for_item( 2 ) );
}
/**
* Test: test_get_total_refunded_for_item
*/
function test_get_total_refunded_for_item() {
$object = new WC_Order();
$this->assertEquals( 0, $object->get_total_refunded_for_item( 2 ) );
}
/**
* Test: get_tax_refunded_for_item
*/
function test_get_tax_refunded_for_item() {
$object = new WC_Order();
$this->assertEquals( 0, $object->get_tax_refunded_for_item( 1, 1 ) );
}
/**
* Test: get_total_tax_refunded_by_rate_id
*/
function test_get_total_tax_refunded_by_rate_id() {
$object = new WC_Order();
$this->assertEquals( 0, $object->get_total_tax_refunded_by_rate_id( 2 ) );
}
/**
* Test: get_remaining_refund_amount
*/
function test_get_remaining_refund_amount() {
$object = new WC_Order();
$object->set_total( 400 );
$id = $object->save();
wc_create_refund( array(
'order_id' => $id,
'amount' => '100',
'line_items' => array(),
) );
$this->assertEquals( 300, $object->get_remaining_refund_amount() );
}
/**
* Test: get_total_tax_refunded_by_rate_id
*/
function test_get_remaining_refund_items() {
$object = WC_Helper_Order::create_order();
$this->assertEquals( 4, $object->get_remaining_refund_items() );
}
} }

View File

@ -0,0 +1,43 @@
<?php
/**
* Meta
* @package WooCommerce\Tests\CRUD
*/
class WC_Tests_CRUD_Refunds extends WC_Unit_Test_Case {
/**
* Test: get_type
*/
function test_get_type() {
$object = new WC_Order_Refund();
$this->assertEquals( 'shop_order_refund', $object->get_type() );
}
/**
* Test: get_refund_amount
*/
function test_get_refund_amount() {
$object = new WC_Order_Refund();
$object->set_refund_amount( 20 );
$this->assertEquals( '20.00', $object->get_refund_amount() );
}
/**
* Test: get_refund_reason
*/
function test_get_refund_reason() {
$object = new WC_Order_Refund();
$object->set_refund_reason( 'Customer is an idiot' );
$this->assertEquals( 'Customer is an idiot', $object->get_refund_reason() );
}
/**
* Test: get_refunded_by
*/
function test_get_refunded_by() {
$object = new WC_Order_Refund();
$object->set_refunded_by( 1 );
$this->assertEquals( 1, $object->get_refunded_by() );
}
}