Packages and products countries test

This commit is contained in:
Almira Krdzic
2018-09-18 13:13:48 +02:00
parent d4c75dcf65
commit cae148ebce
3 changed files with 119 additions and 0 deletions

View File

@@ -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';

View File

@@ -0,0 +1,71 @@
<?php
/**
* Class Wiaas_Countries_Test
*/
class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case {
var $product, $package, $current_country = 'Sweden';
public function setUp() {
parent::setUp();
# set admin as current user
wp_set_current_user(1);
$this->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!');
}
}

View File

@@ -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);
}
}