Handle order project and refactor api

This commit is contained in:
Almira Krdzic
2018-09-24 21:51:55 +02:00
parent 8cc2a7c8bc
commit 11c26aeee1
32 changed files with 1408 additions and 587 deletions

View File

@@ -0,0 +1,81 @@
<?php
class Wiaas_Order_Project_API_Test extends Wiaas_API_Unit_Test_Case {
function setUp() {
parent::setUp();
}
/**
* @covers Wiaas_Order_Projects_API::get_order_projects()
*/
function test_get_order_projects_as_guest() {
$this->check_endpoint_forbidden_for_guest(
new WP_REST_Request( 'GET', '/wiaas/order-projects')
);
}
/**
* @covers Wiaas_Order_Projects_API::get_order_projects()
*/
function test_get_order_projects_as_customer() {
Wiaas_Order_Project::add_order_project('Test Available 1', true);
Wiaas_Order_Project::add_order_project('Test Available 2', true);
$customer_id = $this->create_new_customer();
wp_set_current_user($customer_id);
$data = $this->dispatch_endpoint_request(
new WP_REST_Request( 'GET', '/wiaas/order-projects')
);
$this->assertNotNull($data);
$this->assertTrue(is_array($data));
$this->assertCount(2, $data);
foreach ($data as $project) {
$this->assertArrayHasKey('id', $project);
$this->assertArrayHasKey('name', $project);
$this->assertContains($project['name'], array(
'Test Available 1',
'Test Available 2',
));
}
}
/**
* @covers Wiaas_Order_Projects_API::create_order_project()
*/
function test_create_order_project_as_guest() {
$request = new WP_REST_Request( 'POST', '/wiaas/order-projects');
$request->set_body_params(array(
'name' => 'Test'
));
$this->check_endpoint_forbidden_for_guest($request);
}
/**
* @covers Wiaas_Order_Projects_API::create_order_project()
*/
function test_create_valid_order_project_as_customer() {
$customer_id = $this->create_new_customer();
wp_set_current_user($customer_id);
$request = new WP_REST_Request( 'POST', '/wiaas/order-projects');
$request->set_body_params(array(
'name' => 'Test Project'
));
$data = $this->dispatch_endpoint_request($request);
$this->assertNotNull($data);
$this->assertTrue(is_array($data));
$this->assertArrayHasKey('messages', $data);
$this->assertNotEmpty($data['messages']);
$message = $data['messages'][0];
$this->assertArrayHasKey('code', $message);
$this->assertEquals('success', $message['code']);
}
}

View File

