Add unit tests for backend and refactor few things on frontend
This commit is contained in:
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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user