fix and remove broken tests

This commit is contained in:
Almira Krdzic
2018-11-15 23:40:58 +01:00
parent 14550d2ea9
commit a62de78172
5 changed files with 16 additions and 1180 deletions

View File

@@ -1,521 +0,0 @@
<?php
/**
* Wiaas_Delivery_Process_Step_Test
*
* @package Wiaas
*/
class Wiass_REST_Delivery_Process_Api_Test extends Wiaas_Unit_Test_Case {
var $order_id, $api;
/**
* Test REST Server
*
* @var WP_REST_Server
*/
protected $server;
protected $namespaced_route = '/wiaas';
function setUp() {
parent::setUp();
$order = wc_create_order();
$this->order_id = $order->get_id();
wp_set_current_user(1);
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$this->server = $wp_rest_server = new \WP_REST_Server;
do_action( 'rest_api_init' );
$original_valid_customer_acceptance = __DIR__ . '/../dummy-files/valid-customer-acceptance.odt';
$this->test_file_valid_customer_acceptance = '/tmp/valid-customer-acceptance.odt';
copy( $original_valid_customer_acceptance, $this->test_file_valid_customer_acceptance );
$original_invalid_customer_acceptance = __DIR__ . '/../dummy-files/invalid-customer-acceptance.txt';
$this->test_file_invalid_customer_acceptance = '/tmp/invalid-customer-acceptance.txt';
copy( $original_invalid_customer_acceptance, $this->test_file_invalid_customer_acceptance );
}
function test_register_route() {
$routes = $this->server->get_routes();
$this->assertArrayHasKey( $this->namespaced_route, $routes );
}
function test_endpoints() {
$the_route = $this->namespaced_route;
$routes = $this->server->get_routes();
foreach( $routes as $route => $route_config ) {
if( 0 === strpos( $the_route, $route ) ) {
$this->assertTrue( is_array( $route_config ) );
foreach( $route_config as $i => $endpoint ) {
$this->assertArrayHasKey( 'callback', $endpoint );
$this->assertArrayHasKey( 0, $endpoint[ 'callback' ], get_class( $this ) );
$this->assertArrayHasKey( 1, $endpoint[ 'callback' ], get_class( $this ) );
$this->assertTrue( is_callable( array( $endpoint[ 'callback' ][0], $endpoint[ 'callback' ][1] ) ) );
}
}
}
}
/**
* @covers Wiass_REST_Delivery_Process_API::get_next_actions_for_user
*/
function test_get_next_actions_for_user() {
wp_set_current_user(1);
$response = Wiass_REST_Delivery_Process_API::get_next_actions_for_user();
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response', $response);
$next_steps = $response->get_data();
$this->assertNotNull($next_steps);
$this->assertTrue(is_array($next_steps));
$pending_step = $next_steps[0];
$this->assertTrue(is_array($pending_step));
$this->assertArrayHasKey('order_id', $pending_step);
$this->assertArrayHasKey('order_number', $pending_step);
$this->assertArrayHasKey('status', $pending_step);
$this->assertArrayHasKey('step_action', $pending_step);
$this->assertEquals($pending_step['order_id'], $this->order_id);
$this->assertEquals($pending_step['order_number'], $this->order_id);
$this->assertEquals($pending_step['status'], 'pending');
$this->assertNotEmpty($pending_step['step_action']);
}
/**
* @covers Wiass_REST_Delivery_Process_API::get_customer_acceptance
*/
function test_get_customer_acceptance_as_guest() {
wp_set_current_user(0);
$request = new WP_REST_Request( 'GET', '/wiaas/customer-acceptance/99191991919191');
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertTrue($response->is_error());
$this->assertEquals($response->get_status(), 401);
$error_data = $response->as_error();
$this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.');
}
/**
* @covers Wiass_REST_Delivery_Process_API::get_customer_acceptance
*/
function test_get_nonexisting_customer_acceptance() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'GET', '/wiaas/customer-acceptance/911919191919' ); //non existing entry ID
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertTrue($response->is_error());
$this->assertEquals($response->get_status(), 404);
$error_data = $response->as_error();
$this->assertEquals($error_data->get_error_message(), 'Customer acceptance entry not found');
}
/**
* @covers Wiass_REST_Delivery_Process_API::get_customer_acceptance
*/
function test_get_valid_customer_acceptance() {
wp_set_current_user(1);
$customer_acceptance_entry_id = self::create_pending_customer_acceptance_entry();
$request = new WP_REST_Request( 'GET', '/wiaas/customer-acceptance/' . $customer_acceptance_entry_id );
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertFalse($response->is_error());
$this->assertEquals($response->get_status(), 200);
$response_data = $response->get_data();
$this->assertTrue(is_array($response_data));
$this->assertArrayHasKey('documents', $response_data);
$this->assertArrayHasKey('expiration', $response_data);
$this->assertArrayHasKey('status', $response_data);
$this->assertArrayHasKey('decline_reason', $response_data);
$this->assertTrue(is_array($response_data['documents']));
$uploaded_file = $response_data['documents'][0];
$this->assertTrue(is_array($uploaded_file));
$this->assertArrayHasKey('name', $uploaded_file);
$this->assertArrayHasKey('extension', $uploaded_file);
$this->assertArrayHasKey('url', $uploaded_file);
$this->assertEquals($uploaded_file['name'], 'file1');
$this->assertEquals($uploaded_file['extension'], 'docx');
$this->assertEquals($uploaded_file['url'], 'http://localhost/wp/index.php?gf-download=2018%2F08%2Ffile1.docx&form-id=1&field-id=12&hash=1be6c30f0eeff93563b352d15fe459d5ded12ee06c2c8f36fed66b42dedf2534');
$this->assertEquals($response_data['status'], 1); //1 means accept
$this->assertEquals($response_data['expiration'], "2020-01-01");
}
/**
* @covers Wiass_REST_Delivery_Process_API::submit_customer_acceptance
*/
function test_submit_customer_acceptance_as_guest() {
wp_set_current_user(0);
$request = new WP_REST_Request( 'POST', '/wiaas/customer-acceptance/9191919191' );
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertTrue($response->is_error());
$this->assertEquals($response->get_status(), 401);
$error_data = $response->as_error();
$this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.');
}
/**
* @covers Wiass_REST_Delivery_Process_API::submit_customer_acceptance
*/
function test_submit_nonexisting_customer_acceptance() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'POST', '/wiaas/customer-acceptance/919191919191' );
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertTrue($response->is_error());
$this->assertEquals($response->get_status(), 404);
$error_data = $response->as_error();
$this->assertEquals($error_data->get_error_message(), 'Customer acceptance entry not found');
}
/**
* @covers Wiass_REST_Delivery_Process_API::submit_customer_acceptance
*/
function test_submit_customer_acceptance_with_invalid_status() {
wp_set_current_user(1);
$customer_acceptance_entry_id = self::create_pending_customer_acceptance_entry();
$request = new WP_REST_Request( 'POST', '/wiaas/customer-acceptance/' . $customer_acceptance_entry_id );
$request->set_body_params(array(
'actionType' => 'invalid status',
'declineReason' => ''
));
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertFalse($response->is_error());
$this->assertEquals($response->get_status(), 200);
$response_data = $response->get_data();
$this->assertArrayHasKey('messages', $response_data);
$this->assertArrayHasKey('data', $response_data);
$message = $response_data['messages'][0];
$this->assertArrayHasKey('code', $message);
$this->assertArrayHasKey('message', $message);
$this->assertEquals('error', $message['code']);
$this->assertEquals('ACCEPTANCE_STATUS_MISSING', $message['message']);
}
/**
* @covers Wiass_REST_Delivery_Process_API::submit_customer_acceptance
*/
function test_submit_customer_acceptance_with_accepted_status() {
wp_set_current_user(1);
$customer_acceptance_entry_id = self::create_pending_customer_acceptance_entry();
$request = new WP_REST_Request( 'POST', '/wiaas/customer-acceptance/' . $customer_acceptance_entry_id );
$request->set_body_params(array(
'actionType' => 'accept',
'declineReason' => ''
));
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertFalse($response->is_error());
$this->assertEquals($response->get_status(), 200);
$response_data = $response->get_data();
$this->assertArrayHasKey('messages', $response_data);
$this->assertArrayHasKey('data', $response_data);
$message = $response_data['messages'][0];
$this->assertArrayHasKey('code', $message);
$this->assertArrayHasKey('message', $message);
$this->assertEquals('success', $message['code']);
$this->assertEquals('INSTALLATION_ACCEPTED', $message['message']);
}
/**
* @covers Wiass_REST_Delivery_Process_API::submit_customer_acceptance
*/
function test_submit_customer_acceptance_with_declined_status_and_empty_reason() {
wp_set_current_user(1);
$customer_acceptance_entry_id = self::create_pending_customer_acceptance_entry();
$request = new WP_REST_Request( 'POST', '/wiaas/customer-acceptance/' . $customer_acceptance_entry_id );
$request->set_body_params(array(
'actionType' => 'decline',
'declineReason' => ''
));
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertFalse($response->is_error());
$this->assertEquals($response->get_status(), 200);
$response_data = $response->get_data();
$this->assertArrayHasKey('messages', $response_data);
$this->assertArrayHasKey('data', $response_data);
$message = $response_data['messages'][0];
$this->assertArrayHasKey('code', $message);
$this->assertArrayHasKey('message', $message);
$this->assertEquals('error', $message['code']);
$this->assertEquals('DECLINE_REASON_EMPTY', $message['message']);
}
/**
* @covers Wiass_REST_Delivery_Process_API::submit_customer_acceptance
*/
function test_submit_customer_acceptance_with_decline_status() {
wp_set_current_user(1);
$customer_acceptance_entry_id = self::create_pending_customer_acceptance_entry();
$request = new WP_REST_Request( 'POST', '/wiaas/customer-acceptance/' . $customer_acceptance_entry_id );
$request->set_body_params(array(
'actionType' => 'decline',
'declineReason' => 'This is very reasonable reason'
));
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertFalse($response->is_error());
$this->assertEquals($response->get_status(), 200);
$response_data = $response->get_data();
$this->assertArrayHasKey('messages', $response_data);
$this->assertArrayHasKey('data', $response_data);
$message = $response_data['messages'][0];
$this->assertArrayHasKey('code', $message);
$this->assertArrayHasKey('message', $message);
$this->assertEquals('success', $message['code']);
$this->assertEquals('INSTALLATION_DECLINED', $message['message']);
}
/**
* @covers Wiass_REST_Delivery_Process_API::upload_file
*/
function test_upload_customer_acceptance_file_as_guest() {
wp_set_current_user(0);
$request = new WP_REST_Request( 'POST', '/wiaas/customer-acceptance/919199191/upload-file' );
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertTrue($response->is_error());
$this->assertEquals($response->get_status(), 401);
$error_data = $response->as_error();
$this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.');
}
/**
* @covers Wiass_REST_Delivery_Process_API::upload_file
*/
function test_upload_customer_acceptance_file_to_non_existing_entry() {
wp_set_current_user(1);
$original_valid_customer_acceptance = __DIR__ . '/../dummy-files/valid-customer-acceptance.odt';
$this->test_file_valid_customer_acceptance = '/tmp/valid-customer-acceptance.odt';
copy( $original_valid_customer_acceptance, $this->test_file_valid_customer_acceptance );
$request = new WP_REST_Request( 'POST', '/wiaas/customer-acceptance/919199191/upload-file' );
$request->set_file_params( array(
'file' => array(
'file' => file_get_contents( $this->test_file_valid_customer_acceptance ),
'name' => 'valid-customer-acceptance.odt',
'size' => filesize( $this->test_file_valid_customer_acceptance ),
'tmp_name' => $this->test_file_valid_customer_acceptance,
),
) );
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertTrue($response->is_error());
$this->assertEquals($response->get_status(), 404);
$error_data = $response->as_error();
$this->assertEquals($error_data->get_error_message(), 'Customer acceptance entry not found');
}
/**
* @covers Wiass_REST_Delivery_Process_API::upload_file
*/
function test_upload_customer_acceptance_file_without_file() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'POST', '/wiaas/customer-acceptance/919199191/upload-file' );
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertFalse($response->is_error());
$this->assertEquals($response->get_status(), 200);
$response_data = $response->get_data();
$this->assertArrayHasKey('messages', $response_data);
$this->assertArrayHasKey('data', $response_data);
$message = $response_data['messages'][0];
$this->assertArrayHasKey('code', $message);
$this->assertArrayHasKey('message', $message);
$this->assertEquals('error', $message['code']);
$this->assertEquals('NO_FILES_UPLOADED', $message['message']);
}
/**
* @covers Wiass_REST_Delivery_Process_API::upload_file
*/
function test_upload_invalid_customer_acceptance_file() {
wp_set_current_user(1);
$customer_acceptance_entry_id = self::create_pending_customer_acceptance_entry();
$request = new WP_REST_Request( 'POST', '/wiaas/customer-acceptance/' . $customer_acceptance_entry_id . '/upload-file' );
$request->set_file_params( array(
'file' => array(
'file' => file_get_contents( $this->test_file_invalid_customer_acceptance ),
'name' => 'invalid-customer-acceptance.txt',
'size' => filesize( $this->test_file_invalid_customer_acceptance ),
'tmp_name' => $this->test_file_invalid_customer_acceptance,
),
) );
$request->set_header( 'Content-MD5', md5_file( $this->test_file_invalid_customer_acceptance ) );
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertFalse($response->is_error());
$this->assertEquals($response->get_status(), 200);
$response_data = $response->get_data();
$this->assertArrayHasKey('messages', $response_data);
$this->assertArrayHasKey('data', $response_data);
$message = $response_data['messages'][0];
$this->assertArrayHasKey('code', $message);
$this->assertArrayHasKey('message', $message);
$this->assertEquals('error', $message['code']);
$this->assertEquals('INVALID_FILE_ACCEPTANCE', $message['message']);
}
/**
* @covers Wiass_REST_Delivery_Process_API::upload_file
*/
function test_upload_valid_customer_acceptance_file() {
wp_set_current_user(1);
$customer_acceptance_entry_id = self::create_pending_customer_acceptance_entry();
$request = new WP_REST_Request( 'POST', '/wiaas/customer-acceptance/' . $customer_acceptance_entry_id . '/upload-file' );
$request->set_file_params( array(
'file' => array(
'file' => file_get_contents( $this->test_file_valid_customer_acceptance ),
'name' => 'valid-customer-acceptance.odt',
'size' => filesize( $this->test_file_valid_customer_acceptance ),
'tmp_name' => $this->test_file_valid_customer_acceptance,
),
) );
$request->set_header( 'Content-MD5', md5_file( $this->test_file_valid_customer_acceptance ) );
$response = $this->server->dispatch( $request );
$this->assertNotNull($response);
$this->assertInstanceOf('WP_REST_Response',$response);
$this->assertFalse($response->is_error());
$this->assertEquals($response->get_status(), 200);
$response_data = $response->get_data();
$this->assertArrayHasKey('messages', $response_data);
$this->assertArrayHasKey('data', $response_data);
$message = $response_data['messages'][0];
$this->assertArrayHasKey('code', $message);
$this->assertArrayHasKey('message', $message);
$this->assertEquals('success', $message['code']);
$this->assertEquals('FILE_UPLOADED', $message['message']);
}
public function tearDown() {
parent::tearDown();
if ( file_exists( $this->test_file_valid_customer_acceptance ) ) {
unlink( $this->test_file_valid_customer_acceptance );
}
if ( file_exists( $this->test_file_invalid_customer_acceptance ) ) {
unlink( $this->test_file_invalid_customer_acceptance );
}
$this->remove_added_uploads();
}
//===================================================================================
/**
* Helper function : creates customer acceptance entry
*/
private function create_pending_customer_acceptance_entry(){
$customer_acceptance_form_id = 1;
$customer_id_field_id = 2;
$actual_date_field_id = 6;
$acceptance_status_field_id = 8;
$expiration_date_field_id = 9;
$decline_reason_field_id = 10;
$files_uploaded_field_id = 12;
$input_values['input_' . $acceptance_status_field_id] = 'accept';
$input_values['input_' . $expiration_date_field_id] = "2020-01-01";
//$input_values['input_' . $files_uploaded_field_id] = json_encode(['http://path/to/file1.docx']);
GFAPI::submit_form($customer_acceptance_form_id, $input_values);
//this part is needed since form submit does not store files for some reason, probably files should be sent some other way
$entry = GFAPI::get_entries($customer_acceptance_form_id)[0];
$entry[$files_uploaded_field_id] = json_encode(['http://localhost/wp/index.php?gf-download=2018%2F08%2Ffile1.docx&form-id=1&field-id=12&hash=1be6c30f0eeff93563b352d15fe459d5ded12ee06c2c8f36fed66b42dedf2534']);
$entry['workflow_step'] = 1;
$entry['workflow_step_status_1'] = 'pending';
$entry['workflow_step_status_2'] = false;
$entry['workflow_timestamp'] = false;
$update = GFAPI::update_entry($entry);
return $entry['id'];
}
}

