<?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 =  Wiaas_Delivery_Process_Action::get_action_forms()[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_action_entries_meta_key() {
		return 'wiaas_delivery_step_' . $this->step->get_id() .'_action_entry_ids';
	}

	/**
	 * @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(), 'Delivery Step');

		//$this->assertEquals($this->step->is_visible_to_customer, true);
	}

	/**
	 * @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() .'_action_entry_ids' => array()
		);

		$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->assertFalse($this->step->process());

		# check that entry metadata is not updated
		$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);
	}

	/**
	 * @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_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));
	}
}
