This commit is contained in:
Mike Jolley 2016-10-18 18:38:48 +01:00
parent a8b7ee6929
commit 2819fadd7f
2 changed files with 99 additions and 9 deletions

View File

@ -32,13 +32,14 @@ class WC_Helper_Product {
*
* @return WC_Product_Simple
*/
public static function create_simple_product() {
public static function create_simple_product( $parent = 0 ) {
// Create the product
$product = wp_insert_post( array(
'post_title' => 'Dummy Product',
'post_type' => 'product',
'post_status' => 'publish',
'post_parent' => $parent,
) );
update_post_meta( $product, '_price', '10' );
update_post_meta( $product, '_regular_price', '10' );
@ -47,13 +48,39 @@ class WC_Helper_Product {
update_post_meta( $product, '_manage_stock', 'no' );
update_post_meta( $product, '_tax_status', 'taxable' );
update_post_meta( $product, '_downloadable', 'no' );
update_post_meta( $product, '_virtual', 'taxable' );
update_post_meta( $product, '_virtual', 'no' );
update_post_meta( $product, '_visibility', 'visible' );
update_post_meta( $product, '_stock_status', 'instock' );
return new WC_Product_Simple( $product );
}
/**
* Create grouped product.
*
* @since 2.7.0
*
* @return WC_Product_Grouped
*/
public static function create_grouped_product() {
// Create the product
$product = wp_insert_post( array(
'post_title' => 'Dummy Grouped Product',
'post_type' => 'product',
'post_status' => 'publish',
) );
$simple_product_1 = self::create_simple_product( $product->ID );
$simple_product_2 = self::create_simple_product( $product->ID );
update_post_meta( $product, '_sku', 'DUMMY GROUPED SKU' );
update_post_meta( $product, '_manage_stock', 'no' );
update_post_meta( $product, '_tax_status', 'taxable' );
update_post_meta( $product, '_downloadable', 'no' );
update_post_meta( $product, '_virtual', 'no' );
update_post_meta( $product, '_visibility', 'visible' );
update_post_meta( $product, '_stock_status', 'instock' );
return new WC_Product_Grouped( $product );
}
/**
* Create a dummy variation product.
*

View File

@ -104,13 +104,76 @@ class WC_Tests_Product_CRUD extends WC_Unit_Test_Case {
'purchase_note' => 'A note',
'menu_order' => 2,
);
$product = new WC_Product;
foreach ( $getters_and_setters as $function => $value ) {
$product->{"set_{$function}"}( $value );
}
$product->create();
$product = new WC_Product_Simple( $product->get_id() );
foreach ( $getters_and_setters as $function => $value ) {
$product = new WC_Product;
foreach ( $getters_and_setters as $function => $value ) {
$product->{"set_{$function}"}( $value );
}
$product->create();
$product = new WC_Product_Simple( $product->get_id() );
foreach ( $getters_and_setters as $function => $value ) {
$this->assertEquals( $value, $product->{"get_{$function}"}(), $function );
}
}
/**
* Test creating a new grouped product.
*
* @since 2.7.0
*/
function test_grouped_product_create() {
$simple_product = WC_Helper_Product::create_simple_product();
$product = new WC_Product_Grouped;
$product->set_children( array( $simple_product->get_id() ) );
$product->set_name( 'My Grouped Product' );
$product->create();
$read_product = new WC_Product_Grouped( $product->get_id() );
$this->assertEquals( 'My Grouped Product', $read_product->get_name() );
$this->assertEquals( array( $simple_product->get_id() ), $read_product->get_children() );
}
/**
* Test getting / reading an grouped product.
*
* @since 2.7.0
*/
function test_grouped_product_read() {
$product = WC_Helper_Product::create_grouped_product();
$product = new WC_Product_Grouped( $product->get_id() );
$this->assertEquals( 'Dummy Grouped Product', $read_product->get_name() );
$this->assertEquals( 2, count( $product->get_children() ) );
}
/**
* Test updating an grouped product.
*
* @since 2.7.0
*/
function test_grouped_product_update() {
$product = WC_Helper_Product::create_grouped_product();
$simple_product = WC_Helper_Product::create_simple_product();
$this->assertEquals( 'My Grouped Product', $product->get_name() );
$this->assertEquals( array( $simple_product->get_id() ), $product->get_children() );
$children = $product->get_children();
$children[] = $simple_product->get_id();
$product->set_children( $children );
$product->set_name( 'My Grouped Product 2' );
$product->save();
// Reread from database
$product = new WC_Product_Grouped( $product->get_id() );
$this->assertEquals( 3, count( $product->get_children() ) );
$this->assertEquals( 'My Grouped Product 2', $read_product->get_name() );
}
/**
* Test grouped product setters and getters
*
* @since 2.7.0
*/
public function test_grouped_product_getters_and_setters() {
$getters_and_setters = array(
'children' => array( 1, 2, 3 ),
);
$product = new WC_Product_Grouped;
foreach ( $getters_and_setters as $function => $value ) {
$product->{"set_{$function}"}( $value );
$this->assertEquals( $value, $product->{"get_{$function}"}(), $function );
}
}