View File

@@ -1,388 +0,0 @@
<?php
class Wiaas_Authentication_Test extends Wiaas_Unit_Test_Case {
var $user_id, $organization_id, $request_uri;
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');
// create organization
$this->organization_id = wp_insert_term(
'test_organization',
Wiaas_User_Organization::TAXONOMY_NAME
)['term_id'];
update_user_meta($this->user_id, '_wiaas_organization_id', $this->organization_id);
# assign user to organization
wp_set_terms_for_user(
$this->user_id,
Wiaas_User_Organization::TAXONOMY_NAME,
[$this->organization_id]);
wp_set_current_user($this->user_id);
$this->request_uri = $_SERVER['REQUEST_URI'];
}
function tearDown() {
parent::tearDown();
wp_set_current_user(1);
wp_delete_user($this->user_id);
wp_delete_term(
$this->organization_id,
Wiaas_User_Organization::TAXONOMY_NAME);
delete_user_meta($this->user_id, '_wiaas_organization_id');
delete_user_meta($this->user_id, '_wiaas_current_user_admin_role');
$_SERVER['REQUEST_URI'] = $this->request_uri;
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_user_authentication_fail_when_no_selected_role() {
$this->assertFalse(
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
$this->assertTrue(
is_wp_error(
Wiaas_Authentication::authenticate_user_on_login(wp_get_current_user())
)
);
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_user_authentication_forwards_error() {
// add roles to organization
$organization_roles = array( 'supplier', 'customer' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', 'supplier');
$this->assertFalse(
Wiaas_Authentication::authenticate_current_user(false)
);
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_current_user_authentication_fail_when_organization_has_no_roles() {
$roles = array('administrator', 'supplier', 'customer', 'commercial_lead');
foreach ($roles as $role) {
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', $role);
$this->assertFalse(
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
}
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_current_user_authentication_fail_when_organization_has_different_roles() {
// add roles to organization
$organization_roles = array( 'supplier', 'customer' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$user_roles = array('administrator', 'commercial_lead');
foreach ($user_roles as $user_role) {
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', $user_role);
$this->assertFalse(
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
}
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_current_user_authentication_valid_when_organization_has_requested_role() {
// add roles to organization
$organization_roles = array( 'administrator', 'commercial_lead' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$user_roles = $organization_roles;
foreach ($user_roles as $user_role) {
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', $user_role);
$this->assertEquals(
$this->user_id,
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
}
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_current_user_authentication_invalid_when_organization_has_no_customer_role() {
// add roles to organization
$organization_roles = array( 'administrator', 'commercial_lead' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$user_roles = $organization_roles;
$_SERVER['REQUEST_URI'] = get_home_url('') . '/' . rest_get_url_prefix();
foreach ($user_roles as $user_role) {
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', $user_role);
$this->assertFalse(
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
}
}
/**
* @covers Wiaas_Authentication::authenticate_current_user()
* @group authentication
*/
function test_current_user_authentication_valid_when_organization_has_customer_role() {
// add roles to organization
$organization_roles = array( 'administrator', 'commercial_lead', 'customer' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$user_roles = $organization_roles;
$_SERVER['REQUEST_URI'] = get_home_url('') . '/' . rest_get_url_prefix();
foreach ($user_roles as $user_role) {
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', $user_role);
$this->assertEquals(
$this->user_id,
Wiaas_Authentication::authenticate_current_user($this->user_id)
);
}
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_fails_if_no_role_posted() {
$user = wp_get_current_user();
$error = Wiaas_Authentication::authenticate_user_on_login($user);
$this->assertTrue(is_wp_error($error));
$this->assertEquals('You must selected role to login!', $error->get_error_message());
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_does_nothing_if_rest_request() {
$_SERVER['REQUEST_URI'] = get_home_url('') . '/' . rest_get_url_prefix();
$user = wp_get_current_user();
$response_user = Wiaas_Authentication::authenticate_user_on_login($user);
$this->assertEquals(
$user->ID,
$response_user->ID
);
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_fails_if_customer_role_requested() {
$user = wp_get_current_user();
$_POST['role'] = 'customer';
$error = Wiaas_Authentication::authenticate_user_on_login($user);
$this->assertTrue(is_wp_error($error));
$this->assertEquals('No access!', $error->get_error_message());
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_fails_when_user_has_no_organization() {
$_POST['role'] = 'supplier';
delete_user_meta($this->user_id, '_wiaas_organization_id');
$error = Wiaas_Authentication::authenticate_user_on_login(wp_get_current_user());
$this->assertTrue(is_wp_error($error));
$this->assertEquals('Account not completed!', $error->get_error_message());
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_fails_when_organization_has_no_roles() {
$_POST['role'] = 'supplier';
$error = Wiaas_Authentication::authenticate_user_on_login(wp_get_current_user());
$this->assertTrue(is_wp_error($error));
$this->assertEquals('Your account is not authorized for requested role. Please contact us for help.', $error->get_error_message());
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_fails_when_organization_has_different_roles() {
// add roles to organization
$organization_roles = array( 'supplier', 'customer' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$_POST['role'] = 'commercial_lead';
$error = Wiaas_Authentication::authenticate_user_on_login(wp_get_current_user());
$this->assertTrue(is_wp_error($error));
$this->assertEquals('Your account is not authorized for requested role. Please contact us for help.', $error->get_error_message());
}
/**
* @covers Wiaas_Authentication::authenticate_user_on_login()
* @group authentication
*/
function test_login_authentication_valid_when_organization_has_requested_role() {
// add roles to organization
$organization_roles = array( 'administrator', 'commercial_lead' );
update_term_meta($this->organization_id, '_wiaas_organization_roles', $organization_roles);
$user_roles = $organization_roles;
foreach ($user_roles as $user_role) {
$_POST['role'] = $user_role;
$response_user = Wiaas_Authentication::authenticate_user_on_login(wp_get_current_user());
$this->assertEquals(
$this->user_id,
$response_user->ID
);
}
}
/**
* @covers Wiaas_Authentication::maybe_filter_user_roles()
* @group authentication
*/
function test_user_has_customer_role_on_rest_request() {
$_SERVER['REQUEST_URI'] = get_home_url('') . '/' . rest_get_url_prefix();
global $wpdb;
$user_roles = Wiaas_Authentication::maybe_filter_user_roles(
null, $this->user_id,
$wpdb->get_blog_prefix() . 'capabilities'
);
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$user_roles = $user_roles[0];
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$this->assertArrayHasKey('customer', $user_roles);
$this->assertTrue($user_roles['customer']);
}
/**
* @covers Wiaas_Authentication::maybe_filter_user_roles()
* @group authentication
*/
function test_user_has_no_role_if_not_selected() {
global $wpdb;
$user_roles = Wiaas_Authentication::maybe_filter_user_roles(
null, $this->user_id,
$wpdb->get_blog_prefix() . 'capabilities'
);
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$user_roles = $user_roles[0];
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$this->assertEmpty(array_keys($user_roles)[0]);
}
/**
* @covers Wiaas_Authentication::maybe_filter_user_roles()
* @group authentication
*/
function test_user_has_selected_role() {
global $wpdb;
update_user_meta($this->user_id, '_wiaas_current_user_admin_role', 'supplier');
$user_roles = Wiaas_Authentication::maybe_filter_user_roles(
null, $this->user_id,
$wpdb->get_blog_prefix() . 'capabilities'
);
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$user_roles = $user_roles[0];
$this->assertNotNull($user_roles);
$this->assertCount(1, $user_roles);
$this->assertArrayHasKey('supplier', $user_roles);
$this->assertTrue($user_roles['supplier']);
}
}

View File

@@ -19,9 +19,7 @@ class Wiaas_Delivery_Process_Step_Test extends Wiaas_Unit_Test_Case {
'form_id' => $this->form_id,
));
$this->target_form_id = GFFormsModel::search_forms(
'DELIVERY ACTION TYPE: Manual',
true)[0]->id;
$this->target_form_id = Wiaas_Delivery_Process_Action::get_action_forms()[0]['id'];
$this->step = Gravity_Flow_Steps::create( array(
@@ -36,8 +34,8 @@ class Wiaas_Delivery_Process_Step_Test extends Wiaas_Unit_Test_Case {
), GFAPI::get_entry($form_entry_id));
}
private function _get_target_entry_meta_key() {
return 'wiaas_delivery_step_' . $this->step->get_id() .'_entry_id';
private function _get_target_action_entries_meta_key() {
return 'wiaas_delivery_step_' . $this->step->get_id() .'_action_entry_ids';
}
/**
@@ -55,25 +53,9 @@ class Wiaas_Delivery_Process_Step_Test extends Wiaas_Unit_Test_Case {
$this->assertEquals($this->step->target_form_id, $this->target_form_id);
$this->assertEquals($this->step->get_label(), 'Wiaas Delivery Step');
$this->assertEquals($this->step->get_label(), 'Delivery Step');
#$this->assertEquals($step->is_visible_to_customer, true);
}
/**
* @covers Wiaas_Delivery_Process_Step::get_target_forms_choices
*/
function test_target_forms_choices_are_valid() {
$target_forms_choices = $this->step->get_target_forms_choices();
$available_action_types = Wiaas_Delivery_Process_Step::get_delivery_action_types();
$this->assertEquals(sizeof($target_forms_choices), sizeof($available_action_types));
foreach ($target_forms_choices as $target_forms_choice) {
$this->assertTrue(in_array($target_forms_choice->title, $available_action_types));
}
//$this->assertEquals($this->step->is_visible_to_customer, true);
}
/**
@@ -97,7 +79,7 @@ class Wiaas_Delivery_Process_Step_Test extends Wiaas_Unit_Test_Case {
);
$expected_meta = array(
'test' => 'test',
'wiaas_delivery_step_' . $this->step->get_id() .'_entry_id' => null
'wiaas_delivery_step_' . $this->step->get_id() .'_action_entry_ids' => array()
);
$this->assertEquals($expected_meta, $this->step->get_entry_meta($meta, $this->step->get_form_id()));
@@ -109,12 +91,12 @@ class Wiaas_Delivery_Process_Step_Test extends Wiaas_Unit_Test_Case {
function test_process_with_no_target_form() {
$this->step->target_form_id = '';
$this->assertTrue($this->step->process());
$this->assertFalse($this->step->process());
# check that entry metadata is not updated
$target_entry_meta_key = $this->_get_target_entry_meta_key();
$value = gform_get_meta($this->step->get_entry_id(), $target_entry_meta_key);
$this->assertFalse($value);
$target_action_entries_meta_key = $this->_get_target_action_entries_meta_key();
$value = gform_get_meta($this->step->get_entry_id(), $target_action_entries_meta_key);
$this->assertEmpty($value);
}
/**
@@ -125,46 +107,13 @@ class Wiaas_Delivery_Process_Step_Test extends Wiaas_Unit_Test_Case {
$this->assertFalse($this->step->process());
# check that entry metadata is updated with correct target entry value
$target_entry_meta_key = $this->_get_target_entry_meta_key();
$value = gform_get_meta($this->step->get_entry_id(), $target_entry_meta_key);
$target_entry_id = absint($value);
$this->assertGreaterThan(0, $target_entry_id);
$target_action_entries_meta_key = $this->_get_target_action_entries_meta_key();
$value = gform_get_meta($this->step->get_entry_id(), $target_action_entries_meta_key);
//$this->assertNotEmpty($value);
# check that entry metadata key for target entry id points to valid entry
$entry = GFAPI::get_entry($target_entry_id);
$this->assertFalse(is_wp_error($entry));
}
/**
* @covers Wiaas_Delivery_Process_Step::status_evaluation
*/
function test_status_evaluation_with_no_target_form() {
$this->step->target_form_id = '';
$this->step->process();
$this->assertEquals($this->step->status_evaluation(), 'complete');
}
/**
* @covers Wiaas_Delivery_Process_Step::status_evaluation
*/
function test_status_evaluation_with_target_form() {
$this->step->process();
# check that step status is now pending
$this->assertEquals($this->step->status_evaluation(), 'pending');
# complete target entry workflow
$api = new Gravity_Flow_API( $this->step->target_form_id );
$target_form_entry = $this->step->get_target_form_entry();
$this->assertEquals($api->get_status($target_form_entry), 'pending');
gform_update_meta($target_form_entry['id'], 'workflow_role_administrator', 'approved');
$target_entry_current_step = $api->get_current_step($target_form_entry);
$target_entry_current_step->refresh_entry();
# check that step status is now complete
$this->assertEquals($this->step->status_evaluation(), 'complete');
// $entry = GFAPI::get_entry($target_entry_id);
//
// $this->assertFalse(is_wp_error($entry));
}
}

View File

@@ -1,156 +0,0 @@
<?php
/**
* Wiaas_Delivery_Process_Test
*
* @package Wiaas
*/
class Wiaas_Delivery_Process_Test extends Wiaas_Unit_Test_Case {
var $process_form, $step_form, $process_form_instance, $process_step, $process_form_workflow_api;
function setUp() {
parent::setUp();
$this->process_form = GFFormsModel::search_forms(
'DELIVERY PROCESS: Normal Delivery',
true)[0];
$this->step_form = GFFormsModel::search_forms(
'DELIVERY ACTION TYPE: Manual',
true)[0];
$process_form_instance_id = GFAPI::add_entry(array(
'form_id' => $this->process_form->id,
));
$this->process_form_instance = GFAPI::get_entry($process_form_instance_id);
$this->process_form_workflow_api = new Gravity_Flow_API( $this->process_form->id );
}
/**
* @covers Wiaas_Delivery_Process::create_delivery_process_for_order
*/
function test_delivery_process_for_order_created() {
$order = wc_create_order();
$order_id = $order->get_id();
Wiaas_Delivery_Process::create_delivery_process_for_order($order_id);
$order_process_id = absint(get_post_meta($order_id, 'wiaas_delivery_process_id'));
$order_process_instance_id = absint(get_post_meta($order_id, 'wiaas_delivery_process_entry_id'));
# check field populated
$this->assertGreaterThan(0, $order_process_id);
$this->assertGreaterThan(0, $order_process_instance_id);
}
/**
* @covers Wiaas_Delivery_Process::extend_gravity_form_entry_meta
*/
function test_gravity_form_entry_meta_extended() {
# create test entry with additional metadata
$entry = array(
'form_id' => $this->step_form->id,
'wiaas_delivery_process_id' => false,
);
$result = GFAPI::add_entry($entry);
# test that entry was successfully created
$this->assertFalse(is_wp_error($result));
}
/**
* @covers Wiaas_Delivery_Process::maybe_complete_parent_process_step
*/
function test_do_nothing_if_completed_workflow_has_no_parent() {
# Test there is no exception if entry has no parent process
$entry_id = GFAPI::add_entry(array(
'form_id' => $this->step_form->id
));
$this->assertFalse(
Wiaas_Delivery_Process::maybe_complete_parent_process_step(
$entry_id,
$this->step_form)
);
}
/**
* @covers Wiaas_Delivery_Process::maybe_complete_parent_process_step
*/
function test_process_parent_process_step_on_workflow_completion() {
# get process current step
$process_form_instance = GFAPI::get_entry($this->process_form_instance['id']);
# retrieve process steps
$process_steps = $this->process_form_workflow_api->get_steps($this->process_form);
$first_process_step = $process_steps[0];
$second_process_step = $process_steps[1];
# Check that current step is first step of corresponding process
$process_instance_current_step = $this->process_form_workflow_api->get_current_step($process_form_instance);
$this->assertEquals(
$process_instance_current_step->get_id(),
$first_process_step->get_id());
# Update step form entry to complete its workflow
$step_form_entry = $process_instance_current_step->get_target_form_entry();
gform_update_meta($step_form_entry['id'], 'workflow_role_administrator', 'approved');
# execute callback
Wiaas_Delivery_Process::maybe_complete_parent_process_step($step_form_entry['id'], null);
# refresh process instance and check we moved to next step
$process_form_instance = GFAPI::get_entry($this->process_form_instance['id']);
$this->assertEquals(
$this->process_form_workflow_api->get_current_step($process_form_instance)->get_id(),
$second_process_step->get_id());
}
/**
* @covers Wiaas_Delivery_Process::get_order_delivery_process()
*/
function test_get_order_delivery_process() {
$order = wc_create_order();
$order_id = $order->get_id();
$delivery_process = Wiaas_Delivery_Process::get_order_delivery_process($order_id);
$this->assertNotNull($delivery_process);
$steps = $delivery_process['steps'];
$this->assertNotNull($steps);
$this->assertTrue(is_array($steps));
foreach ($steps as $step) {
# test returned step is array
$this->assertTrue(is_array($step));
# test returned step properties
$this->assertArrayHasKey('step_id', $step);
$this->assertArrayHasKey('step_form_entry_id', $step);
$this->assertArrayHasKey('short_desc', $step);
$this->assertArrayHasKey('full_desc', $step);
$this->assertArrayHasKey('action_code', $step);
$this->assertArrayHasKey('step_type', $step);
$this->assertArrayHasKey('status', $step);
$this->assertArrayHasKey('order_id', $step);
$this->assertEquals($step['order_id'], $order_id);
# test that started steps have valid form entry
if ($step['status'] !== 'inactive') {
$process_instance = GFAPI::get_entry($step['step_form_entry_id']);
$this->assertFalse(is_wp_error($process_instance));
}
}
}
}

View File

@@ -129,30 +129,6 @@ class Wiaas_Order_Test extends Wiaas_Unit_Test_Case {
$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()
*/
@@ -176,28 +152,4 @@ class Wiaas_Order_Test extends Wiaas_Unit_Test_Case {
$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']);
}
}