<?php

/**
 * Class Wiaas_User_Organization_Test
 *
 * @package Wiaas
 */

class Wiaas_User_Organization_Test extends Wiaas_Unit_Test_Case {

	var $user_id, $user_organization_name, $user_organization_id, $user_department_name, $user_department_id;

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

		# set admin as current user
		wp_set_current_user(1);

		# create testing user
		$this->user_id = wp_create_user('test', 'test', 'test@mail.com');
		$this->user_organization_name = 'test_organization';

		$this->user_department_name = 'test_department';

		# create organization
		$this->user_organization_id = wp_insert_term(
			$this->user_organization_name,
			Wiaas_User_Organization::TAXONOMY_NAME
		)['term_id'];

        update_user_meta($this->user_id, '_wiaas_organization_id', $this->user_organization_id);

		# create department
		$this->user_department_id = wp_insert_term(
			$this->user_department_name,
			Wiaas_User_Organization::TAXONOMY_NAME,
			array(
				'parent' => $this->user_organization_id
			)
		)['term_id'];

		# assign user to organization
		wp_set_terms_for_user(
			$this->user_id,
			Wiaas_User_Organization::TAXONOMY_NAME,
			[$this->user_organization_name]);
	}

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

		wp_set_current_user(1);

		wp_delete_user($this->user_id);

		wp_delete_term(
			$this->user_organization_id,
			Wiaas_User_Organization::TAXONOMY_NAME);
		wp_delete_term(
			$this->user_department_id,
			Wiaas_User_Organization::TAXONOMY_NAME);
	}

	/**
	 * @covers Wiaas_User_Organization::get_user_organization()
	 */
	function test_retrieve_user_organization() {
		$organization_id = Wiaas_User_Organization::get_user_organization_id($this->user_id);

		$this->assertNotNull($organization_id);
		$this->assertEquals($organization_id, $this->user_organization_id);
	}

	/**
	 * @covers Wiaas_User_Organization::on_organization_added()
	 */
	function test_access_group_created_when_organization_created() {
		# check organization access group id saved as term metadata
		$organization_access_group_id = get_term_meta($this->user_organization_id, 'group_id', true);
		$this->assertNotNull($organization_access_group_id);

		# check organization access group object exists
		$organization_access_group = Groups_Group::read_by_name($this->user_organization_name);
		$this->assertNotFalse($organization_access_group);

		# check created access group object corresponds to this organization
		$this->assertEquals($organization_access_group->name, $this->user_organization_name);
		$this->assertEquals($organization_access_group->group_id, $organization_access_group_id);

		# check department access group id saved as term metadata
		$department_access_group_id = get_term_meta($this->user_department_id, 'group_id', true);
		$this->assertNotNull($department_access_group_id);

		# check department access group object exists
		$department_access_group = Groups_Group::read_by_name($this->user_department_name);
		$this->assertNotFalse($department_access_group);

		# check created access group object corresponds to this department
		$this->assertEquals($department_access_group->name, $this->user_department_name);
		$this->assertEquals($department_access_group->group_id, $department_access_group_id);
	}

	/**
	 * @covers Wiaas_User_Organization::on_taxonomy_term_assigned()
	 */
	function test_user_added_to_organization_access_groups() {
		# check user added to organization access group
		$organization_access_group = Groups_Group::read_by_name($this->user_organization_name);
		$user_group_relation = Groups_User_Group::read( $this->user_id , $organization_access_group->group_id );

		$this->assertNotFalse($user_group_relation);
		$this->assertEquals($user_group_relation->user_id, $this->user_id);
		$this->assertEquals($user_group_relation->group_id, $organization_access_group->group_id);

		# test user added to department access group
		$department_access_group = Groups_Group::read_by_name($this->user_department_name);
		$user_group_relation = Groups_User_Group::read( $this->user_id , $department_access_group->group_id );

		$this->assertNotFalse($user_group_relation);
		$this->assertEquals($user_group_relation->user_id, $this->user_id);
		$this->assertEquals($user_group_relation->group_id, $department_access_group->group_id);
	}

	/**
	 * @covers Wiaas_User_Organization::on_taxonomy_term_will_be_deleted()
	 */
	function test_access_group_deleted_when_organization_deleted() {
		wp_delete_term($this->user_organization_id, Wiaas_User_Organization::TAXONOMY_NAME);

		$organization_access_group = Groups_Group::read_by_name($this->user_organization_name);
		$this->assertFalse($organization_access_group);
	}

	/**
	 * @covers Wiaas_User_Organization::on_taxonomy_term_unassigned()
	 */
	function test_user_removed_from_organization_access_groups() {
		# remove user from organization
		wp_set_terms_for_user($this->user_id, Wiaas_User_Organization::TAXONOMY_NAME, null);

		# check user removed from organization access group
		$organization_access_group = Groups_Group::read_by_name($this->user_organization_name);
		$user_group_relation = Groups_User_Group::read( $this->user_id , $organization_access_group->group_id );

		$this->assertFalse($user_group_relation);

		# check user removed from department access group
		$department_access_group = Groups_Group::read_by_name($this->user_department_name);
		$user_group_relation = Groups_User_Group::read( $this->user_id , $department_access_group->group_id );

		$this->assertFalse($user_group_relation);
	}

	/**
	 * @covers Wiaas_User_Organization::assign_post_to_user_organization()
	 */
	function test_post_assigned_to_user_organization() {
		$post_id = wp_insert_post(array(
			'post_title' => 'Test',
			'post_content' => 'Test',
			'post_excerpt' => 'Test'
		), true);

		Wiaas_User_Organization::assign_post_to_organization($post_id, $this->user_organization_id);

		$organization_access_group = Groups_Group::read_by_name($this->user_organization_name);

		$access_group_ids = Groups_Post_Access::get_read_group_ids( $post_id );

		# check post added to organization access group
		$this->assertEquals(1, count($access_group_ids));
		$this->assertNotNull($access_group_ids[0]);
		$this->assertEquals($organization_access_group->group_id, $access_group_ids[0]);
	}
}