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