From cae148ebce6d6e1c159e268936982105a47d4662 Mon Sep 17 00:00:00 2001 From: Almira Krdzic Date: Tue, 18 Sep 2018 13:13:48 +0200 Subject: [PATCH 1/2] Packages and products countries test --- backend/app/plugins/wiaas/tests/bootstrap.php | 20 ++++++ .../tests/unit-tests/test-wiaas-countries.php | 71 +++++++++++++++++++ .../wiaas/tests/wiaas-unit-test-case.php | 28 ++++++++ 3 files changed, 119 insertions(+) create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php diff --git a/backend/app/plugins/wiaas/tests/bootstrap.php b/backend/app/plugins/wiaas/tests/bootstrap.php index bd7d1ff..81225a7 100644 --- a/backend/app/plugins/wiaas/tests/bootstrap.php +++ b/backend/app/plugins/wiaas/tests/bootstrap.php @@ -20,17 +20,36 @@ require_once $_tests_dir . '/includes/functions.php'; #load setup tests_add_filter( 'muplugins_loaded', 'load_wiaas_test_setup' ); +tests_add_filter( 'pre_option_active_plugins', 'load_active_plugins_list'); + # Start up the WP testing environment. require $_tests_dir . '/includes/bootstrap.php'; # Require Wiaas Unit Test case class require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/tests/wiaas-unit-test-case.php'; +function load_active_plugins_list() { + return array( + 'woocommerce/woocommerce.php', + 'woocommerce-product-bundles/woocommerce-product-bundles.php', + 'woocommerce-jetpack/woocommerce-jetpack.php', + 'jwt-authentication-for-wp-rest-api/jwt-auth.php', + 'gravityforms/gravityforms.php', + 'gravityflow/gravityflow.php', + 'capability-manager-enhanced/capsman-enhanced.php', + 'groups/groups.php', + 'wp-user-groups/wp-user-groups.php', + 'wiaas/wiaas.php' + ); +} + function load_wiaas_test_setup() { # Activate plugins needed for testing require_once '/tmp/wiaas-backend-test/app/plugins/woocommerce/woocommerce.php'; + require_once '/tmp/wiaas-backend-test/app/plugins/woocommerce-product-bundles/woocommerce-product-bundles.php'; + require_once '/tmp/wiaas-backend-test/app/plugins/gravityforms/gravityforms.php'; require_once '/tmp/wiaas-backend-test/app/plugins/gravityflow/gravityflow.php'; @@ -42,6 +61,7 @@ function load_wiaas_test_setup() { require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/wiaas.php'; + # Require classes needed for db updates require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/includes/class-wiaas-db-update.php'; require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/includes/db-updates/wiaas-db-update-functions.php'; diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php new file mode 100644 index 0000000..caee4f1 --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php @@ -0,0 +1,71 @@ +product = $this->insertNewProduct(); + wp_set_object_terms($this->product->get_id(), $this->current_country, 'product_country', false); + clean_object_term_cache( $this->product->get_id(), 'product_country' ); + + + $this->package = $this->insertNewPackage(); + wp_set_object_terms($this->package->get_id(), $this->current_country, 'product_country', false); + clean_object_term_cache( $this->package->get_id(), 'product_country' ); + } + + + /** + * @covers Wiaas_Countries::register_product_countries_taxonomy() + */ + function test_available_countries_created() { + // test taxonomy is available + $taxonomy = get_taxonomy('product_country'); + + $this->assertInstanceOf(WP_Taxonomy::class, $taxonomy); + + $country_names = array_map(function($term) { + return $term->name; + }, get_terms(array( 'taxonomy' => 'product_country', 'hide_empty' => false ))); + + $this->assertNotEmpty($country_names); + + $this->assertTrue(in_array('Sweden', $country_names)); + $this->assertTrue(in_array('Denmark', $country_names)); + $this->assertTrue(in_array('Finland', $country_names)); + } + + /** + * @covers Wiaas_Countries::get_product_country() + */ + function test_get_product_country() { + + $retrieved_country = Wiaas_Countries::get_product_country($this->product); + + $this->assertNotNull($retrieved_country, 'Product has not country!'); + + $this->assertEquals($retrieved_country['name'], $this->current_country, 'Retrieved product country is incorrect!'); + + } + + /** + * @covers Wiaas_Countries::get_package_country() + */ + function test_get_package_country() { + $retrieved_country = Wiaas_Countries::get_package_country($this->package); + + $this->assertNotNull($retrieved_country, 'Package has not country!'); + + $this->assertEquals($retrieved_country['name'], $this->current_country, 'Retrieved package country is incorrect!'); + } + +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php b/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php index 53971cb..6206ae1 100644 --- a/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php +++ b/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php @@ -15,6 +15,8 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase { wiaas_db_update_setup_gravity(); wiaas_db_update_enable_orders_access_management(); + + Wiaas_Countries::register_product_countries_taxonomy(); define('WP_TEST_IN_PROGRESS',true); } @@ -22,4 +24,30 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase { function tearDown() { parent::tearDown(); } + + function insertNewProduct() { + $post_id = wp_insert_post(array( + 'post_type' => 'product', + 'post_status' => 'publish', + 'post_name' => 'product', + 'post_title' => 'Product', + 'post_content' => 'Product', + 'post_excerpt' => 'Product' + ), true); + + return new WC_Product_Simple($post_id); + } + + function insertNewPackage() { + $post_id = wp_insert_post(array( + 'post_type' => 'product', + 'post_status' => 'publish', + 'post_name' => 'product', + 'post_title' => 'Package', + 'post_content' => 'Package', + 'post_excerpt' => 'Package' + ), true); + + return new WC_Product_Bundle($post_id); + } } \ No newline at end of file From 85deb1d9f8b96eef57fb237ddbe14487361edb3e Mon Sep 17 00:00:00 2001 From: Almira Krdzic Date: Wed, 19 Sep 2018 10:52:59 +0200 Subject: [PATCH 2/2] Tests --- .../wiaas/includes/class-wiaas-pricing.php | 2 +- .../pricing/class-wiaas-product-pricing.php | 10 +- .../package/test-wiaas-package-addon.php | 39 ++ .../test-wiaas-package-option-groups.php | 100 +++++ .../package/test-wiaas-package-type.php | 35 ++ .../pricing/test-wiaas-package-pricing.php | 64 ++++ .../pricing/test-wiaas-product-pricing.php | 74 ++++ .../tests/unit-tests/test-wiaas-countries.php | 10 +- .../tests/unit-tests/test-wiaas-pricing.php | 359 ++++++++++++++++++ .../test-wiaas-product-category.php | 78 ++++ .../wiaas/tests/wiaas-unit-test-case.php | 41 +- 11 files changed, 798 insertions(+), 14 deletions(-) create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-addon.php create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-option-groups.php create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-type.php create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-pricing.php create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-product-pricing.php create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-pricing.php create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-product-category.php diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-pricing.php b/backend/app/plugins/wiaas/includes/class-wiaas-pricing.php index 87f124b..5f63771 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-pricing.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-pricing.php @@ -59,7 +59,7 @@ class Wiaas_Pricing { $total_item_cost = self::get_product_total_cost($product) * $bundled_item->get_quantity(); if (Wiaas_Product_Category::is_installation($product)) { - $total_cost_per_category[$product_cat] += $total_cost_per_category[$product_cat] < $total_item_cost ? + $total_cost_per_category[$product_cat] = $total_cost_per_category[$product_cat] < $total_item_cost ? $total_item_cost : $total_cost_per_category[$product_cat]; } else { diff --git a/backend/app/plugins/wiaas/includes/pricing/class-wiaas-product-pricing.php b/backend/app/plugins/wiaas/includes/pricing/class-wiaas-product-pricing.php index baf9784..9ee5840 100644 --- a/backend/app/plugins/wiaas/includes/pricing/class-wiaas-product-pricing.php +++ b/backend/app/plugins/wiaas/includes/pricing/class-wiaas-product-pricing.php @@ -11,7 +11,7 @@ class Wiaas_Product_Pricing { add_filter('woocommerce_get_price_html', array(__CLASS__, 'get_product_price_html'), 10, 2); - add_filter('woocommerce_bundled_item_price', array(__CLASS__, 'get_bundled_product_fixed_price'), 10, 4); + add_filter('woocommerce_bundled_item_price', array(__CLASS__, 'get_product_fixed_price'), 10, 2); add_filter('woocommerce_bundled_item_is_priced_individually', array(__CLASS__, 'is_bundled_product_price_fixed'), 10, 2); @@ -32,7 +32,7 @@ class Wiaas_Product_Pricing { } /** - * Checks if bunded product has fixed price + * Checks if bundled product has fixed price * @param $is_priced_individually * @param $bundled_item * @@ -51,12 +51,10 @@ class Wiaas_Product_Pricing { * Retrives fixed price for bundled product * @param $price * @param $product - * @param $discount - * @param $bundled_item * * @return int */ - public static function get_bundled_product_fixed_price($price, $product, $discount, $bundled_item) { + public static function get_product_fixed_price($price, $product) { if (self::_get_is_product_price_recurring($product)) { return 0; } @@ -108,6 +106,8 @@ class Wiaas_Product_Pricing { */ public static function set_product_price($product, $price, $is_recurring = false, $pay_period = 0) { $product->set_price($price); + $product->set_regular_price($price); + self::_set_is_product_price_recurring($product, $is_recurring); self::_set_product_price_pay_period($product, $pay_period); diff --git a/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-addon.php b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-addon.php new file mode 100644 index 0000000..5a289a2 --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-addon.php @@ -0,0 +1,39 @@ +assertContains('add_on', $package_types); + } + + /** + * @covers Wiaas_Package_Addon::set_package_addons() + * @covers Wiaas_Package_Addon::get_package_addons() + */ + function test_adding_package_addons() { + $package = $this->create_new_package(); + + $addon1 = $this->create_new_package(); + $addon2 = $this->create_new_package(); + $addons_ids = array( + $addon1->get_id(), + $addon2->get_id() + ); + + Wiaas_Package_Addon::set_package_addons($package, $addons_ids); + + $added_addons = Wiaas_Package_Addon::get_package_addons($package); + + $this->assertNotEmpty($added_addons); + $this->assertCount(2, $added_addons); + + foreach ($added_addons as $added_addon) { + $this->assertTrue(in_array($added_addon->get_id(), $addons_ids)); + } + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-option-groups.php b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-option-groups.php new file mode 100644 index 0000000..448c5ad --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-option-groups.php @@ -0,0 +1,100 @@ +assertContains('option', $package_types); + } + + /** + * @covers Wiaas_Package_Option_Groups::set_package_option_groups() + * @covers Wiaas_Package_Option_Groups::get_package_option_groups() + */ + function test_adding_package_option_group() { + $package = $this->create_new_package(); + + $option_package1 = $this->create_new_package(); + Wiaas_Package_Type::set_package_type($option_package1->get_id(), 'option'); + + $option_package2 = $this->create_new_package(); + Wiaas_Package_Type::set_package_type($option_package2->get_id(), 'option'); + + $option_group1 = array( + 'id' => 'option1', + 'name' => 'Option 1', + 'default' => $option_package1->get_id(), + 'options' => array( $option_package1->get_id() ) + ); + + $option_group2 = array( + 'id' => 'option2', + 'name' => 'Option 2', + 'default' => $option_package2->get_id(), + 'options' => array( $option_package2->get_id() ) + ); + + Wiaas_Package_Option_Groups::set_package_option_groups($package, array( + $option_group1, + $option_group2 + )); + + $added_option_groups = Wiaas_Package_Option_Groups::get_package_option_groups($package); + + $this->assertNotEmpty($added_option_groups); + $this->assertCount(2, $added_option_groups); + + foreach ($added_option_groups as $index => $added_option_group) { + $this->assertNotEmpty($added_option_group); + + $this->assertArrayHasKey('id', $added_option_group); + $this->assertArrayHasKey('name', $added_option_group); + $this->assertArrayHasKey('default', $added_option_group); + $this->assertArrayHasKey('options', $added_option_group); + + $this->assertEquals($added_option_group['id'], 'option' . ($index + 1)); + $this->assertEquals($added_option_group['name'], 'Option ' . ($index + 1)); + $this->assertCount(1, $added_option_group['options']); + + $added_option = $added_option_group['options'][0]; + $this->assertNotNull($added_option); + + $relevant_option = null; + if ($index === 0) { + $relevant_option = $option_package1; + } else { + $relevant_option = $option_package2; + } + + $this->assertEquals($added_option_group['default'], $relevant_option->get_id()); + $this->assertEquals($added_option->get_id(), $relevant_option->get_id()); + } + } + + /** + * @covers Wiaas_Package_Option_Groups::get_group_name_for_package_option() + */ + function test_get_group_name_for_package_option() { + $package = $this->create_new_package(); + + $option_package = $this->create_new_package(); + Wiaas_Package_Type::set_package_type($option_package->get_id(), 'option'); + + $option_group = array( + 'id' => 'option1', + 'name' => 'Option 1', + 'default' => $option_package->get_id(), + 'options' => array( $option_package->get_id() ) + ); + + Wiaas_Package_Option_Groups::set_package_option_groups($package, array( $option_group )); + + $option_group_name = Wiaas_Package_Option_Groups::get_group_name_for_package_option($package, $option_package); + + $this->assertEquals($option_group_name, $option_group['name']); + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-type.php b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-type.php new file mode 100644 index 0000000..a2e5fd0 --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-type.php @@ -0,0 +1,35 @@ +assertInstanceOf(WP_Taxonomy::class, $taxonomy); + + $package_types = Wiaas_Package_Type::get_available_package_types(); + + $this->assertNotEmpty($package_types); + + $this->assertContains('standard', $package_types); + } + + /** + * @covers Wiaas_Package_Type::set_package_type() + * @covers Wiaas_Package_Type::get_package_type() + */ + function test_adding_package_type() { + $package = $this->create_new_package(); + + Wiaas_Package_Type::set_package_type($package->get_id(), 'standard'); + + $package_type = Wiaas_Package_Type::get_package_type($package->get_id()); + + $this->assertNotNull($package_type); + $this->assertEquals($package_type, 'standard'); + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-pricing.php b/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-pricing.php new file mode 100644 index 0000000..648b2dc --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-pricing.php @@ -0,0 +1,64 @@ +create_new_package(); + + $pricing_rules = array( + 'purchase' => array( + 'minimal_fixed_price' => 10, + 'principal_amount' => 0, + 'minimal_services_price' => 0 + ), + 'purchase_24' => array( + 'minimal_fixed_price' => 10, + 'principal_amount' => 0, + 'minimal_services_price' => 10 + ), + 'managed_36' => array( + 'minimal_fixed_price' => 10, + 'principal_amount' => 10, + 'minimal_services_price' => 10 + ) + ); + + $commision = 60; + + Wiaas_Package_Pricing::set_package_prices($package, $pricing_rules, $commision); + + $configured_prices = Wiaas_Package_Pricing::get_package_prices($package); + + $this->assertCount(3, $configured_prices); + + $this->assertArrayHasKey('purchase', $configured_prices); + $this->assertArrayHasKey('purchase_24', $configured_prices); + $this->assertArrayHasKey('managed_36', $configured_prices); + + foreach ($configured_prices as $type => $configured_price) { + + $this->assertArrayHasKey('id', $configured_price); + $this->assertArrayHasKey('payment_type', $configured_price); + $this->assertArrayHasKey('max_contract_period', $configured_price); + $this->assertArrayHasKey('package_pay_period', $configured_price); + $this->assertArrayHasKey('period_unit', $configured_price); + $this->assertArrayHasKey('services_contract_period', $configured_price); + $this->assertArrayHasKey('commision_split', $configured_price); + $this->assertArrayHasKey('minimal_fixed_price', $configured_price); + $this->assertArrayHasKey('principal_amount', $configured_price); + $this->assertArrayHasKey('minimal_services_price', $configured_price); + + $this->assertEquals($configured_price['id'], $type); + $this->assertEquals($configured_price['commision_split'], $commision / 100); + + $this->assertEquals($configured_price['minimal_fixed_price'], $pricing_rules[$type]['minimal_fixed_price']); + $this->assertEquals($configured_price['principal_amount'], $pricing_rules[$type]['principal_amount']); + $this->assertEquals($configured_price['minimal_services_price'], $pricing_rules[$type]['minimal_services_price']); + } + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-product-pricing.php b/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-product-pricing.php new file mode 100644 index 0000000..834a5db --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-product-pricing.php @@ -0,0 +1,74 @@ +create_new_product(10, true, 2); + + $configured_price = Wiaas_Product_Pricing::get_product_price($product); + + $this->assertNotNull($configured_price); + + $this->assertArrayHasKey('price', $configured_price); + $this->assertArrayHasKey('is_recurring', $configured_price); + $this->assertArrayHasKey('pay_period', $configured_price); + + $this->assertEquals($configured_price['price'], 10); + $this->assertEquals($configured_price['is_recurring'], true); + $this->assertEquals($configured_price['pay_period'], 2); + } + + /** + * @covers Wiaas_Product_Pricing::get_product_price_html() + */ + function test_get_product_fixed_price_html() { + $product = $this->create_new_product(); + + $original_price_html = $product->get_price_html(); + + $price_html = Wiaas_Product_Pricing::get_product_price_html($original_price_html, $product); + + $this->assertEquals($price_html, $original_price_html); + } + + /** + * @covers Wiaas_Product_Pricing::get_product_price_html() + */ + function test_get_product_recurrent_price_html() { + $product = $this->create_new_product(10, true, 1); + + $original_price_html = $product->get_price_html(); + + $price_html = Wiaas_Product_Pricing::get_product_price_html($original_price_html, $product); + + $this->assertEquals($price_html, $original_price_html . ' / month'); + } + + /** + * @covers Wiaas_Product_Pricing::get_product_fixed_price() + */ + function test_get_fixed_price_for_fixed_product() { + $product = $this->create_new_product(); + + $original_price = $product->get_price(); + + $fixed_price = Wiaas_Product_Pricing::get_product_fixed_price($original_price, $product); + + $this->assertEquals($fixed_price, $original_price); + } + + /** + * @covers Wiaas_Product_Pricing::get_product_fixed_price() + */ + function test_get_fixed_price_for_recurring_product() { + $product = $this->create_new_product(10, true, 1); + + $fixed_price = Wiaas_Product_Pricing::get_product_fixed_price($product->get_price(), $product); + + $this->assertEquals($fixed_price, 0); + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php index caee4f1..be4b538 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php @@ -13,12 +13,12 @@ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { # set admin as current user wp_set_current_user(1); - $this->product = $this->insertNewProduct(); + $this->product = $this->create_new_product(); wp_set_object_terms($this->product->get_id(), $this->current_country, 'product_country', false); clean_object_term_cache( $this->product->get_id(), 'product_country' ); - $this->package = $this->insertNewPackage(); + $this->package = $this->create_new_package(); wp_set_object_terms($this->package->get_id(), $this->current_country, 'product_country', false); clean_object_term_cache( $this->package->get_id(), 'product_country' ); } @@ -39,9 +39,9 @@ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { $this->assertNotEmpty($country_names); - $this->assertTrue(in_array('Sweden', $country_names)); - $this->assertTrue(in_array('Denmark', $country_names)); - $this->assertTrue(in_array('Finland', $country_names)); + $this->assertContains('Sweden', $country_names); + $this->assertContains('Denmark', $country_names); + $this->assertContains('Finland', $country_names); } /** diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-pricing.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-pricing.php new file mode 100644 index 0000000..5d62fdd --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-pricing.php @@ -0,0 +1,359 @@ +create_new_product(20); + $this->add_product_category($product1, 'hardware'); + + $product2 = $this->create_new_product(20, true, 2); + $this->add_product_category($product2, 'installation'); + + $package = $this->create_new_package(); + + $this->add_products_to_package($package, array( $product1, $product2)); + + $pricing_rules = array( + 'purchase' => array( + 'minimal_fixed_price' => 100, + 'principal_amount' => 0, + 'minimal_services_price' => 0 + ), + 'purchase_24' => array( + 'minimal_fixed_price' => 100, + 'principal_amount' => 0, + 'minimal_services_price' => 100 + ), + 'managed_36' => array( + 'minimal_fixed_price' => 100, + 'principal_amount' => 100, + 'minimal_services_price' => 100 + ) + ); + $commision = 50; + Wiaas_Package_Pricing::set_package_prices($package, $pricing_rules, $commision); + + $expected_prices = array( + 'purchase' => array( + 'fixed_extra' => 100, + 'recurrent_extra' => 0, + 'services_extra' => 0 + ), + 'purchase_24' => array( + 'fixed_extra' => 100, + 'recurrent_extra' => 0, + 'services_extra' => 100 + ), + 'managed_36' => array( + 'fixed_extra' => 100, + 'recurrent_extra' => 3, + 'services_extra' => 100 + ) + ); + + return array( $package, $expected_prices ); + } + + /** + * @covers Wiaas_Pricing::get_product_total_cost() + */ + function test_get_fixed_product_total_cost() { + $product = $this->create_new_product(); + + $total_cost = Wiaas_Pricing::get_product_total_cost($product); + + $this->assertEquals($total_cost, $product->get_price()); + } + + /** + * @covers Wiaas_Pricing::get_product_total_cost() + */ + function test_get_recurring_product_total_cost() { + $product = $this->create_new_product(10, true, 2); + + $total_cost = Wiaas_Pricing::get_product_total_cost($product); + + $this->assertEquals($total_cost, 20); + } + + /** + * @covers Wiaas_Pricing::get_package_total_cost() + */ + function test_get_package_with_fixed_products_total_cost() { + $product1 = $this->create_new_product(20); + $this->add_product_category($product1, 'hardware'); + + $product2 = $this->create_new_product(20); + $this->add_product_category($product2, 'software'); + + $package = $this->create_new_package(); + + $this->add_products_to_package($package, array( $product1, $product2)); + + $expected_total_price = 40; + + $total_price = Wiaas_Pricing::get_package_total_cost($package); + + $this->assertEquals($expected_total_price, $total_price); + } + + /** + * @covers Wiaas_Pricing::get_package_total_cost() + */ + function test_get_package_with_recurring_products_total_cost() { + $product1 = $this->create_new_product(20); + $this->add_product_category($product1, 'hardware'); + + $product2 = $this->create_new_product(20, true, 2); + $this->add_product_category($product2, 'software'); + + $package = $this->create_new_package(); + + $this->add_products_to_package($package, array( $product1, $product2)); + + $expected_total_price = 60; + + $total_price = Wiaas_Pricing::get_package_total_cost($package); + + $this->assertEquals($expected_total_price, $total_price); + } + + /** + * @covers Wiaas_Pricing::get_package_total_cost() + */ + function test_package_with_single_installation_product_total_cost() { + $product1 = $this->create_new_product(20); + $this->add_product_category($product1, 'hardware'); + + $product2 = $this->create_new_product(20, true, 2); + $this->add_product_category($product2, 'installation'); + + $package = $this->create_new_package(); + + $this->add_products_to_package($package, array( $product1, $product2)); + + $expected_total_price = 60; + + $total_price = Wiaas_Pricing::get_package_total_cost($package); + + $this->assertEquals($expected_total_price, $total_price); + } + + /** + * @covers Wiaas_Pricing::get_package_total_cost() + */ + function test_package_with_multiple_installation_products_total_cost() { + $product1 = $this->create_new_product(20); + $this->add_product_category($product1, 'hardware'); + + $product2 = $this->create_new_product(10, true, 2); + $this->add_product_category($product2, 'installation'); + + $this->assertTrue(Wiaas_Product_Category::is_installation($product2)); + + $product3 = $this->create_new_product(20, true, 2); + $this->add_product_category($product3, 'installation'); + + $package = $this->create_new_package(); + + $this->add_products_to_package($package, array( $product1, $product2, $product3)); + + // price will be 20 + 20*2 , more expensive installation will be applied + $expected_total_price = 60; + + $total_price = Wiaas_Pricing::get_package_total_cost($package); + + $this->assertEquals($expected_total_price, $total_price); + } + + /** + * @covers Wiaas_Pricing::get_standard_package_customer_prices() + */ + function test_get_standard_package_customer_price() { + + list( $package, $expected_prices ) = $this->_create_package_to_sell(); + + $customer_prices = Wiaas_Pricing::get_standard_package_customer_prices($package); + + $this->assertCount(3, $customer_prices); + + foreach ($customer_prices as $customer_price) { + + $this->assertArrayHasKey('id', $customer_price); + $this->assertArrayHasKey('payment_type', $customer_price); + $this->assertArrayHasKey('max_contract_period', $customer_price); + $this->assertArrayHasKey('package_pay_period', $customer_price); + $this->assertArrayHasKey('period_unit', $customer_price); + $this->assertArrayHasKey('services_contract_period', $customer_price); + $this->assertArrayHasKey('fixed_extra', $customer_price); + $this->assertArrayHasKey('recurrent_extra', $customer_price); + $this->assertArrayHasKey('services_extra', $customer_price); + + $id = $customer_price['id']; + + $this->assertEquals($customer_price['fixed_extra'], $expected_prices[$id]['fixed_extra']); + $this->assertEquals($customer_price['recurrent_extra'], $expected_prices[$id]['recurrent_extra']); + $this->assertEquals($customer_price['services_extra'], $expected_prices[$id]['services_extra']); + } + + } + + /** + * @covers Wiaas_Pricing::get_addon_package_customer_price() + */ + function test_get_addon_package_customer_price() { + list( $package ) = $this->_create_package_to_sell(); + + $addon_product = $this->create_new_product(20); + $this->add_product_category($addon_product, 'hardware'); + + $addon_package = $this->create_new_package(); + + $this->add_products_to_package($addon_package, array($addon_product)); + + $pricing_rules = array( + 'purchase' => array( + 'minimal_fixed_price' => 500, + 'principal_amount' => 0, + 'minimal_services_price' => 0 + ), + 'purchase_24' => array( + 'minimal_fixed_price' => 500, + 'principal_amount' => 0, + 'minimal_services_price' => 500 + ), + 'managed_36' => array( + 'minimal_fixed_price' => 500, + 'principal_amount' => 500, + 'minimal_services_price' => 500 + ) + ); + $commision = 50; + Wiaas_Package_Pricing::set_package_prices($addon_package, $pricing_rules, $commision); + + Wiaas_Package_Addon::set_package_addons($package, array($addon_package->get_id())); + + $expected_prices = array( + 'purchase' => array( + 'fixed_extra' => 500, + 'recurrent_extra' => 0, + 'services_extra' => 0 + ), + 'purchase_24' => array( + 'fixed_extra' => 500, + 'recurrent_extra' => 0, + 'services_extra' => 500 + ), + 'managed_36' => array( + 'fixed_extra' => 500, + 'recurrent_extra' => 14, + 'services_extra' => 500 + ) + ); + + $customer_prices = Wiaas_Pricing::get_addon_package_customer_price($addon_package, $package); + + $this->assertCount(3, $customer_prices); + + foreach ($customer_prices as $customer_price) { + + $this->assertArrayHasKey('id', $customer_price); + $this->assertArrayHasKey('payment_type', $customer_price); + $this->assertArrayHasKey('max_contract_period', $customer_price); + $this->assertArrayHasKey('package_pay_period', $customer_price); + $this->assertArrayHasKey('period_unit', $customer_price); + $this->assertArrayHasKey('services_contract_period', $customer_price); + $this->assertArrayHasKey('fixed_extra', $customer_price); + $this->assertArrayHasKey('recurrent_extra', $customer_price); + $this->assertArrayHasKey('services_extra', $customer_price); + + $id = $customer_price['id']; + + $this->assertEquals($customer_price['fixed_extra'], $expected_prices[$id]['fixed_extra']); + $this->assertEquals($customer_price['recurrent_extra'], $expected_prices[$id]['recurrent_extra']); + $this->assertEquals($customer_price['services_extra'], $expected_prices[$id]['services_extra']); + } + } + + /** + * @covers Wiaas_Pricing::get_option_package_customer_price() + */ + function test_get_option_package_customer_price() { + list( $package ) = $this->_create_package_to_sell(); + + $option_product = $this->create_new_product(20); + $this->add_product_category($option_product, 'hardware'); + + $option_package = $this->create_new_package(); + + $this->add_products_to_package($option_package, array($option_product)); + + $pricing_rules = array( + 'purchase' => array( + 'minimal_fixed_price' => 500, + 'principal_amount' => 0, + 'minimal_services_price' => 0 + ), + 'purchase_24' => array( + 'minimal_fixed_price' => 500, + 'principal_amount' => 0, + 'minimal_services_price' => 500 + ), + 'managed_36' => array( + 'minimal_fixed_price' => 500, + 'principal_amount' => 500, + 'minimal_services_price' => 500 + ) + ); + $commision = 50; + Wiaas_Package_Pricing::set_package_prices($option_package, $pricing_rules, $commision); + + Wiaas_Package_Option_Groups::set_package_option_groups($package, array( + 'id' => 'option', + 'name' => 'Option', + 'default' => $option_package->get_id(), + 'options' => array( $option_package->get_id() ) + )); + + $expected_prices = array( + 'purchase' => array( + 'fixed_extra' => 500, + 'recurrent_extra' => 0, + 'services_extra' => 0 + ), + 'purchase_24' => array( + 'fixed_extra' => 500, + 'recurrent_extra' => 0, + 'services_extra' => 500 + ), + 'managed_36' => array( + 'fixed_extra' => 500, + 'recurrent_extra' => 14, + 'services_extra' => 500 + ) + ); + + $customer_prices = Wiaas_Pricing::get_option_package_customer_price($option_package, $package); + + $this->assertCount(3, $customer_prices); + + foreach ($customer_prices as $customer_price) { + + $this->assertArrayHasKey('id', $customer_price); + $this->assertArrayHasKey('payment_type', $customer_price); + $this->assertArrayHasKey('max_contract_period', $customer_price); + $this->assertArrayHasKey('package_pay_period', $customer_price); + $this->assertArrayHasKey('period_unit', $customer_price); + $this->assertArrayHasKey('services_contract_period', $customer_price); + $this->assertArrayHasKey('fixed_extra', $customer_price); + $this->assertArrayHasKey('recurrent_extra', $customer_price); + $this->assertArrayHasKey('services_extra', $customer_price); + + $id = $customer_price['id']; + + $this->assertEquals($customer_price['fixed_extra'], $expected_prices[$id]['fixed_extra']); + $this->assertEquals($customer_price['recurrent_extra'], $expected_prices[$id]['recurrent_extra']); + $this->assertEquals($customer_price['services_extra'], $expected_prices[$id]['services_extra']); + } + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-product-category.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-product-category.php new file mode 100644 index 0000000..a485fde --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-product-category.php @@ -0,0 +1,78 @@ +assertInstanceOf(WP_Taxonomy::class, $taxonomy); + + $category_names = array_map(function($term) { + return $term->name; + }, get_terms(array( 'taxonomy' => 'product_cat', 'hide_empty' => false ))); + + $this->assertNotEmpty($category_names); + + $this->assertTrue(in_array('hardware', $category_names)); + $this->assertTrue(in_array('software', $category_names)); + $this->assertTrue(in_array('service', $category_names)); + $this->assertTrue(in_array('installation', $category_names)); + } + + /** + * @covers Wiaas_Product_Category::get_category() + */ + function test_get_category() { + $product = $this->create_new_product(); + $this->add_product_category($product, 'hardware'); + + $product_category = Wiaas_Product_Category::get_category($product); + + $this->assertNotNull($product_category); + $this->assertEquals($product_category, 'hardware'); + } + + /** + * @covers Wiaas_Product_Category::is_service() + */ + function test_is_service_negative() { + $product = $this->create_new_product(); + $this->add_product_category($product, 'hardware'); + + $this->assertFalse(Wiaas_Product_Category::is_service($product)); + } + + /** + * @covers Wiaas_Product_Category::is_service() + */ + function test_is_service_positive() { + $product = $this->create_new_product(); + $this->add_product_category($product, 'service'); + + $this->assertTrue(Wiaas_Product_Category::is_service($product)); + } + + /** + * @covers Wiaas_Product_Category::is_installation() + */ + function test_is_installation_negative() { + $product = $this->create_new_product(); + $this->add_product_category($product, 'hardware'); + + $this->assertFalse(Wiaas_Product_Category::is_installation($product)); + } + + /** + * @covers Wiaas_Product_Category::is_installation() + */ + function test_is_installation_positive() { + $product = $this->create_new_product(); + $this->add_product_category($product, 'installation'); + + $this->assertTrue(Wiaas_Product_Category::is_installation($product)); + } + +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php b/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php index 6206ae1..f763881 100644 --- a/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php +++ b/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php @@ -8,15 +8,22 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase { */ function setUp() { parent::setUp(); + wp_set_current_user(1); # Setup Gravity info since options table is deleted after each test gf_upgrade()->install(); + WC_PB_Install::install(); + wiaas_db_update_setup_gravity(); wiaas_db_update_enable_orders_access_management(); Wiaas_Countries::register_product_countries_taxonomy(); + + Wiaas_Product_Category::register_product_categories(); + + Wiaas_Package_Type::register_package_type_taxonomy(); define('WP_TEST_IN_PROGRESS',true); } @@ -25,7 +32,7 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase { parent::tearDown(); } - function insertNewProduct() { + function create_new_product($price = 10, $is_recurring = false, $pay_period = 0) { $post_id = wp_insert_post(array( 'post_type' => 'product', 'post_status' => 'publish', @@ -35,10 +42,16 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase { 'post_excerpt' => 'Product' ), true); - return new WC_Product_Simple($post_id); + $product = new WC_Product_Simple($post_id); + + Wiaas_Product_Pricing::set_product_price($product, $price, $is_recurring, $pay_period); + + $product->save(); + + return $product; } - function insertNewPackage() { + function create_new_package() { $post_id = wp_insert_post(array( 'post_type' => 'product', 'post_status' => 'publish', @@ -50,4 +63,26 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase { return new WC_Product_Bundle($post_id); } + + function add_product_category($product, $name) { + wp_set_object_terms($product->get_id(), $name, 'product_cat', false); + clean_object_term_cache( $product->get_id(), 'product_cat' ); + } + + function add_products_to_package($package, $products) { + $bundled_data = array(); + + foreach ($products as $product) { + $bundled_data[] = array( + 'product_id' => $product->get_id(), + 'quantity_min' => 1, + 'quantity_max' => 1, + 'priced_individually' => true + ); + } + + $package->set_bundled_data_items($bundled_data); + + $package->sync(true); + } } \ No newline at end of file