From cae148ebce6d6e1c159e268936982105a47d4662 Mon Sep 17 00:00:00 2001 From: Almira Krdzic Date: Tue, 18 Sep 2018 13:13:48 +0200 Subject: [PATCH] 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