Delivery setup

This commit is contained in:
Almira Krdzic
2018-08-06 17:36:57 +02:00
parent ed0e44b964
commit 5afdde434b
27 changed files with 2809 additions and 236 deletions

View File

@@ -0,0 +1,170 @@
<?php
/**
* Wiaas_Delivery_Process_Step_Test
*
* @package Wiaas
*/
class Wiaas_Delivery_Process_Step_Test extends Wiaas_Unit_Test_Case {
var $step, $form_id, $target_form_id;
function setUp() {
parent::setUp();
$this->form_id = GFAPI::add_form(array(
'title' => 'Delivery Process Step Test Form',
));
$form_entry_id = GFAPI::add_entry(array(
'form_id' => $this->form_id,
));
$this->target_form_id = GFFormsModel::search_forms(
'DELIVERY ACTION TYPE: Manual',
true)[0]->id;
$this->step = Gravity_Flow_Steps::create( array(
'form_id'=> $this->form_id,
'is_active'=> '1',
'meta' => array(
'step_name' => 'Test Wiaas Delivery Process Step',
'description' => 'Test Wiaas Delivery Process Step',
'step_type' => 'wiaas_delivery_step',
'target_form_id' => $this->target_form_id
)
), GFAPI::get_entry($form_entry_id));
}
private function _get_target_entry_meta_key() {
return 'wiaas_delivery_step_' . $this->step->get_id() .'_entry_id';
}
/**
* @covers Wiaas_Delivery_Process_Step::get_settings
*/
function test_settings_options_are_valid() {
$this->assertEquals(get_class($this->step), 'Wiaas_Delivery_Process_Step');
$this->assertEquals($this->step->is_active(), true);
$this->assertEquals($this->step->get_name(), 'Test Wiaas Delivery Process Step');
$this->assertEquals($this->step->description, 'Test Wiaas Delivery Process Step');
$this->assertEquals($this->step->target_form_id, $this->target_form_id);
$this->assertEquals($this->step->get_label(), 'Wiaas 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));
}
}
/**
* @covers Wiaas_Delivery_Process_Step::get_entry_meta
*/
function test_entry_meta_skipped_for_non_parent_form() {
$meta = array(
'test' => 'test'
);
# Since this is not parent form id function should return unchanged meta
$this->assertEquals($meta, $this->step->get_entry_meta($meta, 55));
}
/**
* @covers Wiaas_Delivery_Process_Step::get_entry_meta
*/
function test_entry_meta_added_for_parent_form() {
$meta = array(
'test' => 'test'
);
$expected_meta = array(
'test' => 'test',
'wiaas_delivery_step_' . $this->step->get_id() .'_entry_id' => null
);
$this->assertEquals($expected_meta, $this->step->get_entry_meta($meta, $this->step->get_form_id()));
}
/**
* @covers Wiaas_Delivery_Process_Step::process
*/
function test_process_with_no_target_form() {
$this->step->target_form_id = '';
$this->assertTrue($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);
}
/**
* @covers Wiaas_Delivery_Process_Step::process
*/
function test_process_with_target_form() {
# check that processing step returns false since it will wait for target form entry workflow to complete
$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);
# 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');
}
}

View File

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

@@ -0,0 +1,55 @@
<?php
/**
* Wiaas_Delivery_Process_Step_Test
*
* @package Wiaas
*/
class Wiass_REST_Delivery_Process_Api_Test extends Wiaas_Unit_Test_Case {
var $order_id, $api;
function setUp() {
parent::setUp();
$order = wc_create_order();
$this->order_id = $order->get_id();
wp_set_current_user(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));
# check that administrator has one action pending
$this->assertEquals(sizeof($next_steps), 1);
$pending_step = $next_steps[0];
$this->assertTrue(is_array($pending_step));
$this->assertArrayHasKey('idOrder', $pending_step);
$this->assertArrayHasKey('orderNumber', $pending_step);
$this->assertArrayHasKey('status', $pending_step);
$this->assertArrayHasKey('stepAction', $pending_step);
$this->assertEquals($pending_step['idOrder'], $this->order_id);
$this->assertEquals($pending_step['orderNumber'], $this->order_id);
$this->assertEquals($pending_step['status'], 'pending');
$this->assertNotEmpty($pending_step['stepAction']);
}
}