Add unit tests for backend and refactor few things on frontend

This commit is contained in:
Almira Krdzic
2018-08-14 00:44:51 +02:00
parent 47bc6bdab1
commit bb3fecd811
14 changed files with 1221 additions and 5541 deletions

View File

@@ -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() {

View File

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

View File

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

View File

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

View 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']);
}
}

View File

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

View File

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

File diff suppressed because it is too large Load Diff

View File

@@ -14,31 +14,16 @@ const type = 'overview';
class OrderCentralContainer extends Component {
constructor(props) {
super(props);
this.state = {
orders: [],
isViewAllOrdersChecked: false
};
}
componentDidMount() {
this.props.dispatch(fetchOrders());
}
componentWillReceiveProps(nextProps) {
if(nextProps.isViewAllOrdersChecked[type] !== this.state.isViewAllOrdersChecked) {
this.setState({
isViewAllOrdersChecked: nextProps.isViewAllOrdersChecked[type] || false
});
this.props.dispatch(fetchOrders(nextProps.isViewAllOrdersChecked[type]));
}
this.setState({
orders: nextProps.orders
});
}
render() {
const {isLoading} = this.props;
const {isLoading, isViewAllOrdersChecked} = this.props;
const viewAllOrders = isViewAllOrdersChecked && isViewAllOrdersChecked[type];
const orders = viewAllOrders ? this.props.orders : this.props.orders.filter(o => o.isMyOrder);
return (<Col xl="8" lg="8" md="12" sm="12" xs="12">
<WiaasBox id="order-central-gadget" mainTitle={dashboardTexts.labels.ORDER_CENTRAL} customHeader={OrderListHeader} customHeaderParams={type}>
@@ -49,8 +34,8 @@ class OrderCentralContainer extends Component {
</div>
}
{
(this.state.orders && this.state.orders.length > 0 && !isLoading) ?
<OrdersList orders={this.state.orders} isViewAllOrdersChecked={this.props.isViewAllOrdersChecked[type]}/> :
(orders.length > 0 && !isLoading) ?
<OrdersList orders={orders} showOrderCustomer={viewAllOrders}/> :
<div className="dashborad-message">
<Link to='/co-market'>{dashboardTexts.labels.NO_ORDERS}</Link>
</div>
@@ -61,7 +46,7 @@ class OrderCentralContainer extends Component {
}
const mapStateToProps = (state) => ({
orders: state.ordersCentralReducer.orders,
orders: state.ordersCentralReducer.orders || [],
isLoading: state.ordersCentralReducer.isLoading,
isViewAllOrdersChecked: state.ordersReducer.isViewAllOrdersChecked
});

View File

@@ -6,7 +6,7 @@ import {dashboardTexts} from '../../../constants/dashboardConstants';
class OrderItem extends Component {
render() {
const {order, isViewAllOrdersChecked} = this.props;
const {order, showOrderCustomer} = this.props;
return (
<WiaasTableRow className={'order-central-row line-' + order.status}>
@@ -16,7 +16,7 @@ class OrderItem extends Component {
</WiaasTableCol>
<WiaasTableCol header={dashboardTexts.tableHeaders.ORDER_DATE}>{order.dateCreated}</WiaasTableCol>
{
isViewAllOrdersChecked &&
showOrderCustomer &&
<WiaasTableCol header={dashboardTexts.tableHeaders.PLACED_BY}>{order.customer ? order.customer.name : ''}</WiaasTableCol>
}
<WiaasTableCol header={dashboardTexts.tableHeaders.REFERENCE}>{order.reference}</WiaasTableCol>

View File

@@ -8,13 +8,13 @@ class OrdersList extends Component {
super(props);
this.state = {
isViewAllOrdersChecked: this.props.isViewAllOrdersChecked || false
showOrderCustomer: this.props.showOrderCustomer || false
};
this.getHeaderForOrders = this.getHeaderForOrders.bind(this);
}
componentWillReceiveProps(nextProps) {
this.setState({isViewAllOrdersChecked: nextProps.isViewAllOrdersChecked});
this.setState({showOrderCustomer: nextProps.showOrderCustomer});
}
getHeaderForOrders() {
@@ -27,7 +27,7 @@ class OrdersList extends Component {
dashboardTexts.tableHeaders.STATUS,
''
];
if(this.state.isViewAllOrdersChecked) {
if(this.state.showOrderCustomer) {
headers.splice(2, 0, dashboardTexts.tableHeaders.PLACED_BY);
headers.join();
}
@@ -36,7 +36,7 @@ class OrdersList extends Component {
}
render() {
const {orders, isViewAllOrdersChecked} = this.props;
const {orders, showOrderCustomer} = this.props;
return (
<div>
@@ -46,7 +46,7 @@ class OrdersList extends Component {
{
orders &&
orders.map((order, index) =>
<OrderItem key={'order-' + order.number} order={order} isViewAllOrdersChecked={isViewAllOrdersChecked}/>
<OrderItem key={'order-' + order.number} order={order} showOrderCustomer={showOrderCustomer}/>
)
}
</WiaasTableBody>

View File

@@ -12,14 +12,14 @@ class OrdersDataContainer extends Component {
this.props.dispatch(getActiveOrders());
this.props.dispatch(getHistoryOrders());
}
hasCurrentCustomerOrders(orders) {
return orders.some((order) => order.isMyOrder);
}
render() {
const {activeOrders, historyOrders, type, isLoading} = this.props;
const orders = type ? type === 'active' ? activeOrders : historyOrders : {};
const {activeOrders, historyOrders, type, isLoading, isViewAllOrdersChecked} = this.props;
let orders = type ? type === 'active' ? activeOrders : historyOrders : [];
const viewAllOrders = isViewAllOrdersChecked && isViewAllOrdersChecked[type];
if (!viewAllOrders) {
// show only current customer orders
orders = orders.filter(o => o.isMyOrder);
}
const mainTitleOrder = type.charAt(0).toUpperCase() + type.slice(1);
return (
@@ -33,10 +33,10 @@ class OrdersDataContainer extends Component {
</div>
}
{ (orders && !isLoading) &&
<OrderList orders={orders} type={type}/>
<OrderList orders={orders} type={type} showOrderCustomer={viewAllOrders}/>
}
{
((orders && orders.length === 0 && !isLoading) || (orders && !this.hasCurrentCustomerOrders(orders))) &&
!isLoading && orders.length === 0 &&
<Alert color="info">{orderTexts.labels.NO_RECORDS}</Alert>
}
</WiaasBox>
@@ -47,9 +47,10 @@ class OrdersDataContainer extends Component {
}
const mapStateToProps = (state) => ({
activeOrders: state.ordersReducer.activeOrders,
historyOrders: state.ordersReducer.historyOrders,
isLoading: state.ordersReducer.isLoading
activeOrders: state.ordersReducer.activeOrders || [],
historyOrders: state.ordersReducer.historyOrders || [],
isLoading: state.ordersReducer.isLoading,
isViewAllOrdersChecked: state.ordersReducer.isViewAllOrdersChecked,
});
export default connect(mapStateToProps)(OrdersDataContainer);

View File

@@ -1,5 +1,4 @@
import React, {Component} from 'react';
import {connect} from 'react-redux';
import OrderListItem from './OrderListItem.jsx';
import {WiaasTable, WiaasTableHeader, WiaasTableBody} from '../../../mainComponents/table/WiaasTable.jsx';
import {orderTexts} from '../../../constants/ordersConstants';
@@ -34,7 +33,7 @@ class OrderList extends Component {
''
];
if(this.props.isCompanyAdmin && this.props.isViewAllOrdersChecked[type]) {
if(this.props.showOrderCustomer) {
activeOrdersHeader.splice(2, 0, orderTexts.headers.PLACED_BY);
activeOrdersHeader.join();
historyOrdersHeader.splice(2, 0, orderTexts.headers.PLACED_BY);
@@ -45,7 +44,7 @@ class OrderList extends Component {
}
render() {
const {orders, type, isCompanyAdmin, isViewAllOrdersChecked} = this.props;
const {orders, type} = this.props;
return (
<div>
@@ -53,11 +52,7 @@ class OrderList extends Component {
<WiaasTableHeader headers={this.getHeadersByType(type)}/>
<WiaasTableBody>
{
orders &&
orders.map((order, index) =>
(order.isMyOrder || (isCompanyAdmin && isViewAllOrdersChecked[type])) &&
<OrderListItem key={order.id} order={order} type={type} />
)
orders.map(order => <OrderListItem key={order.id} order={order} type={type} />)
}
</WiaasTableBody>
</WiaasTable>
@@ -65,9 +60,4 @@ class OrderList extends Component {
);
}
}
const mapStateToProps = (state) => ({
isCompanyAdmin: true, //state.auth.isCompanyAdmin,
isViewAllOrdersChecked: state.ordersReducer.isViewAllOrdersChecked
});
export default connect(mapStateToProps)(OrderList);
export default OrderList;

View File

@@ -64,7 +64,7 @@ class OrderPackage extends Component {
<div className="package-name">{orderPackage.quantity} x {orderPackage.name}</div>
</WiaasTableCol>
<WiaasTableCol header="Price">
{this.calculateQuantityPrice(orderPackage.quantity, orderPackage.price).toLocaleString()} {orderPackage.packageCurrency && orderPackage.packageCurrency.currency} {' '}
{this.calculateQuantityPrice(orderPackage.quantity, orderPackage.price).toLocaleString()} {order.currency} {' '}
({orderPackage.paymentType})
</WiaasTableCol>
<WiaasTableCol header="Services and support">