From 276bff133f1221e2a2923bdfd9e98a290dc4d706 Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Thu, 14 Dec 2017 12:45:00 +0200 Subject: [PATCH] Improve unit test coverage (#18136) * Test for main WooCommerce class * Check all class instances created from main class. * Test all constants * Test for deprecated hook handler classes as well * Test template path constant * Check static class instances * get_cart_item_quantities test * get_cart_item_quantities & get_cart_contents_weight tests * check_cart_items test * Check_cart_item_stock test * Cart get_cross_sells test * Cart get_tax_totals test * WC_Customer_Download_Data_Store tests --- .../helpers/class-wc-helper-product.php | 1 + tests/unit-tests/cart/cart.php | 109 ++++++++++++++++++ tests/unit-tests/core/main-class.php | 15 +++ .../unit-tests/customer/customer-download.php | 68 +++++++++++ 4 files changed, 193 insertions(+) create mode 100644 tests/unit-tests/customer/customer-download.php diff --git a/tests/framework/helpers/class-wc-helper-product.php b/tests/framework/helpers/class-wc-helper-product.php index b62c2c19832..ec463c8ee0c 100644 --- a/tests/framework/helpers/class-wc-helper-product.php +++ b/tests/framework/helpers/class-wc-helper-product.php @@ -40,6 +40,7 @@ class WC_Helper_Product { update_post_meta( $product, '_downloadable', 'no' ); update_post_meta( $product, '_virtual', 'no' ); update_post_meta( $product, '_stock_status', 'instock' ); + update_post_meta( $product, '_weight', '1.1' ); wp_set_object_terms( $product, 'simple', 'product_type' ); return new WC_Product_Simple( $product ); diff --git a/tests/unit-tests/cart/cart.php b/tests/unit-tests/cart/cart.php index ef9b8efb455..6af5674e912 100644 --- a/tests/unit-tests/cart/cart.php +++ b/tests/unit-tests/cart/cart.php @@ -923,6 +923,7 @@ class WC_Tests_Cart extends WC_Unit_Test_Case { // Check. $this->assertEquals( wc_price( 22 ), WC()->cart->get_total() ); $this->assertEquals( wc_price( 20 ), WC()->cart->get_total_ex_tax() ); + $tax_totals = WC()->cart->get_tax_totals(); // Clean up the cart. WC()->cart->empty_cart(); @@ -1312,4 +1313,112 @@ class WC_Tests_Cart extends WC_Unit_Test_Case { $identical_fees = $cart_fees === $new_cart_fees; $this->assertFalse( $identical_fees, 'Cloned cart fees should not be identical to original cart.' ); } + + public function test_cart_object_istantiation() { + $cart = new WC_Cart(); + $this->assertInstanceOf( 'WC_Cart', $cart ); + } + + public function test_get_cart_item_quantities() { + // Create dummy product. + $product = WC_Helper_Product::create_simple_product(); + WC()->cart->add_to_cart( $product->get_id(), 1 ); + $this->assertEquals( 1, array_sum( WC()->cart->get_cart_item_quantities() ) ); + // Clean up the cart. + WC()->cart->empty_cart(); + // Clean up product. + WC_Helper_Product::delete_product( $product->get_id() ); + } + + public function test_get_cart_contents_weight() { + // Create dummy product. + $product = WC_Helper_Product::create_simple_product(); + WC()->cart->add_to_cart( $product->get_id(), 1 ); + $this->assertEquals( 1.1, WC()->cart->get_cart_contents_weight() ); + // Clean up the cart. + WC()->cart->empty_cart(); + // Clean up product. + WC_Helper_Product::delete_product( $product->get_id() ); + } + + public function test_check_cart_items() { + // Create dummy product. + $product = WC_Helper_Product::create_simple_product(); + WC()->cart->add_to_cart( $product->get_id(), 1 ); + $this->assertEquals( true, WC()->cart->check_cart_items() ); + // Clean up the cart. + WC()->cart->empty_cart(); + // Clean up product. + WC_Helper_Product::delete_product( $product->get_id() ); + } + + public function test_check_cart_item_stock() { + // Create dummy product. + $product = WC_Helper_Product::create_simple_product(); + WC()->cart->add_to_cart( $product->get_id(), 1 ); + $this->assertEquals( true, WC()->cart->check_cart_item_stock() ); + // Clean up the cart. + WC()->cart->empty_cart(); + // Clean up product. + WC_Helper_Product::delete_product( $product->get_id() ); + } + + public function test_get_cross_sells() { + // Create dummy product. + $product = WC_Helper_Product::create_simple_product(); + WC()->cart->add_to_cart( $product->get_id(), 1 ); + $this->assertEquals( array(), WC()->cart->get_cross_sells() ); + // Clean up the cart. + WC()->cart->empty_cart(); + // Clean up product. + WC_Helper_Product::delete_product( $product->get_id() ); + } + + public function test_get_tax_totals() { + global $wpdb; + + // Set calc taxes option. + update_option( 'woocommerce_calc_taxes', 'yes' ); + $tax_rate = array( + 'tax_rate_country' => '', + 'tax_rate_state' => '', + 'tax_rate' => '10.0000', + 'tax_rate_name' => 'TAX', + 'tax_rate_priority' => '1', + 'tax_rate_compound' => '0', + 'tax_rate_shipping' => '1', + 'tax_rate_order' => '1', + 'tax_rate_class' => '', + ); + WC_Tax::_insert_tax_rate( $tax_rate ); + + // Create dummy product. + $product = WC_Helper_Product::create_simple_product(); + + // We need this to have the calculate_totals() method calculate totals. + if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { + define( 'WOOCOMMERCE_CHECKOUT', true ); + } + + // Add product to cart (10). + WC()->cart->add_to_cart( $product->get_id(), 1 ); + + // Check. + $tax_totals = WC()->cart->get_tax_totals(); + $this->assertArrayHasKey( 'TAX-1', $tax_totals ); + $this->assertEquals( 1, $tax_totals['TAX-1']->amount ); + $this->assertEquals( false, $tax_totals['TAX-1']->is_compound ); + $this->assertEquals( 'TAX', $tax_totals['TAX-1']->label ); + + // Clean up the cart. + WC()->cart->empty_cart(); + + // Clean up product. + WC_Helper_Product::delete_product( $product->get_id() ); + + // Restore option. + $wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_tax_rates" ); + $wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_tax_rate_locations" ); + update_option( 'woocommerce_calc_taxes', 'no' ); + } } diff --git a/tests/unit-tests/core/main-class.php b/tests/unit-tests/core/main-class.php index a69f857acf9..f3ea00bedb7 100644 --- a/tests/unit-tests/core/main-class.php +++ b/tests/unit-tests/core/main-class.php @@ -2,6 +2,7 @@ /** * WooCommerce class. + * * @package WooCommerce\Tests\Util */ class WC_Test_WooCommerce extends WC_Unit_Test_Case { @@ -42,9 +43,14 @@ class WC_Test_WooCommerce extends WC_Unit_Test_Case { $this->assertEquals( $this->wc->version, WC_VERSION ); $this->assertEquals( WC_VERSION, WOOCOMMERCE_VERSION ); $this->assertEquals( 4, WC_ROUNDING_PRECISION ); + $this->assertEquals( 2, WC_DISCOUNT_ROUNDING_MODE ); + $this->assertEquals( 'wc_session_id', WC_SESSION_CACHE_GROUP ); $this->assertContains( WC_TAX_ROUNDING_MODE, array( 2, 1, 'auto' ) ); $this->assertEquals( '|', WC_DELIMITER ); $this->assertNotEquals( WC_LOG_DIR, '' ); + $this->assertEquals( false, WC_TEMPLATE_DEBUG_MODE ); + $this->assertLessThanOrEqual( 2, WC_TAX_ROUNDING_MODE ); + $this->assertEquals( $this->wc->template_path(), WC_TEMPLATE_PATH ); } /** @@ -53,11 +59,20 @@ class WC_Test_WooCommerce extends WC_Unit_Test_Case { * @since 2.2 */ public function test_wc_class_instances() { + $this->assertInstanceOf( 'WooCommerce', $this->wc ); $this->assertInstanceOf( 'WC_Product_Factory', $this->wc->product_factory ); $this->assertInstanceOf( 'WC_Order_Factory', $this->wc->order_factory ); $this->assertInstanceOf( 'WC_Countries', $this->wc->countries ); $this->assertInstanceOf( 'WC_Integrations', $this->wc->integrations ); $this->assertInstanceOf( 'WC_Cart', $this->wc->cart ); $this->assertInstanceOf( 'WC_Customer', $this->wc->customer ); + $this->assertInstanceOf( 'WC_Session', $this->wc->session ); + $this->assertInstanceOf( 'WC_Query', $this->wc->query ); + $this->assertInstanceOf( 'WC_Structured_Data', $this->wc->structured_data ); + $this->assertInstanceOf( 'WC_Deprecated_Action_Hooks', $this->wc->deprecated_hook_handlers['actions'] ); + $this->assertInstanceOf( 'WC_Deprecated_Filter_Hooks', $this->wc->deprecated_hook_handlers['filters'] ); + $this->assertInstanceOf( 'WC_Emails', $this->wc->mailer() ); + $this->assertInstanceOf( 'WC_Payment_Gateways', $this->wc->payment_gateways() ); + $this->assertInstanceOf( 'WC_Checkout', $this->wc->checkout() ); } } diff --git a/tests/unit-tests/customer/customer-download.php b/tests/unit-tests/customer/customer-download.php new file mode 100644 index 00000000000..4491941f926 --- /dev/null +++ b/tests/unit-tests/customer/customer-download.php @@ -0,0 +1,68 @@ +set_user_id( $customer_id ); + $download->set_order_id( 1 ); + $download->save(); + $data_store = WC_Data_Store::load( 'customer-download' ); + $data_store->delete( $download ); + $this->assertEquals( 0, $download->get_id() ); + } + + public function test_delete_by_id() { + $customer_id = wc_create_new_customer( 'test@example.com', 'testuser', 'testpassword' ); + $download = new WC_Customer_Download; + $download->set_user_id( $customer_id ); + $download->set_order_id( 1 ); + $download->save(); + $data_store = WC_Data_Store::load( 'customer-download' ); + $data_store->delete_by_id( $download->get_id() ); + $this->assertEquals( 0, $data_store->get_id() ); + } + + public function test_delete_by_download_id() { + $customer_id = wc_create_new_customer( 'test@example.com', 'testuser', 'testpassword' ); + $download = new WC_Customer_Download; + $download->set_user_id( $customer_id ); + $download->set_order_id( 1 ); + $download->save(); + $download_id = $download->get_download_id(); + $data_store = WC_Data_Store::load( 'customer-download' ); + $downloads = $data_store->get_downloads_for_customer( $customer_id ); + $this->assertInstanceOf( 'StdClass', $downloads[0] ); + $data_store->delete_by_download_id( $download_id ); + $downloads = $data_store->get_downloads_for_customer( $customer_id ); + $this->assertEquals( array(), $downloads ); + } + + public function test_get_downloads() { + $customer_id = wc_create_new_customer( 'test@example.com', 'testuser', 'testpassword' ); + $download_1 = new WC_Customer_Download; + $download_1->set_user_id( $customer_id ); + $download_1->set_user_email( 'test@example.com' ); + $download_1->set_order_id( 1 ); + $download_1->save(); + + $download_2 = new WC_Customer_Download; + $download_2->set_user_id( $customer_id ); + $download_2->set_user_email( 'test@example.com' ); + $download_2->set_order_id( 1 ); + $download_2->save(); + + $data_store = WC_Data_Store::load( 'customer-download' ); + $downloads = $data_store->get_downloads( array( 'user_email' => 'test@example.com' ) ); + $this->assertEquals( 2, count( $downloads ) ); + $downloads = $data_store->get_downloads( array( 'user_email' => 'test2@example.com' ) ); + $this->assertEquals( array(), $downloads ); + + } +}