Add unit tests for backend and refactor few things on frontend
This commit is contained in:
@@ -9,7 +9,9 @@ class Wiaas_DB_Update {
|
||||
'20180801222206' => 'wiaas_db_update_setup_gravity',
|
||||
'20180802222206' => 'wiaas_db_update_add_delivery_process_forms',
|
||||
'20180807222206' => 'wiaas_db_update_setup_customer_capabilities',
|
||||
'20180809134511' => 'wiaas_db_update_add_customer_read_permission'
|
||||
'20180809134511' => 'wiaas_db_update_add_customer_read_permission',
|
||||
'20180811134511' => 'wiaas_db_update_enable_orders_access_management',
|
||||
'20180813134511' => 'wiaas_db_update_enable_order_numbers'
|
||||
);
|
||||
|
||||
public static function execute() {
|
||||
|
||||
@@ -84,7 +84,26 @@ function wiaas_db_update_setup_customer_capabilities() {
|
||||
$customer_role->add_cap('read_private_shop_orders');
|
||||
$customer_role->add_cap('read_shop_order');
|
||||
}
|
||||
|
||||
function wiaas_db_update_add_customer_read_permission() {
|
||||
$role = get_role( 'customer' );
|
||||
$role->add_cap( 'read_private_products' );
|
||||
}
|
||||
|
||||
function wiaas_db_update_enable_orders_access_management() {
|
||||
$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );
|
||||
|
||||
$post_types_option['shop_order'] = array(
|
||||
'add_meta_box' => true
|
||||
);
|
||||
|
||||
Groups_Options::update_option(Groups_Post_Access::POST_TYPES, $post_types_option);
|
||||
}
|
||||
|
||||
function wiaas_db_update_enable_order_numbers() {
|
||||
update_option('wcj_order_numbers_enabled', 'yes');
|
||||
update_option('wcj_order_number_sequential_enabled', 'no');
|
||||
update_option('wcj_order_number_counter', '0');
|
||||
update_option('wcj_order_number_counter_reset_enabled', 'no');
|
||||
update_option('wcj_order_number_prefix', '1000000');
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
|
||||
public function __construct()
|
||||
{
|
||||
$args = array(
|
||||
'singular' => __('Organization', 'wp-user-groups'),
|
||||
'plural' => __('Organizations', 'wp-user-groups'),
|
||||
'singular' => __('Organization', 'wiaas'),
|
||||
'plural' => __('Organizations', 'wiaas'),
|
||||
'exclusive' => true,
|
||||
'public' => true,
|
||||
'show_in_rest' => true,
|
||||
@@ -242,13 +242,71 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
|
||||
}
|
||||
|
||||
/**
|
||||
* Show organizations selection without $user info
|
||||
* Show organizations selection on new user form
|
||||
*/
|
||||
function show_organizations_selection() {
|
||||
$terms = get_terms( array(
|
||||
'taxonomy' => self::TAXONOMY_NAME,
|
||||
'hide_empty' => false,
|
||||
) );
|
||||
$taxonomy = get_taxonomy(self::TAXONOMY_NAME);
|
||||
$this->table_contents(null, $taxonomy, $terms);
|
||||
}
|
||||
|
||||
function edit_user_relationships($user = false) {
|
||||
$tax = get_taxonomy( $this->taxonomy );
|
||||
|
||||
// Get the terms of the taxonomy.
|
||||
$terms = get_terms( $this->taxonomy, array(
|
||||
'hide_empty' => false
|
||||
) );
|
||||
|
||||
$this->table_contents( $user, $tax, $terms );
|
||||
}
|
||||
|
||||
function table_contents( $user, $tax, $terms ) {
|
||||
$active_organization_id = -1;
|
||||
if ($user) {
|
||||
$active_organization = self::get_user_organization($user->ID);
|
||||
$active_organization_id = $active_organization ? $active_organization->term_id : -1;
|
||||
}
|
||||
|
||||
?>
|
||||
<table class="form-table">
|
||||
<tbody>
|
||||
<tr class="form-field">
|
||||
<th scope="row">
|
||||
<label for="<?php echo esc_attr( $this->taxonomy ); ?>[]">
|
||||
<?php echo esc_html( $tax->labels->singular_name ); ?>
|
||||
</label>
|
||||
</th>
|
||||
<td>
|
||||
<select name="<?php echo esc_attr( $this->taxonomy ); ?>[]" id="<?php echo esc_attr( $this->taxonomy ); ?>">
|
||||
<?php
|
||||
foreach ( $terms as $term ) :
|
||||
$selected = $active_organization_id === $term->term_id;
|
||||
?>
|
||||
<option
|
||||
value="<?php echo esc_attr( $term->slug ); ?>"
|
||||
<?php selected( $selected ); ?>
|
||||
>
|
||||
<?php echo esc_attr( $term->name ); ?>
|
||||
</option>
|
||||
<?php
|
||||
endforeach;
|
||||
?>
|
||||
</select>
|
||||
<?php
|
||||
wp_nonce_field( $this->taxonomy, $this->get_nonce_key() );
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
}
|
||||
|
||||
private function get_nonce_key() {
|
||||
return "wp_user_taxonomy_{$this->taxonomy}";
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,10 @@ function load_wiaas_test_setup() {
|
||||
|
||||
require_once '/tmp/wiaas-backend-test/app/plugins/gravityflow/gravityflow.php';
|
||||
|
||||
require_once '/tmp/wiaas-backend-test/app/plugins/groups/groups.php';
|
||||
|
||||
require_once '/tmp/wiaas-backend-test/app/plugins/wp-user-groups/wp-user-groups.php';
|
||||
|
||||
require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/wiaas.php';
|
||||
|
||||
# Require classes needed for db updates
|
||||
|
||||
188
backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php
Normal file
188
backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php
Normal file
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Wiaas_Order_Test
|
||||
*/
|
||||
|
||||
class Wiaas_Order_Test extends Wiaas_Unit_Test_Case {
|
||||
|
||||
var $customer_id, $customer_organization_id, $customer_organization_name, $order_id;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
wp_set_current_user(1);
|
||||
|
||||
# create customer user
|
||||
$this->customer_id = wc_create_new_customer(
|
||||
'test_customer@mail.com',
|
||||
'test_customer',
|
||||
'test');
|
||||
|
||||
$this->customer_organization_name = 'test-customer-organization';
|
||||
|
||||
# create customer organization
|
||||
$this->customer_organization_id = wp_insert_term(
|
||||
$this->customer_organization_name,
|
||||
Wiaas_User_Organization::TAXONOMY_NAME
|
||||
)['term_id'];
|
||||
|
||||
# add customer to organization
|
||||
wp_set_terms_for_user(
|
||||
$this->customer_id,
|
||||
Wiaas_User_Organization::TAXONOMY_NAME,
|
||||
[$this->customer_organization_name]);
|
||||
|
||||
wp_set_current_user($this->customer_id);
|
||||
|
||||
$order = wc_create_order(array(
|
||||
'customer_id' => $this->customer_id
|
||||
));
|
||||
|
||||
$this->order_id = $order->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order::assign_order_to_organization()
|
||||
*/
|
||||
function test_order_assigned_to_customer_organization() {
|
||||
|
||||
$organization_access_group = Groups_Group::read_by_name($this->customer_organization_name);
|
||||
$access_group_ids = Groups_Post_Access::get_read_group_ids( $this->order_id );
|
||||
|
||||
$this->assertEquals(1, count($access_group_ids));
|
||||
$this->assertNotNull($access_group_ids[0]);
|
||||
$this->assertEquals($organization_access_group->group_id, $access_group_ids[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order::check_order_access()
|
||||
*/
|
||||
function test_order_customer_can_access_order() {
|
||||
|
||||
$has_access = Wiaas_Order::check_order_access(true, 'view', $this->order_id, 'shop_order');
|
||||
|
||||
$this->assertTrue($has_access);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order::check_order_access()
|
||||
*/
|
||||
function test_customer_cannot_access_order_when_not_in_organization() {
|
||||
$customer_id = wc_create_new_customer(
|
||||
'test_customer1@mail.com',
|
||||
'test_customer1',
|
||||
'test1');
|
||||
wp_set_current_user($customer_id);
|
||||
|
||||
$this->assertTrue(Groups_Post_Access::handles_post_type('shop_order'));
|
||||
|
||||
$has_access = Wiaas_Order::check_order_access(true, 'view', $this->order_id, 'shop_order');
|
||||
|
||||
$this->assertFalse($has_access);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order::wiaas_prepare_rest_orders_query()
|
||||
*/
|
||||
function test_valid_order_query_status_for_active_orders() {
|
||||
$args = array();
|
||||
$request = array(
|
||||
'wiaas_is_active' => '1'
|
||||
);
|
||||
|
||||
$args = Wiaas_Order::wiaas_prepare_rest_orders_query($args, $request);
|
||||
|
||||
$this->assertEquals(
|
||||
array('wc-open', 'wc-processing'),
|
||||
$args['post_status']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order::wiaas_prepare_rest_orders_query()
|
||||
*/
|
||||
function test_valid_order_query_status_for_history_orders() {
|
||||
$args = array();
|
||||
$request = array(
|
||||
'wiaas_is_active' => '0'
|
||||
);
|
||||
|
||||
$args = Wiaas_Order::wiaas_prepare_rest_orders_query($args, $request);
|
||||
|
||||
$this->assertEquals(
|
||||
array('wc-completed', 'wc-cancelled'),
|
||||
$args['post_status']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order::transform_rest_order()
|
||||
*/
|
||||
function test_order_rest_response_has_process() {
|
||||
$order_response = array(
|
||||
'customer_id' => $this->customer_id,
|
||||
'status' => 'processing',
|
||||
'line_items' => array()
|
||||
);
|
||||
$request = array( 'id' => $this->order_id);
|
||||
|
||||
$order_rest_response = new WP_REST_Response($order_response);
|
||||
|
||||
$order_rest_response = Wiaas_Order::transform_rest_order(
|
||||
$order_rest_response,
|
||||
wc_get_order($this->order_id),
|
||||
$request);
|
||||
|
||||
$transformed_order_response = $order_rest_response->get_data();
|
||||
|
||||
$this->assertNotNull($transformed_order_response['delivery-process']);
|
||||
$this->assertTrue(is_array($transformed_order_response['delivery-process']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order::transform_rest_order()
|
||||
*/
|
||||
function test_order_rest_response_has_customer_info() {
|
||||
$order_response = array(
|
||||
'customer_id' => $this->customer_id,
|
||||
'status' => 'processing',
|
||||
'line_items' => array()
|
||||
);
|
||||
|
||||
$order_rest_response = Wiaas_Order::transform_rest_order(
|
||||
new WP_REST_Response($order_response),
|
||||
wc_get_order($this->order_id),
|
||||
array( 'id' => $this->order_id));
|
||||
|
||||
$transformed_order_response = $order_rest_response->get_data();
|
||||
|
||||
$this->assertNotNull($transformed_order_response['customer']);
|
||||
$this->assertTrue(is_array($transformed_order_response['customer']));
|
||||
$this->assertArrayHasKey('name', $transformed_order_response['customer']);
|
||||
$this->assertArrayHasKey('email', $transformed_order_response['customer']);
|
||||
$this->assertArrayHasKey('phone', $transformed_order_response['customer']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_Order::transform_rest_order()
|
||||
*/
|
||||
function test_order_rest_response_has_commercial_lead_info() {
|
||||
$order_response = array(
|
||||
'customer_id' => $this->customer_id,
|
||||
'status' => 'processing',
|
||||
'line_items' => array()
|
||||
);
|
||||
|
||||
$order_rest_response = Wiaas_Order::transform_rest_order(
|
||||
new WP_REST_Response($order_response),
|
||||
wc_get_order($this->order_id),
|
||||
array( 'id' => $this->order_id));
|
||||
|
||||
$transformed_order_response = $order_rest_response->get_data();
|
||||
|
||||
$this->assertNotNull($transformed_order_response['commercial_lead']);
|
||||
$this->assertTrue(is_array($transformed_order_response['commercial_lead']));
|
||||
$this->assertArrayHasKey('name', $transformed_order_response['commercial_lead']);
|
||||
$this->assertArrayHasKey('email', $transformed_order_response['commercial_lead']);
|
||||
$this->assertArrayHasKey('phone', $transformed_order_response['commercial_lead']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Wiaas_User_Organization_Test
|
||||
*
|
||||
* @package Wiaas
|
||||
*/
|
||||
|
||||
class Wiaas_User_Organization_Test extends Wiaas_Unit_Test_Case {
|
||||
|
||||
var $user_id, $user_organization_name, $user_organization_id, $user_department_name, $user_department_id;
|
||||
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
# set admin as current user
|
||||
wp_set_current_user(1);
|
||||
|
||||
# create testing user
|
||||
$this->user_id = wp_create_user('test', 'test', 'test@mail.com');
|
||||
$this->user_organization_name = 'test_organization';
|
||||
|
||||
$this->user_department_name = 'test-department';
|
||||
|
||||
# create organization
|
||||
$this->user_organization_id = wp_insert_term(
|
||||
$this->user_organization_name,
|
||||
Wiaas_User_Organization::TAXONOMY_NAME
|
||||
)['term_id'];
|
||||
|
||||
# create department
|
||||
$this->user_department_id = wp_insert_term(
|
||||
$this->user_department_name,
|
||||
Wiaas_User_Organization::TAXONOMY_NAME,
|
||||
array(
|
||||
'parent' => $this->user_organization_id
|
||||
)
|
||||
)['term_id'];
|
||||
|
||||
# assign user to organization
|
||||
wp_set_terms_for_user(
|
||||
$this->user_id,
|
||||
Wiaas_User_Organization::TAXONOMY_NAME,
|
||||
[$this->user_organization_name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_User_Organization::get_user_organization()
|
||||
*/
|
||||
function test_retrieve_user_organization() {
|
||||
$organization = Wiaas_User_Organization::get_user_organization($this->user_id);
|
||||
|
||||
$this->assertNotNull($organization);
|
||||
$this->assertEquals($organization->term_id, $this->user_organization_id);
|
||||
$this->assertEquals($organization->name, $this->user_organization_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_User_Organization::on_organization_added()
|
||||
*/
|
||||
function test_access_group_created_when_organization_created() {
|
||||
# check organization access group id saved as term metadata
|
||||
$organization_access_group_id = get_term_meta($this->user_organization_id, 'group_id', true);
|
||||
$this->assertNotNull($organization_access_group_id);
|
||||
|
||||
# check organization access group object exists
|
||||
$organization_access_group = Groups_Group::read_by_name($this->user_organization_name);
|
||||
$this->assertNotFalse($organization_access_group);
|
||||
|
||||
# check created access group object corresponds to this organization
|
||||
$this->assertEquals($organization_access_group->name, $this->user_organization_name);
|
||||
$this->assertEquals($organization_access_group->group_id, $organization_access_group_id);
|
||||
|
||||
# check department access group id saved as term metadata
|
||||
$department_access_group_id = get_term_meta($this->user_department_id, 'group_id', true);
|
||||
$this->assertNotNull($department_access_group_id);
|
||||
|
||||
# check department access group object exists
|
||||
$department_access_group = Groups_Group::read_by_name($this->user_department_name);
|
||||
$this->assertNotFalse($department_access_group);
|
||||
|
||||
# check created access group object corresponds to this department
|
||||
$this->assertEquals($department_access_group->name, $this->user_department_name);
|
||||
$this->assertEquals($department_access_group->group_id, $department_access_group_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_User_Organization::on_taxonomy_term_assigned()
|
||||
*/
|
||||
function test_user_added_to_organization_access_groups() {
|
||||
# check user added to organization access group
|
||||
$organization_access_group = Groups_Group::read_by_name($this->user_organization_name);
|
||||
$user_group_relation = Groups_User_Group::read( $this->user_id , $organization_access_group->group_id );
|
||||
|
||||
$this->assertNotFalse($user_group_relation);
|
||||
$this->assertEquals($user_group_relation->user_id, $this->user_id);
|
||||
$this->assertEquals($user_group_relation->group_id, $organization_access_group->group_id);
|
||||
|
||||
# test user added to department access group
|
||||
$department_access_group = Groups_Group::read_by_name($this->user_department_name);
|
||||
$user_group_relation = Groups_User_Group::read( $this->user_id , $department_access_group->group_id );
|
||||
|
||||
$this->assertNotFalse($user_group_relation);
|
||||
$this->assertEquals($user_group_relation->user_id, $this->user_id);
|
||||
$this->assertEquals($user_group_relation->group_id, $department_access_group->group_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_User_Organization::on_taxonomy_term_will_be_deleted()
|
||||
*/
|
||||
function test_access_group_deleted_when_organization_deleted() {
|
||||
wp_delete_term($this->user_organization_id, Wiaas_User_Organization::TAXONOMY_NAME);
|
||||
|
||||
$organization_access_group = Groups_Group::read_by_name($this->user_organization_name);
|
||||
$this->assertFalse($organization_access_group);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_User_Organization::on_taxonomy_term_unassigned()
|
||||
*/
|
||||
function test_user_removed_from_organization_access_groups() {
|
||||
# remove user from organization
|
||||
wp_set_terms_for_user($this->user_id, Wiaas_User_Organization::TAXONOMY_NAME, null);
|
||||
|
||||
# check user removed from organization access group
|
||||
$organization_access_group = Groups_Group::read_by_name($this->user_organization_name);
|
||||
$user_group_relation = Groups_User_Group::read( $this->user_id , $organization_access_group->group_id );
|
||||
|
||||
$this->assertFalse($user_group_relation);
|
||||
|
||||
# check user removed from department access group
|
||||
$department_access_group = Groups_Group::read_by_name($this->user_department_name);
|
||||
$user_group_relation = Groups_User_Group::read( $this->user_id , $department_access_group->group_id );
|
||||
|
||||
$this->assertFalse($user_group_relation);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Wiaas_User_Organization::assign_post_to_user_organization()
|
||||
*/
|
||||
function test_post_assigned_to_user_organization() {
|
||||
$post_id = wp_insert_post(array(
|
||||
'post_title' => 'Test',
|
||||
'post_content' => 'Test',
|
||||
'post_excerpt' => 'Test'
|
||||
), true);
|
||||
|
||||
Wiaas_User_Organization::assign_post_to_user_organization($post_id, $this->user_id);
|
||||
|
||||
$organization_access_group = Groups_Group::read_by_name($this->user_organization_name);
|
||||
|
||||
$access_group_ids = Groups_Post_Access::get_read_group_ids( $post_id );
|
||||
|
||||
# check post added to organization access group
|
||||
$this->assertEquals(1, count($access_group_ids));
|
||||
$this->assertNotNull($access_group_ids[0]);
|
||||
$this->assertEquals($organization_access_group->group_id, $access_group_ids[0]);
|
||||
}
|
||||
}
|
||||
@@ -13,5 +13,7 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase {
|
||||
gf_upgrade()->install();
|
||||
|
||||
wiaas_db_update_setup_gravity();
|
||||
|
||||
wiaas_db_update_enable_orders_access_management();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user