@@ -0,0 +1,597 @@
<?php
/**
* Wiaas_Customer_Test
*
* @package Wiaas
*/
class Wiass_REST_Customer_Api_Test extends Wiaas_Unit_Test_Case {
protected $server;
function setUp() {
parent::setUp();
/** @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' );
}
/**
* @covers Wiass_REST_Customer_API::update_customer_profile_addresses
*/
function test_update_customer_profile_addresses_as_guest() {
wp_set_current_user(0);
$dummy_address = self::create_dummy_address();
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/0/profile-addresses');
$request->set_body_params(array(
'profile_address' => $dummy_address
));
$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_Customer_API::update_customer_profile_addresses
*/
function test_add_customer_profile_addresses_with_valid_address() {
wp_set_current_user(1);
$dummy_address = self::create_dummy_address();
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/profile-addresses');
$request->set_body_params(array(
'profile_address' => json_encode($dummy_address)
));
$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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('profile_addresses', $profile_info);
$this->assertEquals($profile_info['profile_addresses'][0]['city'], $dummy_address['city']);
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'PROFILE_ADDRESS_UPDATED');
}
/**
* @covers Wiass_REST_Customer_API::update_customer_profile_addresses
*/
function test_add_customer_profile_addresses_with_invalid_address() {
wp_set_current_user(1);
$dummy_address = self::create_dummy_address();
$dummy_address['zip_code'] = 'this is not a zip code';
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/profile-addresses');
$request->set_body_params(array(
'profile_address' => json_encode($dummy_address)
));
$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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('profile_addresses', $profile_info);
$this->assertEquals($profile_info['profile_addresses'][0], NULL);
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'PROFILE_ADDRESS_NOT_CHANGED');
}
/**
* @covers Wiass_REST_Customer_API::update_customer_billing_addresses
*/
function test_update_customer_billing_addresses_as_guest() {
wp_set_current_user(0);
$dummy_address = self::create_dummy_address();
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/0/billing-addresses');
$request->set_body_params(array(
'billing_address' => $dummy_address
));
$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_Customer_API::update_customer_billing_addresses
*/
function test_add_customer_billing_addresses_with_valid_address() {
wp_set_current_user(1);
$dummy_address = self::create_dummy_address();
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/billing-addresses');
$request->set_body_params(array(
'billing_address' => json_encode($dummy_address)
));
$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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('billing_addresses', $profile_info);
$this->assertEquals($profile_info['billing_addresses'][0]['city'], $dummy_address['city']);
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'BILLING_ADDRESS_UPDATED');
}
/**
* @covers Wiass_REST_Customer_API::update_customer_billing_addresses
*/
function test_add_customer_billing_addresses_with_invalid_address() {
wp_set_current_user(1);
$dummy_address = self::create_dummy_address();
$dummy_address['zip_code'] = 'this is not a zip code';
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/billing-addresses');
$request->set_body_params(array(
'billing_address' => json_encode($dummy_address)
));
$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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('billing_addresses', $profile_info);
$this->assertEquals($profile_info['billing_addresses'][0], NULL);
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'BILLING_ADDRESS_NOT_CHANGED');
}
/**
* @covers Wiass_REST_Customer_API::delete_customer_billing_addresses
*/
function test_delete_customer_billing_addresses_as_guest() {
$address_id = self::insert_dummy_billing_address();
wp_set_current_user(0);
$request = new WP_REST_Request( 'DELETE', '/wiaas/customer/1/billing-addresses/' . $address_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(), 401);
$error_data = $response->as_error();
$this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.');
}
/**
* @covers Wiass_REST_Customer_API::delete_customer_billing_addresses
*/
function test_delete_customer_billing_addresses() {
$address_id = self::insert_dummy_billing_address();
wp_set_current_user(1);
$request = new WP_REST_Request( 'DELETE', '/wiaas/customer/1/billing-addresses/' . $address_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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('billing_addresses', $profile_info);
$this->assertEquals($profile_info['billing_addresses'][0], NULL);
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'ADDRESS_REMOVED');
}
/**
* @covers Wiass_REST_Customer_API::delete_customer_profile_addresses
*/
function test_delete_customer_delivery_addresses_as_guest() {
$address_id = self::insert_dummy_profile_address();
wp_set_current_user(0);
$request = new WP_REST_Request( 'DELETE', '/wiaas/customer/1/profile-addresses/' . $address_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(), 401);
$error_data = $response->as_error();
$this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.');
}
/**
* @covers Wiass_REST_Customer_API::delete_customer_profile_addresses
*/
function test_delete_customer_profile_addresses() {
$address_id = self::insert_dummy_profile_address();
wp_set_current_user(1);
$request = new WP_REST_Request( 'DELETE', '/wiaas/customer/1/profile-addresses/' . $address_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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('profile_addresses', $profile_info);
$this->assertEquals($profile_info['profile_addresses'][0], NULL);
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'ADDRESS_REMOVED');
}
/**
* @covers Wiass_REST_Customer_API::update_customer_personal_info
*/
function test_update_customer_personal_info_as_guest() {
wp_set_current_user(0);
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info');
$request->set_body_params(array(
'first_name' => 'Trunks',
'last_name' => 'Goten',
'phone' => 11111
));
$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_Customer_API::update_customer_personal_info
*/
function test_update_customer_personal_info_with_empty_phone() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info');
$request->set_body_params(array(
'first_name' => 'Trunks',
'last_name' => 'Goten',
'phone' => ''
));
$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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('phone', $profile_info);
$this->assertEquals($profile_info['phone'], '');
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'ADD_PHONE_NUMBER');
}
/**
* @covers Wiass_REST_Customer_API::update_customer_personal_info
*/
function test_update_customer_personal_info_with_empty_name() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info');
$request->set_body_params(array(
'first_name' => '',
'last_name' => '',
'phone' => '23232'
));
$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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('phone', $profile_info);
$this->assertEquals($profile_info['phone'], '');
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'ADD_NAME');
}
/**
* @covers Wiass_REST_Customer_API::update_customer_personal_info
*/
function test_update_customer_personal_info() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info');
$request->set_body_params(array(
'first_name' => 'Trunks',
'last_name' => 'Goten',
'phone' => '3434'
));
$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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('phone', $profile_info);
$this->assertArrayHasKey('name', $profile_info);
$this->assertEquals($profile_info['phone'], '3434');
$this->assertEquals($profile_info['name'], 'Trunks Goten');
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'PROFILE_UPDATED');
}
/**
* @covers Wiass_REST_Customer_API::update_customer_company_info
*/
function test_update_customer_company_info_as_guest() {
wp_set_current_user(0);
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info');
$request->set_body_params(array(
'company_name' => 'Saburly',
'vat_code' => '12345'
));
$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_Customer_API::update_customer_company_info
*/
function test_update_customer_company_info_with_empty_company_name() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info');
$request->set_body_params(array(
'company_name' => '',
'vat_code' => '12345'
));
$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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('company_name', $profile_info);
$this->assertArrayHasKey('vat_code', $profile_info);
$this->assertEquals($profile_info['company_name'], '');
$this->assertEquals($profile_info['vat_code'], '');
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'ADD_COMPANY_NAME');
}
/**
* @covers Wiass_REST_Customer_API::update_customer_company_info
*/
function test_update_customer_company_info_with_empty_vat() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info');
$request->set_body_params(array(
'company_name' => 'Saburly',
'vat_code' => ''
));
$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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('company_name', $profile_info);
$this->assertArrayHasKey('vat_code', $profile_info);
$this->assertEquals($profile_info['company_name'], '');
$this->assertEquals($profile_info['vat_code'], '');
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'ADD_VAT');
}
/**
* @covers Wiass_REST_Customer_API::update_customer_company_info
*/
function test_update_customer_company_info() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info');
$request->set_body_params(array(
'company_name' => 'Saburly',
'vat_code' => '123'
));
$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);
$data = $response->get_data();
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('messages', $data);
$profile_info = $data['data'];
$messages = $data['messages'][0];
$this->assertArrayHasKey('company_name', $profile_info);
$this->assertArrayHasKey('vat_code', $profile_info);
$this->assertEquals($profile_info['company_name'], 'Saburly');
$this->assertEquals($profile_info['vat_code'], '123');
$this->assertArrayHasKey('message', $messages);
$this->assertEquals($messages['message'], 'COMPANY_UPDATED');
}
//Helper functions
private function create_dummy_address(){
return array(
'country_name' => 'Sweden',
'delivery_mail' => 'delivery@email.com',
'id_country_selected' => 1,
'city' => 'Stockholm',
'detailed_address' => 'Dummy Street 7',
'zip_code' => 124443,
'first_name' => 'Tester',
'last_name' => 'Retset',
'invoice_mail' => 'invoice@mail.com'
);
}
private function insert_dummy_billing_address(){
wp_set_current_user(1);
$dummy_address = self::create_dummy_address();
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/billing-addresses');
$request->set_body_params(array(
'billing_address' => json_encode($dummy_address)
));
$response = $this->server->dispatch( $request );
return $response->get_data()['data']['billing_addresses'][0]['id'];
}
private function insert_dummy_profile_address(){
wp_set_current_user(1);
$dummy_address = self::create_dummy_address();
$request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/profile-addresses');
$request->set_body_params(array(
'profile_address' => json_encode($dummy_address)
));
$response = $this->server->dispatch( $request );
return $response->get_data()['data']['profile_addresses'][0]['id'];
}
}

