<?php

class Wiaas_Access_Management_Test extends Wiaas_Unit_Test_Case {

	/**
	 * @var WC_Order
	 */
	var $order;

	/**
	 * @var int
	 */
	var $organization_id;

	/**
	 * @var int
	 */
	var $customer_user_id;

	public function setUp() {
		parent::setUp();

		wp_set_current_user(1);

		$this->customer_user_id = wp_insert_user(array(
			'user_login' => 'test_customer',
			'user_pass'  => 'test',
			'user_email' => 'test_customer@mail.com',
			'role'       => 'customer',
		));

		$this->organization_id = $this->factory->organization->create_new_organization();

		$this->factory->organization->assign_user_to_organization($this->customer_user_id, $this->organization_id);

		$this->order = $this->factory->order->create_new_order();
	}

	function tearDown() {
		parent::tearDown();

		wp_set_current_user(1);

		wp_delete_user($this->customer_user_id);

		$this->factory->organization->delete_organizations();
	}

	/**
	 * @covers Wiaas_Access_Management::maybe_handle_product_access()
	 */
	function test_simple_product_has_admin_access() {

		$simple_product = $this->factory->product->create_simple_product();

		Wiaas_Access_Management::maybe_handle_product_access($simple_product->get_id(), get_post($simple_product->get_id()));

		$access_group_ids = Groups_Post_Access::get_read_group_ids( $simple_product->get_id() );
		$this->assertEquals(1, count($access_group_ids));
		$admin_access_group = Groups_Group::read_by_name('admin');
		$this->assertEquals($admin_access_group->group_id, $access_group_ids[0]);
	}

	/**
	 * @covers Wiaas_Access_Management::maybe_handle_product_access()
	 */
	function test_bundle_with_no_price_has_admin_access() {

		$bundle_product = $this->factory->product->create_product_bundle();

		Wiaas_Access_Management::maybe_handle_product_access($bundle_product->get_id(), get_post($bundle_product->get_id()));

		$access_group_ids = Groups_Post_Access::get_read_group_ids( $bundle_product->get_id() );
		$this->assertEquals(1, count($access_group_ids));
		$admin_access_group = Groups_Group::read_by_name('admin');
		$this->assertEquals($admin_access_group->group_id, $access_group_ids[0]);
	}

	/**
	 * @covers Wiaas_Access_Management::maybe_handle_product_access()
	 */
	function test_bundle_with_prices_has_registered_access() {

		$bundle_product = $this->factory->product->create_product_bundle();
		$this->factory->product->set_product_bundle_prices($bundle_product);

		Wiaas_Access_Management::maybe_handle_product_access($bundle_product->get_id(), get_post($bundle_product->get_id()));

		$access_group_ids = Groups_Post_Access::get_read_group_ids( $bundle_product->get_id() );
		$this->assertEquals(1, count($access_group_ids));
		$registered_access_group = Groups_Group::read_by_name('Registered');
		$this->assertEquals($registered_access_group->group_id, $access_group_ids[0]);
	}

	/**
	 * @covers Wiaas_Access_Management::assign_order_to_customer_organization()
	 */
	function test_order_assigned_to_customer_organization() {

		wp_set_current_user($this->customer_user_id);

		Wiaas_Access_Management::assign_order_to_customer_organization($this->order->get_id());

		$organization_access_group = Groups_Group::read_by_name('organization');
		$access_group_ids = Groups_Post_Access::get_read_group_ids( $this->order->get_id() );

		$this->assertEquals(1, count($access_group_ids));
		$this->assertNotNull($access_group_ids[0]);
		$this->assertEquals($organization_access_group->group_id, $access_group_ids[0]);

	}

	/**
	 * @covers Wiaas_Access_Management::assign_order_to_commercial_lead_organization()
	 */
	function test_order_assigned_to_commercial_lead_organization() {

		$this->order->update_meta_data('_wiaas_commercial_lead_id', $this->organization_id);
		$this->order->save_meta_data();

		Wiaas_Access_Management::assign_order_to_commercial_lead_organization($this->order->get_id());

		$organization_access_group = Groups_Group::read_by_name('organization');
		$access_group_ids = Groups_Post_Access::get_read_group_ids( $this->order->get_id() );

		$this->assertEquals(1, count($access_group_ids));
		$this->assertNotNull($access_group_ids[0]);
		$this->assertEquals($organization_access_group->group_id, $access_group_ids[0]);
	}

	/**
	 * @covers Wiaas_Access_Management::assign_order_to_supplier_organizations()
	 */
	function test_order_assigned_to_supplier_organizations() {

		$items = array();
		$items[] = $this->factory->order->create_new_simple_product_order_item(array(
			'category' => 'hardware',
			'supplier_organization_id' => $this->factory->organization->create_new_organization( array( 'name' => 'hardware supplier'))
		));
		$items[] = $this->factory->order->create_new_simple_product_order_item(array(
			'category' => 'software',
			'supplier_organization_id' => $this->factory->organization->create_new_organization( array( 'name' => 'software supplier'))
		));
		$items[] = $this->factory->order->create_new_simple_product_order_item(array(
			'category' => 'service',
			'supplier_organization_id' => $this->factory->organization->create_new_organization( array( 'name' => 'service supplier'))
		));
		$items[] = $this->factory->order->create_new_simple_product_order_item(array(
			'category' => 'installation',
			'supplier_organization_id' => $this->factory->organization->create_new_organization( array( 'name' => 'installation supplier'))
		));

		$this->factory->order->add_order_items($this->order, $items);

		Wiaas_Access_Management::assign_order_to_supplier_organizations($this->order->get_id());

		$access_group_ids = Groups_Post_Access::get_read_group_ids( $this->order->get_id() );
		$this->assertEquals(3, count($access_group_ids));

		$organization_access_group = Groups_Group::read_by_name('hardware supplier');
		$this->assertContains($organization_access_group->group_id, $access_group_ids);

		$organization_access_group = Groups_Group::read_by_name('software supplier');
		$this->assertContains($organization_access_group->group_id, $access_group_ids);

		$organization_access_group = Groups_Group::read_by_name('service supplier');
		$this->assertContains($organization_access_group->group_id, $access_group_ids);

	}

	/**
	 * @covers Wiaas_Access_Management::assign_order_to_installation_organization()
	 */
	function test_order_assigned_to_installation_organization() {

		$bundle_item = $this->factory->order->create_new_bundle_order_item();

		$installation_item = $this->factory->order->create_new_simple_product_order_item(array(
			'category' => 'installation',
			'supplier_organization_id' => $this->organization_id
		));

		$this->order->add_item($bundle_item);
		$this->order->add_item($installation_item);

		Wiaas_Access_Management::assign_order_to_installation_organization($this->order, $bundle_item, $installation_item);

		$organization_access_group = Groups_Group::read_by_name('organization');
		$access_group_ids = Groups_Post_Access::get_read_group_ids( $this->order->get_id() );

		$this->assertEquals(1, count($access_group_ids));
		$this->assertNotNull($access_group_ids[0]);
		$this->assertEquals($organization_access_group->group_id, $access_group_ids[0]);
	}
}