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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user