View File

@@ -0,0 +1,521 @@
<?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

@@ -0,0 +1,108 @@
<?php
/**
* Wiaas_Delivery_Process_Step_Test
*
* @package Wiaas
*/
class Wiass_REST_User_Api_Test extends Wiaas_Unit_Test_Case {
protected $server;
function setUp() {
parent::setUp();
/** @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' );
}
/**
* @covers Wiass_REST_User_API::validate_token
*/
function test_validate_token_as_guest() {
wp_set_current_user(0);
$request = new WP_REST_Request( 'POST', '/wiaas/user/validate-token');
$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_User_API::validate_token
*/
function test_validate_token_with_valid_user() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'POST', '/wiaas/user/validate-token');
$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);
$user_info = $response->get_data()['userInfo'];
$this->assertNotNull($user_info);
$this->assertTrue(is_array($user_info));
$this->assertArrayHasKey('wiaas_id_user', $user_info);
$this->assertArrayHasKey('wiaas_is_company_admin', $user_info);
$this->assertArrayHasKey('wiaas_user_full_name', $user_info);
$this->assertArrayHasKey('wiaas_user_type', $user_info);
$this->assertArrayHasKey('wiaas_username', $user_info);
$this->assertEquals($user_info['wiaas_id_user'], '1');
$this->assertEquals($user_info['wiaas_is_company_admin'], '1');
$this->assertEquals($user_info['wiaas_username'], 'admin');
}
/**
* @covers Wiass_REST_User_API::get_countries
*/
function test_get_countries_as_guest() {
wp_set_current_user(0);
$request = new WP_REST_Request( 'GET', '/wiaas/user/get-countries');
$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_User_API::get_countries
*/
function test_get_countries() {
wp_set_current_user(1);
$request = new WP_REST_Request( 'GET', '/wiaas/user/get-countries');
$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);
$data = $response->get_data();
$this->assertNotNull($data);
$this->assertTrue(is_array($data));
$country = $data[0];
$this->assertNotNull($country);
$this->assertTrue(is_array($country));
$this->assertArrayHasKey('country_id', $country);
$this->assertArrayHasKey('country_name', $country);
}
}